forked from andikleen/pmu-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ocperf.py
executable file
·1216 lines (1122 loc) · 40.5 KB
/
ocperf.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
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
# Copyright (c) 2011-2020, Intel Corporation
# Author: Andi Kleen
#
# This program is free software; you can redistribute it and/or modify it
# under the terms and conditions of the GNU General Public License,
# version 2, as published by the Free Software Foundation.
#
# This program is distributed in the hope it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
# more details.
#
# wrapper for perf for using named events and events with additional MSRs.
# syntax is like perf, except Intel events are listed and can be specified
#
# or library for other python program to convert intel event names to
# perf/raw format
#
# Features:
# - map intel events to raw perf events
# - enable disable workarounds for specific events
# - resolve uncore events
# - handle offcore event on older kernels
# For the later must run as root and only as a single instance per machine
# Normal events (mainly not OFFCORE) can be handled unprivileged
# For events you can specify additional intel names from the list
#
# env variables:
# PERF=... perf binary to use (default "perf")
# EVENTMAP=eventmap
# EVENTMAP2=eventmap
# EVENTMAP3=eventmap
# OFFCORE=eventmap
# UNCORE=eventmap
# UNCORE2=eventmap
# eventmap is a path name to a json file. can contain wildcards.
# When eventmap is not specified, look in ~/.cache/pmu-events/
# The eventmap is automatically downloaded there
# eventmap can be also a CPU identifer (GenuineIntel-FAMILY-MODEL, like GenuineIntel-06-37)
# (Note that the numbers are in upper case hex)
#
# TOPOLOGY=topologyfile
# topologyfile is a dump of the sysfs of another system (find /sys > file)
# Needed for uncore units. This is useful to generate perf command lines for other systems.
#
# OCVERBOSE=1 print which files are opened
#
# Special arguments:
# --no-period Never add a period
# --print only print
# --force-download Force event list download
# --experimental Support experimental events
# --noexplode Don't list all sub pmus for uncore events. Rely on perf stat to merge.
from __future__ import print_function
import sys
import os
import subprocess
import json
import re
import copy
import textwrap
if sys.version_info.major == 2:
from pipes import quote
else:
from shlex import quote
import itertools
import glob
from pmudef import EVENTSEL_ANY, EVENTSEL_INV, EVMASK, extra_flags
if sys.version_info.major == 3:
import typing # noqa
from typing import Set, List, Dict, Any, Tuple, DefaultDict # noqa
import msr as msrmod
import latego
import event_download
force_download = False
experimental = False
ocverbose = os.getenv("OCVERBOSE") is not None
exists_cache = dict() # type: Dict[str,bool]
emap_list = [] # type: List[EmapNativeJSON]
topology = None # type: None | Set[str]
def file_exists(s):
if s in exists_cache:
return exists_cache[s]
global topology
if topology is None:
top = os.getenv("TOPOLOGY")
topology = set()
if top:
try:
topology = {x.strip() for x in open(top).readlines()}
except OSError:
print("Cannot open topology", top, file=sys.stderr)
if s in topology:
return True
found = os.path.exists(s)
exists_cache[s] = found
return found
def has_format(s, pmu):
return file_exists("/sys/devices/%s/format/%s" % (pmu, s))
def has_format_any(f, pmu):
return has_format(f, pmu) or has_format(f, pmu + "_0")
warned = set()
def warn_once(s):
if s not in warned:
print(s, file=sys.stderr)
warned.add(s)
class PerfVersion(object):
def __init__(self):
minor = 0
perf = os.getenv("PERF")
if not perf:
perf = "perf"
try:
version = subprocess.Popen([perf, "--version"],
stdout=subprocess.PIPE).communicate()[0]
except OSError:
print("Cannot run", perf)
version = ""
if not isinstance(version, str):
version = version.decode('utf-8')
m = re.match(r"perf version (\d+)\.(\d+)\.", version)
version = 412 # assume that no match is new enough
if m:
major = int(m.group(1))
minor = int(m.group(2))
version = major * 100 + minor
pmu = "cpu_core" if os.path.exists("/sys/devices/cpu_core") else "cpu"
self.direct = os.getenv("DIRECT_MSR") or version < 400
self.offcore = has_format("offcore_rsp", pmu) and not self.direct
self.ldlat = has_format("ldlat", pmu) and not self.direct
self.has_name = version >= 304
self.has_uncore_expansion = version >= 412
version = PerfVersion()
class MSR(object):
def __init__(self):
self.reg = {}
def writemsr(self, msrnum, val, print_only = False):
print("msr %x = %x" % (msrnum, val, ))
if print_only:
return
msrmod.writemsr(msrnum, val)
def checked_writemsr(self, msr, val, print_only = False):
if msr in self.reg:
sys.exit("Multiple events use same register")
self.writemsr(msr, val, print_only)
self.reg[msr] = 1
qual_map = (
("amt1", "any=1", EVENTSEL_ANY, ""),
("percore", "percore=1", 0, ""),
("perf_metrics", "", 0, ""),
("i1", "inv=1", EVENTSEL_INV, ""),
("e1", "edge=1", 0, ""),
("e0", "edge=0", 0, ""),
("tx", "in_tx=1", 0, ""),
("sup", "", 0, "k"),
("usr=yes", "", 0, "u"),
("usr=no", "", 0, "k"),
("os=yes", "", 0, "k"),
("os=no", "", 0, "u"),
("anythr=yes", "any=1", 0, ""),
("anythr=no", "any=0", 0, ""),
("pdir", "", 0, "ppp"),
("precise=yes", "", 0, "pp"),
("cp", "in_tx_cp=1", 0, ""))
number = "(0x[0-9a-fA-F]+|[0-9]+)"
qualval_map = (
(r"event_select=" + number, "event=%#x", 0),
(r"u" + number, "umask=%#x", 0),
(r"c(?:mask=)?" + number, "cmask=%d", 24),
(r"e(?:dge=)?" + number, "edge=%d", 18),
(r"(?:sa|sample-after|period)=" + number, "period=%d", 0))
uncore_map = (
(r'e(\d)', 'edge='),
(r't=(\d+)', "thresh="),
(r'match=(0x[0-9a-fA-F]+)', "filter_occ="),
(r'filter1=(0x[0-9a-fA-F]+)', "config1=", 32),
(r"nc=(\d+)", "filter_nc="),
(r'filter=(0x[0-9a-fA-F]+)', "config1="),
(r'one_unit', '', ),
(r"u" + number, "umask="),
(r"opc=?" + number, "filter_opc="),
(r"tid=?" + number, "filter_tid="),
(r"state=?" + number, "filter_state="))
uncore_map_thresh = (
(r"c(?:mask=)?" + number, "thresh="),)
uncore_map_cmask = (
(r"c(?:mask=)?" + number, "cmask="),)
# newe gets modified
def convert_extra(extra, val, newe):
nextra = ""
while extra:
if extra[0] == ":":
extra = extra[1:]
continue
found = False
for j in qualval_map:
m = re.match(j[0], extra, re.I)
if m:
if j[2]:
val |= int(m.group(1), 0) << j[2]
newe.append(j[1] % (int(m.group(1), 0)))
extra = extra[len(m.group(0)):]
found = True
break
if found:
continue
found = False
for j in qual_map:
if extra.lower().startswith(j[0]):
val |= j[2]
newe.append(j[1])
extra = extra[len(j[0]):]
nextra += j[3]
found = True
break
if found:
continue
if not extra:
break
if extra[0] in perf_qual + "p":
nextra += extra[0]
extra = extra[1:]
continue
print("bad event qualifier", extra, file=sys.stderr)
break
return nextra, val
def gen_name(n, sup):
n = n.replace(".", "_").replace(":", "_").replace("=", "_")
if sup:
n += "_k"
return n
class Event(object):
def __init__(self, name, val, desc):
self.val = val
self.name = name
self.extra = ""
self.userextra = ""
self.msr = 0
self.msrval = 0
self.desc = desc
self.precise = 0
self.collectpebs = 0
self.newextra = ""
self.overflow = None
self.errata = None
self.counter = ""
self.period = 0
self.pname = None
# XXX return with pmu to be consistent with Uncore and fix callers
def output_newstyle(self, extra="", noname=False, period=False, name="", noexplode=False):
"""Format an perf event for output and return as perf event string.
Always uses new style (cpu/.../)."""
val = self.val
if extra:
extra = self.newextra + "," + extra
else:
extra = self.newextra
if self.pname:
e = self.pname
else:
e = "event=0x%x,umask=0x%x" % (val & 0xff, (val >> 8) & 0xff)
e += extra
if version.has_name:
if name:
e += ",name=" + name
elif not noname:
e += ",name=%s" % (gen_name(self.name, "sup" in (self.extra + extra)))
if period and self.period and ",period=" not in e:
e += ",period=%d" % self.period
return e
def output(self, use_raw=False, flags="", noname=False, period=False, name="", noexplode=False):
"""Format an event for output and return as perf event string.
use_raw when true return old style perf string (rXXX).
Otherwise chose between old and new style based on the
capabilities of the installed perf executable.
flags when set add perf flags (e.g. u for user, p for pebs)."""
val = self.val
newe = []
extra = "".join(sorted(merge_extra(extra_set(self.extra), extra_set(flags))))
extra, val = convert_extra(":" + extra, val, newe)
if version.direct or use_raw:
if self.pname:
ename = self.pname
else:
ename = "r%x" % (val,)
if extra:
ename += ":" + extra
# XXX should error for extras that don't fit into raw
else:
p = self.output_newstyle(extra=",".join(newe),
noname=noname, period=period, name=name)
ename = "%s/%s/" % (self.pmu, p) + extra
return ename
def filter_qual(self):
def check_qual(q):
if q == "":
return True
if "=" in q:
q, _ = q.split("=")
if has_format_any(q, self.pmu):
return True
warn_once("%s: format %s not supported. Filtering out" % (self. pmu, q))
return False
self.newextra = ",".join(filter(check_qual, self.newextra.split(",")))
box_to_perf = {
"cbo": "cbox",
"qpi_ll": "qpi",
"sbo": "sbox",
}
def box_exists(box):
return file_exists("/sys/devices/uncore_%s" % (box))
def int_or_zero(row, name):
if name in row:
if row[name] == 'False':
return 0
if row[name] == 'True':
return 1
return int(row[name])
return 0
uncore_units = {
"imph-u": "arb",
"kti ll": "upi",
"m3kti": "m3upi",
"upi ll": "upi",
}
def convert_uncore(flags, extra_map):
o = ""
while flags:
for j in uncore_map + extra_map:
if flags[0] == ",":
flags = flags[1:]
match, repl = j[0], j[1]
m = re.match(match, flags, re.I)
if m:
if repl == "":
pass
elif len(j) > 2:
o += "," + repl + ("%#x" % (int(m.group(1), 0) << j[2]))
else:
o += "," + repl + m.group(1)
flags = flags[m.end():]
if flags == "":
break
if flags[0:1] == ":":
flags = flags[1:]
else:
if flags != "":
if len(extra_map) > 0:
print("Uncore cannot parse", flags, file=sys.stderr)
break
return o
class UncoreEvent(object):
def __init__(self, name, row):
self.name = name
e = self
if 'PublicDescription' in row:
e.desc = row['PublicDescription'].strip()
elif 'BriefDescription' in row:
e.desc = row['BriefDescription'].strip()
else:
e.desc = row['Description'].strip()
e.code = int(row['EventCode'], 16)
if 'Internal' in row and int(row['Internal']) != 0:
e.code |= int(row['Internal']) << 21
e.umask = int(row['UMask'], 16)
e.cmask = int_or_zero(row, 'CounterMask')
e.inv = int_or_zero(row, 'Invert')
e.edge = int_or_zero(row, 'EdgeDetect')
e.unit = row['Unit'].lower()
if e.unit in uncore_units:
e.unit = uncore_units[e.unit]
if e.unit == "ncu":
e.unit = "clock" if box_exists("clock") else "cbox"
e.umask = 0
e.code = 0xff
# xxx subctr
if e.unit in box_to_perf:
e.unit = box_to_perf[e.unit]
e.msr = None
e.overflow = 0
e.counter = "1" # dummy for toplev
e.newextra = ""
if 'Errata' in row:
e.errata = row['Errata']
else:
e.errata = None
self.extra = ''
self.userextra = ''
# {
# "Unit": "CBO",
# "EventCode": "0x22",
# "UMask": "0x21",
# "EventName": "UNC_CBO_XSNP_RESPONSE.MISS_EXTERNAL",
# "Description": "An external snoop misses in some processor core.",
# "Counter": "0,1",
# "CounterMask": "0",
# "Invert": "0",
# "EdgeDetect": "0"
# },
# XXX cannot separate sockets
# extra: perf flags
# flags: emon flags
def output_newstyle(self, newextra="", noname=False, period=False, name="", flags="", noexplode=False):
e = self
o = "/event=%#x" % e.code
if e.umask:
o += ",umask=%#x" % e.umask
if e.cmask:
o += ",cmask=%#x" % e.cmask
if e.edge:
o += ",edge=1"
if e.inv:
o += ",inv=1"
if e.newextra:
if flags:
flags += ","
flags += e.newextra
one_unit = "one_unit" in flags
if has_format_any("cmask", "uncore_" + e.unit):
extra_map = uncore_map_cmask
else:
extra_map = uncore_map_thresh
o += convert_uncore(flags, extra_map)
# xxx subctr, occ_sel, filters
if version.has_name and not noname:
if name == "":
name = gen_name(e.name, False)
o += ",name=" + name + "_NUM"
if newextra:
o += "," + ",".join(newextra)
o += "/"
# explode boxes if needed
def box_name(n):
return "%s_%d" % (e.unit, n)
def box_n_exists(n):
if one_unit and n > 0:
return False
return box_exists(box_name(n))
if not noexplode and not box_exists(e.unit) and box_n_exists(0):
return ",".join(["uncore_" + box_name(x) + o.replace("_NUM", "_%d" % (x)) for x in
itertools.takewhile(box_n_exists, itertools.count())])
return "uncore_%s%s" % (e.unit, o.replace("_NUM", ""))
def filter_qual(self):
def check_qual(q):
if q == "":
return False
if q == "one_unit":
return True
if "=" in q:
q, _ = q.split("=")
if has_format_any(q, "uncore_" + self.unit):
return True
warn_once("%s: format %s not supported. Filtering out" % (self.unit, q))
return False
self.newextra = ",".join(filter(check_qual, convert_uncore(self.newextra, ()).split(",")))
output = output_newstyle
def ffs(flag):
assert flag != 0
m = 1
j = 0
while (flag & m) == 0:
m = m << 1
j += 1
return j
perf_qual = "kuhGHSD" # without pebs
def extra_set(e):
return set(map(lambda x: x[0],
re.findall(r"(" + "|".join([x[0] for x in qual_map + qualval_map + uncore_map]) + "|[" + perf_qual + "]|p+)", e)))
def merge_extra(a, b):
m = a | b
if 'ppp' in m:
m = m - set(['p', 'pp'])
if 'pp' in m:
m = m - set(['p'])
m = m - set([':'])
return m
def print_event(name, desc, f, human, wrap, pmu=""):
desc = "".join([y for y in desc if y < chr(127)])
print(" %-42s" % (name,), end='', file=f)
if pmu:
print(" [%s]" % pmu, end='', file=f)
if human:
print("\n%s" % (wrap.fill(desc),), file=f)
else:
print(" [%s]" % (desc,), file=f)
def uncore_exists(box, postfix=""):
if file_exists("/sys/devices/uncore_" + box + postfix):
return True
if file_exists("/sys/devices/uncore_" + box + "_0" + postfix):
return True
return False
missing_boxes = set()
def check_uncore_event(e, extramsg):
if uncore_exists(e.unit):
if e.cmask and not uncore_exists(e.unit, "/format/cmask"):
warn_once("Uncore unit " + e.unit + " missing cmask for " + e.name)
extramsg.append("not supported due to missing cmask in PMU")
return None
if e.umask and not uncore_exists(e.unit, "/format/umask"):
warn_once("Uncore unit " + e.unit + " missing umask for " + e.name)
extramsg.append("not supported due to missing umask in PMU")
return None
return e
if e.unit not in missing_boxes:
warn_once("Uncore unit " + e.unit + " missing")
missing_boxes.add(e.unit)
extramsg.append("not supported due to missing PMU")
return None
fixed_counters = {
"inst_retired.any": (0xc0, 0, 0),
"cpu_clk_unhalted.thread": (0x3c, 0, 0),
"cpu_clk_unhalted.thread_any": (0x3c, 0, 1),
"cpu_clk_unhalted.core": (0x3c, 0, 1),
}
def update_ename(ev, name):
if ev:
ev = copy.deepcopy(ev)
ev.name = name
return ev
def json_open(name):
if ocverbose:
print("open", name, file=sys.stderr)
d = open(name, "rb").read()
if not isinstance(d, str):
d = d.decode('utf-8')
json_data = json.loads(d)
if isinstance(json_data, dict) and 'Events' in json_data:
json_data = json_data['Events']
return json_data
class EmapNativeJSON(object):
"""Read an event table."""
def __init__(self, name, pmu):
self.events = {}
self.perf_events = {}
self.codes = {}
self.desc = {}
self.pevents = {}
self.latego = False
self.uncore_events = {}
self.error = False
self.pmu = pmu
self.read_events(name)
def add_event(self, e):
self.events[e.name] = e
self.perf_events[e.name.replace('.', '_')] = e # workaround for perf-style naming
if e.pname:
self.pevents[e.pname] = e
self.codes[e.val] = e
self.desc[e.name] = e.desc
e.pmu = self.pmu
def read_table(self, r):
for row in r:
def get(x):
return row[x]
def gethex(x):
return int(get(x).split(",")[0], 16)
def getdec(x):
return int(get(x), 10)
name = get(u'EventName').lower().rstrip()
try:
code = gethex(u'EventCode')
umask = gethex(u'UMask')
except ValueError:
if ocverbose:
print("cannot parse event", name)
continue
anyf = 0
if name in fixed_counters:
code, umask, anyf = fixed_counters[name]
if u'Other' in row:
other = gethex(u'Other') << 16
else:
other = 0
other |= gethex(u'EdgeDetect') << 18
if u'AnyThread' in row:
other |= (gethex(u'AnyThread') | anyf) << 21
other |= getdec(u'CounterMask') << 24
other |= gethex(u'Invert') << 23
val = code | (umask << 8) | other
val &= EVMASK
d = get(u'PublicDescription')
if d is None:
d = ''
d = d.strip()
e = Event(name, val, d)
e.newextra = ""
if other & ((1 << 16)|(1 << 17)):
if other & (1<<16):
e.extra += "u"
if other & (1 << 17):
e.extra += "k"
e.perfqual = None
if u'MSRIndex' in row and get(u'MSRIndex') and get(u'MSRValue'):
msrnum = gethex(u'MSRIndex')
msrval = gethex(u'MSRValue')
if version.offcore and msrnum in (0x1a6, 0x1a7):
e.newextra = ",offcore_rsp=0x%x" % (msrval, )
e.perfqual = "offcore_rsp"
elif version.ldlat and msrnum in (0x3f6,):
e.newextra = ",ldlat=0x%x" % (msrval, )
e.perfqual = "ldlat"
elif msrnum == 0x3f7:
e.newextra = ",frontend=%#x" % (msrval, )
e.perfqual = "frontend"
# add new msr here
else:
e.msrval = msrval
e.msr = msrnum
if u'SampleAfterValue' in row:
e.overflow = get(u'SampleAfterValue')
e.counter = get(u'Counter')
e.precise = getdec(u'Precise') if u'Precise' in row else 0
e.collectpebs = getdec(u'CollectPEBS') if u'CollectPEBS' in row else 0
e.pebs = getdec(u'PEBS') if u'PEBS' in row else 0
if e.collectpebs > 1 or e.pebs >= 2:
e.extra += "pp"
if len(e.counter.split(",")) == 1:
e.extra += "p"
try:
if get(u'Errata') != "null":
try:
d += " Errata: "
d += get(u'Errata')
e.errata = get(u'Errata')
except UnicodeDecodeError:
pass
except KeyError:
pass
e.desc = d
for (flag, name) in extra_flags:
if val & flag:
e.newextra += ",%s=%d" % (name, (val & flag) >> ffs(flag), )
e.period = int(get(u'SampleAfterValue')) if u'SampleAfterValue' in row else 0
self.add_event(e)
def getevent(self, e, nocheck=False, extramsg=[]):
"""Retrieve an event with name e. Return Event object or None.
When nocheck is set don't check against current system."""
e = e.lower()
extra = ""
edelim = ""
m = re.match(r'([^:]+):request=([^:]+):response=([^:]+)', e)
if m:
ename = m.group(1) + "." + m.group(2) + "." + m.group(3)
return update_ename(self.getevent(ename, nocheck=nocheck, extramsg=extramsg), e)
m = re.match(r'(.*?):(.*)', e)
if m:
extra = m.group(2)
edelim = ":"
e = m.group(1)
if e in self.events:
# hack for now. Avoid ambiguity with :p
# Should handle qualmap properly here
extra = extra.replace("period=", "sample-after=")
userextra = extra
extra = extra_set(extra)
ev = self.events[e]
ev_extra = extra_set(ev.extra)
if extra and merge_extra(ev_extra, extra) > ev_extra:
ev = copy.deepcopy(self.events[e])
ev.userextra = userextra
ev.extra = "".join(sorted(merge_extra(ev_extra, extra)))
return ev
return self.events[e]
elif e.endswith("_ps"):
return update_ename(self.getevent(e[:-3] + ":p" + extra), e)
elif e.startswith("offcore") and (e + "_0") in self.events:
return update_ename(self.getevent(e + "_0" + edelim + extra), e)
elif e in self.uncore_events:
ev = self.uncore_events[e]
if ev and not nocheck:
ev = check_uncore_event(ev, extramsg)
if ev and extra:
ev = copy.deepcopy(ev)
ev.newextra = extra
return ev
elif e in self.perf_events:
return self.perf_events[e]
elif e in self.pevents:
return self.pevents[e]
extramsg.append("event not found for %s" % self.pmu)
return None
# XXX need to handle exploded events
def update_event(self, e, ev):
if e not in self.pevents:
self.pevents[e] = ev
def getraw(self, r):
e = "r%x" % (r)
if e in self.pevents:
ev = self.pevents[e]
s = ev.name
if ev.extra:
s += ":" + ev.extra
return s
return "!Raw 0x%x" % (r,)
def getperf(self, p):
if p in self.pevents:
e = self.pevents[p]
n = e.name
if e.userextra:
n += ":" + e.userextra
return n
return p
def dumpevents(self, f=sys.stdout, human=True, uncore=True):
"""Print all events with descriptions to the file descriptor f.
When human is true word wrap all the descriptions."""
wrap = None
if human:
wrap = textwrap.TextWrapper(initial_indent=" ",
subsequent_indent=" ")
for k in sorted(self.events.keys()):
print_event(k, self.desc[k], f, human, wrap, self.pmu)
if uncore:
for k in sorted(self.uncore_events.keys()):
print_event(k, self.uncore_events[k].desc, f, human, wrap)
def read_events(self, name):
"""Read JSON normal events table."""
if name.find("JKT") >= 0 or name.find("Jaketown") >= 0:
self.latego = True
try:
data = json_open(name)
except ValueError as e:
print("Cannot parse", name + ":", e.message, file=sys.stderr)
self.error = True
return
self.read_table(data)
if "topdown.slots" in self.events:
self.add_topdown()
def add_offcore(self, name):
"""Read offcore table."""
data = json_open(name)
# {
# "MATRIX_REQUEST": "DEMAND_DATA_RD",
# "MATRIX_RESPONSE": "NULL",
# "MATRIX_VALUE": "0x0000000001",
# "MATRIX_REGISTER": "0,1",
# "DESCRIPTION": "Counts demand data reads that"
# },
offcore_response = self.getevent("OFFCORE_RESPONSE")
if not offcore_response:
return
requests = []
responses = []
for row in data:
if row[u"MATRIX_REQUEST"].upper() != "NULL":
requests.append((row[u"MATRIX_REQUEST"], row[u"MATRIX_VALUE"], row[u"DESCRIPTION"]))
if row[u"MATRIX_RESPONSE"].upper() != "NULL":
responses.append((row[u"MATRIX_RESPONSE"], row[u"MATRIX_VALUE"], row[u"DESCRIPTION"]))
def create_event(req_name, req_val, req_desc, res_name, res_val, res_desc):
oe = copy.deepcopy(offcore_response)
oe.name = ("OFFCORE_RESPONSE.%s.%s" % (req_name, res_name)).lower()
if oe.name.lower() in self.events:
return
oe.msrval = int(req_val, 16) | (int(res_val, 16) << 16)
oe.desc = req_desc + " " + res_desc
if version.offcore:
oe.newextra = ",offcore_rsp=0x%x" % (oe.msrval, )
else:
oe.msr = 0x1a6
self.add_event(oe)
for a, b in itertools.product(requests, responses):
create_event(*(a + b))
def add_uncore(self, name, force=False):
data = json_open(name)
for row in data:
name = row['EventName'].lower()
try:
self.uncore_events[name] = UncoreEvent(name, row)
except UnicodeEncodeError:
pass
def add_topdown(self):
def td_event(name, pname, desc, counter):
e = Event(name, 0, desc)
e.counter = counter
e.pname = pname
self.add_event(e)
td_event("perf_metrics.retiring", "topdown-retiring", "Number of slots the pipeline was frontend bound.", "32")
td_event("perf_metrics.bad_speculation", "topdown-bad-spec", "Number of slots the pipeline was doing bad speculation.", "33")
td_event("perf_metrics.frontend_bound", "topdown-fe-bound", "Number of slots the pipeline was frontend bound.", "34")
td_event("perf_metrics.backend_bound", "topdown-be-bound", "Number of slots the pipeline was backend bound.", "35")
td_event("topdown.slots", "slots", "Number of slots", "36")
if "topdown.memory_bound_slots" in self.events:
td_event("perf_metrics.heavy_operations", "topdown-heavy-ops", "Number of slots pipeline retired microcode instructions with >2 uops", "36")
td_event("perf_metrics.branch_mispredicts", "topdown-br-mispredict", "Number of slots frontend was bound by branch mispredictions", "37")
td_event("perf_metrics.memory_bound", "topdown-mem-bound", "Number of slots backend was bound by memory", "38")
td_event("perf_metrics.fetch_latency", "topdown-fetch-lat", "Number of slots frontend was bound by memory fetch latency", "39")
pmu_to_type = {
"cpu_core": ("hybridcore", "Core"),
"cpu_atom": ("hybridcore", "Atom"),
"cpu": ("core", None),
}
def json_with_extra(el, eventmap_is_file, pmu):
typ = pmu_to_type[pmu]
name = event_download.eventlist_name(el, key=typ[0], hybridkey=typ[1])
if not os.path.exists(name):
if pmu == "cpu_core":
name = event_download.eventlist_name(el, "core")
else:
name = event_download.eventlist_name(el, "hybridcore", hybridkey="Core")
emap = EmapNativeJSON(name, pmu)
if not emap or emap.error:
print("parsing", name, "failed", file=sys.stderr)
return None
if experimental:
try:
emap.read_events(event_download.eventlist_name(el, "core experimental"))
except IOError:
pass
add_extra_env(emap, el, eventmap_is_file)
return emap
def add_extra_env(emap, el, eventmap_is_file):
try:
oc = os.getenv("OFFCORE")
if oc:
oc = canon_emapvar(oc, "matrix")
oc = event_download.eventlist_name(el, "offcore")
emap.add_offcore(oc)
elif not eventmap_is_file:
oc = event_download.eventlist_name(el, "offcore")
if os.path.exists(oc) and el != oc:
emap.add_offcore(oc)
if experimental:
oc = event_download.eventlist_name(el, "offcore experimental")
if os.path.exists(oc) and oc != el:
emap.add_offcore(oc)
except IOError:
print("Cannot open offcore", oc, file=sys.stderr)
try:
uc = os.getenv("UNCORE")
if uc:
uc = canon_emapvar(uc, "uncore")
uc = event_download.eventlist_name(uc, "uncore")
emap.add_uncore(uc)
elif not eventmap_is_file:
uc = event_download.eventlist_name(el, "uncore")
if os.path.exists(uc) and uc != el:
emap.add_uncore(uc)
if experimental:
uc = event_download.eventlist_name(el, "uncore experimental")
if os.path.exists(uc) and uc != el:
emap.add_uncore(uc)
except IOError:
print("Cannot open uncore", uc, file=sys.stderr)
def read_map(env, typ, r):
try:
e2 = os.getenv(env)
if e2:
e2 = canon_emapvar(e2, typ)
r(e2)
# don't try to download for now
except IOError:
print("Cannot open " + env, e2, file=sys.stderr)
read_map("EVENTMAP2", "core", emap.read_events)
read_map("EVENTMAP3", "core", emap.read_events)
read_map("UNCORE2", "uncore", emap.add_uncore)
def canon_emapvar(el, typ):
if ("*" in el or "." in el or "_" in el) and "/" not in el and not file_exists(el):
el = "%s/%s" % (event_download.getdir(), el)
if '*' in el:
l = glob.glob(el)
if l:
if len(l) > 1:
l = [x for x in l if x.find(typ) >= 0]
if l:
el = l[0]
return el
def find_emap(eventvar="EVENTMAP", pmu="cpu"):
"""Search and read a perfmon event map.
When the EVENTMAP environment variable is set read that, otherwise
read the map for the current CPU. EVENTMAP can be a CPU specifier
in the map file or a path name.
Dito for the OFFCORE and UNCORE environment variables.
Optionally pass the name of the EVENTMAP variable, and the cpu pmu name.
Return an emap object that contains the events and can be queried
or None if nothing is found or the current CPU is unknown."""
el = os.getenv(eventvar)
if not el:
eventmap_is_file = False
el = event_download.get_cpustr()
else:
eventmap_is_file = "/" in el
el = canon_emapvar(el, "core")
if "/" in el:
try:
emap = EmapNativeJSON(el, pmu)
if not emap or emap.error:
return None
add_extra_env(emap, el, eventmap_is_file)
return emap
except IOError:
return None
try:
if not force_download:
return json_with_extra(el, eventmap_is_file, pmu)
except IOError:
pass
try:
toget = ["core"]
if not os.getenv("OFFCORE"):
toget.append("offcore")
if not os.getenv("UNCORE"):
toget.append("uncore")
if experimental:
toget += [x + " experimental" for x in toget]
event_download.download(el, toget)
return json_with_extra(el, eventmap_is_file, pmu)
except IOError:
pass
return None
def process_events(event, print_only, period, noexplode):
overflow = None
# replace inner commas so we can split events
event = re.sub(r"([a-z][a-z0-9]+/)([^/]+)/",
lambda m: m.group(1) + m.group(2).replace(",", "#") + "/",
event)