1 // This file contains the library routines for managing the
2 // file_summary_by_instance table.
3 package file_summary_by_instance
14 "github.com/sjmudd/pstop/lib"
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)
48 // foo/../bar --> foo/bar perl: $new =~ s{[^/]+/\.\./}{/};
49 // /./ --> / perl: $new =~ s{/\./}{};
50 // // --> / perl: $new =~ s{//}{/};
52 re_encoded = `@(\d{4})` // FIXME - add me to catch @0024 --> $ for example
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$`)
72 type file_summary_by_instance_row struct {
82 SUM_TIMER_WRITE uint64
85 SUM_NUMBER_OF_BYTES_READ uint64
86 SUM_NUMBER_OF_BYTES_WRITE uint64
89 // represents a table or set of rows
90 type file_summary_by_instance_rows []file_summary_by_instance_row
92 // Return the name using the FILE_NAME attribute.
93 func (r *file_summary_by_instance_row) name() string {
97 // Return a formatted pretty name for the row.
98 func (r *file_summary_by_instance_row) pretty_name() string {
103 return fmt.Sprintf("%-30s", s)
106 func (r *file_summary_by_instance_row) headings() string {
107 return fmt.Sprintf("%-30s %10s %6s|%6s %6s %6s|%8s %8s|%8s %6s %6s %6s",
122 // generate a printable result
123 func (row *file_summary_by_instance_row) row_content(totals file_summary_by_instance_row) string {
126 // We assume that if COUNT_STAR = 0 then there's no data at all...
127 // when we have no data we really don't want to show the name either.
128 if row.COUNT_STAR == 0 {
131 name = row.pretty_name()
134 return fmt.Sprintf("%-30s %10s %6s|%6s %6s %6s|%8s %8s|%8s %6s %6s %6s",
136 lib.FormatTime(row.SUM_TIMER_WAIT),
137 lib.FormatPct(lib.MyDivide(row.SUM_TIMER_WAIT, totals.SUM_TIMER_WAIT)),
138 lib.FormatPct(lib.MyDivide(row.SUM_TIMER_READ, row.SUM_TIMER_WAIT)),
139 lib.FormatPct(lib.MyDivide(row.SUM_TIMER_WRITE, row.SUM_TIMER_WAIT)),
140 lib.FormatPct(lib.MyDivide(row.SUM_TIMER_MISC, row.SUM_TIMER_WAIT)),
141 lib.FormatAmount(row.SUM_NUMBER_OF_BYTES_READ),
142 lib.FormatAmount(row.SUM_NUMBER_OF_BYTES_WRITE),
143 lib.FormatAmount(row.COUNT_STAR),
144 lib.FormatPct(lib.MyDivide(row.COUNT_READ, row.COUNT_STAR)),
145 lib.FormatPct(lib.MyDivide(row.COUNT_WRITE, row.COUNT_STAR)),
146 lib.FormatPct(lib.MyDivide(row.COUNT_MISC, row.COUNT_STAR)))
149 func (this *file_summary_by_instance_row) add(other file_summary_by_instance_row) {
150 this.COUNT_STAR += other.COUNT_STAR
151 this.COUNT_READ += other.COUNT_READ
152 this.COUNT_WRITE += other.COUNT_WRITE
153 this.COUNT_MISC += other.COUNT_MISC
155 this.SUM_TIMER_WAIT += other.SUM_TIMER_WAIT
156 this.SUM_TIMER_READ += other.SUM_TIMER_READ
157 this.SUM_TIMER_WRITE += other.SUM_TIMER_WRITE
158 this.SUM_TIMER_MISC += other.SUM_TIMER_MISC
160 this.SUM_NUMBER_OF_BYTES_READ += other.SUM_NUMBER_OF_BYTES_READ
161 this.SUM_NUMBER_OF_BYTES_WRITE += other.SUM_NUMBER_OF_BYTES_WRITE
164 func (this *file_summary_by_instance_row) subtract(other file_summary_by_instance_row) {
165 this.COUNT_STAR -= other.COUNT_STAR
166 this.COUNT_READ -= other.COUNT_READ
167 this.COUNT_WRITE -= other.COUNT_WRITE
168 this.COUNT_MISC -= other.COUNT_MISC
170 this.SUM_TIMER_WAIT -= other.SUM_TIMER_WAIT
171 this.SUM_TIMER_READ -= other.SUM_TIMER_READ
172 this.SUM_TIMER_WRITE -= other.SUM_TIMER_WRITE
173 this.SUM_TIMER_MISC -= other.SUM_TIMER_MISC
175 this.SUM_NUMBER_OF_BYTES_READ -= other.SUM_NUMBER_OF_BYTES_READ
176 this.SUM_NUMBER_OF_BYTES_WRITE -= other.SUM_NUMBER_OF_BYTES_WRITE
179 // return the totals of a slice of rows
180 func (t file_summary_by_instance_rows) totals() file_summary_by_instance_row {
181 var totals file_summary_by_instance_row
182 totals.FILE_NAME = "TOTALS"
191 // clean up the given path reducing redundant stuff and return the clean path
192 func cleanup_path(path string) string {
196 path = re_one_or_the_other.ReplaceAllString(path, "/")
197 path = re_slash_dot_dot_slash.ReplaceAllString(path, "/")
198 if orig_path == path { // no change so give up
206 // From the original FILE_NAME we want to generate a simpler name to use.
207 // This simpler name may also merge several different filenames into one.
208 func (t file_summary_by_instance_row) simple_name(global_variables map[string]string) string {
212 // FIXME and make this work.
213 // re4 := regexp.MustCompile(re_encoded)
214 // if m4 := re4.FindStringSubmatch(path); m4 != nil {
215 // if value, err := strconv.ParseInt(m4[1], 16, 16); err != nil {
216 // // missing replace @.... with char(value) in path
221 // this should probably be ordered from most expected regexp to least
222 if m1 := re_table_file.FindStringSubmatch(path); m1 != nil {
223 // we may match temporary tables so check for them
224 if m2 := re_temp_table.FindStringSubmatch(m1[2]); m2 != nil {
225 return "<temp_table>"
228 // we may match partitioned tables so check for them
229 if m3 := re_part_table.FindStringSubmatch(m1[2]); m3 != nil {
230 return m1[1] + "." + m3[1] // <schema>.<table> (less partition info)
233 return m1[1] + "." + m1[2] // <schema>.<table>
235 if re_ibdata.MatchString(path) == true {
238 if re_redo_log.MatchString(path) == true {
241 if re_binlog.MatchString(path) == true {
244 if re_db_opt.MatchString(path) == true {
247 if re_slowlog.MatchString(path) == true {
250 if re_auto_cnf.MatchString(path) == true {
253 // relay logs are a bit complicated. If a full path then easy to
254 // identify,but if a relative path we may need to add $datadir,
255 // but also if as I do we have a ../blah/somewhere/path then we
256 // need to make it match too.
257 if len(global_variables["relay_log"]) > 0 {
258 relay_log := global_variables["relay_log"]
259 if relay_log[0] != '/' { // relative path
260 relay_log = cleanup_path(global_variables["datadir"] + relay_log) // datadir always ends in /
262 re_relay_log := relay_log + `\.(\d{6}|index)$`
263 if regexp.MustCompile(re_relay_log).MatchString(path) == true {
267 if re_pid_file.MatchString(path) == true {
270 if re_error_msg.MatchString(path) == true {
273 if re_charset.MatchString(path) == true {
279 // Convert the imported "table" to a merged one with merged data.
280 // Combine all entries with the same "FILE_NAME" by adding their values.
281 func merge_by_table_name(orig file_summary_by_instance_rows, global_variables map[string]string) file_summary_by_instance_rows {
283 t := make(file_summary_by_instance_rows, 0, len(orig))
285 m := make(map[string]file_summary_by_instance_row)
287 // iterate over source table
288 for i := range orig {
290 var new_row file_summary_by_instance_row
293 if orig_row.COUNT_STAR > 0 {
294 file_name = orig_row.simple_name(global_variables)
296 // check if we have an entry in the map
297 if _, found := m[file_name]; found {
298 new_row = m[file_name]
300 new_row.FILE_NAME = file_name
302 new_row.add(orig_row)
303 m[file_name] = new_row // update the map with the new value
307 // add the map contents back into the table
308 for _, row := range m {
312 lib.Logger.Println("merge_by_table_name() took:", time.Duration(time.Since(start)).String())
316 // Select the raw data from the database into file_summary_by_instance_rows
317 // - filter out empty values
318 // - merge rows with the same name into a single row
319 // - change FILE_NAME into a more descriptive value.
320 func select_fsbi_rows(dbh *sql.DB) file_summary_by_instance_rows {
321 var t file_summary_by_instance_rows
324 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"
326 rows, err := dbh.Query(sql)
333 var r file_summary_by_instance_row
335 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 {
340 if err := rows.Err(); err != nil {
343 lib.Logger.Println("select_fsbi_rows() took:", time.Duration(time.Since(start)).String())
348 // remove the initial values from those rows where there's a match
349 // - if we find a row we can't match ignore it
350 func (this *file_summary_by_instance_rows) subtract(initial file_summary_by_instance_rows) {
351 i_by_name := make(map[string]int)
353 // iterate over rows by name
354 for i := range initial {
355 i_by_name[initial[i].name()] = i
358 for i := range *this {
359 if _, ok := i_by_name[(*this)[i].name()]; ok {
360 initial_i := i_by_name[(*this)[i].name()]
361 (*this)[i].subtract(initial[initial_i])
366 func (t file_summary_by_instance_rows) Len() int { return len(t) }
367 func (t file_summary_by_instance_rows) Swap(i, j int) { t[i], t[j] = t[j], t[i] }
368 func (t file_summary_by_instance_rows) Less(i, j int) bool {
369 return (t[i].SUM_TIMER_WAIT > t[j].SUM_TIMER_WAIT) ||
370 ((t[i].SUM_TIMER_WAIT == t[j].SUM_TIMER_WAIT) && (t[i].FILE_NAME < t[j].FILE_NAME))
373 func (t *file_summary_by_instance_rows) sort() {
377 // if the data in t2 is "newer", "has more values" than t then it needs refreshing.
378 // check this by comparing totals.
379 func (t file_summary_by_instance_rows) needs_refresh(t2 file_summary_by_instance_rows) bool {
380 my_totals := t.totals()
381 t2_totals := t2.totals()
383 return my_totals.SUM_TIMER_WAIT > t2_totals.SUM_TIMER_WAIT