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) pretty_name() string {
44 func (r *table_row) headings() string {
45 return fmt.Sprintf("%-30s %10s %6s %6s", "Mutex Name", "Latency", "MtxCnt", "%")
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" {
55 return fmt.Sprintf("%-30s|%10s %6s %6s",
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)))
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
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
75 lib.Logger.Println("WARNING: table_row.subtract() - subtraction problem! (not subtracting)")
76 lib.Logger.Println("this=", this)
77 lib.Logger.Println("other=", other)
81 func (t table_rows) totals() table_row {
83 totals.EVENT_NAME = "Totals"
92 func select_rows(dbh *sql.DB) table_rows {
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/%'"
98 rows, err := dbh.Query(sql)
109 &r.COUNT_STAR); err != nil {
112 // we collect all information even if it's mainly empty as we may reference it later
115 if err := rows.Err(); err != nil {
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] }
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
130 func (t table_rows) Sort() {
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)
139 // iterate over rows by name
140 for i := range initial {
141 initial_by_name[initial[i].name()] = i
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])
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()
159 return my_totals.SUM_TIMER_WAIT > t2_totals.SUM_TIMER_WAIT
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",
166 lib.FormatTime(r.SUM_TIMER_WAIT),
167 lib.FormatAmount(r.COUNT_STAR))
170 // describe a whole table
171 func (t table_rows) String() string {
172 s := make([]string, len(t))
175 s = append(s, t[i].String())
178 return strings.Join(s, "\n")