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
12 "github.com/sjmudd/pstop/lib"
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 {
23 type table_rows []table_row
25 // trim off the leading 'wait/synch/mutex/innodb/'
26 func (r *table_row) name() string {
28 if r.EVENT_NAME == "Totals" {
30 } else if len(r.EVENT_NAME) >= 24 {
31 n += r.EVENT_NAME[24:]
36 func (r *table_row) headings() string {
37 return fmt.Sprintf("%10s %6s %6s %s", "Latency", "MtxCnt", "%", "Mutex Name")
40 // generate a printable result
41 func (r *table_row) row_content(totals table_row) string {
43 if r.COUNT_STAR == 0 && name != "Totals" {
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)),
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
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
67 lib.Logger.Println("WARNING: table_row.subtract() - subtraction problem! (not subtracting)")
68 lib.Logger.Println("this=", this)
69 lib.Logger.Println("other=", other)
73 func (t table_rows) totals() table_row {
75 totals.EVENT_NAME = "Totals"
84 func select_rows(dbh *sql.DB) table_rows {
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/%'"
90 rows, err := dbh.Query(sql)
101 &r.COUNT_STAR); err != nil {
104 // we collect all information even if it's mainly empty as we may reference it later
107 if err := rows.Err(); err != nil {
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] }
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
122 func (t table_rows) Sort() {
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)
131 // iterate over rows by name
132 for i := range initial {
133 initial_by_name[initial[i].name()] = i
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])
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()
151 return my_totals.SUM_TIMER_WAIT > t2_totals.SUM_TIMER_WAIT
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),
162 // describe a whole table
163 func (t table_rows) String() string {
164 s := make([]string, len(t))
167 s = append(s, t[i].String())
170 return strings.Join(s, "\n")