1 // This file contains the library routines for managing the
2 // table_io_waits_by_table table.
3 package table_io_waits_summary_by_table
12 "github.com/sjmudd/pstop/lib"
15 // a row from performance_schema.table_io_waits_summary_by_table
16 type table_row struct {
17 // Note: upper case names to match the performance_schema column names
18 // This type is _not_ exported.
20 OBJECT_TYPE string // in theory redundant but keep anyway
21 OBJECT_SCHEMA string // in theory redundant but keep anyway
22 OBJECT_NAME string // in theory redundant but keep anyway
26 SUM_TIMER_WRITE uint64
27 SUM_TIMER_FETCH uint64
28 SUM_TIMER_INSERT uint64
29 SUM_TIMER_UPDATE uint64
30 SUM_TIMER_DELETE uint64
40 type table_rows []table_row
42 // // return the table name from the columns as '<schema>.<table>'
43 func (r *table_row) name() string {
45 if len(r.OBJECT_SCHEMA) > 0 {
49 if len(r.OBJECT_NAME) > 0 {
50 n += "." + r.OBJECT_NAME
53 if len(r.OBJECT_NAME) > 0 {
60 func (r *table_row) latency_headings() string {
61 return fmt.Sprintf("%10s %6s|%6s %6s %6s %6s|%s", "Latency", "%", "Fetch", "Insert", "Update", "Delete", "Table Name")
63 func (r *table_row) ops_headings() string {
64 return fmt.Sprintf("%10s %6s|%6s %6s %6s %6s|%s", "Ops", "%", "Fetch", "Insert", "Update", "Delete", "Table Name")
67 // generate a printable result
68 func (r *table_row) latency_row_content(totals table_row) string {
69 // assume the data is empty so hide it.
71 if r.COUNT_STAR == 0 && name != "Totals" {
75 return fmt.Sprintf("%10s %6s|%6s %6s %6s %6s|%s",
76 lib.FormatTime(r.SUM_TIMER_WAIT),
77 lib.FormatPct(lib.MyDivide(r.SUM_TIMER_WAIT, totals.SUM_TIMER_WAIT)),
78 lib.FormatPct(lib.MyDivide(r.SUM_TIMER_FETCH, r.SUM_TIMER_WAIT)),
79 lib.FormatPct(lib.MyDivide(r.SUM_TIMER_INSERT, r.SUM_TIMER_WAIT)),
80 lib.FormatPct(lib.MyDivide(r.SUM_TIMER_UPDATE, r.SUM_TIMER_WAIT)),
81 lib.FormatPct(lib.MyDivide(r.SUM_TIMER_DELETE, r.SUM_TIMER_WAIT)),
85 // generate a printable result for ops
86 func (r *table_row) ops_row_content(totals table_row) string {
87 // assume the data is empty so hide it.
89 if r.COUNT_STAR == 0 && name != "Totals" {
93 return fmt.Sprintf("%10s %6s|%6s %6s %6s %6s|%s",
94 lib.FormatAmount(r.COUNT_STAR),
95 lib.FormatPct(lib.MyDivide(r.COUNT_STAR, totals.COUNT_STAR)),
96 lib.FormatPct(lib.MyDivide(r.COUNT_FETCH, r.COUNT_STAR)),
97 lib.FormatPct(lib.MyDivide(r.COUNT_INSERT, r.COUNT_STAR)),
98 lib.FormatPct(lib.MyDivide(r.COUNT_UPDATE, r.COUNT_STAR)),
99 lib.FormatPct(lib.MyDivide(r.COUNT_DELETE, r.COUNT_STAR)),
103 func (this *table_row) add(other table_row) {
104 this.SUM_TIMER_WAIT += other.SUM_TIMER_WAIT
105 this.SUM_TIMER_FETCH += other.SUM_TIMER_FETCH
106 this.SUM_TIMER_INSERT += other.SUM_TIMER_INSERT
107 this.SUM_TIMER_UPDATE += other.SUM_TIMER_UPDATE
108 this.SUM_TIMER_DELETE += other.SUM_TIMER_DELETE
109 this.SUM_TIMER_READ += other.SUM_TIMER_READ
110 this.SUM_TIMER_WRITE += other.SUM_TIMER_WRITE
112 this.COUNT_STAR += other.COUNT_STAR
113 this.COUNT_FETCH += other.COUNT_FETCH
114 this.COUNT_INSERT += other.COUNT_INSERT
115 this.COUNT_UPDATE += other.COUNT_UPDATE
116 this.COUNT_DELETE += other.COUNT_DELETE
117 this.COUNT_READ += other.COUNT_READ
118 this.COUNT_WRITE += other.COUNT_WRITE
121 // subtract the countable values in one row from another
122 func (this *table_row) subtract(other table_row) {
123 // check for issues here (we have a bug) and log it
124 // - this situation should not happen so there's a logic bug somewhere else
125 if this.SUM_TIMER_WAIT >= other.SUM_TIMER_WAIT {
126 this.SUM_TIMER_WAIT -= other.SUM_TIMER_WAIT
127 this.SUM_TIMER_FETCH -= other.SUM_TIMER_FETCH
128 this.SUM_TIMER_INSERT -= other.SUM_TIMER_INSERT
129 this.SUM_TIMER_UPDATE -= other.SUM_TIMER_UPDATE
130 this.SUM_TIMER_DELETE -= other.SUM_TIMER_DELETE
131 this.SUM_TIMER_READ -= other.SUM_TIMER_READ
132 this.SUM_TIMER_WRITE -= other.SUM_TIMER_WRITE
134 this.COUNT_STAR -= other.COUNT_STAR
135 this.COUNT_FETCH -= other.COUNT_FETCH
136 this.COUNT_INSERT -= other.COUNT_INSERT
137 this.COUNT_UPDATE -= other.COUNT_UPDATE
138 this.COUNT_DELETE -= other.COUNT_DELETE
139 this.COUNT_READ -= other.COUNT_READ
140 this.COUNT_WRITE -= other.COUNT_WRITE
142 lib.Logger.Println("WARNING: table_row.subtract() - subtraction problem! (not subtracting)")
143 lib.Logger.Println("this=", this)
144 lib.Logger.Println("other=", other)
148 func (t table_rows) totals() table_row {
150 totals.OBJECT_SCHEMA = "Totals"
159 func select_rows(dbh *sql.DB) table_rows {
162 // we collect all information even if it's mainly empty as we may reference it later
163 sql := "SELECT OBJECT_TYPE, OBJECT_SCHEMA, OBJECT_NAME, COUNT_STAR, SUM_TIMER_WAIT, COUNT_READ, SUM_TIMER_READ, COUNT_WRITE, SUM_TIMER_WRITE, COUNT_FETCH, SUM_TIMER_FETCH, COUNT_INSERT, SUM_TIMER_INSERT, COUNT_UPDATE, SUM_TIMER_UPDATE, COUNT_DELETE, SUM_TIMER_DELETE FROM table_io_waits_summary_by_table WHERE SUM_TIMER_WAIT > 0"
165 rows, err := dbh.Query(sql)
190 &r.SUM_TIMER_DELETE); err != nil {
193 // we collect all information even if it's mainly empty as we may reference it later
196 if err := rows.Err(); err != nil {
203 func (t table_rows) Len() int { return len(t) }
204 func (t table_rows) Swap(i, j int) { t[i], t[j] = t[j], t[i] }
206 // sort by value (descending) but also by "name" (ascending) if the values are the same
207 func (t table_rows) Less(i, j int) bool {
208 return (t[i].SUM_TIMER_WAIT > t[j].SUM_TIMER_WAIT) ||
209 ((t[i].SUM_TIMER_WAIT == t[j].SUM_TIMER_WAIT) &&
210 (t[i].OBJECT_SCHEMA < t[j].OBJECT_SCHEMA) &&
211 (t[i].OBJECT_NAME < t[j].OBJECT_NAME))
215 type ByOps table_rows
217 func (t ByOps) Len() int { return len(t) }
218 func (t ByOps) Swap(i, j int) { t[i], t[j] = t[j], t[i] }
219 func (t ByOps) Less(i, j int) bool {
220 return (t[i].COUNT_STAR > t[j].COUNT_STAR) ||
221 ((t[i].SUM_TIMER_WAIT == t[j].SUM_TIMER_WAIT) &&
222 (t[i].OBJECT_SCHEMA < t[j].OBJECT_SCHEMA) &&
223 (t[i].OBJECT_NAME < t[j].OBJECT_NAME))
226 func (t table_rows) Sort(want_latency bool) {
234 // remove the initial values from those rows where there's a match
235 // - if we find a row we can't match ignore it
236 func (this *table_rows) subtract(initial table_rows) {
237 initial_by_name := make(map[string]int)
239 // iterate over rows by name
240 for i := range initial {
241 initial_by_name[initial[i].name()] = i
244 for i := range *this {
245 this_name := (*this)[i].name()
246 if _, ok := initial_by_name[this_name]; ok {
247 initial_index := initial_by_name[this_name]
248 (*this)[i].subtract(initial[initial_index])
253 // if the data in t2 is "newer", "has more values" than t then it needs refreshing.
254 // check this by comparing totals.
255 func (t table_rows) needs_refresh(t2 table_rows) bool {
256 my_totals := t.totals()
257 t2_totals := t2.totals()
259 return my_totals.SUM_TIMER_WAIT > t2_totals.SUM_TIMER_WAIT
262 // describe a whole row
263 func (r table_row) String() string {
264 return fmt.Sprintf("%s|%10s %10s %10s %10s %10s|%10s %10s|%10s %10s %10s %10s %10s|%10s %10s",
266 lib.FormatTime(r.SUM_TIMER_WAIT),
267 lib.FormatTime(r.SUM_TIMER_FETCH),
268 lib.FormatTime(r.SUM_TIMER_INSERT),
269 lib.FormatTime(r.SUM_TIMER_UPDATE),
270 lib.FormatTime(r.SUM_TIMER_DELETE),
272 lib.FormatTime(r.SUM_TIMER_READ),
273 lib.FormatTime(r.SUM_TIMER_WRITE),
275 lib.FormatAmount(r.COUNT_STAR),
276 lib.FormatAmount(r.COUNT_FETCH),
277 lib.FormatAmount(r.COUNT_INSERT),
278 lib.FormatAmount(r.COUNT_UPDATE),
279 lib.FormatAmount(r.COUNT_DELETE),
281 lib.FormatAmount(r.COUNT_READ),
282 lib.FormatAmount(r.COUNT_WRITE))
285 // describe a whole table
286 func (t table_rows) String() string {
287 s := make([]string, len(t))
290 s = append(s, t[i].String())
293 return strings.Join(s, "\n")