Sensibly format rows with table name on the right.
[pstop.git] / p_s / file_summary_by_instance / private.go
1 // This file contains the library routines for managing the
2 // file_summary_by_instance table.
3 package file_summary_by_instance
4
5 import (
6         "database/sql"
7         "fmt"
8         "log"
9         "regexp"
10         "sort"
11         "time"
12
13         "github.com/sjmudd/pstop/key_value_cache"
14         "github.com/sjmudd/pstop/lib"
15 )
16
17 /*
18 CREATE TABLE `file_summary_by_instance` (
19   `FILE_NAME` varchar(512) NOT NULL,
20   `EVENT_NAME` varchar(128) NOT NULL,                           // not collected
21   `OBJECT_INSTANCE_BEGIN` bigint(20) unsigned NOT NULL,         // not collected
22   `COUNT_STAR` bigint(20) unsigned NOT NULL,
23   `SUM_TIMER_WAIT` bigint(20) unsigned NOT NULL,
24   `MIN_TIMER_WAIT` bigint(20) unsigned NOT NULL,
25   `AVG_TIMER_WAIT` bigint(20) unsigned NOT NULL,
26   `MAX_TIMER_WAIT` bigint(20) unsigned NOT NULL,
27   `COUNT_READ` bigint(20) unsigned NOT NULL,
28   `SUM_TIMER_READ` bigint(20) unsigned NOT NULL,
29   `MIN_TIMER_READ` bigint(20) unsigned NOT NULL,
30   `AVG_TIMER_READ` bigint(20) unsigned NOT NULL,
31   `MAX_TIMER_READ` bigint(20) unsigned NOT NULL,
32   `SUM_NUMBER_OF_BYTES_READ` bigint(20) NOT NULL,
33   `COUNT_WRITE` bigint(20) unsigned NOT NULL,
34   `SUM_TIMER_WRITE` bigint(20) unsigned NOT NULL,
35   `MIN_TIMER_WRITE` bigint(20) unsigned NOT NULL,
36   `AVG_TIMER_WRITE` bigint(20) unsigned NOT NULL,
37   `MAX_TIMER_WRITE` bigint(20) unsigned NOT NULL,
38   `SUM_NUMBER_OF_BYTES_WRITE` bigint(20) NOT NULL,
39   `COUNT_MISC` bigint(20) unsigned NOT NULL,
40   `SUM_TIMER_MISC` bigint(20) unsigned NOT NULL,
41   `MIN_TIMER_MISC` bigint(20) unsigned NOT NULL,
42   `AVG_TIMER_MISC` bigint(20) unsigned NOT NULL,
43   `MAX_TIMER_MISC` bigint(20) unsigned NOT NULL
44 ) ENGINE=PERFORMANCE_SCHEMA DEFAULT CHARSET=utf8
45 1 row in set (0.00 sec)
46 */
47
48 //     foo/../bar --> foo/bar   perl: $new =~ s{[^/]+/\.\./}{/};
49 //     /./        --> /         perl: $new =~ s{/\./}{};
50 //     //         --> /         perl: $new =~ s{//}{/};
51 const (
52         re_encoded = `@(\d{4})` // FIXME - add me to catch @0024 --> $ for example
53 )
54
55 var (
56         re_one_or_the_other    *regexp.Regexp = regexp.MustCompile(`/(\.)?/`)
57         re_slash_dot_dot_slash *regexp.Regexp = regexp.MustCompile(`[^/]+/\.\./`)
58         re_table_file          *regexp.Regexp = regexp.MustCompile(`/([^/]+)/([^/]+)\.(frm|ibd|MYD|MYI|CSM|CSV|par)$`)
59         re_temp_table          *regexp.Regexp = regexp.MustCompile(`#sql-[0-9_]+`)
60         re_part_table          *regexp.Regexp = regexp.MustCompile(`(.+)#P#p(\d+|MAX)`)
61         re_ibdata              *regexp.Regexp = regexp.MustCompile(`/ibdata\d+$`)
62         re_redo_log            *regexp.Regexp = regexp.MustCompile(`/ib_logfile\d+$`)
63         re_binlog              *regexp.Regexp = regexp.MustCompile(`/binlog\.(\d{6}|index)$`)
64         re_db_opt              *regexp.Regexp = regexp.MustCompile(`/db\.opt$`)
65         re_slowlog             *regexp.Regexp = regexp.MustCompile(`/slowlog$`)
66         re_auto_cnf            *regexp.Regexp = regexp.MustCompile(`/auto\.cnf$`)
67         re_pid_file            *regexp.Regexp = regexp.MustCompile(`/[^/]+\.pid$`)
68         re_error_msg           *regexp.Regexp = regexp.MustCompile(`/share/[^/]+/errmsg\.sys$`)
69         re_charset             *regexp.Regexp = regexp.MustCompile(`/share/charsets/Index\.xml$`)
70         re_dollar              *regexp.Regexp = regexp.MustCompile(`@0024`) // FIXME - add me to catch @0024 --> $ (specific case)
71
72         cache key_value_cache.KeyValueCache
73 )
74
75 type table_row struct {
76         FILE_NAME string
77
78         COUNT_STAR  uint64
79         COUNT_READ  uint64
80         COUNT_WRITE uint64
81         COUNT_MISC  uint64
82
83         SUM_TIMER_WAIT  uint64
84         SUM_TIMER_READ  uint64
85         SUM_TIMER_WRITE uint64
86         SUM_TIMER_MISC  uint64
87
88         SUM_NUMBER_OF_BYTES_READ  uint64
89         SUM_NUMBER_OF_BYTES_WRITE uint64
90 }
91
92 // represents a table or set of rows
93 type table_rows []table_row
94
95 // Return the name using the FILE_NAME attribute.
96 func (r *table_row) name() string {
97         return r.FILE_NAME
98 }
99
100 func (r *table_row) headings() string {
101         return fmt.Sprintf("%10s %6s|%6s %6s %6s|%8s %8s|%8s %6s %6s %6s|%s",
102                 "Latency",
103                 "%",
104                 "Read",
105                 "Write",
106                 "Misc",
107                 "Rd bytes",
108                 "Wr bytes",
109                 "Ops",
110                 "R Ops",
111                 "W Ops",
112                 "M Ops",
113                 "Table Name")
114 }
115
116 // generate a printable result
117 func (row *table_row) row_content(totals table_row) string {
118         var name string = row.name()
119
120         // We assume that if COUNT_STAR = 0 then there's no data at all...
121         // when we have no data we really don't want to show the name either.
122         if row.COUNT_STAR == 0 && name != "Totals" {
123                 name = ""
124         }
125
126         return fmt.Sprintf("%10s %6s|%6s %6s %6s|%8s %8s|%8s %6s %6s %6s|%s",
127                 lib.FormatTime(row.SUM_TIMER_WAIT),
128                 lib.FormatPct(lib.MyDivide(row.SUM_TIMER_WAIT, totals.SUM_TIMER_WAIT)),
129                 lib.FormatPct(lib.MyDivide(row.SUM_TIMER_READ, row.SUM_TIMER_WAIT)),
130                 lib.FormatPct(lib.MyDivide(row.SUM_TIMER_WRITE, row.SUM_TIMER_WAIT)),
131                 lib.FormatPct(lib.MyDivide(row.SUM_TIMER_MISC, row.SUM_TIMER_WAIT)),
132                 lib.FormatAmount(row.SUM_NUMBER_OF_BYTES_READ),
133                 lib.FormatAmount(row.SUM_NUMBER_OF_BYTES_WRITE),
134                 lib.FormatAmount(row.COUNT_STAR),
135                 lib.FormatPct(lib.MyDivide(row.COUNT_READ, row.COUNT_STAR)),
136                 lib.FormatPct(lib.MyDivide(row.COUNT_WRITE, row.COUNT_STAR)),
137                 lib.FormatPct(lib.MyDivide(row.COUNT_MISC, row.COUNT_STAR)),
138                 name)
139 }
140
141 func (this *table_row) add(other table_row) {
142         this.COUNT_STAR += other.COUNT_STAR
143         this.COUNT_READ += other.COUNT_READ
144         this.COUNT_WRITE += other.COUNT_WRITE
145         this.COUNT_MISC += other.COUNT_MISC
146
147         this.SUM_TIMER_WAIT += other.SUM_TIMER_WAIT
148         this.SUM_TIMER_READ += other.SUM_TIMER_READ
149         this.SUM_TIMER_WRITE += other.SUM_TIMER_WRITE
150         this.SUM_TIMER_MISC += other.SUM_TIMER_MISC
151
152         this.SUM_NUMBER_OF_BYTES_READ += other.SUM_NUMBER_OF_BYTES_READ
153         this.SUM_NUMBER_OF_BYTES_WRITE += other.SUM_NUMBER_OF_BYTES_WRITE
154 }
155
156 func (this *table_row) subtract(other table_row) {
157         this.COUNT_STAR -= other.COUNT_STAR
158         this.COUNT_READ -= other.COUNT_READ
159         this.COUNT_WRITE -= other.COUNT_WRITE
160         this.COUNT_MISC -= other.COUNT_MISC
161
162         this.SUM_TIMER_WAIT -= other.SUM_TIMER_WAIT
163         this.SUM_TIMER_READ -= other.SUM_TIMER_READ
164         this.SUM_TIMER_WRITE -= other.SUM_TIMER_WRITE
165         this.SUM_TIMER_MISC -= other.SUM_TIMER_MISC
166
167         this.SUM_NUMBER_OF_BYTES_READ -= other.SUM_NUMBER_OF_BYTES_READ
168         this.SUM_NUMBER_OF_BYTES_WRITE -= other.SUM_NUMBER_OF_BYTES_WRITE
169 }
170
171 // return the totals of a slice of rows
172 func (t table_rows) totals() table_row {
173         var totals table_row
174         totals.FILE_NAME = "Totals"
175
176         for i := range t {
177                 totals.add(t[i])
178         }
179
180         return totals
181 }
182
183 // clean up the given path reducing redundant stuff and return the clean path
184 func cleanup_path(path string) string {
185
186         for {
187                 orig_path := path
188                 path = re_one_or_the_other.ReplaceAllString(path, "/")
189                 path = re_slash_dot_dot_slash.ReplaceAllString(path, "/")
190                 if orig_path == path { // no change so give up
191                         break
192                 }
193         }
194
195         return path
196 }
197
198 // From the original FILE_NAME we want to generate a simpler name to use.
199 // This simpler name may also merge several different filenames into one.
200 func (t table_row) simple_name(global_variables map[string]string) string {
201
202         path := t.FILE_NAME
203
204         if cached_result, err := cache.Get(path); err == nil {
205                 return cached_result
206         }
207
208         // @0024 --> $ (should do this more generically)
209         path = re_dollar.ReplaceAllLiteralString(path, "$")
210
211         // this should probably be ordered from most expected regexp to least
212         if m1 := re_table_file.FindStringSubmatch(path); m1 != nil {
213                 // we may match temporary tables so check for them
214                 if m2 := re_temp_table.FindStringSubmatch(m1[2]); m2 != nil {
215                         return cache.Put(path, "<temp_table>")
216                 }
217
218                 // we may match partitioned tables so check for them
219                 if m3 := re_part_table.FindStringSubmatch(m1[2]); m3 != nil {
220                         return cache.Put(path, m1[1]+"."+m3[1]) // <schema>.<table> (less partition info)
221                 }
222
223                 return cache.Put(path, m1[1]+"."+m1[2]) // <schema>.<table>
224         }
225         if re_ibdata.MatchString(path) == true {
226                 return cache.Put(path, "<ibdata>")
227         }
228         if re_redo_log.MatchString(path) == true {
229                 return cache.Put(path, "<redo_log>")
230         }
231         if re_binlog.MatchString(path) == true {
232                 return cache.Put(path, "<binlog>")
233         }
234         if re_db_opt.MatchString(path) == true {
235                 return cache.Put(path, "<db_opt>")
236         }
237         if re_slowlog.MatchString(path) == true {
238                 return cache.Put(path, "<slow_log>")
239         }
240         if re_auto_cnf.MatchString(path) == true {
241                 return cache.Put(path, "<auto_cnf>")
242         }
243         // relay logs are a bit complicated. If a full path then easy to
244         // identify,but if a relative path we may need to add $datadir,
245         // but also if as I do we have a ../blah/somewhere/path then we
246         // need to make it match too.
247         if len(global_variables["relay_log"]) > 0 {
248                 relay_log := global_variables["relay_log"]
249                 if relay_log[0] != '/' { // relative path
250                         relay_log = cleanup_path(global_variables["datadir"] + relay_log) // datadir always ends in /
251                 }
252                 re_relay_log := relay_log + `\.(\d{6}|index)$`
253                 if regexp.MustCompile(re_relay_log).MatchString(path) == true {
254                         return cache.Put(path, "<relay_log>")
255                 }
256         }
257         if re_pid_file.MatchString(path) == true {
258                 return cache.Put(path, "<pid_file>")
259         }
260         if re_error_msg.MatchString(path) == true {
261                 return cache.Put(path, "<errmsg>")
262         }
263         if re_charset.MatchString(path) == true {
264                 return cache.Put(path, "<charset>")
265         }
266         // clean up datadir to <datadir>
267         if len(global_variables["datadir"]) > 0 {
268                 re_datadir := regexp.MustCompile("^" + global_variables["datadir"])
269                 path = re_datadir.ReplaceAllLiteralString(path, "<datadir>/")
270         }
271
272         return cache.Put(path, path)
273 }
274
275 // Convert the imported "table" to a merged one with merged data.
276 // Combine all entries with the same "FILE_NAME" by adding their values.
277 func merge_by_table_name(orig table_rows, global_variables map[string]string) table_rows {
278         start := time.Now()
279         t := make(table_rows, 0, len(orig))
280
281         m := make(map[string]table_row)
282
283         // iterate over source table
284         for i := range orig {
285                 var file_name string
286                 var new_row table_row
287                 orig_row := orig[i]
288
289                 if orig_row.COUNT_STAR > 0 {
290                         file_name = orig_row.simple_name(global_variables)
291
292                         // check if we have an entry in the map
293                         if _, found := m[file_name]; found {
294                                 new_row = m[file_name]
295                         } else {
296                                 new_row.FILE_NAME = file_name
297                         }
298                         new_row.add(orig_row)
299                         m[file_name] = new_row // update the map with the new value
300                 }
301         }
302
303         // add the map contents back into the table
304         for _, row := range m {
305                 t = append(t, row)
306         }
307
308         lib.Logger.Println("merge_by_table_name() took:", time.Duration(time.Since(start)).String())
309         return t
310 }
311
312 // Select the raw data from the database into table_rows
313 // - filter out empty values
314 // - merge rows with the same name into a single row
315 // - change FILE_NAME into a more descriptive value.
316 func select_rows(dbh *sql.DB) table_rows {
317         var t table_rows
318         start := time.Now()
319
320         sql := "SELECT FILE_NAME, COUNT_STAR, SUM_TIMER_WAIT, COUNT_READ, SUM_TIMER_READ, SUM_NUMBER_OF_BYTES_READ, COUNT_WRITE, SUM_TIMER_WRITE, SUM_NUMBER_OF_BYTES_WRITE, COUNT_MISC, SUM_TIMER_MISC FROM file_summary_by_instance"
321
322         rows, err := dbh.Query(sql)
323         if err != nil {
324                 log.Fatal(err)
325         }
326         defer rows.Close()
327
328         for rows.Next() {
329                 var r table_row
330
331                 if err := rows.Scan(&r.FILE_NAME, &r.COUNT_STAR, &r.SUM_TIMER_WAIT, &r.COUNT_READ, &r.SUM_TIMER_READ, &r.SUM_NUMBER_OF_BYTES_READ, &r.COUNT_WRITE, &r.SUM_TIMER_WRITE, &r.SUM_NUMBER_OF_BYTES_WRITE, &r.COUNT_MISC, &r.SUM_TIMER_MISC); err != nil {
332                         log.Fatal(err)
333                 }
334                 t = append(t, r)
335         }
336         if err := rows.Err(); err != nil {
337                 log.Fatal(err)
338         }
339         lib.Logger.Println("select_rows() took:", time.Duration(time.Since(start)).String())
340
341         return t
342 }
343
344 // remove the initial values from those rows where there's a match
345 // - if we find a row we can't match ignore it
346 func (this *table_rows) subtract(initial table_rows) {
347         i_by_name := make(map[string]int)
348
349         // iterate over rows by name
350         for i := range initial {
351                 i_by_name[initial[i].name()] = i
352         }
353
354         for i := range *this {
355                 if _, ok := i_by_name[(*this)[i].name()]; ok {
356                         initial_i := i_by_name[(*this)[i].name()]
357                         (*this)[i].subtract(initial[initial_i])
358                 }
359         }
360 }
361
362 func (t table_rows) Len() int      { return len(t) }
363 func (t table_rows) Swap(i, j int) { t[i], t[j] = t[j], t[i] }
364 func (t table_rows) Less(i, j int) bool {
365         return (t[i].SUM_TIMER_WAIT > t[j].SUM_TIMER_WAIT) ||
366                 ((t[i].SUM_TIMER_WAIT == t[j].SUM_TIMER_WAIT) && (t[i].FILE_NAME < t[j].FILE_NAME))
367 }
368
369 func (t *table_rows) sort() {
370         sort.Sort(t)
371 }
372
373 // if the data in t2 is "newer", "has more values" than t then it needs refreshing.
374 // check this by comparing totals.
375 func (t table_rows) needs_refresh(t2 table_rows) bool {
376         my_totals := t.totals()
377         t2_totals := t2.totals()
378
379         return my_totals.SUM_TIMER_WAIT > t2_totals.SUM_TIMER_WAIT
380 }