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