forked from intel/PerfSpect
-
Notifications
You must be signed in to change notification settings - Fork 0
/
perf-postprocess.py
916 lines (809 loc) · 32.6 KB
/
perf-postprocess.py
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
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
#!/usr/bin/env python3
###########################################################################################################
# Copyright (C) 2021-2023 Intel Corporation
# SPDX-License-Identifier: BSD-3-Clause
###########################################################################################################
import json
import logging
import numpy as np
import os
import pandas as pd
import re
import sys
from argparse import ArgumentParser
from enum import Enum
from simpleeval import simple_eval
from src.common import crash
from src import perf_helpers
from src import report
logging.basicConfig(
format="%(asctime)s %(levelname)s: %(message)s",
datefmt="%H:%M:%S",
level=logging.NOTSET,
handlers=[logging.FileHandler("debug.log"), logging.StreamHandler(sys.stdout)],
)
log = logging.getLogger(__name__)
class Mode(Enum):
System = 1
Socket = 2
Core = 3
# get the filenames for miscellaneous outputs
def get_extra_out_file(out_file, t):
dirname = os.path.dirname(out_file)
filename = os.path.basename(out_file)
t_file = ""
if t == "a":
text = "sys.average"
elif t == "r":
text = "sys.raw"
elif t == "s":
text = "socket"
elif t == "sa":
text = "socket.average"
elif t == "sr":
text = "socket.raw"
elif t == "c":
text = "core"
elif t == "ca":
text = "core.average"
elif t == "cr":
text = "core.raw"
elif t == "m":
text = "sys"
parts = os.path.splitext(filename)
if len(parts) == 1:
t_file = text + "." + filename
else:
t_file = parts[-2] + "." + text + ".csv"
return os.path.join(dirname, t_file)
def get_args(script_path):
parser = ArgumentParser(description="perf-postprocess: perf post process")
required_arg = parser.add_argument_group("required arguments")
required_arg.add_argument(
"-r",
"--rawfile",
type=str,
default=None,
help="Raw CSV output from perf-collect",
)
parser.add_argument(
"--version", "-V", help="display version information", action="store_true"
)
parser.add_argument(
"-o",
"--outfile",
type=str,
default=script_path + "/results/metric_out.csv",
help="perf stat outputs in csv format, default=results/metric_out.csv",
)
parser.add_argument(
"-v",
"--verbose",
help="include debugging information, keeps all intermediate csv files",
action="store_true",
)
parser.add_argument(
"--rawevents",
help="save raw events in .csv format",
action="store_true",
)
parser.add_argument(
"-html",
"--html",
type=str,
default=None,
help="Static HTML report",
)
args = parser.parse_args()
# if args.version, print version then exit
if args.version:
print(perf_helpers.get_tool_version())
sys.exit()
# check rawfile argument is given
if args.rawfile is None:
crash("Missing raw file, please provide raw csv generated using perf-collect")
# check rawfile argument exists
if args.rawfile and not os.path.isfile(args.rawfile):
crash("perf raw data file not found, please provide valid raw file")
# check output file is valid
if not perf_helpers.validate_outfile(args.outfile, True):
crash(
"Output filename: "
+ args.outfile
+ " not accepted. Filename should be a .csv without special characters"
)
# check output file is writable
if not perf_helpers.check_file_writeable(args.outfile):
crash("Output file %s not writeable " % args.outfile)
return args
# get metadata lines and perf events' lines in three separate lists
def get_all_data_lines(input_file_path):
with open(input_file_path, "r") as infile:
lines = infile.readlines()
# input file has three headers:
# 1- ### META DATA ###,
# 2- ### PERF EVENTS ###,
# 3- ### PERF DATA ###,
meta_data_lines = []
perf_events_lines = []
perf_data_lines = []
meta_data_started = False
perf_events_started = False
perf_data_started = False
for idx, line in enumerate(lines):
if line.strip() == "": # skip empty lines
continue
# check first line is META Data header
elif (idx == 0) and ("### META DATA ###" not in line):
crash(
"The perf raw file doesn't contain metadata, please re-collect perf raw data"
)
elif "### META DATA ###" in line:
meta_data_started = True
perf_events_started = False
perf_data_started = False
elif "### PERF EVENTS ###" in line:
meta_data_started = False
perf_events_started = True
perf_data_started = False
elif "### PERF DATA ###" in line:
meta_data_started = False
perf_events_started = False
perf_data_started = True
elif meta_data_started:
meta_data_lines.append(line.strip())
elif perf_events_started:
perf_events_lines.append(line.strip())
elif perf_data_started:
if line.startswith("# started on"):
# this line is special, it is under "PERF DATA" (printed by perf), but it is treatesd as metadata
meta_data_lines.append(line.strip())
else:
fields = line.split(",")
perf_data_lines.append(fields)
infile.close()
return meta_data_lines, perf_events_lines, perf_data_lines
# get_metadata
def get_metadata_as_dict(meta_data_lines):
meta_data = {}
meta_data["constants"] = {}
for line in meta_data_lines:
if line.startswith("TSC"):
meta_data["constants"]["CONST_TSC_FREQ"] = (
float(line.split(",")[1]) * 1000000
)
elif line.startswith("CPU"):
meta_data["constants"]["CONST_CORE_COUNT"] = float(line.split(",")[1])
elif line.startswith("HT"):
meta_data["constants"]["CONST_HT_COUNT"] = float(line.split(",")[1])
meta_data["constants"]["CONST_THREAD_COUNT"] = float(
line.split(",")[1]
) # we use both constants interchangeably
elif line.startswith("SOCKET"):
meta_data["constants"]["CONST_SOCKET_COUNT"] = float(line.split(",")[1])
elif line.startswith("IMC"):
meta_data["constants"]["CONST_IMC_COUNT"] = float(line.split(",")[1])
elif line.startswith("CHA") or line.startswith("CBOX"):
meta_data["constants"]["CONST_CHA_COUNT"] = float(line.split(",")[1])
elif line.startswith("Sampling"):
meta_data["constants"]["CONST_INTERVAL"] = float(line.split(",")[1])
elif line.startswith("Architecture"):
meta_data["constants"]["CONST_ARCH"] = str(line.split(",")[1])
elif line.startswith("Event grouping"):
meta_data["EVENT_GROUPING"] = (
True if (str(line.split(",")[1]) == "enabled") else False
)
elif line.startswith("cgroups"):
if line.startswith("cgroups=disabled"):
meta_data["CGROUPS"] = "disabled"
continue
# Get cgroup status and cgroup_id to container_name mapping
meta_data["CGROUPS"] = "enabled"
meta_data["CGROUP_HASH"] = dict(
item.split("=")
for item in line.split("cgroups=enabled,")[1].rstrip(",\n").split(",")
)
docker_HASH = []
docker_HASH = list(meta_data["CGROUP_HASH"].values())
elif (
line.startswith("cpusets")
and "CGROUPS" in meta_data
and meta_data["CGROUPS"] == "enabled"
):
line = line.replace("cpusets,", "")
docker_SETS = []
docker_SETS = line.split(",")
docker_SETS = docker_SETS[:-1]
# here lognth of docker_HASH should be exactly len(docker_SETS)
assert len(docker_HASH) == len(docker_SETS)
meta_data["CPUSETS"] = {}
for i in range(1, len(docker_SETS)):
docker_SET = str(docker_SETS[i])
docker_SET = (
int(docker_SET.split("-")[1]) - int(docker_SET.split("-")[0]) + 1
)
meta_data["CPUSETS"][docker_HASH[i]] = docker_SET
elif line.startswith("Percore mode"):
meta_data["PERCORE_MODE"] = (
True if (str(line.split(",")[1]) == "enabled") else False
)
elif line.startswith("Persocket mode"):
meta_data["PERSOCKET_MODE"] = (
True if (str(line.split(",")[1]) == "enabled") else False
)
elif line.startswith("# started on"):
meta_data["TIME_ZONE"] = str(line.split("# started on")[1])
elif line.startswith("Socket"):
if "SOCKET_CORES" not in meta_data:
meta_data["SOCKET_CORES"] = []
cores = ((line.split("\n")[0]).split(",")[1]).split(";")[:-1]
meta_data["SOCKET_CORES"].append(cores)
return meta_data
def set_CONST_TSC(meta_data, perf_mode, num_cpus=0):
if perf_mode == Mode.System:
meta_data["constants"]["CONST_TSC"] = (
meta_data["constants"]["CONST_TSC_FREQ"]
* meta_data["constants"]["CONST_CORE_COUNT"]
* meta_data["constants"]["CONST_HT_COUNT"]
* meta_data["constants"]["CONST_SOCKET_COUNT"]
)
elif perf_mode == Mode.Socket:
meta_data["constants"]["CONST_TSC"] = (
meta_data["constants"]["CONST_TSC_FREQ"]
* meta_data["constants"]["CONST_CORE_COUNT"]
* meta_data["constants"]["CONST_HT_COUNT"]
)
elif perf_mode == Mode.Core: # Core should be changed to thread
meta_data["constants"]["CONST_TSC"] = meta_data["constants"]["CONST_TSC_FREQ"]
elif meta_data["CGROUPS"] == "enabled":
meta_data["constants"]["CONST_TSC"] = (
meta_data["constants"]["CONST_TSC_FREQ"] * num_cpus
)
return
def get_event_name(event_line):
event_name = event_line
if "name=" in event_name:
matches = re.findall(r"\.*name=\'(.*?)\'.*", event_name)
assert len(matches) > 0
event_name = matches[0]
if event_name.endswith(":c"): # core event
event_name = event_name.split(":c")[0]
if event_name.endswith(":u"): # uncore event
event_name = event_name.split(":u")[0]
# clean up , or ;
event_name = event_name.replace(",", "").replace(";", "")
return event_name
def get_event_groups(event_lines):
groups = {}
group_indx = 0
current_group = []
for event in event_lines:
if ";" in event: # end of group
current_group.append(get_event_name(event))
groups["group_" + str(group_indx)] = current_group
group_indx += 1
current_group = []
else:
current_group.append(get_event_name(event))
return groups
def get_metric_file_name(microarchitecture):
metric_file = ""
if microarchitecture == "broadwell":
metric_file = "metric_bdx.json"
elif microarchitecture == "skylake" or microarchitecture == "cascadelake":
metric_file = "metric_skx_clx.json"
elif microarchitecture == "icelake":
metric_file = "metric_icx.json"
elif microarchitecture == "sapphirerapids":
metric_file = "metric_spr.json"
else:
crash("Suitable metric file not found")
# Convert path of json file to relative path if being packaged by pyInstaller into a binary
if getattr(sys, "frozen", False):
basepath = getattr(sys, "_MEIPASS", os.path.dirname(os.path.abspath(__file__)))
metric_file = os.path.join(basepath, metric_file)
elif __file__:
metric_file = script_path + "/events/" + metric_file
else:
crash("Unknown application type")
return metric_file
def validate_file(fname):
if not os.access(fname, os.R_OK):
crash(str(fname) + " not accessible")
def get_metrics_formula(architecture):
# get the metric file name based on architecture
metric_file = get_metric_file_name(architecture)
validate_file(metric_file)
with open(metric_file, "r") as f_metric:
try:
metrics = json.load(f_metric)
for m in metrics:
m["events"] = re.findall(r"\[(.*?)\]", m["expression"])
return metrics
except json.decoder.JSONDecodeError:
crash("Invalid JSON, please provide a valid JSON as metrics file")
return
def get_socket_number(sockets_dict, core):
core_index = core.replace("CPU", "")
for s in range(len(sockets_dict)):
if core_index in sockets_dict[s]:
return s
return
def extract_dataframe(perf_data_lines, meta_data, perf_mode):
# parse event data into dataframe and set header names
perf_data_df = pd.DataFrame(perf_data_lines)
if "CGROUPS" in meta_data and meta_data["CGROUPS"] == "enabled":
# 1.001044566,6261968509,,L1D.REPLACEMENT,/system.slice/docker-826c1c9de0bde13b0c3de7c4d96b38710cfb67c2911f30622508905ece7e0a16.scope,6789274819,5.39,,
assert len(perf_data_df.columns) >= 7
columns = [
"ts",
"value",
"col0",
"metric",
"cgroup",
"col1",
"percentage",
]
# add dummy col names for remaining columns
for col in range(7, len(perf_data_df.columns)):
columns.append("col" + str(col))
perf_data_df.columns = columns
elif perf_mode == Mode.System:
# Ubuntu 16.04 returns 6 columns, later Ubuntu's and other OS's return 8 columns
assert len(perf_data_df.columns) >= 6
columns = [
"ts",
"value",
"col0",
"metric",
"value2",
"percentage",
]
# add dummy col names for remaining columns
for col in range(6, len(perf_data_df.columns)):
columns.append("col" + str(col))
perf_data_df.columns = columns
elif perf_mode == Mode.Core or perf_mode == Mode.Socket:
assert len(perf_data_df.columns) >= 7
columns = [
"ts",
"cpu",
"value",
"col0",
"metric",
"value2",
"percentage",
]
# add dummy col names for remaining columns
for col in range(7, len(perf_data_df.columns)):
columns.append("col" + str(col))
perf_data_df.columns = columns
# Add socket column
perf_data_df["socket"] = perf_data_df.apply(
lambda x: "S" + str(get_socket_number(meta_data["SOCKET_CORES"], x["cpu"])),
axis=1,
)
# fix metric name X.1, X.2, etc -> just X
perf_data_df["metric"] = perf_data_df.apply(
lambda x: ".".join(x["metric"].split(".")[:-1])
if len(re.findall(r"^[0-9]*$", x["metric"].split(".")[-1])) > 0
else x["metric"],
axis=1,
)
# set data frame types
# perf_data_df = perf_data_df.astype({'value': 'float'})
perf_data_df["value"] = pd.to_numeric(
perf_data_df["value"], errors="coerce"
).fillna(0)
# perf_data_df = perf_data_df.astype({'value2': 'float'})
# perf_data_df = perf_data_df.astype({"percentage":"float"})
return perf_data_df
# get group data frame after grouping
def get_group_df(time_slice_df, start_index, end_of_group_index, perf_mode):
g_df = time_slice_df[start_index:end_of_group_index]
if perf_mode == Mode.System:
g_df = g_df[["metric", "value"]].groupby("metric")["value"].sum().to_frame()
elif perf_mode == Mode.Socket:
if "socket" in g_df:
g_df = (
g_df[["metric", "socket", "value"]]
.groupby(["metric", "socket"])["value"]
.sum()
.to_frame()
)
else:
crash("No socket information found, exiting...")
elif perf_mode == Mode.Core: # check dataframe has cpu colmn, otherwise raise error
if "cpu" in g_df:
g_df = (
g_df[["metric", "cpu", "value"]]
.groupby(["metric", "cpu"])["value"]
.sum()
.to_frame()
)
else:
crash("No CPU information found, exiting...")
return g_df
def get_event_expression_from_group(
expressions_to_evaluate, event_df, exp_to_evaluate, event
):
if event_df.shape == (1,): # system wide
if "sys" not in expressions_to_evaluate:
expressions_to_evaluate["sys"] = exp_to_evaluate.replace(
"[" + event + "]", str(event_df[0])
)
else:
expressions_to_evaluate["sys"] = expressions_to_evaluate["sys"].replace(
"[" + event + "]", str(event_df[0])
)
else:
for index, value in event_df.iterrows():
if index not in expressions_to_evaluate:
expressions_to_evaluate[index] = exp_to_evaluate
expressions_to_evaluate[index] = expressions_to_evaluate[index].replace(
"[" + event + "]", str(value[0])
)
return
def generate_metrics_time_series(time_series_df, perf_mode, out_file_path):
time_series_df_T = time_series_df.T
time_series_df_T.index.name = "time"
metric_file_name = ""
if perf_mode == Mode.System:
metric_file_name = get_extra_out_file(out_file_path, "m")
if perf_mode == Mode.Socket:
metric_file_name = get_extra_out_file(out_file_path, "s")
if perf_mode == Mode.Core:
metric_file_name = get_extra_out_file(out_file_path, "c")
# generate metrics with time indexes
time_series_df_T.to_csv(metric_file_name)
return
def generate_metrics_averages(time_series_df, perf_mode, out_file_path):
average_metric_file_name = ""
if perf_mode == Mode.System:
average_metric_file_name = get_extra_out_file(out_file_path, "a")
if perf_mode == Mode.Socket:
average_metric_file_name = get_extra_out_file(out_file_path, "sa")
if perf_mode == Mode.Core:
average_metric_file_name = get_extra_out_file(out_file_path, "ca")
time_series_df.index.name = "metrics"
avgcol = time_series_df.mean(numeric_only=True, axis=1).to_frame().reset_index()
p95col = time_series_df.quantile(q=0.95, axis=1).to_frame().reset_index()
mincol = time_series_df.min(axis=1).to_frame().reset_index()
maxcol = time_series_df.max(axis=1).to_frame().reset_index()
# define columns headers
avgcol.columns = ["metrics", "avg"]
p95col.columns = ["metrics", "p95"]
mincol.columns = ["metrics", "min"]
maxcol.columns = ["metrics", "max"]
# merge columns
time_series_df = time_series_df.merge(avgcol, on="metrics", how="outer")
time_series_df = time_series_df.merge(p95col, on="metrics", how="outer")
time_series_df = time_series_df.merge(mincol, on="metrics", how="outer")
time_series_df = time_series_df.merge(maxcol, on="metrics", how="outer")
time_series_df[["metrics", "avg", "p95", "min", "max"]].to_csv(
average_metric_file_name, index=False
)
return
def log_skip_metric(metric, instance, msg):
log.warning(
msg
+ ': metric "'
+ metric["name"]
+ '" expression "'
+ metric["expression"]
+ '" values "'
+ instance
+ '"'
)
def generate_metrics(
perf_data_df,
out_file_path,
group_to_event,
metadata,
metrics,
perf_mode,
verbose=False,
):
time_slice_groups = perf_data_df.groupby("ts", sort=False)
time_metrics_result = {}
for time_slice, item in time_slice_groups:
time_slice_df = time_slice_groups.get_group(time_slice)
current_group_indx = 0
group_to_df = {}
start_index = 0
end_of_group_index = 0
for index, row in time_slice_df.iterrows():
if row["metric"] in event_groups["group_" + str(current_group_indx)]:
end_of_group_index += 1
continue
else: # move to next group
group_to_df["group_" + str(current_group_indx)] = get_group_df(
time_slice_df, start_index, end_of_group_index, perf_mode
)
start_index = end_of_group_index
end_of_group_index += 1
current_group_indx += 1
# add last group
group_to_df["group_" + str(current_group_indx)] = get_group_df(
time_slice_df, start_index, time_slice_df.shape[0], perf_mode
)
metrics_results = {}
for m in metrics:
non_constant_mertics = []
exp_to_evaluate = m["expression"]
# substitute constants
for event in m["events"]:
if (
event.upper() in metadata["constants"]
): # all constants are save in metadata in Uppercase
exp_to_evaluate = exp_to_evaluate.replace(
"[" + event + "]", str(metadata["constants"][event.upper()])
)
else:
non_constant_mertics.append(event)
# find a single group with the events
single_group = False
for g in group_to_event:
if set(non_constant_mertics) <= set(
group_to_event[g]
): # if all events in metric m exist in group g
single_group = True
g_df = group_to_df[g]
expressions_to_evaluate = {}
for event in non_constant_mertics:
event_df = g_df.loc[event]
get_event_expression_from_group(
expressions_to_evaluate, event_df, exp_to_evaluate, event
)
for instance in expressions_to_evaluate:
if (
"[" in expressions_to_evaluate[instance]
or "]" in expressions_to_evaluate[instance]
):
if verbose:
log_skip_metric(
m,
expressions_to_evaluate[instance],
"MISSING DATA",
)
continue # cannot evaluate expression, skipping
try:
result = str(
"{:.8f}".format(
simple_eval(
expressions_to_evaluate[instance],
functions={"min": min, "max": max},
)
)
)
except ZeroDivisionError:
if verbose:
log_skip_metric(
m,
expressions_to_evaluate[instance],
"ZERO DIVISION",
)
result = 0
sub_txt = "" if instance == "sys" else "." + instance
metrics_results[m["name"] + sub_txt] = float(result)
break # no need to check other groups
if not single_group:
if verbose:
log.warning('MULTIPLE GROUPS: metric "' + m["name"] + '"')
# get events from multiple groups
remaining_events_to_find = list(non_constant_mertics)
expressions_to_evaluate = {}
for event in non_constant_mertics:
for g in group_to_event:
if event in group_to_event[g]:
remaining_events_to_find.remove(event)
g_df = group_to_df[g]
event_df = g_df.loc[event]
get_event_expression_from_group(
expressions_to_evaluate,
event_df,
exp_to_evaluate,
event,
)
break # no need to check in other groups
if len(remaining_events_to_find) == 0: # all events are found
for (
instance
) in (
expressions_to_evaluate
): # instance is either system, specific core, or specific socket
if (
"[" in expressions_to_evaluate[instance]
or "]" in expressions_to_evaluate[instance]
):
if verbose:
log_skip_metric(
m,
expressions_to_evaluate[instance],
"MISSING DATA",
)
continue
try:
result = str(
"{:.8f}".format(
simple_eval(
expressions_to_evaluate[instance],
functions={"min": min, "max": max},
)
)
)
except ZeroDivisionError:
if verbose:
log_skip_metric(
m,
expressions_to_evaluate[instance],
"ZERO DIVISION",
)
result = 0
sub_txt = "" if instance == "sys" else "." + instance
metrics_results[m["name"] + sub_txt] = float(result)
else: # some events are missing
if verbose:
log.warning(
'MISSING EVENTS: metric "'
+ m["name"]
+ '" events "'
+ str(remaining_events_to_find)
+ '"'
)
continue # skip metric
time_metrics_result[time_slice] = metrics_results
time_series_df = pd.DataFrame(time_metrics_result)
generate_metrics_time_series(time_series_df, perf_mode, out_file_path)
generate_metrics_averages(time_series_df, perf_mode, out_file_path)
return
def generate_raw_events_system_wide(perf_data_df, out_file_path):
perf_data_df_system_raw = (
perf_data_df[["metric", "value"]].groupby("metric")["value"].sum().to_frame()
)
last_time_stamp = float(perf_data_df["ts"].tail(1).values[0])
# average per second. Last time stamp = total collection duration in seconds
perf_data_df_system_raw["avg"] = np.where(
perf_data_df_system_raw["value"] > 0,
perf_data_df_system_raw["value"] / last_time_stamp,
0,
)
sys_raw_file_name = get_extra_out_file(out_file_path, "r")
perf_data_df_system_raw["avg"].to_csv(sys_raw_file_name)
return
def generate_raw_events_socket(perf_data_df, out_file_path):
# print raw values persocket
perf_data_df_scoket_raw = (
perf_data_df[["metric", "socket", "value"]]
.groupby(["metric", "socket"])["value"]
.sum()
.to_frame()
)
last_time_stamp = float(perf_data_df["ts"].tail(1).values[0])
perf_data_df_scoket_raw["avg"] = np.where(
perf_data_df_scoket_raw["value"] > 0,
perf_data_df_scoket_raw["value"] / last_time_stamp,
0,
)
metric_per_socket_frame = pd.pivot_table(
perf_data_df_scoket_raw,
index="metric",
columns="socket",
values="avg",
fill_value=0,
)
socket_raw_file_name = get_extra_out_file(out_file_path, "sr")
metric_per_socket_frame.to_csv(socket_raw_file_name)
return
def generate_raw_events_percore(perf_data_df, out_file_path):
# print raw values percore
perf_data_df_core_raw = (
perf_data_df[["metric", "cpu", "value"]]
.groupby(["metric", "cpu"])["value"]
.sum()
.to_frame()
)
last_time_stamp = float(perf_data_df["ts"].tail(1).values[0])
perf_data_df_core_raw["avg"] = np.where(
perf_data_df_core_raw["value"] > 0,
perf_data_df_core_raw["value"] / last_time_stamp,
0,
)
metric_per_cpu_frame = pd.pivot_table(
perf_data_df_core_raw, index="metric", columns="cpu", values="avg", fill_value=0
)
# drop uncore and power metrics
to_drop = []
for metric in metric_per_cpu_frame.index:
if metric.startswith("UNC_") or metric.startswith("power/"):
to_drop.append(metric)
metric_per_cpu_frame.drop(to_drop, inplace=True)
core_raw_file_name = get_extra_out_file(out_file_path, "cr", excelsheet=False)
metric_per_cpu_frame.to_csv(core_raw_file_name)
return
def generate_raw_events(perf_data_df, out_file_path, perf_mode):
if perf_mode.System:
generate_raw_events_system_wide(perf_data_df, out_file_path)
elif perf_mode.Socket:
generate_raw_events_socket(perf_data_df, out_file_path)
elif perf_mode.Core:
generate_raw_events_percore(perf_data_df, out_file_path)
if __name__ == "__main__":
script_path = os.path.dirname(os.path.realpath(__file__))
if "_MEI" in script_path:
script_path = script_path.rsplit("/", 1)[0]
# Parse arguments and check validity
args = get_args(script_path)
input_file_path = args.rawfile
out_file_path = args.outfile
# read all metadata, perf evernts, and perf data lines
# Note: this might not be feasible for very large files
meta_data_lines, perf_event_lines, perf_data_lines = get_all_data_lines(
input_file_path
)
# parse metadata and get mode (system, socket, or core)
meta_data = get_metadata_as_dict(meta_data_lines)
perf_mode = Mode.System
if "PERSOCKET_MODE" in meta_data and meta_data["PERSOCKET_MODE"]:
perf_mode = Mode.Socket
elif "PERCORE_MODE" in meta_data and meta_data["PERCORE_MODE"]:
perf_mode = Mode.Core
# set const TSC accoding to perf_mode
set_CONST_TSC(meta_data, perf_mode)
# parse event groups
event_groups = get_event_groups(perf_event_lines)
# extract data frame
perf_data_df = extract_dataframe(perf_data_lines, meta_data, perf_mode)
# parse metrics expressions
metrics = get_metrics_formula(meta_data["constants"]["CONST_ARCH"])
if args.rawevents: # generate raw events for system, persocket and percore
generate_raw_events(perf_data_df, out_file_path, perf_mode)
# generate metrics for each cgroup
if "CGROUPS" in meta_data and meta_data["CGROUPS"] == "enabled":
for cid in meta_data["CGROUP_HASH"]:
cid_perf_data_df = perf_data_df[perf_data_df["cgroup"] == cid]
cid_out_file_path = (
out_file_path.rsplit(".csv", 1)[0]
+ "_"
+ meta_data["CGROUP_HASH"][cid]
+ ".csv"
)
generate_metrics(
cid_perf_data_df,
cid_out_file_path,
event_groups,
meta_data,
metrics,
perf_mode,
args.verbose,
)
log.info("Generated results file(s) in: " + out_file_path.rsplit("/", 1)[0])
if args.html:
report.write_html(
cid_out_file_path,
perf_mode,
meta_data["constants"]["CONST_ARCH"],
args.html.replace(
".html", "_" + meta_data["CGROUP_HASH"][cid] + ".html"
),
)
# generate metrics for system, persocket or percore
else:
generate_metrics(
perf_data_df,
out_file_path,
event_groups,
meta_data,
metrics,
perf_mode,
args.verbose,
)
log.info("Generated results file(s) in: " + out_file_path.rsplit("/", 1)[0])
if args.html:
report.write_html(
out_file_path,
perf_mode,
meta_data["constants"]["CONST_ARCH"],
args.html,
)
log.info("Done!")