Initial import of pstop v0.0.10
[pstop.git] / performance_schema / table_io_waits_summary_by_table / table_io_waits_summary_by_table_row.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_io_waits_summary_by_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   int
25         SUM_TIMER_READ   int
26         SUM_TIMER_WRITE  int
27         SUM_TIMER_FETCH  int
28         SUM_TIMER_INSERT int
29         SUM_TIMER_UPDATE int
30         SUM_TIMER_DELETE int
31
32         COUNT_STAR   int
33         COUNT_READ   int
34         COUNT_WRITE  int
35         COUNT_FETCH  int
36         COUNT_INSERT int
37         COUNT_UPDATE int
38         COUNT_DELETE int
39 }
40 type table_io_waits_summary_by_table_rows []table_io_waits_summary_by_table_row
41
42 // // return the table name from the columns as '<schema>.<table>'
43 func (r *table_io_waits_summary_by_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_io_waits_summary_by_table_row) pretty_name() string {
61         s := r.name()
62         if len(s) > 30 {
63                 s = s[:29]
64         }
65         return fmt.Sprintf("%-30s", s)
66 }
67
68 func (r *table_io_waits_summary_by_table_row) latency_headings() string {
69         return fmt.Sprintf("%-30s %10s %6s|%6s %6s %6s %6s", "Table Name", "Latency", "%", "Fetch", "Insert", "Update", "Delete")
70 }
71 func (r *table_io_waits_summary_by_table_row) ops_headings() string {
72         return fmt.Sprintf("%-30s %10s %6s|%6s %6s %6s %6s", "Table Name", "Ops", "%", "Fetch", "Insert", "Update", "Delete")
73 }
74
75 // generate a printable result
76 func (r *table_io_waits_summary_by_table_row) latency_row_content(totals table_io_waits_summary_by_table_row) string {
77         // assume the data is empty so hide it.
78         name := r.pretty_name()
79         if r.COUNT_STAR == 0 {
80                 name = ""
81         }
82
83         return fmt.Sprintf("%-30s %10s %6s|%6s %6s %6s %6s",
84                 name,
85                 lib.FormatTime(r.SUM_TIMER_WAIT),
86                 lib.FormatPct(lib.MyDivide(r.SUM_TIMER_WAIT, totals.SUM_TIMER_WAIT)),
87                 lib.FormatPct(lib.MyDivide(r.SUM_TIMER_FETCH, r.SUM_TIMER_WAIT)),
88                 lib.FormatPct(lib.MyDivide(r.SUM_TIMER_INSERT, r.SUM_TIMER_WAIT)),
89                 lib.FormatPct(lib.MyDivide(r.SUM_TIMER_UPDATE, r.SUM_TIMER_WAIT)),
90                 lib.FormatPct(lib.MyDivide(r.SUM_TIMER_DELETE, r.SUM_TIMER_WAIT)))
91 }
92
93 // generate a printable result for ops
94 func (r *table_io_waits_summary_by_table_row) ops_row_content(totals table_io_waits_summary_by_table_row) string {
95         // assume the data is empty so hide it.
96         name := r.pretty_name()
97         if r.COUNT_STAR == 0 {
98                 name = ""
99         }
100
101         return fmt.Sprintf("%-30s %10s %6s|%6s %6s %6s %6s",
102                 name,
103                 lib.FormatAmount(r.COUNT_STAR),
104                 lib.FormatPct(lib.MyDivide(r.COUNT_STAR, totals.COUNT_STAR)),
105                 lib.FormatPct(lib.MyDivide(r.COUNT_FETCH, r.COUNT_STAR)),
106                 lib.FormatPct(lib.MyDivide(r.COUNT_INSERT, r.COUNT_STAR)),
107                 lib.FormatPct(lib.MyDivide(r.COUNT_UPDATE, r.COUNT_STAR)),
108                 lib.FormatPct(lib.MyDivide(r.COUNT_DELETE, r.COUNT_STAR)))
109 }
110
111 func (this *table_io_waits_summary_by_table_row) add(other table_io_waits_summary_by_table_row) {
112         this.SUM_TIMER_WAIT += other.SUM_TIMER_WAIT
113         this.SUM_TIMER_FETCH += other.SUM_TIMER_FETCH
114         this.SUM_TIMER_INSERT += other.SUM_TIMER_INSERT
115         this.SUM_TIMER_UPDATE += other.SUM_TIMER_UPDATE
116         this.SUM_TIMER_DELETE += other.SUM_TIMER_DELETE
117         this.SUM_TIMER_READ += other.SUM_TIMER_READ
118         this.SUM_TIMER_WRITE += other.SUM_TIMER_WRITE
119
120         this.COUNT_STAR += other.COUNT_STAR
121         this.COUNT_FETCH += other.COUNT_FETCH
122         this.COUNT_INSERT += other.COUNT_INSERT
123         this.COUNT_UPDATE += other.COUNT_UPDATE
124         this.COUNT_DELETE += other.COUNT_DELETE
125         this.COUNT_READ += other.COUNT_READ
126         this.COUNT_WRITE += other.COUNT_WRITE
127 }
128
129 func (this *table_io_waits_summary_by_table_row) subtract(other table_io_waits_summary_by_table_row) {
130         this.SUM_TIMER_WAIT -= other.SUM_TIMER_WAIT
131         this.SUM_TIMER_FETCH -= other.SUM_TIMER_FETCH
132         this.SUM_TIMER_INSERT -= other.SUM_TIMER_INSERT
133         this.SUM_TIMER_UPDATE -= other.SUM_TIMER_UPDATE
134         this.SUM_TIMER_DELETE -= other.SUM_TIMER_DELETE
135         this.SUM_TIMER_READ -= other.SUM_TIMER_READ
136         this.SUM_TIMER_WRITE -= other.SUM_TIMER_WRITE
137
138         this.COUNT_STAR -= other.COUNT_STAR
139         this.COUNT_FETCH -= other.COUNT_FETCH
140         this.COUNT_INSERT -= other.COUNT_INSERT
141         this.COUNT_UPDATE -= other.COUNT_UPDATE
142         this.COUNT_DELETE -= other.COUNT_DELETE
143         this.COUNT_READ -= other.COUNT_READ
144         this.COUNT_WRITE -= other.COUNT_WRITE
145 }
146
147 func (t table_io_waits_summary_by_table_rows) totals() table_io_waits_summary_by_table_row {
148         var totals table_io_waits_summary_by_table_row
149         totals.OBJECT_SCHEMA = "TOTALS"
150
151         for i := range t {
152                 totals.add(t[i])
153         }
154
155         return totals
156 }
157
158 func select_tiwsbt_rows(dbh *sql.DB) table_io_waits_summary_by_table_rows {
159         var t table_io_waits_summary_by_table_rows
160
161         // we collect all information even if it's mainly empty as we may reference it later
162         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"
163
164         rows, err := dbh.Query(sql)
165         if err != nil {
166                 log.Fatal(err)
167         }
168         defer rows.Close()
169
170         for rows.Next() {
171                 var r table_io_waits_summary_by_table_row
172                 if err := rows.Scan(
173                         &r.OBJECT_TYPE,
174                         &r.OBJECT_SCHEMA,
175                         &r.OBJECT_NAME,
176                         &r.COUNT_STAR,
177                         &r.SUM_TIMER_WAIT,
178                         &r.COUNT_READ,
179                         &r.SUM_TIMER_READ,
180                         &r.COUNT_WRITE,
181                         &r.SUM_TIMER_WRITE,
182                         &r.COUNT_FETCH,
183                         &r.SUM_TIMER_FETCH,
184                         &r.COUNT_INSERT,
185                         &r.SUM_TIMER_INSERT,
186                         &r.COUNT_UPDATE,
187                         &r.SUM_TIMER_UPDATE,
188                         &r.COUNT_DELETE,
189                         &r.SUM_TIMER_DELETE); err != nil {
190                         log.Fatal(err)
191                 }
192                 // we collect all information even if it's mainly empty as we may reference it later
193                 t = append(t, r)
194         }
195         if err := rows.Err(); err != nil {
196                 log.Fatal(err)
197         }
198
199         return t
200 }
201
202 func (t table_io_waits_summary_by_table_rows) Len() int      { return len(t) }
203 func (t table_io_waits_summary_by_table_rows) Swap(i, j int) { t[i], t[j] = t[j], t[i] }
204
205 // sort by value (descending) but also by "name" (ascending) if the values are the same
206 func (t table_io_waits_summary_by_table_rows) Less(i, j int) bool {
207         return (t[i].SUM_TIMER_WAIT > t[j].SUM_TIMER_WAIT) ||
208                 ((t[i].SUM_TIMER_WAIT == t[j].SUM_TIMER_WAIT) &&
209                         (t[i].OBJECT_SCHEMA < t[j].OBJECT_SCHEMA) &&
210                         (t[i].OBJECT_NAME < t[j].OBJECT_NAME))
211 }
212
213 // for sorting
214 type ByOps table_io_waits_summary_by_table_rows
215
216 func (t ByOps) Len() int      { return len(t) }
217 func (t ByOps) Swap(i, j int) { t[i], t[j] = t[j], t[i] }
218 func (t ByOps) Less(i, j int) bool {
219         return (t[i].COUNT_STAR > t[j].COUNT_STAR) ||
220                 ((t[i].SUM_TIMER_WAIT == t[j].SUM_TIMER_WAIT) &&
221                         (t[i].OBJECT_SCHEMA < t[j].OBJECT_SCHEMA) &&
222                         (t[i].OBJECT_NAME < t[j].OBJECT_NAME))
223 }
224
225 func (t table_io_waits_summary_by_table_rows) Sort(want_latency bool) {
226         if want_latency {
227                 sort.Sort(t)
228         } else {
229                 sort.Sort(ByOps(t))
230         }
231 }
232
233 // remove the initial values from those rows where there's a match
234 // - if we find a row we can't match ignore it
235 func (this *table_io_waits_summary_by_table_rows) subtract(initial table_io_waits_summary_by_table_rows) {
236         i_by_name := make(map[string]int)
237
238         // iterate over rows by name
239         for i := range initial {
240                 i_by_name[initial[i].name()] = i
241         }
242
243         for i := range *this {
244                 if _, ok := i_by_name[(*this)[i].name()]; ok {
245                         initial_i := i_by_name[(*this)[i].name()]
246                         (*this)[i].subtract(initial[initial_i])
247                 }
248         }
249 }
250
251 // if the data in t2 is "newer", "has more values" than t then it needs refreshing.
252 // check this by comparing totals.
253 func (t table_io_waits_summary_by_table_rows) needs_refresh(t2 table_io_waits_summary_by_table_rows) bool {
254         my_totals := t.totals()
255         t2_totals := t2.totals()
256
257         return my_totals.SUM_TIMER_WAIT > t2_totals.SUM_TIMER_WAIT
258 }
259
260 // describe a whole row
261 func (r table_io_waits_summary_by_table_row) String() string {
262         return fmt.Sprintf("%-30s|%10s %10s %10s %10s %10s|%10s %10s|%10s %10s %10s %10s %10s|%10s %10s",
263                 r.pretty_name(),
264                 lib.FormatTime(r.SUM_TIMER_WAIT),
265                 lib.FormatTime(r.SUM_TIMER_FETCH),
266                 lib.FormatTime(r.SUM_TIMER_INSERT),
267                 lib.FormatTime(r.SUM_TIMER_UPDATE),
268                 lib.FormatTime(r.SUM_TIMER_DELETE),
269
270                 lib.FormatTime(r.SUM_TIMER_READ),
271                 lib.FormatTime(r.SUM_TIMER_WRITE),
272
273                 lib.FormatAmount(r.COUNT_STAR),
274                 lib.FormatAmount(r.COUNT_FETCH),
275                 lib.FormatAmount(r.COUNT_INSERT),
276                 lib.FormatAmount(r.COUNT_UPDATE),
277                 lib.FormatAmount(r.COUNT_DELETE),
278
279                 lib.FormatAmount(r.COUNT_READ),
280                 lib.FormatAmount(r.COUNT_WRITE))
281 }
282
283 // describe a whole table
284 func (t table_io_waits_summary_by_table_rows) String() string {
285         s := make([]string, len(t))
286
287         for i := range t {
288                 s = append(s, t[i].String())
289         }
290
291         return strings.Join(s, "\n")
292 }