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, // not used
23 `AVG_TIMER_WAIT` bigint(20) unsigned NOT NULL, // not used
24 `MAX_TIMER_WAIT` bigint(20) unsigned NOT NULL // not used
25 ) ENGINE=PERFORMANCE_SCHEMA DEFAULT CHARSET=utf8
26 1 row in set (0.00 sec)
28 **************************************************************************/
31 type table_row struct {
38 type table_rows []table_row
40 // select the rows into table
41 func select_rows(dbh *sql.DB) table_rows {
44 lib.Logger.Println("events_stages_summary_global_by_event_name.select_rows()")
45 sql := "SELECT EVENT_NAME, COUNT_STAR, SUM_TIMER_WAIT FROM events_stages_summary_global_by_event_name WHERE SUM_TIMER_WAIT > 0"
47 rows, err := dbh.Query(sql)
58 &r.SUM_TIMER_WAIT); err != nil {
63 if err := rows.Err(); err != nil {
66 lib.Logger.Println("recovered", len(t), "row(s):")
72 // if the data in t2 is "newer", "has more values" than t then it needs refreshing.
73 // check this by comparing totals.
74 func (t table_rows) needs_refresh(t2 table_rows) bool {
75 my_totals := t.totals()
76 t2_totals := t2.totals()
78 return my_totals.SUM_TIMER_WAIT > t2_totals.SUM_TIMER_WAIT
81 // generate the totals of a table
82 func (t table_rows) totals() table_row {
84 totals.EVENT_NAME = "Totals"
93 // return the stage name, removing any leading stage/sql/
94 func (r *table_row) name() string {
95 if len(r.EVENT_NAME) > 10 && r.EVENT_NAME[0:10] == "stage/sql/" {
96 return r.EVENT_NAME[10:]
102 // stage name limited to 40 characters
103 func (r *table_row) pretty_name() string {
111 // add the values of one row to another one
112 func (this *table_row) add(other table_row) {
113 this.SUM_TIMER_WAIT += other.SUM_TIMER_WAIT
114 this.COUNT_STAR += other.COUNT_STAR
117 // subtract the countable values in one row from another
118 func (this *table_row) subtract(other table_row) {
119 // check for issues here (we have a bug) and log it
120 // - this situation should not happen so there's a logic bug somewhere else
121 if this.SUM_TIMER_WAIT >= other.SUM_TIMER_WAIT {
122 this.SUM_TIMER_WAIT -= other.SUM_TIMER_WAIT
123 this.COUNT_STAR -= other.COUNT_STAR
125 lib.Logger.Println("WARNING: table_row.subtract() - subtraction problem! (not subtracting)")
126 lib.Logger.Println("this=", this)
127 lib.Logger.Println("other=", other)
131 func (t table_rows) Len() int { return len(t) }
132 func (t table_rows) Swap(i, j int) { t[i], t[j] = t[j], t[i] }
134 // sort by value (descending) but also by "name" (ascending) if the values are the same
135 func (t table_rows) Less(i, j int) bool {
136 return (t[i].SUM_TIMER_WAIT > t[j].SUM_TIMER_WAIT) ||
137 ((t[i].SUM_TIMER_WAIT == t[j].SUM_TIMER_WAIT) && (t[i].EVENT_NAME < t[j].EVENT_NAME))
140 func (t table_rows) Sort() {
144 // remove the initial values from those rows where there's a match
145 // - if we find a row we can't match ignore it
146 func (this *table_rows) subtract(initial table_rows) {
147 initial_by_name := make(map[string]int)
149 // iterate over rows by name
150 for i := range initial {
151 initial_by_name[initial[i].name()] = i
154 for i := range *this {
155 this_name := (*this)[i].name()
156 if _, ok := initial_by_name[this_name]; ok {
157 initial_index := initial_by_name[this_name]
158 (*this)[i].subtract(initial[initial_index])
164 func (r *table_row) headings() string {
165 return fmt.Sprintf("%-40s|%10s %6s %8s|", "Stage Name", "Latency", "%", "Counter")
168 // generate a printable result
169 func (r *table_row) row_content(totals table_row) string {
170 name := r.pretty_name()
171 if r.COUNT_STAR == 0 && name != "Totals" {
175 return fmt.Sprintf("%-40s|%10s %6s %8s|",
177 lib.FormatTime(r.SUM_TIMER_WAIT),
178 lib.FormatPct(lib.MyDivide(r.SUM_TIMER_WAIT, totals.SUM_TIMER_WAIT)),
179 lib.FormatAmount(r.COUNT_STAR))
182 // describe a whole row
183 func (r table_row) String() string {
184 return fmt.Sprintf("%-40s %10s %10s",
186 lib.FormatTime(r.SUM_TIMER_WAIT),
187 lib.FormatAmount(r.COUNT_STAR))
190 // describe a whole table
191 func (t table_rows) String() string {
192 s := make([]string, len(t))
195 s = append(s, t[i].String())
198 return strings.Join(s, "\n")