forked from andikleen/pmu-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
event_download.py
executable file
·299 lines (282 loc) · 10.2 KB
/
event_download.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
#!/usr/bin/env python3
# Copyright (c) 2014-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.
#
# Automatic event list downloader
#
# event_download.py download for current cpu
# event_download.py -a download all
# event_download.py cpustr... Download for specific CPU
#
# env:
# CPUINFO=... override /proc/cpuinfo file
# MAPFILE=... override mapfile.csv
# PERFMONDIR=... override download prefix for perfmon data, can be a local clone (file:///tmp/perfmon)
from __future__ import print_function
import sys
import re
try:
from urllib.request import urlopen
from urllib.error import URLError
except ImportError:
from urllib2 import urlopen, URLError
import os
import string
from fnmatch import fnmatch
urlpath = os.environ.get('PERFMONDIR', 'https://raw.githubusercontent.com/intel/perfmon/main')
mapfile = 'mapfile.csv'
modelpath = urlpath "/" mapfile
def get_cpustr():
cpuinfo = os.getenv("CPUINFO")
if cpuinfo is None:
cpuinfo = '/proc/cpuinfo'
f = open(cpuinfo, 'r')
cpu = [None, None, None, None]
for j in f:
n = j.split()
if n[0] == 'vendor_id':
cpu[0] = n[2]
elif n[0] == 'model' and n[1] == ':':
cpu[2] = int(n[2])
elif n[0] == 'cpu' and n[1] == 'family':
cpu[1] = int(n[3])
elif n[0] == 'stepping' and n[1] == ':':
cpu[3] = int(n[2])
if all(v is not None for v in cpu):
break
# stepping for SKX only
stepping = cpu[0] == "GenuineIntel" and cpu[1] == 6 and cpu[2] == 0x55
if stepping:
return "%s-%d-%X-%X" % tuple(cpu)
return "%s-%d-%X" % tuple(cpu)[:3]
def sanitize(s, a):
o = ""
for j in s:
if j in a:
o = j
return o
def getdir():
try:
d = os.getenv("XDG_CACHE_HOME")
xd = d
if not d:
home = os.getenv("HOME")
d = "%s/.cache" % (home)
d = "/pmu-events"
if not os.path.isdir(d):
# try to handle the sudo case
if not xd:
user = os.getenv("SUDO_USER")
if user:
nd = os.path.expanduser("~" user) "/.cache/pmu-events"
if os.path.isdir(nd):
return nd
os.makedirs(d)
return d
except OSError:
raise Exception('Cannot access ' d)
NUM_TRIES = 3
def getfile(url, dirfn, fn):
tries = 0
print("Downloading", url, "to", fn)
while True:
try:
f = urlopen(url)
data = f.read()
except IOError:
tries = 1
if tries >= NUM_TRIES:
raise
print("retrying download")
continue
break
o = open(os.path.join(dirfn, fn), "wb")
o.write(data)
o.close()
f.close()
printed = set()
def warn_once(msg):
if msg not in printed:
print(msg, file=sys.stderr)
printed.add(msg)
def cpu_without_step(match):
if match.count("-") < 3:
return match
n = match.split("-")
return "%s-%s-%s" % tuple(n[:3])
allowed_chars = string.ascii_letters '_-.' string.digits
def parse_map_file(match, key=None, link=True, onlyprint=False,
acceptfile=False, hybridkey=None):
match2 = cpu_without_step(match)
files = []
dirfn = getdir()
try:
mfn = os.getenv("MAPFILE")
if mfn:
mapfn = mfn
acceptfile = True
else:
mapfn = os.path.join(dirfn, mapfile)
if onlyprint and not os.path.exists(mapfn) and not mfn:
print("Download", mapfn, "first for --print")
return []
if acceptfile and os.path.exists(mapfn):
pass
elif not onlyprint and not mfn:
getfile(modelpath, dirfn, mapfile)
models = open(mapfn)
for j in models:
if j.startswith("Family-model"):
continue
n = j.rstrip().split(",")
if len(n) < 4:
if len(n) > 0:
print("Cannot parse", n)
continue
cpu, version, name, typ = n[:4]
if not (fnmatch(cpu, match) or fnmatch(cpu, match2) or
fnmatch(match2, cpu) or fnmatch(match, cpu)):
continue
if key is not None and typ not in key:
continue
if hybridkey and len(n) >= 7 and n[6] != hybridkey:
continue
cpu = sanitize(cpu, allowed_chars)
url = urlpath name
matchfn = match
if matchfn == "*":
matchfn = cpu
if ".json" not in matchfn:
if hybridkey:
fn = "%s-%s-%s.json" % (matchfn, sanitize(typ, allowed_chars), hybridkey)
else:
fn = "%s-%s.json" % (matchfn, sanitize(typ, allowed_chars))
path = os.path.join(dirfn, fn)
if acceptfile and os.path.exists(path):
if onlyprint:
print(path)
continue
else:
try:
os.remove(path)
except OSError:
pass
if onlyprint:
print(path)
continue
if mfn:
print("error accessing", path)
continue
try:
fn = fn.replace("01234", "4")
fn = fn.replace("56789ABCDEF", "5") # XXX
getfile(url, dirfn, fn)
except URLError as e:
print("error accessing %s: %s" % (url, e), file=sys.stderr)
if match == '*':
continue
raise
if link:
lname = re.sub(r'.*/', '', name)
lname = sanitize(lname, allowed_chars)
try:
os.remove(os.path.join(dirfn, lname))
except OSError:
pass
try:
os.symlink(fn, os.path.join(dirfn, lname))
except OSError as e:
print("Cannot link %s to %s:" % (name, lname), e, file=sys.stderr)
files.append(fn)
models.close()
for file_name in ["README.md", "LICENSE"]:
if not onlyprint and not os.path.exists(os.path.join(dirfn, file_name)) and not mfn:
getfile(urlpath "/" file_name, dirfn, file_name)
except URLError as e:
print("Cannot access event server:", e, file=sys.stderr)
warn_once("""
If you need a proxy to access the internet please set it with:
\texport https_proxy=http://proxyname...
If you are not connected to the internet please run this on a connected system:
\tevent_download.py '%s'
and then copy ~/.cache/pmu-events to the system under test
To get events for all possible CPUs use:
\tevent_download.py -a""" % match)
except OSError as e:
print("Cannot write events file:", e, file=sys.stderr)
return files
def download(match, key=None, link=True, onlyprint=False, acceptfile=False):
return len(parse_map_file(match, key, link, onlyprint, acceptfile))
def download_current(link=False, onlyprint=False):
"""Download JSON event list for current cpu.
Returns >0 when a event list is found"""
return download(get_cpustr(), link=link, onlyprint=onlyprint, )
def eventlist_name(name=None, key="core", hybridkey=None):
if not name:
name = get_cpustr()
cache = getdir()
fn = name
if os.path.exists(fn):
return fn
if ".json" not in name:
if hybridkey:
fn = "%s-%s-%s.json" % (name, key, hybridkey)
else:
fn = "%s-%s.json" % (name, key)
if "/" not in fn:
fn = "%s/%s" % (cache, fn)
if not os.path.exists(fn):
files = parse_map_file(name, key, acceptfile=True, hybridkey=hybridkey)
if files:
return files[0]
name = cpu_without_step(name)
if "*" in fn:
fn = "%s/%s" % (cache, name)
elif hybridkey:
fn = "%s/%s-%s-%s.json" % (cache, name, key, hybridkey)
else:
fn = "%s/%s-%s.json" % (cache, name, key)
files = parse_map_file(name, key, acceptfile=True, hybridkey=hybridkey)
if files:
fn = files[0]
return fn
if __name__ == '__main__':
# only import argparse when actually called from command line
# this makes ocperf work on older python versions without it.
import argparse
p = argparse.ArgumentParser(usage='download Intel event files')
p.add_argument('--all', '-a', help='Download all available event files', action='store_true')
p.add_argument('--verbose', '-v', help='Be verbose', action='store_true')
p.add_argument('--mine', help='Print name of current CPU', action='store_true')
p.add_argument('--link', help='Create links with the original event file name', action='store_true', default=True)
p.add_argument('--print', help='Print file names of all event files instead of downloading. Requires existing mapfile.csv.',
dest='print_', action='store_true')
p.add_argument('cpus', help='CPU identifiers to download', nargs='*')
args = p.parse_args()
if args.verbose or args.mine:
print(get_cpustr())
if args.mine:
sys.exit(0)
d = getdir()
if args.all:
found = download('*', link=args.link, onlyprint=args.print_)
elif len(args.cpus) == 0:
found = download_current(link=args.link, onlyprint=args.print_)
else:
found = 0
for j in args.cpus:
found = download(j, link=args.link, onlyprint=args.print_)
if found == 0 and not args.print_:
print("Nothing found", file=sys.stderr)
el = eventlist_name()
if os.path.exists(el) and not args.print_:
print("my event list", el)