/
row.go
315 lines (283 loc) · 10.5 KB
/
row.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
// Package file_io_latency contains the routines for
// managing the file_summary_by_instance table.
package file_io_latency
import (
"fmt"
"regexp"
"github.com/sjmudd/ps-top/global"
"github.com/sjmudd/ps-top/lib"
"github.com/sjmudd/ps-top/logger"
"github.com/sjmudd/ps-top/rc"
)
/*
CREATE TABLE `file_summary_by_instance` (
`FILE_NAME` varchar(512) NOT NULL,
`EVENT_NAME` varchar(128) NOT NULL, // not collected
`OBJECT_INSTANCE_BEGIN` bigint(20) unsigned NOT NULL, // not collected
`COUNT_STAR` bigint(20) unsigned NOT NULL,
`SUM_TIMER_WAIT` bigint(20) unsigned NOT NULL,
`MIN_TIMER_WAIT` bigint(20) unsigned NOT NULL,
`AVG_TIMER_WAIT` bigint(20) unsigned NOT NULL,
`MAX_TIMER_WAIT` bigint(20) unsigned NOT NULL,
`COUNT_READ` bigint(20) unsigned NOT NULL,
`SUM_TIMER_READ` bigint(20) unsigned NOT NULL,
`MIN_TIMER_READ` bigint(20) unsigned NOT NULL,
`AVG_TIMER_READ` bigint(20) unsigned NOT NULL,
`MAX_TIMER_READ` bigint(20) unsigned NOT NULL,
`SUM_NUMBER_OF_BYTES_READ` bigint(20) NOT NULL,
`COUNT_WRITE` bigint(20) unsigned NOT NULL,
`SUM_TIMER_WRITE` bigint(20) unsigned NOT NULL,
`MIN_TIMER_WRITE` bigint(20) unsigned NOT NULL,
`AVG_TIMER_WRITE` bigint(20) unsigned NOT NULL,
`MAX_TIMER_WRITE` bigint(20) unsigned NOT NULL,
`SUM_NUMBER_OF_BYTES_WRITE` bigint(20) NOT NULL,
`COUNT_MISC` bigint(20) unsigned NOT NULL,
`SUM_TIMER_MISC` bigint(20) unsigned NOT NULL,
`MIN_TIMER_MISC` bigint(20) unsigned NOT NULL,
`AVG_TIMER_MISC` bigint(20) unsigned NOT NULL,
`MAX_TIMER_MISC` bigint(20) unsigned NOT NULL
) ENGINE=PERFORMANCE_SCHEMA DEFAULT CHARSET=utf8
1 row in set (0.00 sec)
*/
// Row contains a row from file_summary_by_instance
type Row struct {
name string
countStar uint64
countRead uint64
countWrite uint64
countMisc uint64
sumTimerWait uint64
sumTimerRead uint64
sumTimerWrite uint64
sumTimerMisc uint64
sumNumberOfBytesRead uint64
sumNumberOfBytesWrite uint64
}
// foo/../bar --> foo/bar perl: $new =~ s{[^/]+/\.\./}{/};
// /./ --> / perl: $new =~ s{/\./}{};
// // --> / perl: $new =~ s{//}{/};
const (
reEncoded = `@(\d{4})` // FIXME - add me to catch @0024 --> $ for example
)
var (
reOneOrTheOther = regexp.MustCompile(`/(\.)?/`)
reSlashDotDotSlash = regexp.MustCompile(`[^/]+/\.\./`)
reTableFile = regexp.MustCompile(`/([^/]+)/([^/]+)\.(frm|ibd|MYD|MYI|CSM|CSV|par)$`)
reTempTable = regexp.MustCompile(`#sql-[0-9_]+`)
rePartTable = regexp.MustCompile(`(.+)#P#p(\d+|MAX)`)
reDoubleWrite = regexp.MustCompile(`/#ib_[0-9_]+\.dblwr$`) // i1/#ib_16384_0.dblwr
reIbdata = regexp.MustCompile(`/ibdata\d+$`)
reIbtmp = regexp.MustCompile(`/ibtmp\d+$`)
reRedoLog = regexp.MustCompile(`/ib_logfile\d+$`)
reUndoLog = regexp.MustCompile(`/undo_\d+$`)
reBinlog = regexp.MustCompile(`/binlog\.(\d{6}|index)$`)
reDbOpt = regexp.MustCompile(`/db\.opt$`)
reSlowlog = regexp.MustCompile(`/slowlog$`)
reAutoCnf = regexp.MustCompile(`/auto\.cnf$`)
rePidFile = regexp.MustCompile(`/[^/]+\.pid$`)
reErrorMsg = regexp.MustCompile(`/share/[^/]+/errmsg\.sys$`)
reCharset = regexp.MustCompile(`/share/charsets/Index\.xml$`)
reDollar = regexp.MustCompile(`@0024`) // FIXME - add me to catch @0024 --> $ (specific case)
)
func (row Row) headings() string {
return fmt.Sprintf("%10s %6s|%6s %6s %6s|%8s %8s|%8s %6s %6s %6s|%s",
"Latency",
"%",
"Read",
"Write",
"Misc",
"Rd bytes",
"Wr bytes",
"Ops",
"R Ops",
"W Ops",
"M Ops",
"Table Name")
}
func (row Row) String() string {
return fmt.Sprintf("%s: %9d %9d %9d %9d %9d %9d %9d %9d %9d %9d",
row.name,
row.countStar,
row.countRead,
row.countWrite,
row.countMisc,
row.sumTimerWait,
row.sumTimerRead,
row.sumTimerWrite,
row.sumTimerMisc,
row.sumNumberOfBytesRead,
row.sumNumberOfBytesWrite)
}
// Valid checks if the row is valid and if asked to do so logs the problem
func (row Row) Valid(logProblem bool) bool {
var problem bool
if (row.countStar < row.countRead) ||
(row.countStar < row.countWrite) ||
(row.countStar < row.countMisc) {
problem = true
if logProblem {
logger.Println("Row.Valid() FAILED (count)", row)
}
}
if (row.sumTimerWait < row.sumTimerRead) ||
(row.sumTimerWait < row.sumTimerWrite) ||
(row.sumTimerWait < row.sumTimerMisc) {
problem = true
if logProblem {
logger.Println("Row.Valid() FAILED (sumTimer)", row)
}
}
return problem
}
// generate a printable result
func (row Row) content(totals Row) string {
var name = row.name
// We assume that if countStar = 0 then there's no data at all...
// when we have no data we really don't want to show the name either.
if (row.sumTimerWait == 0 && row.countStar == 0 && row.sumNumberOfBytesRead == 0 && row.sumNumberOfBytesWrite == 0) && name != "Totals" {
name = ""
}
return fmt.Sprintf("%10s %6s|%6s %6s %6s|%8s %8s|%8s %6s %6s %6s|%s",
lib.FormatTime(row.sumTimerWait),
lib.FormatPct(lib.Divide(row.sumTimerWait, totals.sumTimerWait)),
lib.FormatPct(lib.Divide(row.sumTimerRead, row.sumTimerWait)),
lib.FormatPct(lib.Divide(row.sumTimerWrite, row.sumTimerWait)),
lib.FormatPct(lib.Divide(row.sumTimerMisc, row.sumTimerWait)),
lib.FormatAmount(row.sumNumberOfBytesRead),
lib.FormatAmount(row.sumNumberOfBytesWrite),
lib.FormatAmount(row.countStar),
lib.FormatPct(lib.Divide(row.countRead, row.countStar)),
lib.FormatPct(lib.Divide(row.countWrite, row.countStar)),
lib.FormatPct(lib.Divide(row.countMisc, row.countStar)),
name)
}
// Add rows together, keeping the name of first row
func add(row, other Row) Row {
newRow := row
newRow.countStar += other.countStar
newRow.countRead += other.countRead
newRow.countWrite += other.countWrite
newRow.countMisc += other.countMisc
newRow.sumTimerWait += other.sumTimerWait
newRow.sumTimerRead += other.sumTimerRead
newRow.sumTimerWrite += other.sumTimerWrite
newRow.sumTimerMisc += other.sumTimerMisc
newRow.sumNumberOfBytesRead += other.sumNumberOfBytesRead
newRow.sumNumberOfBytesWrite += other.sumNumberOfBytesWrite
return newRow
}
// sometimes the values can drop and catch us out. This routines hides that by returning 0
func validSubtract(this, that uint64) uint64 {
if this > that {
return this - that
}
return 0
}
// subtract one set of values from another one keeping the original row name
// - use validSubtract() to catch negative jumps which do happen from time to time.
func subtract(row, other Row) Row {
newRow := row
newRow.countStar = validSubtract(row.countStar, other.countStar)
newRow.countRead = validSubtract(row.countRead, other.countRead)
newRow.countWrite = validSubtract(row.countWrite, other.countWrite)
newRow.countMisc = validSubtract(row.countMisc, other.countMisc)
newRow.sumTimerWait = validSubtract(row.sumTimerWait, other.sumTimerWait)
newRow.sumTimerRead = validSubtract(row.sumTimerRead, other.sumTimerRead)
newRow.sumTimerWrite = validSubtract(row.sumTimerWrite, other.sumTimerWrite)
newRow.sumTimerMisc = validSubtract(row.sumTimerMisc, other.sumTimerMisc)
newRow.sumNumberOfBytesRead = validSubtract(row.sumNumberOfBytesRead, other.sumNumberOfBytesRead)
newRow.sumNumberOfBytesWrite = validSubtract(row.sumNumberOfBytesWrite, other.sumNumberOfBytesWrite)
return newRow
}
// simplifyName converts the filename into something easier to
// recognise. This simpler name may also merge several different
// filenames into one. To help with performance the path replacements
// are stored in a cache so they can be used again on the next run.
func (row Row) simplifyName(globalVariables *global.Variables) string {
path := row.name
if cachedResult, err := cache.get(path); err == nil {
return cachedResult
}
// @0024 --> $ (should do this more generically)
path = reDollar.ReplaceAllLiteralString(path, "$")
// this should probably be ordered from most expected regexp to least
if m1 := reTableFile.FindStringSubmatch(path); m1 != nil {
// we may match temporary tables so check for them
if m2 := reTempTable.FindStringSubmatch(m1[2]); m2 != nil {
return cache.put(path, "<temp_table>")
}
// we may match partitioned tables so check for them
if m3 := rePartTable.FindStringSubmatch(m1[2]); m3 != nil {
return cache.put(path, lib.TableName(m1[1], m3[1])) // <schema>.<table> (less partition info)
}
return cache.put(path, rc.Munge(lib.TableName(m1[1], m1[2]))) // <schema>.<table>
}
if reIbtmp.MatchString(path) {
return cache.put(path, "<ibtmp>")
}
if reIbdata.MatchString(path) {
return cache.put(path, "<ibdata>")
}
if reUndoLog.MatchString(path) {
return cache.put(path, "<undo_log>")
}
if reRedoLog.MatchString(path) {
return cache.put(path, "<redo_log>")
}
if reDoubleWrite.MatchString(path) {
return cache.put(path, "<doublewrite>")
}
if reBinlog.MatchString(path) {
return cache.put(path, "<binlog>")
}
if reDbOpt.MatchString(path) {
return cache.put(path, "<db_opt>")
}
if reSlowlog.MatchString(path) {
return cache.put(path, "<slow_log>")
}
if reAutoCnf.MatchString(path) {
return cache.put(path, "<auto_cnf>")
}
// relay logs are a bit complicated. If a full path then easy to
// identify, but if a relative path we may need to add $datadir,
// but also if as I do we have a ../blah/somewhere/path then we
// need to make it match too.
if len(globalVariables.Get("relay_log")) > 0 {
relayLog := globalVariables.Get("relay_log")
if relayLog[0] != '/' { // relative path
relayLog = cleanupPath(globalVariables.Get("datadir") + relayLog) // datadir always ends in /
}
reRelayLog := relayLog + `\.(\d{6}|index)$`
if regexp.MustCompile(reRelayLog).MatchString(path) {
return cache.put(path, "<relay_log>")
}
}
if rePidFile.MatchString(path) {
return cache.put(path, "<pid_file>")
}
if reErrorMsg.MatchString(path) {
return cache.put(path, "<errmsg>")
}
if reCharset.MatchString(path) {
return cache.put(path, "<charset>")
}
// clean up datadir to <datadir>
if len(globalVariables.Get("datadir")) > 0 {
reDatadir := regexp.MustCompile("^" + globalVariables.Get("datadir"))
path = reDatadir.ReplaceAllLiteralString(path, "<datadir>/")
}
return cache.put(path, path)
}
// clean up the given path reducing redundant stuff and return the clean path
func cleanupPath(path string) string {
for {
origPath := path
path = reOneOrTheOther.ReplaceAllString(path, "/")
path = reSlashDotDotSlash.ReplaceAllString(path, "/")
if origPath == path { // no change so give up
break
}
}
return path
}