Initial import of pstop v0.0.10
[pstop.git] / performance_schema / file_summary_by_instance / file_summary_by_instance.go
1 // performance_schema - library routines for pstop.
2 //
3 // This file contains the library routines for managing the
4 // file_summary_by_instance table.
5 package file_summary_by_instance
6
7 import (
8         "database/sql"
9
10         //      "github.com/sjmudd/pstop/lib"
11         ps "github.com/sjmudd/pstop/performance_schema"
12 )
13
14 /*
15 CREATE TABLE `file_summary_by_instance` (
16   `FILE_NAME` varchar(512) NOT NULL,
17   `EVENT_NAME` varchar(128) NOT NULL,                           // not collected
18   `OBJECT_INSTANCE_BEGIN` bigint(20) unsigned NOT NULL,         // not collected
19   `COUNT_STAR` bigint(20) unsigned NOT NULL,
20   `SUM_TIMER_WAIT` bigint(20) unsigned NOT NULL,
21   `MIN_TIMER_WAIT` bigint(20) unsigned NOT NULL,
22   `AVG_TIMER_WAIT` bigint(20) unsigned NOT NULL,
23   `MAX_TIMER_WAIT` bigint(20) unsigned NOT NULL,
24   `COUNT_READ` bigint(20) unsigned NOT NULL,
25   `SUM_TIMER_READ` bigint(20) unsigned NOT NULL,
26   `MIN_TIMER_READ` bigint(20) unsigned NOT NULL,
27   `AVG_TIMER_READ` bigint(20) unsigned NOT NULL,
28   `MAX_TIMER_READ` bigint(20) unsigned NOT NULL,
29   `SUM_NUMBER_OF_BYTES_READ` bigint(20) NOT NULL,
30   `COUNT_WRITE` bigint(20) unsigned NOT NULL,
31   `SUM_TIMER_WRITE` bigint(20) unsigned NOT NULL,
32   `MIN_TIMER_WRITE` bigint(20) unsigned NOT NULL,
33   `AVG_TIMER_WRITE` bigint(20) unsigned NOT NULL,
34   `MAX_TIMER_WRITE` bigint(20) unsigned NOT NULL,
35   `SUM_NUMBER_OF_BYTES_WRITE` bigint(20) NOT NULL,
36   `COUNT_MISC` bigint(20) unsigned NOT NULL,
37   `SUM_TIMER_MISC` bigint(20) unsigned NOT NULL,
38   `MIN_TIMER_MISC` bigint(20) unsigned NOT NULL,
39   `AVG_TIMER_MISC` bigint(20) unsigned NOT NULL,
40   `MAX_TIMER_MISC` bigint(20) unsigned NOT NULL
41 ) ENGINE=PERFORMANCE_SCHEMA DEFAULT CHARSET=utf8
42 1 row in set (0.00 sec)
43
44 */
45
46 // a table of rows
47 type File_summary_by_instance struct {
48         ps.RelativeStats
49         ps.InitialTime
50         initial          file_summary_by_instance_rows
51         current          file_summary_by_instance_rows
52         results          file_summary_by_instance_rows
53         totals           file_summary_by_instance_row
54         global_variables map[string]string
55 }
56
57 // reset the statistics to current values
58 func (t *File_summary_by_instance) UpdateInitialValues() {
59         t.SetNow()
60         t.initial = make(file_summary_by_instance_rows, len(t.current))
61         copy(t.initial, t.current)
62
63         t.results = make(file_summary_by_instance_rows, len(t.current))
64         copy(t.results, t.current)
65
66         if t.WantRelativeStats() {
67                 t.results.subtract(t.initial) // should be 0 if relative
68         }
69
70         t.results.sort()
71         t.totals = t.results.totals()
72 }
73
74 // Collect data from the db, then merge it in.
75 func (t *File_summary_by_instance) Collect(dbh *sql.DB) {
76         // UPDATE current from db handle
77         t.current = merge_by_table_name(select_fsbi_rows(dbh), t.global_variables)
78
79         // copy in initial data if it was not there
80         if len(t.initial) == 0 && len(t.current) > 0 {
81                 t.initial = make(file_summary_by_instance_rows, len(t.current))
82                 copy(t.initial, t.current)
83         }
84
85         // check for reload initial characteristics
86         if t.initial.needs_refresh(t.current) {
87                 t.initial = make(file_summary_by_instance_rows, len(t.current))
88                 copy(t.initial, t.current)
89         }
90
91         // update results to current value
92         t.results = make(file_summary_by_instance_rows, len(t.current))
93         copy(t.results, t.current)
94
95         // make relative if need be
96         if t.WantRelativeStats() {
97                 t.results.subtract(t.initial)
98         }
99
100         // sort the results
101         t.results.sort()
102
103         // setup the totals
104         t.totals = t.results.totals()
105 }
106
107 // return the headings for a table
108 func (t File_summary_by_instance) Headings() string {
109         var r file_summary_by_instance_row
110
111         return r.headings()
112 }
113
114 // return the rows we need for displaying
115 func (t File_summary_by_instance) RowContent(max_rows int) []string {
116         rows := make([]string, 0, max_rows)
117
118         for i := range t.results {
119                 if i < max_rows {
120                         rows = append(rows, t.results[i].row_content(t.totals))
121                 }
122         }
123
124         return rows
125 }
126
127 // return all the totals
128 func (t File_summary_by_instance) TotalRowContent() string {
129         return t.totals.row_content(t.totals)
130 }
131
132 // return an empty string of data (for filling in)
133 func (t File_summary_by_instance) EmptyRowContent() string {
134         var emtpy file_summary_by_instance_row
135         return emtpy.row_content(emtpy)
136 }
137
138 func (t File_summary_by_instance) Description() string {
139         return "File I/O by filename (file_summary_by_instance)"
140 }
141
142 // create a new structure and include various variable values:
143 // - datadir, relay_log
144 // There's no checking that these are actually provided!
145 func NewFileSummaryByInstance(global_variables map[string]string) *File_summary_by_instance {
146         n := new(File_summary_by_instance)
147
148         n.global_variables = global_variables
149
150         return n
151 }