Adapt to collect stage information
[pstop.git] / p_s / events_stages_summary_global_by_event_name / public.go
1 package events_stages_summary_global_by_event_name
2
3 import (
4         "database/sql"
5         "fmt"
6         "time"
7
8         "github.com/sjmudd/pstop/lib"
9         "github.com/sjmudd/pstop/p_s"
10 )
11
12 /*
13
14 root@localhost [performance_schema]> select * from events_stages_summary_global_by_event_name where sum_timer_wait > 0;
15 +--------------------------------+------------+----------------+----------------+----------------+----------------+
16 | EVENT_NAME                     | COUNT_STAR | SUM_TIMER_WAIT | MIN_TIMER_WAIT | AVG_TIMER_WAIT | MAX_TIMER_WAIT |
17 +--------------------------------+------------+----------------+----------------+----------------+----------------+
18 | stage/sql/After create         |          3 |       21706000 |         558000 |        7235000 |       11693000 |
19 | stage/sql/checking permissions |       5971 |    92553236000 |         406000 |       15500000 |    12727728000 |
20 | stage/sql/cleaning up          |       6531 |     4328103000 |         154000 |         662000 |       23464000 |
21 | stage/sql/closing tables       |       4281 |    18303106000 |         118000 |        4275000 |       71505000 |
22 | stage/sql/Creating sort index  |          2 |    31300648000 |    14183237000 |    15650324000 |    17117411000 |
23 | stage/sql/creating table       |          2 |   138276471000 |    64077127000 |    69138235000 |    74199344000 |
24 | stage/sql/end                  |       4254 |     9940694000 |         220000 |        2336000 |       42683000 |
25 | stage/sql/executing            |       1256 |   252300800000 |         151000 |      200876000 |    59564212000 |
26 | stage/sql/freeing items        |       3733 |    83966405000 |        5341000 |       22493000 |     2527549000 |
27 | stage/sql/init                 |       4256 |    63836793000 |        1990000 |       14999000 |     7656920000 |
28 | stage/sql/Opening tables       |       6002 |  1489653915000 |        1411000 |      248192000 |   216300236000 |
29 | stage/sql/optimizing           |       1257 |  2685426016000 |         255000 |     2136377000 |  2656149827000 |
30 | stage/sql/preparing            |       1166 |     8626237000 |        1666000 |        7398000 |       91804000 |
31 | stage/sql/query end            |       4280 |    37299265000 |         411000 |        8714000 |    12018400000 |
32 | stage/sql/removing tmp table   |       1187 |    11890909000 |        1838000 |       10017000 |     2365358000 |
33 | stage/sql/Sending data         |       1165 |  3071893676000 |        2925000 |     2636818000 |    63354201000 |
34 | stage/sql/Sorting result       |          2 |        4128000 |        1930000 |        2064000 |        2198000 |
35 | stage/sql/statistics           |       1166 |    26655651000 |        2078000 |       22860000 |     8446818000 |
36 | stage/sql/System lock          |       4263 |  1901250693000 |         584000 |      445988000 |  1651181465000 |
37 | stage/sql/update               |          4 |     8246608000 |       78145000 |     2061652000 |     7597263000 |
38 | stage/sql/updating             |       2994 |  1608420140000 |      285867000 |      537214000 |    15651495000 |
39 | stage/sql/starting             |       6532 |   364087027000 |        2179000 |       55738000 |    23420395000 |
40 +--------------------------------+------------+----------------+----------------+----------------+----------------+
41 22 rows in set (0.01 sec)
42
43 */
44
45 // public view of object
46 type Object struct {
47         p_s.RelativeStats
48         p_s.InitialTime
49         initial table_rows // initial data for relative values
50         current table_rows // last loaded values
51         results table_rows // results (maybe with subtraction)
52         totals  table_row  // totals of results
53 }
54
55 // Collect() collects data from the db, updating initial
56 // values if needed, and then subtracting initial values if we want
57 // relative values, after which it stores totals.
58 func (t *Object) Collect(dbh *sql.DB) {
59         start := time.Now()
60         t.current = select_rows(dbh)
61         lib.Logger.Println("t.current collected", len(t.current), "row(s) from SELECT")
62
63         if len(t.initial) == 0 && len(t.current) > 0 {
64                 lib.Logger.Println("t.initial: copying from t.current (initial setup)")
65                 t.initial = make(table_rows, len(t.current))
66                 copy(t.initial, t.current)
67         }
68
69         // check for reload initial characteristics
70         if t.initial.needs_refresh(t.current) {
71                 lib.Logger.Println("t.initial: copying from t.current (data needs refreshing)")
72                 t.initial = make(table_rows, len(t.current))
73                 copy(t.initial, t.current)
74         }
75
76         t.make_results()
77
78         // lib.Logger.Println( "t.initial:", t.initial )
79         // lib.Logger.Println( "t.current:", t.current )
80         lib.Logger.Println("t.initial.totals():", t.initial.totals())
81         lib.Logger.Println("t.current.totals():", t.current.totals())
82         // lib.Logger.Println("t.results:", t.results)
83         // lib.Logger.Println("t.totals:", t.totals)
84         lib.Logger.Println("Table_io_waits_summary_by_table.Collect() END, took:", time.Duration(time.Since(start)).String())
85 }
86
87 func (t *Object) Headings() string {
88         return t.totals.headings()
89 }
90
91 func (t Object) RowContent(max_rows int) []string {
92         rows := make([]string, 0, max_rows)
93
94         for i := range t.results {
95                 if i < max_rows {
96                         rows = append(rows, t.results[i].row_content(t.totals))
97                 }
98         }
99
100         return rows
101 }
102
103 func (t Object) EmptyRowContent() string {
104         var e table_row
105
106         return e.row_content(e)
107 }
108
109 func (t Object) TotalRowContent() string {
110         return t.totals.row_content(t.totals)
111 }
112
113 func (t Object) Description() string {
114         count := t.count_rows()
115         return fmt.Sprintf("Latency by SQL stage (events_stages_summary_global_by_event_name) %d rows", count)
116 }
117
118 func (t *Object) SyncReferenceValues() {
119 }
120
121 func (t *Object) make_results() {
122         // lib.Logger.Println( "- t.results set from t.current" )
123         t.results = make(table_rows, len(t.current))
124         copy(t.results, t.current)
125         if t.WantRelativeStats() {
126                 t.results.subtract(t.initial)
127         }
128
129         t.results.Sort()
130         t.totals = t.results.totals()
131 }
132
133 func (t Object) count_rows() int {
134         var count int
135         for row := range t.results {
136                 if t.results[row].SUM_TIMER_WAIT > 0 {
137                         count++
138                 }
139         }
140         return count
141 }