forked from kiliakis/pyprof
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpapiprof.py
350 lines (298 loc) · 13.6 KB
/
papiprof.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
import numpy as np
from functools import wraps
import ctypes as ct
import time
import os
import inspect
libpapi = ct.CDLL(os.path.dirname(__file__) + '/src/lib/libmypapi.so')
# Metrics as postfix expressions
preset_metrics = {
'IPC': ['INSTRUCTIONS_RETIRED', 'CPU_CLK_UNHALTED', '/'],
'CPI': ['CPU_CLK_UNHALTED', 'INSTRUCTIONS_RETIRED', '/'],
'L2_MISS_RATE': ['PAPI_L2_TCM', 'PAPI_L2_TCA', '/', 100., '*'],
'L3_MISS_RATE': ['PAPI_L3_TCM', 'PAPI_L3_TCA', '/', 100., '*'],
'LLC_MISS_RATE': ['perf::CACHE-MISSES', 'perf::CACHE-REFERENCES', '/', 100., '*'],
# 'BRANCH_MISS_RATE': ['perf::BRANCH-MISSES', 'perf::BRANCHES', '/', 100., '*'],
'BRANCH_MSP_RATE': ['PAPI_BR_MSP', 'PAPI_BR_CN', '/', 100., '*'],
'FRONT_BOUND%': ['IDQ_UOPS_NOT_DELIVERED:CORE',
4., 'CPU_CLK_UNHALTED', '*', '/', 100., '*'],
'BAD_SPECULATION%': ['UOPS_ISSUED:ANY', 'UOPS_RETIRED:RETIRE_SLOTS', '-',
4., 'INT_MISC:RECOVERY_CYCLES', '*', '+',
4., 'CPU_CLK_UNHALTED', '*', '/', 100., '*'],
'RETIRING%': ['UOPS_RETIRED:RETIRE_SLOTS',
4., 'CPU_CLK_UNHALTED', '*', '/', 100., '*'],
'BACK_BOUND%': [1.,
'FRONTEND_BOUND', 'RETIRING', '+',
'BAD_SPECULATION', '+', '-', 100., '*'],
'BACK_BOUND_AT_EXE%': ['CYCLE_ACTIVITY:CYCLES_NO_EXECUTE',
'UOPS_EXECUTED:CYCLES_GE_1_UOP_EXEC', '+',
'UOPS_EXECUTED:CYCLES_GE_2_UOPS_EXEC', '-',
'CPU_CLK_UNHALTED', '/', 100., '*'],
'MEMORY_BOUND%': ['CYCLE_ACTIVITY:STALLS_LDM_PENDING', 'CPU_CLK_UNHALTED',
'/', 100., '*'],
'L2_COST%': [12., 'MEM_LOAD_UOPS_RETIRED:L2_HIT', '*',
'CPU_CLK_UNHALTED', '/', 100., '*'],
'L3_COST%': [26., 'MEM_LOAD_UOPS_RETIRED:L3_HIT', '*',
'CPU_CLK_UNHALTED', '/', 100., '*'],
'MEMORY_COST%': [200., 'MEM_LOAD_UOPS_LLC_HIT_RETIRED:XNSP_HIT', '*',
'CPU_CLK_UNHALTED', '/', 100., '*'],
'L1_BOUND%': ['CYCLE_ACTIVITY:STALLS_LDM_PENDING',
'CYCLE_ACTIVITY:STALLS_L1D_PENDING', '-',
'CPU_CLK_UNHALTED', '/', 100., '*'],
'L2_BOUND%': ['CYCLE_ACTIVITY:STALLS_L1D_PENDING',
'CYCLE_ACTIVITY:STALLS_L2_PENDING', '-',
'CPU_CLK_UNHALTED', '/', 100., '*'],
'L3_BOUND%': ['CYCLE_ACTIVITY:STALLS_L2_PENDING', 'L3_Hit_fraction', '*',
'CPU_CLK_UNHALTED', '/', 100., '*'],
'L3_HIT_FRACTION': ['MEM_LOAD_UOPS_RETIRED:L3_HIT',
'MEM_LOAD_UOPS_RETIRED:L3_HIT',
'MEM_L3_WEIGHT', 'MEM_LOAD_UOPS_RETIRED:L3_MISS', '*',
'+',
'/'],
# MEM_L3_WEIGHT: External memory to L3 cache latency ratio, 7 can be used
# for 3rd generation intel
'MEM_L3_WEIGHT': [7.],
'L3_MISS_FRACTION': ['MEM_LOAD_UOPS_RETIRED:L3_MISS',
'MEM_LOAD_UOPS_RETIRED:L3_MISS',
'MEM_LOAD_UOPS_RETIRED:L3_HIT',
'+',
'/'],
'MEM_BOUND%': ['CYCLE_ACTIVITY:STALLS_L2_PENDING', 'L3_MISS_FRACTION', '*',
'CPU_CLK_UNHALTED', '/', 100., '*'],
'UNCORE_BOUND%': ['CYCLE_ACTIVITY:STALLS_L2_PENDING', 'CPU_CLK_UNHALTED',
'/', 100., '*'],
'CORE_BOUND%': ['BACK_BOUND_AT_EXE', 'MEM_BOUND', '-', 100., '*'],
'RESOURCE_STALLS_COST%': ['RESOURCE_STALLS:ALL', 'CPU_CLK_UNHALTED', '/'],
'LOCK_CONTENTION%': ['MEM_LOAD_UOPS_L3_HIT_RETIRED:XSNP_HITM',
'MEM_UOPS_RETIRED:LOCK_LOADS', '/', 100., '*'],
'BR_MISP_COST%': [20., 'BR_MISP_RETIRED:ALL_BRANCHES', '*',
'CPU_CLK_UNHALTED', '/', 100., '*']
}
class PAPIProf(object):
"""docstring for PAPIProf"""
def __init__(self, metrics=[], events=[]):
self.events = []
self.counters = {}
self.eventSet = np.zeros(1, dtype=ct.c_int)
self.metrics = []
self.ts = 0
self.papi_init(self.eventSet)
if len(metrics):
self.add_metrics(metrics)
if len(events):
self.add_events(events)
def add_events(self, event_list):
new_events = list(set(event_list) - set(self.events))
self.papi_add_events(self.eventSet, new_events)
self.events += new_events
def list_events(self):
string = '\n' + ''.join(['=']*80) + \
'\n List of Tracked Events\n' + ''.join(['=']*80)
for e in self.events:
string += '\n' + e
string += '\n' +''.join(['=']*80) + \
'\n End\n' + ''.join(['=']*80)
print(string)
def list_metrics(self):
string = '\n' + ''.join(['=']*80) + \
'\n List of Tracked Metrics\n' + ''.join(['=']*80)
for e in self.metrics:
string += '\n' + e
string += '\n' + ''.join(['=']*80) + \
'\n End\n' + ''.join(['=']*80)
print(string)
def list_avail_metrics(self):
string = '\n' + ''.join(['=']*80) + \
'\n List of Available Metrics\n' + ''.join(['=']*80)
for e in preset_metrics.keys():
string += '\n' + e
string += '\n' + ''.join(['=']*80) + \
'\n End\n' + ''.join(['=']*80)
print(string)
def start_counters(self):
self.ts = time.time()
parent = inspect.stack()[1]
self.linestart = str(parent.lineno)
if(len(self.events) > 0):
self.papi_start(self.eventSet)
def stop_counters(self):
eventValues = np.zeros(len(self.events), dtype=ct.c_longlong)
if(len(self.events) > 0):
self.papi_stop(self.eventSet, eventValues)
te = time.time()
parent = inspect.stack()[1]
key = parent.filename+':'+self.linestart+'-'+str(parent.lineno)
if(key not in self.counters):
self.counters[key] = {}
self.counters[key]['time(ms)'] = []
self.counters[key]['time(ms)'].append((te - self.ts) * 1000)
for e, c in zip(self.events, eventValues):
if e not in self.counters[key]:
self.counters[key][e] = []
self.counters[key][e].append(c)
def clear_counters(self):
pass
def add_metrics(self, metrics, helper=False):
new_metrics = list(set(metrics) - set(self.metrics))
# self.metrics = np.unique(self.metrics + metrics)
events = []
for m in new_metrics:
equation = preset_metrics[m]
for symbol in equation:
if symbol in preset_metrics:
self.add_metrics([symbol], helper=True)
elif isinstance(symbol, str) and \
(symbol not in ['/', '+', '-', '*']):
events.append(symbol)
if not helper:
self.metrics = self.metrics + new_metrics
events = list(set(events) - set(self.events))
if len(events):
self.events += events
self.papi_add_events(self.eventSet, events)
def decorate(self, filename='', classname='', metrics=[], events=[]):
if len(metrics):
self.add_metrics(metrics)
if len(events):
self.add_events(events)
def decorator(func):
@wraps(func)
def wrapper(*args, **kw):
eventValues = np.zeros(len(self.events), dtype=ct.c_longlong)
ts = time.time()
if(len(self.events) > 0):
self.papi_start(self.eventSet)
result = func(*args, **kw)
if(len(self.events) > 0):
self.papi_stop(self.eventSet, eventValues)
te = time.time()
key = func.__name__
if classname:
key = classname + '.' + key
if filename:
key = filename + '.' + key
if(key not in self.counters):
self.counters[key] = {}
self.counters[key]['time(ms)'] = []
self.counters[key]['time(ms)'].append((te - ts) * 1000)
for e, c in zip(self.events, eventValues):
if e not in self.counters[key]:
self.counters[key][e] = []
self.counters[key][e].append(c)
return result
return wrapper
return decorator
def report_metrics(self):
def calculate(equation, counters):
import operator
ops = {
'+': operator.add,
'-': operator.sub,
'*': operator.mul,
'/': operator.truediv
}
stack = []
# for symbol in equation:
while len(equation) != 0:
# print(equation)
# print(stack)
symbol = equation.pop(0)
if isinstance(symbol, float) or isinstance(symbol, int):
stack.append(float(symbol))
elif (symbol in ops):
operand2 = stack.pop()
operand1 = stack.pop()
stack.append(ops[symbol](operand1, operand2))
elif (isinstance(symbol, (str)) or isinstance(symbol, bytes)):
if(symbol in counters):
stack.append(np.sum(counters[symbol]))
elif(symbol in preset_metrics):
equation = preset_metrics[symbol] + equation
# result = calculate(preset_metrics[symbol], counters)
# print('The result was: ', result)
# stack.append(result)
else:
print('Symbol %s is needed but missing' % str(symbol))
return float('nan')
return stack.pop()
string = '\n' + ''.join(['=']*80) + \
'\n Metrics Report Begin\n' + ''.join(['=']*80)
print(string)
print('function\tmetric\taverage_value\tstd(%)\tcalls')
for func, counters in sorted(self.counters.items()):
for metric_name in sorted(self.metrics):
equation = preset_metrics[metric_name]
result = calculate(equation.copy(), counters)
print('%s\t%s\t%.3f\t%s\t%s' %
(func, metric_name, result, 'na', 'na'))
string = ''.join(['=']*80) + \
'\n Metrics Report End\n' + ''.join(['=']*80)
print(string+'\n')
def report_counters(self):
string = '\n' + ''.join(['=']*80) + \
'\n Counters Report Begin\n' + ''.join(['=']*80)
print(string)
print('function\tcounter\taverage_value\tstd(%)\tcalls')
for func, counters in sorted(self.counters.items()):
for counter, v in counters.items():
if 'time' in str(counter):
continue
format_str = '%s\t%s\t%d\t%.2f\t%d'
format_values = (func, counter, np.sum(v),
100. * len(v) * np.std(v) / np.sum(v), len(v))
print(format_str % format_values)
string = ''.join(['=']*80) + \
'\n Counters Report End\n' + ''.join(['=']*80)
print(string+'\n')
def report_timing(self):
string = '\n' + ''.join(['=']*80) + \
'\n Timing Report Begin\n' + ''.join(['=']*80)
print(string)
print('function\tcounter\taverage_value\tstd(%)\tcalls')
for func, counters in sorted(self.counters.items()):
for counter, v in counters.items():
if 'time' in str(counter):
format_str = '%s\t%s\t%.3f\t%.2f\t%d'
format_values = (func, counter, np.mean(v),
100.0*np.std(v)/np.mean(v), len(v))
print(format_str % format_values)
string = ''.join(['=']*80) + \
'\n Timing Report End\n' + ''.join(['=']*80)
print(string+'\n')
def papi_init(self, eventSet):
libpapi.init.restype = ct.c_int
libpapi.init.argtypes = [ct.POINTER(ct.c_int)]
libpapi.init(eventSet.ctypes.data_as(ct.POINTER(ct.c_int)))
def papi_add_events(self, eventSet, eventNames):
libpapi.add_events.restype = ct.c_int
libpapi.add_events.argtypes = [ct.c_int,
ct.POINTER(ct.c_char_p),
ct.c_int]
N = len(eventNames)
strArray = (ct.c_char_p * N)()
strArray[:] = [x.encode() for x in eventNames]
libpapi.add_events(eventSet[0], strArray, N)
def papi_start(self, eventSet):
libpapi.start.restype = ct.c_int
libpapi.start.argtypes = [ct.c_int]
libpapi.start(eventSet[0])
def papi_reset(self, eventSet):
libpapi.reset.restype = ct.c_int
libpapi.reset.argtypes = [ct.c_int]
libpapi.reset(eventSet[0])
def papi_stop(self, eventSet, eventValues):
libpapi.stop.restype = ct.c_int
libpapi.stop.argtypes = [ct.c_int,
ct.POINTER(ct.c_longlong)]
libpapi.stop(eventSet[0],
eventValues.ctypes.data_as(ct.POINTER(ct.c_longlong)))
def papi_read(self, eventSet, eventValues):
libpapi.read.restype = ct.c_int
libpapi.read.argtypes = [ct.c_int,
ct.POINTER(ct.c_longlong)]
libpapi.read(eventSet[0],
eventValues.ctypes.data_as(ct.POINTER(ct.c_longlong)))
def papi_destroy(self, eventSet):
libpapi.destroy.restype = ct.c_int
libpapi.destroy.argtypes = [ct.POINTER(ct.c_int)]
libpapi.destroy(eventSet.ctypes.data_as(ct.POINTER(ct.c_int)))