/
performance.go
485 lines (438 loc) · 12.9 KB
/
performance.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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
// Package cli provides easy-to-use commands to manage, monitor, and utilize AIS clusters.
// This file contains util functions and types.
/*
* Copyright (c) 2023-2024, NVIDIA CORPORATION. All rights reserved.
*/
package cli
import (
"fmt"
"regexp"
"strings"
"time"
"github.com/NVIDIA/aistore/api/apc"
"github.com/NVIDIA/aistore/cmd/cli/teb"
"github.com/NVIDIA/aistore/cmn"
"github.com/NVIDIA/aistore/cmn/cos"
"github.com/NVIDIA/aistore/cmn/debug"
"github.com/NVIDIA/aistore/core/meta"
"github.com/NVIDIA/aistore/stats"
"github.com/urfave/cli"
)
type (
perfcb func(c *cli.Context,
metrics cos.StrKVs, mapBegin, mapEnd teb.StstMap, elapsed time.Duration) bool
)
// _statically_ defined for `latency` table (compare with counter and throughput tabs)
var selectedLatency = []string{
stats.GetLatency, stats.GetSize, stats.GetCount, stats.GetColdCount, stats.GetColdSize, stats.GetRedirLatency, stats.GetColdRwLatency,
stats.PutLatency, stats.PutSize, stats.PutCount, stats.PutRedirLatency,
stats.AppendLatency, stats.AppendCount,
}
// true when called by top-level handler
var allPerfTabs bool
var (
showPerfFlags = append(
longRunFlags,
noHeaderFlag,
regexColsFlag,
unitsFlag,
averageSizeFlag,
nonverboseFlag,
)
// alias
perfCmd = cli.Command{
Name: commandPerf,
Usage: showPerfArgument,
Subcommands: []cli.Command{
makeAlias(showCmdPeformance, "", true, commandShow),
},
}
// `show performance` command
showCmdPeformance = cli.Command{
Name: commandPerf,
Usage: showPerfArgument,
ArgsUsage: optionalTargetIDArgument,
Flags: showPerfFlags,
Action: showPerfHandler,
Subcommands: []cli.Command{
showCounters,
showThroughput,
showLatency,
showCmdMpathCapacity,
makeAlias(showCmdDisk, "", true /*silent*/, cmdShowDisk),
},
}
showCounters = cli.Command{
Name: cmdShowCounters,
Usage: "show (GET, PUT, DELETE, RENAME, EVICT, APPEND) object counts, as well as:\n" +
indent2 + "\t- numbers of list-objects requests;\n" +
indent2 + "\t- (GET, PUT, etc.) cumulative and average sizes;\n" +
indent2 + "\t- associated error counters, if any, and more.",
ArgsUsage: optionalTargetIDArgument,
Flags: showPerfFlags,
Action: showCountersHandler,
BashComplete: suggestTargets,
}
showThroughput = cli.Command{
Name: cmdShowThroughput,
Usage: "show GET and PUT throughput, associated (cumulative, average) sizes and counters",
ArgsUsage: optionalTargetIDArgument,
Flags: showPerfFlags,
Action: showThroughputHandler,
BashComplete: suggestTargets,
}
showLatency = cli.Command{
Name: cmdShowLatency,
Usage: "show GET, PUT, and APPEND latencies and average sizes",
ArgsUsage: optionalTargetIDArgument,
Flags: showPerfFlags,
Action: showLatencyHandler,
BashComplete: suggestTargets,
}
showCmdMpathCapacity = cli.Command{
Name: cmdCapacity,
Usage: "show target mountpaths, disks, and used/available capacity",
ArgsUsage: optionalTargetIDArgument,
Flags: append(showPerfFlags, mountpathFlag),
Action: showMpathCapHandler,
BashComplete: suggestTargets,
}
)
func showPerfHandler(c *cli.Context) error {
allPerfTabs = true // global (TODO: consider passing as param)
if c.NArg() > 1 && strings.HasPrefix(c.Args().Get(1), "-") {
return fmt.Errorf("misplaced flags in %v (hint: change the order of arguments or %s specific view)",
c.Args(), tabtab)
}
if err := showCountersHandler(c); err != nil {
return err
}
fmt.Fprintln(c.App.Writer)
if err := showThroughputHandler(c); err != nil {
return err
}
fmt.Fprintln(c.App.Writer)
if err := showLatencyHandler(c); err != nil {
return err
}
fmt.Fprintln(c.App.Writer)
return nil
}
func _warnThruLatIters(c *cli.Context) {
if flagIsSet(c, refreshFlag) || flagIsSet(c, nonverboseFlag) {
return
}
warn := fmt.Sprintf("for better results, use %s option and/or run several iterations\n", qflprn(refreshFlag))
actionWarn(c, warn)
}
func perfCptn(c *cli.Context, tab string) {
stamp := cos.FormatNowStamp()
repeat := 40 - len(stamp) - len(tab)
s := " " + strings.Repeat("-", repeat) + " " + stamp
actionCptn(c, tab, s)
}
// show non-zero counters _and_ sizes (unless `allColumnsFlag`)
func showCountersHandler(c *cli.Context) error {
metrics, err := getMetricNames(c)
if err != nil {
return err
}
selected := make(cos.StrKVs, len(metrics))
for name, kind := range metrics {
if metrics[name] == stats.KindCounter || metrics[name] == stats.KindSize {
selected[name] = kind
}
}
return showPerfTab(c, selected, nil, cmdShowCounters, nil, false)
}
func showThroughputHandler(c *cli.Context) error {
var (
totals = make(map[string]int64, 4) // throughput metrics ("columns") to tally up
metrics, err = getMetricNames(c)
)
if err != nil {
return err
}
_warnThruLatIters(c)
selected := make(cos.StrKVs, len(metrics))
for name, kind := range metrics {
switch {
case kind == stats.KindThroughput:
// 1. all throughput
selected[name] = kind
totals[name] = 0
case name == stats.GetSize || name == stats.GetColdSize || name == stats.PutSize ||
name == stats.GetCount || name == stats.GetColdCount || name == stats.PutCount:
// 2. to show average get/put sizes
selected[name] = kind
case stats.IsErrMetric(name):
// 3. errors
if strings.Contains(name, "get") || strings.Contains(name, "put") ||
strings.Contains(name, "read") || strings.Contains(name, "write") {
selected[name] = kind
}
}
}
// `true` to show average get/put sizes
return showPerfTab(c, selected, _throughput /*cb*/, cmdShowThroughput, totals, true)
}
// update mapBegin <= (size/s)
func _throughput(c *cli.Context, metrics cos.StrKVs, mapBegin, mapEnd teb.StstMap, elapsed time.Duration) (idle bool) {
var (
seconds = max(int64(elapsed.Seconds()), 1) // averaging per second
num int
)
for tid, begin := range mapBegin {
end := mapEnd[tid]
if end == nil {
warn := fmt.Sprintf("missing %s in the get-stats-and-status results\n", meta.Tname(tid))
actionWarn(c, warn)
continue
}
for name, v := range begin.Tracker {
if kind, ok := metrics[name]; !ok || kind != stats.KindThroughput {
continue
}
vend := end.Tracker[name]
if vend.Value <= v.Value {
// no changes, nothing to show
v.Value = 0
begin.Tracker[name] = v
continue
}
v.Value = (vend.Value - v.Value) / seconds
begin.Tracker[name] = v
num++
}
}
idle = num == 0
return
}
// NOTE: two built-in assumptions: one cosmetic, another major
// - ".ns" => ".n" correspondence is the cosmetic one
// - the naive way to recompute latency using the total elapsed, not the actual, time to execute so many requests...
func showLatencyHandler(c *cli.Context) error {
metrics, err := getMetricNames(c)
if err != nil {
return err
}
_warnThruLatIters(c)
// statically filter metrics (names)
selected := make(cos.StrKVs, len(selectedLatency))
for name, kind := range metrics {
if cos.StringInSlice(name, selectedLatency) {
selected[name] = kind
} else if stats.IsErrMetric(name) {
if strings.Contains(name, "get") || strings.Contains(name, "put") || strings.Contains(name, "append") {
selected[name] = kind
}
}
}
// `true` to show (and put request latency numbers in perspective)
return showPerfTab(c, selected, _latency, cmdShowLatency, nil, true)
}
// update mapBegin <= (elapsed/num-samples)
func _latency(c *cli.Context, metrics cos.StrKVs, mapBegin, mapEnd teb.StstMap, _ time.Duration) (idle bool) {
var num int // num computed latencies
for tid, begin := range mapBegin {
end := mapEnd[tid]
if end == nil {
warn := fmt.Sprintf("missing %s in the get-stats-and-status results\n", meta.Tname(tid))
actionWarn(c, warn)
continue
}
for name, v := range begin.Tracker {
if kind, ok := metrics[name]; !ok || kind != stats.KindLatency {
continue
}
vend := end.Tracker[name]
ncounter := name[:len(name)-1] // ".ns" => ".n"
switch name {
case stats.GetLatency, stats.GetRedirLatency:
ncounter = stats.GetCount
case stats.GetColdRwLatency:
ncounter = stats.GetColdCount
case stats.PutLatency, stats.PutRedirLatency:
ncounter = stats.PutCount
case stats.AppendLatency:
ncounter = stats.AppendCount
}
if cntBegin, ok1 := begin.Tracker[ncounter]; ok1 {
if cntEnd, ok2 := end.Tracker[ncounter]; ok2 && cntEnd.Value > cntBegin.Value {
// (cumulative-end-time - cumulative-begin-time) / num-requests
v.Value = (vend.Value - v.Value) / (cntEnd.Value - cntBegin.Value)
begin.Tracker[name] = v
num++
continue
}
}
// no changes, nothing to show
v.Value = 0
begin.Tracker[name] = v
}
}
idle = num == 0
return
}
// (main method)
func showPerfTab(c *cli.Context, metrics cos.StrKVs, cb perfcb, tag string, totals map[string]int64, inclAvgSize bool) error {
var (
regex *regexp.Regexp
regexStr = parseStrFlag(c, regexColsFlag)
hideHeader = flagIsSet(c, noHeaderFlag)
units, errU = parseUnitsFlag(c, unitsFlag)
)
if errU != nil {
return errU
}
avgSize := flagIsSet(c, averageSizeFlag)
if inclAvgSize {
avgSize = true // caller override
}
var (
tid string
node, _, err = arg0Node(c)
)
if err != nil {
return err
}
if node != nil {
debug.Assert(node.IsTarget())
tid = node.ID()
}
if regexStr != "" {
regex, err = regexp.Compile(regexStr)
if err != nil {
return err
}
}
smap, tstatusMap, _, err := fillNodeStatusMap(c, apc.Target)
if err != nil {
return err
}
params := getLongRunParams(c)
if params != nil {
if params.mapBegin == nil {
params.mapBegin = tstatusMap
} else {
params.mapEnd = tstatusMap
}
}
if numTs := smap.CountActiveTs(); numTs == 1 || tid != "" {
totals = nil // sum implies multiple
} else if numTs == 0 {
return cmn.NewErrNoNodes(apc.Target, smap.CountTargets())
}
// (1) no recompute, no totals; "long-run" (if spec-ed) via app.go
if cb == nil {
lfooter := 72
if allPerfTabs {
lfooter = 0
}
setLongRunParams(c, lfooter)
ctx := teb.PerfTabCtx{Smap: smap, Sid: tid, Metrics: metrics, Regex: regex, Units: units, AvgSize: avgSize}
table, num, err := teb.NewPerformanceTab(tstatusMap, &ctx)
if err != nil {
return err
}
if allPerfTabs {
perfCptn(c, tag)
}
if num == 0 && tag == cmdShowCounters {
if regex == nil {
actionNote(c, "the cluster is completely idle: all collected counters have zero values\n")
}
}
out := table.Template(hideHeader)
return teb.Print(tstatusMap, out)
}
// (2) `cb` recompute at each cycle
if params != nil && params.mapEnd == nil {
return nil // won't be nil starting next long-run iteration
}
var (
refresh = flagIsSet(c, refreshFlag)
sleep = _refreshRate(c)
cntRun = &longRun{mapBegin: tstatusMap}
)
if sleep < time.Second || sleep > time.Minute {
return fmt.Errorf("invalid %s value, got %v, expecting [1s - 1m]", qflprn(refreshFlag), sleep)
}
cntRun.init(c, true /*run once unless*/)
for countdown := cntRun.count; countdown > 0 || cntRun.isForever(); countdown-- {
var mapBegin, mapEnd teb.StstMap
for name := range totals { // reset
totals[name] = 0
}
if params != nil {
mapBegin, mapEnd = params.mapBegin, params.mapEnd
} else {
mapBegin, mapEnd, err = _cluStatusBeginEnd(c, cntRun.mapBegin, sleep)
if err != nil {
return err
}
cntRun.mapBegin = mapEnd
}
idle := cb(c, metrics, mapBegin, mapEnd, sleep) // call back to recompute
perfCptn(c, tag)
// tally up recomputed
totalsHdr := cluTotal
if totals != nil {
for _, begin := range mapBegin {
for name, v := range begin.Tracker {
if _, ok := totals[name]; ok {
totals[name] += v.Value
}
}
// TODO: avoid summing up with oneself - check TargetCDF mountpaths
}
}
ctx := teb.PerfTabCtx{Smap: smap, Sid: tid, Metrics: metrics, Regex: regex, Units: units,
Totals: totals, TotalsHdr: totalsHdr, AvgSize: avgSize, Idle: idle}
table, _, err := teb.NewPerformanceTab(mapBegin, &ctx)
if err != nil {
return err
}
out := table.Template(hideHeader)
err = teb.Print(mapBegin, out)
if err != nil || !refresh || allPerfTabs {
return err
}
}
return nil
}
func showMpathCapHandler(c *cli.Context) error {
var (
tid string
regex *regexp.Regexp
regexStr = parseStrFlag(c, regexColsFlag)
hideHeader = flagIsSet(c, noHeaderFlag)
showMpaths = flagIsSet(c, mountpathFlag)
units, errU = parseUnitsFlag(c, unitsFlag)
)
if errU != nil {
return errU
}
node, _, err := arg0Node(c)
if err != nil {
return err
}
if node != nil {
tid = node.ID()
}
if regexStr != "" {
regex, err = regexp.Compile(regexStr)
if err != nil {
return err
}
}
setLongRunParams(c, 72)
smap, tstatusMap, _, err := fillNodeStatusMap(c, apc.Target)
if err != nil {
return err
}
ctx := teb.PerfTabCtx{Smap: smap, Sid: tid, Regex: regex, Units: units}
table := teb.NewMpathCapTab(tstatusMap, &ctx, showMpaths)
out := table.Template(hideHeader)
return teb.Print(tstatusMap, out)
}