-
Notifications
You must be signed in to change notification settings - Fork 345
/
Copy pathucevent.py
executable file
·1137 lines (1032 loc) · 34.6 KB
/
ucevent.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 python
# Copyright (c) 2013, 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.
#
# run perf with uncore events and output data in a vmstat/turbostat like running
# normalized format.
#
# requires a perf driver for the uncore (recent Intel server CPUs) and may
# need some kernel patches (see documentation)
#
# ucevent event .. -- perf arguments
# e.g. ucevent event
# no event => list
# ucevent -h to show other arguments
from __future__ import print_function
import argparse
import sys
import textwrap
import os
import re
import subprocess
import pty
import locale
import copy
import fnmatch
import glob
from collections import defaultdict
import ucexpr
import ucmsg
dbg = ucmsg.debug_msg
cpu_mapping = {
45: "jkt",
62: "ivt",
63: "hsx",
86: "bdxde",
79: "bdx",
85: "skx",
106: "icx",
108: "icx",
}
args = None
per_socket = False
class CPU:
"""Query CPU information."""
def cpumap(self):
if (self.vendor == "GenuineIntel" and
self.family == 6 and
self.model in cpu_mapping):
return cpu_mapping[self.model]
print("Cannot identify CPU model %d" % (self.model))
return None
# assumes that physical ids, nodes are all in the same name space
def __init__(self):
self.socket = dict()
f = open("/proc/cpuinfo", "r")
for l in f:
n = l.split()
if len(n) == 0:
continue
if n[0] == "processor":
cpu = int(n[2])
elif n[0] == "physical" and n[1] == "id":
s = int(n[3])
if s not in self.socket:
self.socket[s] = cpu
elif n[0] == "vendor_id":
self.vendor = n[2]
elif n[0] == "cpu" and n[1] == "family":
self.family = int(n[3])
elif n[0] == "model" and n[1] == ":":
self.model = int(n[2])
f.close()
def max_node(self):
return max(self.socket)
def socket_to_cpu(self, s):
if s in self.socket:
return self.socket[s]
sys.exit("Invalid socket %d" % s)
cpu = CPU()
cputype = os.getenv("FORCECPU")
if not cputype:
cputype = cpu.cpumap()
if not cputype:
sys.exit(1)
if cputype == "skx":
import skx_uc as uc
import skx_extra as extra
elif cputype == "bdx":
import bdx_uc as uc
import bdx_extra as extra
elif cputype == "bdxde":
import bdxde_uc as uc
import bdxde_extra as extra
elif cputype == "hsx":
import hsx_uc as uc
import hsx_extra as extra
elif cputype == "ivt":
import ivt_uc as uc
import ivt_extra as extra
elif cputype == "jkt":
import jkt_uc as uc
import jkt_extra as extra
elif cputype == "icx":
import icx_uc as uc
import icx_extra as extra
else:
sys.exit("Unknown CPU " + cputype)
import ucaux
cpu_aux = ucaux.Aux()
def lookup_event(name):
if name in uc.events:
return uc.events[name]
if name in uc.derived:
return uc.derived[name]
return None
def print_list(f, ls, c, description, equation, ehdr):
count = 0
if description:
wrap = textwrap.TextWrapper(initial_indent=" ",
subsequent_indent=" ")
for i in sorted(ls.keys()):
ev = ls[i]
if ev["Category"] == c:
count += 1
desc = "?"
if "Desc" in ev:
desc = ev["Desc"]
derived = ""
if "Broken" in ev and not args.broken:
continue
if "Equation" in ev:
if args.unsupported:
derived = " (Derived)"
elif "Obscure" in ev:
continue
elif not args.unsupported:
continue
if args.name_only:
print(i, file=f)
continue
ehdr.out()
print(" %-30s %-40s" % (i, desc + derived), file=f)
if description:
defn = ""
if "Defn" in ev:
defn = ev["Defn"]
if "Notes" in ev:
defn += " Notes: " + ev["Notes"]
if "MaxIncCyc" in ev and ev["MaxIncCyc"] > 1:
defn += (" May increase upto %d units per cycle." %
(ev["MaxIncCyc"]))
print(wrap.fill(defn), file=f)
if "Equation" in ev:
eql, equations = convert_equation(ev, dict(), True, True)
q = set()
for eq in eql:
if eq not in ['[',']']:
q |= get_qualifiers(perf_box(eq))
else:
q = get_qualifiers(format_box(ev))
if q:
print(wrap.fill("Qualifiers: " + " ".join(q)), file=f)
if "Filter" in ev:
print(wrap.fill("Filter: " + show_filter(ev["Filter"])), file=f)
if equation:
if "Equation" in ev:
print(" Equation: ", ev["Equation"], file=f)
return count
def show_one_filter(f):
if f in cpu_aux.qual_display_alias:
return cpu_aux.qual_display_alias[f]
q, v = ucexpr.convert_qual(f, "0")
return q
def show_filter(f):
return " ".join(map(show_one_filter, f.split(",")))
qual_cache = dict()
def get_qualifiers(box):
if not box:
return set()
obox = box
if box in qual_cache:
return qual_cache[box]
if not box_exists(box):
box += "_0"
if not box_exists(box):
return set()
d = os.listdir("/sys/devices/uncore_%s/format" % (box))
q = set()
for j in d:
if j not in ["event", "umask"]:
q.add(j)
qual_cache[obox] = q
return q
def expand_acronyms(n):
for j in cpu_aux.acronyms:
n = n.replace(j, j + " (" + cpu_aux.acronyms[j] + ")")
return n
class EventsHeader:
def __init__(self, name, f):
self.printed = False
self.name = name
self.f = f
def out(self):
if not self.printed and not args.name_only:
n = expand_acronyms(self.name)
print(file=self.f)
print(n, file=self.f)
self.printed = True
def cmp_cat(a, b):
# XXX move interesting ones first
al = a.lower()
bl = b.lower()
return (al > bl) - (bl < al)
def get_pager():
if args.no_pager:
return sys.stdout, None
f = sys.stdout
if f.isatty():
try:
if sys.version_info.major == 3:
sp = subprocess.Popen(["less", "-F"], stdin=subprocess.PIPE,
universal_newlines=True)
else:
sp = subprocess.Popen(["less", "-F"], stdin=subprocess.PIPE)
return sp.stdin, sp
except OSError:
f = sys.stdout
return f, None
def print_events(cat, desc, equation):
f, proc = get_pager()
ecount = 0
dcount = 0
if (args.unsupported or args.broken) and not args.name_only:
print("\nNot all of these events have been tested and they may be broken", file=f)
print("USE AT YOUR OWN RISK!", file=f)
for c in sorted(uc.categories, key=lambda x: x.lower()):
if cat and expand_acronyms(c).lower().find(cat.lower()) < 0:
continue
ehdr = EventsHeader(c, f)
ecount += print_list(f, uc.events, c, desc, equation, ehdr)
dcount += print_list(f, uc.derived, c, desc, equation, ehdr)
if proc:
f.close()
proc.wait()
def format_equation(ev, qual, quiet=False):
e = ucexpr.parse(ev["Equation"], ev["Box"], quiet or args.quiet, "UserEq" in ev, qual)
return e
def format_umask(u):
u = u[1:] # drop b
u = u.replace("x", "0")
return int(u, 2)
box_dir_cache = []
def find_boxes(prefix):
if prefix.startswith("uncore_"):
prefix = prefix[6:]
global box_dir_cache
if len(box_dir_cache) == 0:
box_dir_cache += [x for x in os.listdir("/sys/devices/") if x.startswith("uncore")]
l = [x.replace("uncore_", "") for x in box_dir_cache if x.startswith("uncore_" + prefix)]
return sorted(l)
box_cache = dict()
def box_exists(box):
n = "/sys/devices/uncore_%s" % (box)
if n not in box_cache:
box_cache[n] = os.path.exists(n)
return box_cache[n]
def format_event(ev):
if "Equation" in ev:
return format_equation(ev, None)
return format_reg_event(ev, dict())
# format an event's attributes
def format_attrs(ev, box):
evsel = ev["EvSel"]
if "Umask" in ev and ev["Umask"] and box == "pcu":
evsel |= format_umask(ev["Umask"])
if "ExtSel" in ev and ev["ExtSel"] != "":
evsel |= (ev["ExtSel"] << 21)
attrs = "event=%#x" % (evsel)
if "Umask" in ev and ev["Umask"] and box != "pcu":
attrs += ",umask=%#x" % (format_umask(ev["Umask"]))
if box == "pcu":
if "SubCtr" in ev and int(ev["SubCtr"]) > 0:
attrs += ",occ_sel=%d" % (ev["SubCtr"])
return attrs
box_to_perf = {
"cbo": "cbox",
"qpi_ll": "qpi",
"upi_ll": "upi",
"sbo": "sbox",
}
def format_box(ev):
box = ev["Box"].lower()
if box in box_to_perf:
box = box_to_perf[box]
return box
# format a single event for perf
def format_reg_event(ev, qual):
box = format_box(ev)
attrs = format_attrs(ev, box)
if qual:
attrs += "," + qual
if not box_exists(box):
ret = ["uncore_%s/%s/" % (j, attrs) for j in find_boxes(box)]
if ret:
return ret
return ["uncore_%s/%s/" % (box, attrs)]
HEADER_INTERVAL = 50
def is_pct(s):
return s.find("_PCT") >= 0 or s.find("PCT_") >= 0
nonecount = 0
def get_box(s):
m = re.match(r"([^.]+)\.(.*)", s)
if m:
return re.sub(r"[0-9]+$", "", m.group(1)), m.group(2)
global nonecount
nonecount += 1
return "%d" % (nonecount)
def sum_event(a, b):
if args.no_sum or a.find("iMC") >= 0 or a.find("QPI_LL") >= 0:
return False
return get_box(a) == get_box(b)
def is_str(x):
return isinstance(x, str)
def scale_val(val):
if args.scale and not is_str(val):
val = val / units[args.scale]
return val
PCT_FIELDLEN = 7
OVER_THRESHOLD = 3
units = { "GB": 1024**3, "MB": 1024**2, "KB": 1024 }
class Output:
"""Output data in human readable columns. This also merges boxes
and implements adaptive column widths."""
def __init__(self, fieldlen=14, adaptive=False):
self.headers = []
self.vals = []
self.num_output = 0
locale.setlocale(locale.LC_ALL, '')
self.FIELDLEN = fieldlen
self.over = 0
self.columns = dict()
self.adaptive = adaptive
self.timestamp = None
def out(self, name, r, timestamp):
if self.vals and sum_event(self.headers[-1], name):
if is_str(self.vals[-1]) or is_str(r):
self.vals[-1] = str(self.vals[-1]) + str(r)
else:
self.vals[-1] += r
self.headers[-1] = self.headers[-1].replace("0.", ".", 1)
else:
self.headers.append(name)
self.vals.append(r)
self.timestamp = timestamp
def fieldlen(self, hdr):
if self.columns and hdr in self.columns:
return self.columns[hdr]
elif is_pct(hdr):
return PCT_FIELDLEN
else:
return self.FIELDLEN
def update_column(self, hdr, l):
if not self.adaptive:
return
if hdr in self.columns:
old = self.columns[hdr]
if l > old:
self.over += 1
if self.over >= OVER_THRESHOLD:
self.columns[hdr] = l
self.num_output = -1 # force redisplay
else:
self.columns[hdr] = l
def format_field(self, j, h, fieldlen):
if isinstance(j, float):
if is_pct(h):
j *= 100.0
fmt = "%.2f"
j = scale_val(j)
elif is_str(j):
fmt = "%s"
else:
fmt = "%d"
j = scale_val(j)
num = locale.format_string(fmt, j, grouping=True)
if len(num) >= fieldlen:
num += " "
return num
def print_header(self):
pre = ""
for v, j in zip(self.vals, self.headers):
l = self.fieldlen(j)
l = max(len(self.format_field(v, j, l)), l)
print(pre + j, file=args.output)
pre += "|" + " "*(l - 1)
self.columns[j] = l
def flush(self):
if (self.num_output % HEADER_INTERVAL) == 0:
self.print_header()
self.over = 0
out = ""
for j, h in zip(self.vals, self.headers):
fieldlen = self.fieldlen(h)
num = self.format_field(j, h, fieldlen)
out += "%-*s" % (fieldlen, num)
self.update_column(h, len(num))
print(out, file=args.output)
self.vals = []
self.headers = []
self.num_output += 1
class OutputCSV(Output):
"""CSV version of Output."""
def __init__(self, csv):
Output.__init__(self)
self.csv = csv
def flush(self):
if self.num_output == 0:
print(self.csv.join(["timestamp"] + self.headers), file=args.output)
scaled_vals = map(scale_val, [self.timestamp] + self.vals)
print(self.csv.join(map(str, scaled_vals)), file=args.output)
self.vals = []
self.headers = []
self.num_output += 1
groupings = ('[', ']', '{', '}', '[[', ']]')
out = None
class PerfRun:
"""Control a perf process"""
def __init__(self):
self.perf = None
# for testing purposes
def mock(self, logfile, evl):
f = open(logfile, "w")
for t in range(0, 5):
num = 10000 + t
for i in evl:
if i in groupings:
continue
i = i.replace("{","").replace("}","")
o = "%s,%s" % (num, i)
to = "%d," % (t)
print(to + "S0,1,"+o, file=f)
print(to + "S1,1,"+o, file=f)
num += 10000
f.close()
def execute(self, s, logfile, evl):
if not args.quiet:
l = list(map(lambda x: "'" + x + "'" if x.find("{") >= 0 else x, s))
i = l.index('--log-fd')
del l[i:i+2]
print(" ".join(l), file=args.output)
if args.mock:
self.mock(logfile, evl)
self.perf = None
else:
if sys.version_info.major == 3 and sys.version_info.minor >= 2:
# close_fds mysteriously doesn't work anymore with python 3.7
self.perf = subprocess.Popen(s, pass_fds=(int(s[s.index('--log-fd')+1]),))
else:
self.perf = subprocess.Popen(s, close_fds=False)
def perf_box(x):
m = re.match(r"uncore_([^/]+)(_\d+)?/", x)
if not m:
return None
return m.group(1)
def available_counters(box):
if box in cpu_aux.limited_counters:
return cpu_aux.limited_counters[box]
return cpu_aux.DEFAULT_COUNTERS
def count_filter(ev):
return sum(map(lambda x: ev.count("," + x), cpu_aux.filters))
def is_fixed(x):
return x.find("/clockticks/") >= 0
# determine if equation can run in a group
def supports_group(evl, nl):
evl = filter(lambda x: not is_fixed(x), evl)
boxes = defaultdict(list)
bnames = defaultdict(list)
for j, n in zip(evl, nl):
box = perf_box(j)
if not box:
continue
boxes[box].append(j)
bnames[box].append(n)
for box in boxes:
# some events have too complicated counter constraints for
# this pear brain scheduler to decide if groups work or not. Just do
# not do groups for them.
for n in bnames[box]:
ev = lookup_event(n)
if complicated_counters(ev):
return False
evl = boxes[box]
filters = sum(map(count_filter, evl))
if len(evl) > available_counters(box) or filters > 1:
return False
return True
def count_box(box):
return len(find_boxes(box))
# run a equation
def evaluate(eq, EV):
SAMPLE_INTERVAL = float(args.interval)*1000000 # noqa F841
ROUND = lambda x: round(x) # noqa F841
KB = 1024 # noqa F841
MB = 1024*KB # noqa F841
GB = 1024*MB # noqa F841
KILO = 1000 # noqa F841
MEGA = 1000*KILO # noqa F841
GIGA = 1000*MEGA # noqa F841
NUM_R3QPI = count_box("r3qpi") # noqa F841 XXX add generic function
dbg("evaluate", eq)
try:
return eval(eq)
except SyntaxError:
return "#EVAL"
except NameError:
return "#EVAL"
except ZeroDivisionError:
return 0.0
def is_error(x):
return is_str(x) and x.startswith("#")
# read perf output and output results
def gen_res(evl, res, evp, equations, evnames, timestamp):
dbg("evl", evl)
dbg("res", res)
dbg("evp", evp)
dbg("equations", equations)
cur_eq = None
eql = equations
for j in evl:
if j == '[' or j == '[[':
cur_eq = eql[0]
eq_events = dict()
elif j == ']' or j == ']]':
r = None
for x in eq_events:
if is_error(eq_events[x]):
r = eq_events[x]
break
if r is None:
if '/' in equations[0]:
EV = lambda x, n: float(eq_events[x])
else:
EV = lambda x, n: int(eq_events[x])
r = evaluate(equations[0], EV)
dbg("result", r)
out.out(evnames[0], r, timestamp)
equations = equations[1:]
cur_eq = None
evnames = evnames[1:]
elif cur_eq:
assert evp[0] == j
eq_events[evp[0]] = res[0]
res = res[1:]
evp = evp[1:]
elif j in ('{', '}'):
continue
else:
assert evp[0] == j
if re.match(r"[0-9]+", res[0]):
r = int(res[0])
else:
r = res[0]
out.out(evnames[0], r, timestamp)
evnames = evnames[1:]
res = res[1:]
evp = evp[1:]
out.flush()
assert len(res) == 0
assert len(evp) == 0
# replace internal [] equation groups with perf format
def gen_events(evl):
e = ""
prev = ""
for j in evl:
if j == '[' or j == ']':
continue
if j == '[[':
j = '{'
elif j == ']]':
j = '}'
sep = ""
if prev:
match = [prev in groupings, j in groupings]
if match == [True, True] or match == [False, False]:
sep = ","
if prev in ['[', '{'] and match[1] is False:
sep = ""
if prev in [']', '}']:
sep = ","
if match[0] is False and j in ['[', '{']:
sep = ","
e += sep + j
prev = j
return e
def concat(d):
x = []
for j in sorted(d.keys()):
x += d[j]
return x
def gennames(names, sockets):
x = []
for s in sorted(sockets):
if s != "":
s += "-"
for n in names:
x.append(s + n)
return x
def check_per_socket(s, warned):
if (not warned and not args.mock and
sorted([int(x[1:]) for x in s]) != list(range(0, len(s)))):
ucmsg.warning("perf --per-socket appears broken. Please update perf.")
ucmsg.warning("Data on socket > 0 will be likely incorrect.")
return True
return warned
perf_errors = {
"<not supported>": "#NS",
"<not counted>": "#NC",
}
# run perf and output results
def measure(evl, argl, equations, evnames):
warned = False
all_events = gen_events(evl)
# use a pty because perf doesn't do enough fflush() otherwise
outp, inp = pty.openpty()
logfile = "ulog.%d" % (os.getpid())
run = PerfRun()
run.execute([perf, "stat", "--log-fd", "%d" % (inp), "-e", all_events] + argl, logfile, evl)
prev_timestamp = None
evp = defaultdict(list)
res = defaultdict(list)
socket = ""
try:
if args.mock:
f = open(logfile, 'r')
else:
f = os.fdopen(outp, 'r')
os.close(inp)
while True:
try:
# force line-by-line buffering
l = f.readline()
if not l:
break
except (KeyboardInterrupt, IOError):
break
l = l.strip()
dbg("perf", l)
if l.startswith('#') or l == "":
continue
if l.startswith("S"): # bogus summary lines. ignore
continue
if per_socket:
ts, socket, _, rest = l.split(",", 3)
l = ts + "," + rest
# uncore// contains commas!
m = re.match(r"([0-9.]+),([0-9]+|<.*>),?,(.*)$", l)
if not m:
print("PERF-UNREADABLE", l, end=" ")
continue
timestamp = m.group(1)
if timestamp != prev_timestamp:
if per_socket and not args.quiet:
warned = check_per_socket(res.keys(), warned)
if evp:
num = len(res)
gen_res(evl*num, concat(res), concat(evp),
equations*num, gennames(evnames, res.keys()), timestamp)
res = defaultdict(list)
evp = defaultdict(list)
prev_timestamp = timestamp
r = m.group(2)
if r.startswith("<"):
if r in perf_errors:
r = perf_errors[r]
else:
r = "#NA"
res[socket].append(r)
p = m.group(3)
if re.search(r"/u?,", p):
p = re.sub(r"/u?,.*", "", p) + "/"
if p.startswith(","):
p = p[1:]
evp[socket].append(p)
f.close()
if args.mock:
os.remove(logfile)
except IOError:
# ptys always seem to end with EIO
#print("Error talking to perf", e)
pass
if evp:
num = len(res)
gen_res(evl*num, concat(res), concat(evp), equations*num,
gennames(evnames, res.keys()), timestamp)
if run.perf:
run.perf.wait()
dummy_count = 1000
def ev_append(ovl, x, nl, n):
if x not in ovl:
ovl.append(x)
if n not in nl:
nl.append(n)
global dummy_count
dummy_count += 1
return dummy_count # dummy value to avoid division by zero
class WarnOnce:
def __init__(self):
self.warned = False
def warn(self, msg):
if not self.warned:
ucmsg.warning(msg)
self.warned = True
def add_group(x, nl, in_group, mw):
if len(x) == 0:
return x
if len(x) > 1 and not in_group:
if supports_group(x, nl):
return ['[['] + x + [']]']
mw.warn("Equation will multiplex and may produce inaccurate results (see manual)")
return ['['] + x + [']']
# convert a equation to perf form
def convert_equation(ev, qual, in_group, quiet=False):
mw = WarnOnce()
nnl = []
evl = []
equations = []
eql = format_equation(ev, qual, quiet)
for p in eql:
ovl = []
nl = []
# run equation to collect events
r = evaluate(p, lambda x, n: ev_append(ovl, x, nl, n))
if is_error(r) and not args.quiet:
print("Cannot evaluate equation", ev["Equation"], file=sys.stderr)
nnl.append(nl)
evl.append(ovl)
equations.append(p)
flat_eql = []
for j, nl in zip(evl, nnl):
flat_eql += add_group(j, nl, in_group, mw)
return flat_eql, equations
standard_events = ("cycles", "ref-cycles", "instructions")
# convert a single event to perf form
def convert_one(evn, evl, evnames, equations, qual, in_group):
ev = lookup_event(evn)
if not ev:
print("unknown event", evn, file=sys.stderr)
sys.exit(1)
if "Equation" in ev:
nvl, neql = convert_equation(ev, qual, in_group)
equations += neql
num = len(neql)
else:
nvl = format_reg_event(ev, qual)
# should run this in a group XXX
num = len(nvl)
evl += nvl
# add index numbers to names
if num > 1:
evnames += map(lambda x, y: x.replace(".", "%d." % (y),1),
[evn] * num, range(0, num))
else:
evnames.append(evn)
return evl, evnames, equations
# equation on command line
def user_equation(evn, in_group):
# XXX no qualifiers for now, as the the , conflicts with "with:"
ev = dict()
ev["Equation"] = evn
ev["Box"] = ""
ev["UserEq"] = True
return convert_equation(ev, dict(), in_group)
# convert command line events to perf form
def convert_events(arg_events):
print_events = []
equations = []
evnames = []
evl = []
j = 0
in_group = 0
for evn in arg_events:
if evn == "--":
j += 1
break
if evn in standard_events:
evnames.append(evn)
evl.append(evn)
j += 1
continue
if evn in ['{', '}']:
if evn == '{':
in_group += 1
else:
in_group -= 1
evl += evn
j += 1
continue
if evn.count(".") == 0:
break
print_events.append(evn)
# not checking for * here, as it conflicts with wildcards
if re.search(r"[ /()+-]", evn):
nvl, neql = user_equation(evn, in_group)
equations += neql
evl += nvl
evnames += [evn] * len(nvl)
j += 1
continue
qual = ""
n = evn.split(",", 1)
if len(n) > 1:
evn = n[0]
qual = n[1]
if re.search(r"[\[*?]", evn):
for me in sorted(list(uc.events.keys()) + list(uc.derived.keys())):
if fnmatch.fnmatch(me, evn):
evl, evnames, equations = convert_one(me, evl, evnames,
equations, qual, in_group)
else:
evl, evnames, equations = convert_one(evn, evl, evnames, equations,
qual, in_group)
j += 1
if not args.quiet:
print("Events:", " ".join(print_events), file=args.output)
return evl, equations, evnames, args.events[j:]
def fix_field(nev, key, old, num):
nev[key] = nev[key].replace(old, old.replace("x", str(num)))
# expand a single event
def expand_ev(table, name, num):
ev = table[name]
for n in range(0, num):
nev = copy.deepcopy(ev)
fix_field(nev, "Equation", ",x", n)
fix_field(nev, "Equation", "=x", n)
fix_field(nev, "Equation", "RANKx", n)
fix_field(nev, "Equation", "NODEx", n)
fix_field(nev, "Desc", "Rank x", n)
fix_field(nev, "Desc", "Node x", n)
fix_field(nev, "Defn", "(x)", n)
table[name.replace("x", str(n))] = nev
del table[name]
def maybe_expand_ev(table, name, max_node):
if name.find("NODEx") >= 0:
expand_ev(table, name, max_node)
elif name.find("RANKx") >= 0:
expand_ev(table, name, cpu_aux.MAX_RANK)
# XXX should do this at generation time
def expand_events():
max_node = cpu.max_node() + 1
for j in list(uc.derived.keys()):
maybe_expand_ev(uc.derived, j, max_node)
for j in list(uc.events.keys()):
maybe_expand_ev(uc.events, j, max_node)
for j in extra.extra_derived:
uc.derived[j] = extra.extra_derived[j]
def get_counter(c):
m = re.match(r"(\d+)-(\d+)", c)
if m:
start = int(m.group(1))
return start, int(m.group(2))
return int(c), int(c)
# index=0 start of ranges, index=1 end of ranges
def counter_list(counters, index):
return map(lambda x: get_counter(x)[index], str(counters).split(","))
# check for complicated counters our simple scheduler cannot handle
def complicated_counters(ev):
counters = ev["Counters"]
return (max(counter_list(counters, 1)) < available_counters(format_box(ev)) - 1 or
max(counter_list(counters, 0)) > 0)
def check_events():
ae = dict()
for j in sorted(uc.events.keys()):
ev = uc.events[j]
box = j[:j.index(".")]
if "EvSel" not in ev:
print(j,"has no evsel")
umask = ""
extsel = 0
if "Umask" in ev:
umask = ev['Umask']
if "ExtSel" in ev:
extsel = ev['ExtSel']
if extsel == "":
extsel = 0
key = box, ev['EvSel'], extsel, umask
if key in ae:
print(ae[key],"duplicated with",j,key)
else:
ae[key] = j
if complicated_counters(ev):
print("event %s has complicated counters: %s" % (j, ev["Counters"]))
def check_multiplex():
if args.quiet:
return
found = False
for j in glob.iglob("/sys/devices/uncore_*/perf_event_mux_interval_ms"):
found = True
break