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 // add the values of one row to another one
103 func (this *table_row) add(other table_row) {
104 this.SUM_TIMER_WAIT += other.SUM_TIMER_WAIT
105 this.COUNT_STAR += other.COUNT_STAR
108 // subtract the countable values in one row from another
109 func (this *table_row) subtract(other table_row) {
110 // check for issues here (we have a bug) and log it
111 // - this situation should not happen so there's a logic bug somewhere else
112 if this.SUM_TIMER_WAIT >= other.SUM_TIMER_WAIT {
113 this.SUM_TIMER_WAIT -= other.SUM_TIMER_WAIT
114 this.COUNT_STAR -= other.COUNT_STAR
116 lib.Logger.Println("WARNING: table_row.subtract() - subtraction problem! (not subtracting)")
117 lib.Logger.Println("this=", this)
118 lib.Logger.Println("other=", other)
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) ||
128 ((t[i].SUM_TIMER_WAIT == t[j].SUM_TIMER_WAIT) && (t[i].EVENT_NAME < t[j].EVENT_NAME))
131 func (t table_rows) Sort() {
135 // remove the initial values from those rows where there's a match
136 // - if we find a row we can't match ignore it
137 func (this *table_rows) subtract(initial table_rows) {
138 initial_by_name := make(map[string]int)
140 // iterate over rows by name
141 for i := range initial {
142 initial_by_name[initial[i].name()] = i
145 for i := range *this {
146 this_name := (*this)[i].name()
147 if _, ok := initial_by_name[this_name]; ok {
148 initial_index := initial_by_name[this_name]
149 (*this)[i].subtract(initial[initial_index])
155 func (r *table_row) headings() string {
156 return fmt.Sprintf("%10s %6s %8s|%s", "Latency", "%", "Counter", "Stage Name")
159 // generate a printable result
160 func (r *table_row) row_content(totals table_row) string {
162 if r.COUNT_STAR == 0 && name != "Totals" {
166 return fmt.Sprintf("%10s %6s %8s|%s",
167 lib.FormatTime(r.SUM_TIMER_WAIT),
168 lib.FormatPct(lib.MyDivide(r.SUM_TIMER_WAIT, totals.SUM_TIMER_WAIT)),
169 lib.FormatAmount(r.COUNT_STAR),
173 // describe a whole row
174 func (r table_row) String() string {
175 return fmt.Sprintf("%10s %10s %s",
176 lib.FormatTime(r.SUM_TIMER_WAIT),
177 lib.FormatAmount(r.COUNT_STAR),
181 // describe a whole table
182 func (t table_rows) String() string {
183 s := make([]string, len(t))
186 s = append(s, t[i].String())
189 return strings.Join(s, "\n")