1 package events_stages_summary_global_by_event_name
10 "github.com/sjmudd/pstop/lib"
13 /**************************************************************************
15 root@localhost [performance_schema]> show create table events_stages_summary_global_by_event_name\G
16 *************************** 1. row ***************************
17 Table: events_stages_summary_global_by_event_name
18 Create Table: CREATE TABLE `events_stages_summary_global_by_event_name` (
19 `EVENT_NAME` varchar(128) NOT NULL,
20 `COUNT_STAR` bigint(20) unsigned NOT NULL,
21 `SUM_TIMER_WAIT` bigint(20) unsigned NOT NULL,
22 `MIN_TIMER_WAIT` bigint(20) unsigned NOT NULL,
23 `AVG_TIMER_WAIT` bigint(20) unsigned NOT NULL,
24 `MAX_TIMER_WAIT` bigint(20) unsigned NOT NULL
25 ) ENGINE=PERFORMANCE_SCHEMA DEFAULT CHARSET=utf8
26 1 row in set (0.00 sec)
28 **************************************************************************/
30 type table_row struct {
36 type table_rows []table_row
38 func select_rows(dbh *sql.DB) table_rows {
41 lib.Logger.Println("events_stages_summary_global_by_event_name.select_rows()")
42 // we collect all information even if it's mainly empty as we may reference it later
43 sql := "SELECT EVENT_NAME, COUNT_STAR, SUM_TIMER_WAIT FROM events_stages_summary_global_by_event_name WHERE SUM_TIMER_WAIT > 0"
45 rows, err := dbh.Query(sql)
56 &r.SUM_TIMER_WAIT); err != nil {
61 if err := rows.Err(); err != nil {
64 lib.Logger.Println("recovered", len(t), "row(s):")
70 // if the data in t2 is "newer", "has more values" than t then it needs refreshing.
71 // check this by comparing totals.
72 func (t table_rows) needs_refresh(t2 table_rows) bool {
73 my_totals := t.totals()
74 t2_totals := t2.totals()
76 return my_totals.SUM_TIMER_WAIT > t2_totals.SUM_TIMER_WAIT
79 func (t table_rows) totals() table_row {
81 totals.EVENT_NAME = "Totals"
90 func (this *table_row) name() string {
91 return this.EVENT_NAME
94 func (r *table_row) pretty_name() string {
102 func (this *table_row) add(other table_row) {
103 this.SUM_TIMER_WAIT += other.SUM_TIMER_WAIT
104 this.COUNT_STAR += other.COUNT_STAR
107 // subtract the countable values in one row from another
108 func (this *table_row) subtract(other table_row) {
109 // check for issues here (we have a bug) and log it
110 // - this situation should not happen so there's a logic bug somewhere else
111 if this.SUM_TIMER_WAIT >= other.SUM_TIMER_WAIT {
112 this.SUM_TIMER_WAIT -= other.SUM_TIMER_WAIT
113 this.COUNT_STAR -= other.COUNT_STAR
115 lib.Logger.Println("WARNING: table_row.subtract() - subtraction problem! (not subtracting)")
116 lib.Logger.Println("this=", this)
117 lib.Logger.Println("other=", other)
121 func (t table_rows) Len() int { return len(t) }
122 func (t table_rows) Swap(i, j int) { t[i], t[j] = t[j], t[i] }
124 // sort by value (descending) but also by "name" (ascending) if the values are the same
125 func (t table_rows) Less(i, j int) bool {
126 return (t[i].SUM_TIMER_WAIT > t[j].SUM_TIMER_WAIT) ||
127 ((t[i].SUM_TIMER_WAIT == t[j].SUM_TIMER_WAIT) && (t[i].EVENT_NAME < t[j].EVENT_NAME))
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 func (r *table_row) headings() string {
154 return fmt.Sprintf("%-30s %10s %6s %6s", "Stage Name", "Latency", "%", "Count")
157 // generate a printable result
158 func (r *table_row) row_content(totals table_row) string {
159 name := r.pretty_name()
160 if r.COUNT_STAR == 0 && name != "Totals" {
164 return fmt.Sprintf("%-30s|%10s %6s %6s",
166 lib.FormatTime(r.SUM_TIMER_WAIT),
167 lib.FormatPct(lib.MyDivide(r.SUM_TIMER_WAIT, totals.SUM_TIMER_WAIT)),
168 lib.FormatAmount(r.COUNT_STAR))
171 // describe a whole row
172 func (r table_row) String() string {
173 return fmt.Sprintf("%-30s %10s %10s",
175 lib.FormatTime(r.SUM_TIMER_WAIT),
176 lib.FormatAmount(r.COUNT_STAR))
179 // describe a whole table
180 func (t table_rows) String() string {
181 s := make([]string, len(t))
184 s = append(s, t[i].String())
187 return strings.Join(s, "\n")