Sensibly format rows with table name on the right.
[pstop.git] / p_s / events_waits_summary_global_by_event_name / private.go
1 // This file contains the library routines for managing the
2 // table_io_waits_by_table table.
3 package events_waits_summary_global_by_event_name
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.events_waits_summary_global_by_event_name
16 // Note: upper case names to match the performance_schema column names.
17 // This type is _not_ meant to be exported.
18 type table_row struct {
19         EVENT_NAME     string
20         SUM_TIMER_WAIT uint64
21         COUNT_STAR     uint64
22 }
23 type table_rows []table_row
24
25 // trim off the leading 'wait/synch/mutex/innodb/'
26 func (r *table_row) name() string {
27         var n string
28         if r.EVENT_NAME == "Totals" {
29                 n += r.EVENT_NAME
30         } else if len(r.EVENT_NAME) >= 24 {
31                 n += r.EVENT_NAME[24:]
32         }
33         return n
34 }
35
36 func (r *table_row) headings() string {
37         return fmt.Sprintf("%10s %6s %6s %s", "Latency", "MtxCnt", "%", "Mutex Name")
38 }
39
40 // generate a printable result
41 func (r *table_row) row_content(totals table_row) string {
42         name := r.name()
43         if r.COUNT_STAR == 0 && name != "Totals" {
44                 name = ""
45         }
46
47         return fmt.Sprintf("%10s %6s %6s|%s",
48                 lib.FormatTime(r.SUM_TIMER_WAIT),
49                 lib.FormatAmount(r.COUNT_STAR),
50                 lib.FormatPct(lib.MyDivide(r.SUM_TIMER_WAIT, totals.SUM_TIMER_WAIT)),
51                 name)
52 }
53
54 func (this *table_row) add(other table_row) {
55         this.SUM_TIMER_WAIT += other.SUM_TIMER_WAIT
56         this.COUNT_STAR += other.COUNT_STAR
57 }
58
59 // subtract the countable values in one row from another
60 func (this *table_row) subtract(other table_row) {
61         // check for issues here (we have a bug) and log it
62         // - this situation should not happen so there's a logic bug somewhere else
63         if this.SUM_TIMER_WAIT >= other.SUM_TIMER_WAIT {
64                 this.SUM_TIMER_WAIT -= other.SUM_TIMER_WAIT
65                 this.COUNT_STAR -= other.COUNT_STAR
66         } else {
67                 lib.Logger.Println("WARNING: table_row.subtract() - subtraction problem! (not subtracting)")
68                 lib.Logger.Println("this=", this)
69                 lib.Logger.Println("other=", other)
70         }
71 }
72
73 func (t table_rows) totals() table_row {
74         var totals table_row
75         totals.EVENT_NAME = "Totals"
76
77         for i := range t {
78                 totals.add(t[i])
79         }
80
81         return totals
82 }
83
84 func select_rows(dbh *sql.DB) table_rows {
85         var t table_rows
86
87         // we collect all information even if it's mainly empty as we may reference it later
88         sql := "SELECT EVENT_NAME, SUM_TIMER_WAIT, COUNT_STAR FROM events_waits_summary_global_by_event_name WHERE SUM_TIMER_WAIT > 0 AND EVENT_NAME LIKE 'wait/synch/mutex/innodb/%'"
89
90         rows, err := dbh.Query(sql)
91         if err != nil {
92                 log.Fatal(err)
93         }
94         defer rows.Close()
95
96         for rows.Next() {
97                 var r table_row
98                 if err := rows.Scan(
99                         &r.EVENT_NAME,
100                         &r.SUM_TIMER_WAIT,
101                         &r.COUNT_STAR); err != nil {
102                         log.Fatal(err)
103                 }
104                 // we collect all information even if it's mainly empty as we may reference it later
105                 t = append(t, r)
106         }
107         if err := rows.Err(); err != nil {
108                 log.Fatal(err)
109         }
110
111         return t
112 }
113
114 func (t table_rows) Len() int      { return len(t) }
115 func (t table_rows) Swap(i, j int) { t[i], t[j] = t[j], t[i] }
116
117 // sort by value (descending) but also by "name" (ascending) if the values are the same
118 func (t table_rows) Less(i, j int) bool {
119         return t[i].SUM_TIMER_WAIT > t[j].SUM_TIMER_WAIT
120 }
121
122 func (t table_rows) Sort() {
123         sort.Sort(t)
124 }
125
126 // remove the initial values from those rows where there's a match
127 // - if we find a row we can't match ignore it
128 func (this *table_rows) subtract(initial table_rows) {
129         initial_by_name := make(map[string]int)
130
131         // iterate over rows by name
132         for i := range initial {
133                 initial_by_name[initial[i].name()] = i
134         }
135
136         for i := range *this {
137                 this_name := (*this)[i].name()
138                 if _, ok := initial_by_name[this_name]; ok {
139                         initial_index := initial_by_name[this_name]
140                         (*this)[i].subtract(initial[initial_index])
141                 }
142         }
143 }
144
145 // if the data in t2 is "newer", "has more values" than t then it needs refreshing.
146 // check this by comparing totals.
147 func (t table_rows) needs_refresh(t2 table_rows) bool {
148         my_totals := t.totals()
149         t2_totals := t2.totals()
150
151         return my_totals.SUM_TIMER_WAIT > t2_totals.SUM_TIMER_WAIT
152 }
153
154 // describe a whole row
155 func (r table_row) String() string {
156         return fmt.Sprintf("%10s %10s %10s %10s %10s|%10s %10s|%10s %10s %10s %10s %10s|%10s %10s|%s",
157                 lib.FormatTime(r.SUM_TIMER_WAIT),
158                 lib.FormatAmount(r.COUNT_STAR),
159                 r.name())
160 }
161
162 // describe a whole table
163 func (t table_rows) String() string {
164         s := make([]string, len(t))
165
166         for i := range t {
167                 s = append(s, t[i].String())
168         }
169
170         return strings.Join(s, "\n")
171 }