6bfa6a99dcd5097e0d70937c9c2f00343694e002
[pstop.git] / p_s / table_io_waits_summary_by_table / private.go
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
4
5 import (
6         "database/sql"
7         "fmt"
8         "log"
9         "sort"
10         "strings"
11
12         "github.com/sjmudd/pstop/lib"
13 )
14
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.
19
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
23
24         SUM_TIMER_WAIT   uint64
25         SUM_TIMER_READ   uint64
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
31
32         COUNT_STAR   uint64
33         COUNT_READ   uint64
34         COUNT_WRITE  uint64
35         COUNT_FETCH  uint64
36         COUNT_INSERT uint64
37         COUNT_UPDATE uint64
38         COUNT_DELETE uint64
39 }
40 type table_rows []table_row
41
42 // // return the table name from the columns as '<schema>.<table>'
43 func (r *table_row) name() string {
44         var n string
45         if len(r.OBJECT_SCHEMA) > 0 {
46                 n += r.OBJECT_SCHEMA
47         }
48         if len(n) > 0 {
49                 if len(r.OBJECT_NAME) > 0 {
50                         n += "." + r.OBJECT_NAME
51                 }
52         } else {
53                 if len(r.OBJECT_NAME) > 0 {
54                         n += r.OBJECT_NAME
55                 }
56         }
57         return n
58 }
59
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")
62 }
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")
65 }
66
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.
70         name := r.name()
71         if r.COUNT_STAR == 0 && name != "Totals" {
72                 name = ""
73         }
74
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)),
82                 name)
83 }
84
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.
88         name := r.name()
89         if r.COUNT_STAR == 0 && name != "Totals" {
90                 name = ""
91         }
92
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)),
100                 name)
101 }
102
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
111
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
119 }
120
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
133
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
141         } else {
142                 lib.Logger.Println("WARNING: table_row.subtract() - subtraction problem! (not subtracting)")
143                 lib.Logger.Println("this=", this)
144                 lib.Logger.Println("other=", other)
145         }
146 }
147
148 func (t table_rows) totals() table_row {
149         var totals table_row
150         totals.OBJECT_SCHEMA = "Totals"
151
152         for i := range t {
153                 totals.add(t[i])
154         }
155
156         return totals
157 }
158
159 func select_rows(dbh *sql.DB) table_rows {
160         var t table_rows
161
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"
164
165         rows, err := dbh.Query(sql)
166         if err != nil {
167                 log.Fatal(err)
168         }
169         defer rows.Close()
170
171         for rows.Next() {
172                 var r table_row
173                 if err := rows.Scan(
174                         &r.OBJECT_TYPE,
175                         &r.OBJECT_SCHEMA,
176                         &r.OBJECT_NAME,
177                         &r.COUNT_STAR,
178                         &r.SUM_TIMER_WAIT,
179                         &r.COUNT_READ,
180                         &r.SUM_TIMER_READ,
181                         &r.COUNT_WRITE,
182                         &r.SUM_TIMER_WRITE,
183                         &r.COUNT_FETCH,
184                         &r.SUM_TIMER_FETCH,
185                         &r.COUNT_INSERT,
186                         &r.SUM_TIMER_INSERT,
187                         &r.COUNT_UPDATE,
188                         &r.SUM_TIMER_UPDATE,
189                         &r.COUNT_DELETE,
190                         &r.SUM_TIMER_DELETE); err != nil {
191                         log.Fatal(err)
192                 }
193                 // we collect all information even if it's mainly empty as we may reference it later
194                 t = append(t, r)
195         }
196         if err := rows.Err(); err != nil {
197                 log.Fatal(err)
198         }
199
200         return t
201 }
202
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] }
205
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))
212 }
213
214 // for sorting
215 type ByOps table_rows
216
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))
224 }
225
226 func (t table_rows) Sort(want_latency bool) {
227         if want_latency {
228                 sort.Sort(t)
229         } else {
230                 sort.Sort(ByOps(t))
231         }
232 }
233
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)
238
239         // iterate over rows by name
240         for i := range initial {
241                 initial_by_name[initial[i].name()] = i
242         }
243
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])
249                 }
250         }
251 }
252
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()
258
259         return my_totals.SUM_TIMER_WAIT > t2_totals.SUM_TIMER_WAIT
260 }
261
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",
265                 r.name(),
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),
271
272                 lib.FormatTime(r.SUM_TIMER_READ),
273                 lib.FormatTime(r.SUM_TIMER_WRITE),
274
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),
280
281                 lib.FormatAmount(r.COUNT_READ),
282                 lib.FormatAmount(r.COUNT_WRITE))
283 }
284
285 // describe a whole table
286 func (t table_rows) String() string {
287         s := make([]string, len(t))
288
289         for i := range t {
290                 s = append(s, t[i].String())
291         }
292
293         return strings.Join(s, "\n")
294 }