-
Notifications
You must be signed in to change notification settings - Fork 56
Expand file tree
/
Copy pathtelemetry_renderers.go
More file actions
825 lines (804 loc) · 27.4 KB
/
telemetry_renderers.go
File metadata and controls
825 lines (804 loc) · 27.4 KB
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
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
// Copyright (C) 2021-2025 Intel Corporation
// SPDX-License-Identifier: BSD-3-Clause
package telemetry
import (
"fmt"
"log/slog"
"perfspect/internal/report"
"perfspect/internal/table"
"perfspect/internal/util"
"slices"
"sort"
"strconv"
"strings"
)
// computeAxisMax examines all datasets and returns a Y-axis hard max string.
// If outliers are detected (actual max > P99 * 1.5), it returns a value slightly
// above P99. Otherwise it returns "" (no constraint, use auto-scale).
func computeAxisMax(data [][]float64) string {
var all []float64
for _, dataset := range data {
all = append(all, dataset...)
}
if len(all) < 4 {
return ""
}
sorted := make([]float64, len(all))
copy(sorted, all)
slices.Sort(sorted)
p99Idx := int(float64(len(sorted)-1) * 0.99)
p99 := sorted[p99Idx]
actualMax := sorted[len(sorted)-1]
if p99 > 0 && actualMax > p99*1.5 {
return fmt.Sprintf("%f", p99*1.1)
}
return ""
}
func telemetryTableHTMLRenderer(tableValues table.TableValues, data [][]float64, datasetNames []string, chartConfig report.ChartTemplateStruct, datasetHiddenFlags []bool) string {
if len(tableValues.Fields) == 0 {
slog.Error("no fields in table", slog.String("table", tableValues.Name))
return ""
}
// Auto-detect outliers and set hard Y-axis max for auto-scaled charts
if chartConfig.YaxisMax == "" && chartConfig.SuggestedMax == "0" {
chartConfig.YaxisMax = computeAxisMax(data)
}
tsFieldIdx := 0
var timestamps []string
for i := range tableValues.Fields[0].Values {
timestamp := tableValues.Fields[tsFieldIdx].Values[i]
if !slices.Contains(timestamps, timestamp) { // could be slow if list is long
timestamps = append(timestamps, timestamp)
}
}
return renderLineChart(timestamps, data, datasetNames, chartConfig, datasetHiddenFlags)
}
// renderLineChart generates an HTML string for a line chart using the provided data and configuration.
//
// Parameters:
//
// xAxisLabels - Slice of strings representing the labels for the X axis.
// data - 2D slice of float64 values, where each inner slice represents a dataset's data points.
// datasetNames - Slice of strings representing the names of each dataset.
// config - chartTemplateStruct containing chart configuration options.
// datasetHiddenFlags - Slice of booleans indicating whether each dataset should be hidden initially.
//
// Returns:
//
// A string containing the rendered HTML for the line chart.
func renderLineChart(xAxisLabels []string, data [][]float64, datasetNames []string, config report.ChartTemplateStruct, datasetHiddenFlags []bool) string {
allFormattedPoints := []string{}
for dataIdx := range data {
formattedPoints := []string{}
for _, point := range data[dataIdx] {
formattedPoints = append(formattedPoints, fmt.Sprintf("%f", point))
}
allFormattedPoints = append(allFormattedPoints, strings.Join(formattedPoints, ","))
}
return report.RenderChart("line", allFormattedPoints, datasetNames, xAxisLabels, config, datasetHiddenFlags)
}
func cpuUtilizationTelemetryTableHTMLRenderer(tableValues table.TableValues, targetName string) string {
if len(tableValues.Fields) < 3 {
slog.Error("insufficient fields in table, expected at least 3", slog.String("table", tableValues.Name), slog.Int("fields", len(tableValues.Fields)))
return ""
}
data := [][]float64{}
datasetNames := []string{}
// collect the busy (100 - idle) values for each CPU
cpuBusyStats := make(map[int][]float64)
idleFieldIdx := len(tableValues.Fields) - 1
cpuFieldIdx := 1
for i := range tableValues.Fields[0].Values {
idle, err := strconv.ParseFloat(tableValues.Fields[idleFieldIdx].Values[i], 64)
if err != nil {
continue
}
busy := 100 - idle
cpu, err := strconv.Atoi(tableValues.Fields[cpuFieldIdx].Values[i])
if err != nil {
continue
}
if _, ok := cpuBusyStats[cpu]; !ok {
cpuBusyStats[cpu] = []float64{}
}
cpuBusyStats[cpu] = append(cpuBusyStats[cpu], busy)
}
// sort map keys by cpu number
var keys []int
for cpu := range cpuBusyStats {
keys = append(keys, cpu)
}
sort.Ints(keys)
// build the data
for _, cpu := range keys {
if len(cpuBusyStats[cpu]) > 0 {
data = append(data, cpuBusyStats[cpu])
datasetNames = append(datasetNames, fmt.Sprintf("CPU %d", cpu))
}
}
chartConfig := report.ChartTemplateStruct{
ID: fmt.Sprintf("%s%d", tableValues.Name, util.RandUint(10000)),
XaxisText: "Time",
YaxisText: "% Utilization",
TitleText: "",
DisplayTitle: "false",
DisplayLegend: "false",
AspectRatio: "2",
SuggestedMin: "0",
SuggestedMax: "100",
}
return telemetryTableHTMLRenderer(tableValues, data, datasetNames, chartConfig, nil)
}
func utilizationCategoriesTelemetryTableHTMLRenderer(tableValues table.TableValues, targetName string) string {
if len(tableValues.Fields) < 2 {
slog.Error("insufficient fields in table, expected at least 2", slog.String("table", tableValues.Name), slog.Int("fields", len(tableValues.Fields)))
return ""
}
data := [][]float64{}
datasetNames := []string{}
for _, field := range tableValues.Fields[1:] {
points := []float64{}
for _, val := range field.Values {
if val == "" {
break
}
util, err := strconv.ParseFloat(val, 64)
if err != nil {
slog.Error("error parsing percentage", slog.String("error", err.Error()))
return ""
}
points = append(points, util)
}
if len(points) > 0 {
data = append(data, points)
datasetNames = append(datasetNames, field.Name)
}
}
chartConfig := report.ChartTemplateStruct{
ID: fmt.Sprintf("%s%d", tableValues.Name, util.RandUint(10000)),
XaxisText: "Time",
YaxisText: "% Utilization",
TitleText: "",
DisplayTitle: "false",
DisplayLegend: "true",
AspectRatio: "2",
SuggestedMin: "0",
SuggestedMax: "100",
}
return telemetryTableHTMLRenderer(tableValues, data, datasetNames, chartConfig, nil)
}
func irqRateTelemetryTableHTMLRenderer(tableValues table.TableValues, targetName string) string {
if len(tableValues.Fields) < 3 || len(tableValues.Fields[0].Values) == 0 {
slog.Error("insufficient fields or empty values in table, expected at least 3 fields with values", slog.String("table", tableValues.Name), slog.Int("fields", len(tableValues.Fields)))
return ""
}
data := [][]float64{}
datasetNames := []string{}
for _, field := range tableValues.Fields[2:] { // 1 data set per field, e.g., %usr, %nice, etc., skip Time and CPU fields
datasetNames = append(datasetNames, field.Name)
// sum the values in the field per timestamp, store the sum as a point
timeStamp := tableValues.Fields[0].Values[0]
points := []float64{}
total := 0.0
for i := range field.Values {
if i >= len(tableValues.Fields[0].Values) {
slog.Error("field values length mismatch", slog.String("table", tableValues.Name), slog.Int("index", i))
break
}
if tableValues.Fields[0].Values[i] != timeStamp { // new timestamp?
points = append(points, total)
total = 0.0
timeStamp = tableValues.Fields[0].Values[i]
}
val, err := strconv.ParseFloat(field.Values[i], 64)
if err != nil {
slog.Error("error parsing value", slog.String("error", err.Error()))
return ""
}
total += val
}
points = append(points, total) // add the point for the last timestamp
// save the points in the data slice
data = append(data, points)
}
chartConfig := report.ChartTemplateStruct{
ID: fmt.Sprintf("%s%d", tableValues.Name, util.RandUint(10000)),
XaxisText: "Time",
YaxisText: "IRQ/s",
TitleText: "",
DisplayTitle: "false",
DisplayLegend: "true",
AspectRatio: "2",
SuggestedMin: "0",
SuggestedMax: "0",
}
return telemetryTableHTMLRenderer(tableValues, data, datasetNames, chartConfig, nil)
}
// driveTelemetryTableHTMLRenderer renders charts of drive statistics
// - one scatter chart per drive, showing the drive's utilization over time
// - each drive stat is a separate dataset within the chart
func driveTelemetryTableHTMLRenderer(tableValues table.TableValues, targetName string) string {
if len(tableValues.Fields) < 3 {
slog.Error("insufficient fields in table, expected at least 3", slog.String("table", tableValues.Name), slog.Int("fields", len(tableValues.Fields)))
return ""
}
var out strings.Builder
driveStats := make(map[string][][]string)
for i := range tableValues.Fields[0].Values {
if i >= len(tableValues.Fields[1].Values) {
slog.Error("field values length mismatch", slog.String("table", tableValues.Name), slog.Int("index", i))
break
}
drive := tableValues.Fields[1].Values[i]
if _, ok := driveStats[drive]; !ok {
driveStats[drive] = make([][]string, len(tableValues.Fields)-2)
}
for j := range len(tableValues.Fields) - 2 {
if i >= len(tableValues.Fields[j+2].Values) {
slog.Error("field values length mismatch", slog.String("table", tableValues.Name), slog.Int("field", j+2), slog.Int("index", i))
continue
}
driveStats[drive][j] = append(driveStats[drive][j], tableValues.Fields[j+2].Values[i])
}
}
var keys []string
for drive := range driveStats {
keys = append(keys, drive)
}
sort.Strings(keys)
for _, drive := range keys {
data := [][]float64{}
datasetNames := []string{}
for i, statVals := range driveStats[drive] {
points := []float64{}
for i, val := range statVals {
if val == "" {
slog.Error("empty stat value", slog.String("drive", drive), slog.Int("index", i))
return ""
}
util, err := strconv.ParseFloat(val, 64)
if err != nil {
slog.Error("error parsing stat", slog.String("error", err.Error()))
return ""
}
points = append(points, util)
}
if len(points) > 0 {
data = append(data, points)
datasetNames = append(datasetNames, tableValues.Fields[i+2].Name)
}
}
chartConfig := report.ChartTemplateStruct{
ID: fmt.Sprintf("%s%d", tableValues.Name, util.RandUint(10000)),
XaxisText: "Time",
YaxisText: "",
TitleText: drive,
DisplayTitle: "true",
DisplayLegend: "true",
AspectRatio: "2",
SuggestedMin: "0",
SuggestedMax: "0",
}
out.WriteString(telemetryTableHTMLRenderer(tableValues, data, datasetNames, chartConfig, nil))
}
return out.String()
}
// networkTelemetryTableHTMLRenderer renders charts of network device statistics
// - one scatter chart per network device, showing the device's utilization over time
// - each network stat is a separate dataset within the chart
func networkTelemetryTableHTMLRenderer(tableValues table.TableValues, targetName string) string {
if len(tableValues.Fields) < 3 {
slog.Error("insufficient fields in table, expected at least 3", slog.String("table", tableValues.Name), slog.Int("fields", len(tableValues.Fields)))
return ""
}
var out strings.Builder
nicStats := make(map[string][][]string)
for i := range tableValues.Fields[0].Values {
if i >= len(tableValues.Fields[1].Values) {
slog.Error("field values length mismatch", slog.String("table", tableValues.Name), slog.Int("index", i))
break
}
drive := tableValues.Fields[1].Values[i]
if _, ok := nicStats[drive]; !ok {
nicStats[drive] = make([][]string, len(tableValues.Fields)-2)
}
for j := range len(tableValues.Fields) - 2 {
if i >= len(tableValues.Fields[j+2].Values) {
slog.Error("field values length mismatch", slog.String("table", tableValues.Name), slog.Int("field", j+2), slog.Int("index", i))
continue
}
nicStats[drive][j] = append(nicStats[drive][j], tableValues.Fields[j+2].Values[i])
}
}
var keys []string
for drive := range nicStats {
keys = append(keys, drive)
}
sort.Strings(keys)
for _, nic := range keys {
data := [][]float64{}
datasetNames := []string{}
for i, statVals := range nicStats[nic] {
points := []float64{}
for i, val := range statVals {
if val == "" {
slog.Error("empty stat value", slog.String("nic", nic), slog.Int("index", i))
return ""
}
util, err := strconv.ParseFloat(val, 64)
if err != nil {
slog.Error("error parsing stat", slog.String("error", err.Error()))
return ""
}
points = append(points, util)
}
if len(points) > 0 {
data = append(data, points)
datasetNames = append(datasetNames, tableValues.Fields[i+2].Name)
}
}
chartConfig := report.ChartTemplateStruct{
ID: fmt.Sprintf("%s%d", tableValues.Name, util.RandUint(10000)),
XaxisText: "Time",
YaxisText: "",
TitleText: nic,
DisplayTitle: "true",
DisplayLegend: "true",
AspectRatio: "2",
SuggestedMin: "0",
SuggestedMax: "0",
}
out.WriteString(telemetryTableHTMLRenderer(tableValues, data, datasetNames, chartConfig, nil))
}
return out.String()
}
func memoryTelemetryTableHTMLRenderer(tableValues table.TableValues, targetName string) string {
if len(tableValues.Fields) < 2 {
slog.Error("insufficient fields in table, expected at least 2", slog.String("table", tableValues.Name), slog.Int("fields", len(tableValues.Fields)))
return ""
}
data := [][]float64{}
datasetNames := []string{}
for _, field := range tableValues.Fields[1:] {
points := []float64{}
for _, val := range field.Values {
if val == "" {
break
}
stat, err := strconv.ParseFloat(val, 64)
if err != nil {
slog.Error("error parsing stat", slog.String("error", err.Error()))
return ""
}
points = append(points, stat)
}
if len(points) > 0 {
data = append(data, points)
datasetNames = append(datasetNames, field.Name)
}
}
chartConfig := report.ChartTemplateStruct{
ID: fmt.Sprintf("%s%d", tableValues.Name, util.RandUint(10000)),
XaxisText: "Time",
YaxisText: "kilobytes",
TitleText: "",
DisplayTitle: "false",
DisplayLegend: "true",
AspectRatio: "2",
SuggestedMin: "0",
SuggestedMax: "0",
}
return telemetryTableHTMLRenderer(tableValues, data, datasetNames, chartConfig, nil)
}
func averageFrequencyTelemetryTableHTMLRenderer(tableValues table.TableValues, targetName string) string {
if len(tableValues.Fields) < 2 {
slog.Error("insufficient fields in table, expected at least 2", slog.String("table", tableValues.Name), slog.Int("fields", len(tableValues.Fields)))
return ""
}
data := [][]float64{}
datasetNames := []string{}
for _, field := range tableValues.Fields[1:] {
points := []float64{}
for _, val := range field.Values {
if val == "" {
break
}
stat, err := strconv.ParseFloat(val, 64)
if err != nil {
slog.Error("error parsing stat", slog.String("error", err.Error()))
return ""
}
points = append(points, stat)
}
if len(points) > 0 {
data = append(data, points)
datasetNames = append(datasetNames, field.Name)
}
}
chartConfig := report.ChartTemplateStruct{
ID: fmt.Sprintf("%s%d", tableValues.Name, util.RandUint(10000)),
XaxisText: "Time",
YaxisText: "MHz",
TitleText: "",
DisplayTitle: "false",
DisplayLegend: "true",
AspectRatio: "2",
SuggestedMin: "0",
SuggestedMax: "0",
}
return telemetryTableHTMLRenderer(tableValues, data, datasetNames, chartConfig, nil)
}
func powerTelemetryTableHTMLRenderer(tableValues table.TableValues, targetName string) string {
if len(tableValues.Fields) < 2 {
slog.Error("insufficient fields in table, expected at least 2", slog.String("table", tableValues.Name), slog.Int("fields", len(tableValues.Fields)))
return ""
}
data := [][]float64{}
datasetNames := []string{}
for _, field := range tableValues.Fields[1:] {
points := []float64{}
for _, val := range field.Values {
if val == "" {
break
}
stat, err := strconv.ParseFloat(val, 64)
if err != nil {
slog.Error("error parsing stat", slog.String("error", err.Error()))
return ""
}
points = append(points, stat)
}
if len(points) > 0 {
data = append(data, points)
datasetNames = append(datasetNames, field.Name)
}
}
chartConfig := report.ChartTemplateStruct{
ID: fmt.Sprintf("%s%d", tableValues.Name, util.RandUint(10000)),
XaxisText: "Time",
YaxisText: "Watts",
TitleText: "",
DisplayTitle: "false",
DisplayLegend: "true",
AspectRatio: "2",
SuggestedMin: "0",
SuggestedMax: "0",
}
return telemetryTableHTMLRenderer(tableValues, data, datasetNames, chartConfig, nil)
}
func temperatureTelemetryTableHTMLRenderer(tableValues table.TableValues, targetName string) string {
if len(tableValues.Fields) < 2 {
slog.Error("insufficient fields in table, expected at least 2", slog.String("table", tableValues.Name), slog.Int("fields", len(tableValues.Fields)))
return ""
}
data := [][]float64{}
datasetNames := []string{}
for _, field := range tableValues.Fields[1:] {
points := []float64{}
for _, val := range field.Values {
if val == "" {
break
}
stat, err := strconv.ParseFloat(val, 64)
if err != nil {
slog.Error("error parsing stat", slog.String("error", err.Error()))
return ""
}
points = append(points, stat)
}
if len(points) > 0 {
data = append(data, points)
datasetNames = append(datasetNames, field.Name)
}
}
chartConfig := report.ChartTemplateStruct{
ID: fmt.Sprintf("%s%d", tableValues.Name, util.RandUint(10000)),
XaxisText: "Time",
YaxisText: "Celsius",
TitleText: "",
DisplayTitle: "false",
DisplayLegend: "true",
AspectRatio: "2",
SuggestedMin: "0",
SuggestedMax: "0",
}
return telemetryTableHTMLRenderer(tableValues, data, datasetNames, chartConfig, nil)
}
func ipcTelemetryTableHTMLRenderer(tableValues table.TableValues, targetName string) string {
if len(tableValues.Fields) < 2 {
slog.Error("insufficient fields in table, expected at least 2", slog.String("table", tableValues.Name), slog.Int("fields", len(tableValues.Fields)))
return ""
}
data := [][]float64{}
datasetNames := []string{}
for _, field := range tableValues.Fields[1:] {
points := []float64{}
for _, val := range field.Values {
if val == "" {
break
}
stat, err := strconv.ParseFloat(val, 64)
if err != nil {
slog.Error("error parsing stat", slog.String("error", err.Error()))
return ""
}
points = append(points, stat)
}
if len(points) > 0 {
data = append(data, points)
datasetNames = append(datasetNames, field.Name)
}
}
chartConfig := report.ChartTemplateStruct{
ID: fmt.Sprintf("%s%d", tableValues.Name, util.RandUint(10000)),
XaxisText: "Time",
YaxisText: "IPC",
TitleText: "",
DisplayTitle: "false",
DisplayLegend: "true",
AspectRatio: "2",
SuggestedMin: "0",
SuggestedMax: "0",
}
return telemetryTableHTMLRenderer(tableValues, data, datasetNames, chartConfig, nil)
}
func cstatesTelemetryTableHTMLRenderer(tableValues table.TableValues, targetName string) string {
if len(tableValues.Fields) < 2 {
slog.Error("insufficient fields in table, expected at least 2", slog.String("table", tableValues.Name), slog.Int("fields", len(tableValues.Fields)))
return ""
}
data := [][]float64{}
datasetNames := []string{}
for _, field := range tableValues.Fields[1:] {
points := []float64{}
for _, val := range field.Values {
if val == "" {
break
}
stat, err := strconv.ParseFloat(val, 64)
if err != nil {
slog.Error("error parsing stat", slog.String("error", err.Error()))
return ""
}
points = append(points, stat)
}
if len(points) > 0 {
data = append(data, points)
datasetNames = append(datasetNames, field.Name)
}
}
chartConfig := report.ChartTemplateStruct{
ID: fmt.Sprintf("%s%d", tableValues.Name, util.RandUint(10000)),
XaxisText: "Time",
YaxisText: "% Residency",
TitleText: "",
DisplayTitle: "false",
DisplayLegend: "true",
AspectRatio: "2",
SuggestedMin: "0",
SuggestedMax: "0",
}
return telemetryTableHTMLRenderer(tableValues, data, datasetNames, chartConfig, nil)
}
// instructionTelemetryTableHTMLRenderer renders instruction set usage statistics.
// Each category is a separate dataset within the chart.
// Categories with zero total usage are hidden by default.
// Categories are sorted in two tiers: first, all non-zero categories are sorted alphabetically;
// then, all zero-sum categories are sorted alphabetically and placed after the non-zero categories.
func instructionTelemetryTableHTMLRenderer(tableValues table.TableValues, targetname string) string {
// Collect entries with their sums so we can sort per requirements
type instrEntry struct {
name string
points []float64
sum float64
}
entries := []instrEntry{}
if len(tableValues.Fields) < 2 {
slog.Error("insufficient fields in table, expected at least 2", slog.String("table", tableValues.Name), slog.Int("fields", len(tableValues.Fields)))
return ""
}
for _, field := range tableValues.Fields[1:] { // skip timestamp field
points := []float64{}
sum := 0.0
for _, val := range field.Values {
if val == "" { // end of data for this category
break
}
stat, err := strconv.ParseFloat(val, 64)
if err != nil {
slog.Error("error parsing stat", slog.String("error", err.Error()))
return ""
}
points = append(points, stat)
sum += stat
}
if len(points) > 0 { // only include categories with at least one point
entries = append(entries, instrEntry{name: field.Name, points: points, sum: sum})
}
}
// Partition into non-zero and zero-sum groups
nonZero := []instrEntry{}
zero := []instrEntry{}
for _, e := range entries {
if e.sum > 0 {
nonZero = append(nonZero, e)
} else {
zero = append(zero, e)
}
}
sort.Slice(nonZero, func(i, j int) bool { return nonZero[i].name < nonZero[j].name })
sort.Slice(zero, func(i, j int) bool { return zero[i].name < zero[j].name })
ordered := append(nonZero, zero...)
data := make([][]float64, 0, len(ordered))
datasetNames := make([]string, 0, len(ordered))
hiddenFlags := make([]bool, 0, len(ordered))
for _, e := range ordered {
data = append(data, e.points)
datasetNames = append(datasetNames, e.name)
// hide zero-sum categories by default
hiddenFlags = append(hiddenFlags, e.sum == 0)
}
chartConfig := report.ChartTemplateStruct{
ID: fmt.Sprintf("%s%d", tableValues.Name, util.RandUint(10000)),
XaxisText: "Time",
YaxisText: "% Samples",
TitleText: "",
DisplayTitle: "false",
DisplayLegend: "true",
AspectRatio: "1", // extra tall due to large number of data sets
SuggestedMin: "0",
SuggestedMax: "0",
}
return telemetryTableHTMLRenderer(tableValues, data, datasetNames, chartConfig, hiddenFlags)
}
func renderGaudiStatsChart(tableValues table.TableValues, chartStatFieldName string, titleText string, yAxisText string, suggestedMax string) string {
data := [][]float64{}
datasetNames := []string{}
// timestamp is in the first field
// find the module_id field index
moduleIdFieldIdx, err := table.GetFieldIndex("module_id", tableValues)
if err != nil {
slog.Error("no gaudi module_id field found")
return ""
}
// find the chartStatFieldName field index
chartStatFieldIndex, err := table.GetFieldIndex(chartStatFieldName, tableValues)
if err != nil {
slog.Error("no gaudi chartStatFieldName field found")
return ""
}
if len(tableValues.Fields) == 0 {
slog.Error("no fields in table", slog.String("table", tableValues.Name))
return ""
}
// group the data points by module_id
moduleStat := make(map[string][]float64)
for i := range tableValues.Fields[0].Values {
if i >= len(tableValues.Fields[moduleIdFieldIdx].Values) || i >= len(tableValues.Fields[chartStatFieldIndex].Values) {
slog.Error("field values length mismatch", slog.String("table", tableValues.Name), slog.Int("index", i))
break
}
moduleId := tableValues.Fields[moduleIdFieldIdx].Values[i]
val, err := strconv.ParseFloat(tableValues.Fields[chartStatFieldIndex].Values[i], 64)
if err != nil {
slog.Error("error parsing utilization", slog.String("error", err.Error()))
return ""
}
if _, ok := moduleStat[moduleId]; !ok {
moduleStat[moduleId] = []float64{}
}
moduleStat[moduleId] = append(moduleStat[moduleId], val)
}
// sort the module ids
var moduleIds []string
for moduleId := range moduleStat {
moduleIds = append(moduleIds, moduleId)
}
sort.Strings(moduleIds)
// build the data
for _, moduleId := range moduleIds {
if len(moduleStat[moduleId]) > 0 {
data = append(data, moduleStat[moduleId])
datasetNames = append(datasetNames, "module "+moduleId)
}
}
chartConfig := report.ChartTemplateStruct{
ID: fmt.Sprintf("%s%d", tableValues.Name, util.RandUint(10000)),
XaxisText: "Time",
YaxisText: yAxisText,
TitleText: titleText,
DisplayTitle: "true",
DisplayLegend: "true",
AspectRatio: "2",
SuggestedMin: "0",
SuggestedMax: suggestedMax,
}
return telemetryTableHTMLRenderer(tableValues, data, datasetNames, chartConfig, nil)
}
func gaudiTelemetryTableHTMLRenderer(tableValues table.TableValues, targetName string) string {
out := ""
out += renderGaudiStatsChart(tableValues, "utilization.aip [%]", "Utilization", "% Utilization", "100")
out += renderGaudiStatsChart(tableValues, "memory.free [MiB]", "Memory Free", "Memory (MiB)", "0")
out += renderGaudiStatsChart(tableValues, "memory.used [MiB]", "Memory Used", "Memory (MiB)", "0")
out += renderGaudiStatsChart(tableValues, "power.draw [W]", "Power", "Watts", "0")
out += renderGaudiStatsChart(tableValues, "temperature.aip [C]", "Temperature", "Temperature (C)", "0")
return out
}
func pduTelemetryTableHTMLRenderer(tableValues table.TableValues, targetName string) string {
if len(tableValues.Fields) < 2 {
slog.Error("insufficient fields in table, expected at least 2", slog.String("table", tableValues.Name), slog.Int("fields", len(tableValues.Fields)))
return ""
}
data := [][]float64{}
for _, field := range tableValues.Fields[1:] {
points := []float64{}
for _, val := range field.Values {
if val == "" {
break
}
stat, err := strconv.ParseFloat(val, 64)
if err != nil {
slog.Error("error parsing stat", slog.String("error", err.Error()))
return ""
}
points = append(points, stat)
}
if len(points) > 0 {
data = append(data, points)
}
}
datasetNames := []string{}
for _, field := range tableValues.Fields[1:] {
datasetNames = append(datasetNames, field.Name)
}
chartConfig := report.ChartTemplateStruct{
ID: fmt.Sprintf("%s%d", tableValues.Name, util.RandUint(10000)),
XaxisText: "Time",
YaxisText: "Watts",
TitleText: "",
DisplayTitle: "false",
DisplayLegend: "true",
AspectRatio: "2",
SuggestedMin: "0",
SuggestedMax: "0",
}
return telemetryTableHTMLRenderer(tableValues, data, datasetNames, chartConfig, nil)
}
func kernelTelemetryTableHTMLRenderer(tableValues table.TableValues, targetName string) string {
if len(tableValues.Fields) < 2 {
slog.Error("insufficient fields in table, expected at least 2", slog.String("table", tableValues.Name), slog.Int("fields", len(tableValues.Fields)))
return ""
}
data := [][]float64{}
datasetNames := []string{}
for _, field := range tableValues.Fields[1:] {
points := []float64{}
for _, val := range field.Values {
if val == "" {
break
}
stat, err := strconv.ParseFloat(val, 64)
if err != nil {
slog.Error("error parsing stat", slog.String("error", err.Error()))
return ""
}
points = append(points, stat)
}
if len(points) > 0 {
data = append(data, points)
datasetNames = append(datasetNames, field.Name)
}
}
chartConfig := report.ChartTemplateStruct{
ID: fmt.Sprintf("%s%d", tableValues.Name, util.RandUint(10000)),
XaxisText: "Time",
YaxisText: "count per second",
TitleText: "",
DisplayTitle: "false",
DisplayLegend: "true",
AspectRatio: "2",
SuggestedMin: "0",
SuggestedMax: "0",
}
return telemetryTableHTMLRenderer(tableValues, data, datasetNames, chartConfig, nil)
}