-
Notifications
You must be signed in to change notification settings - Fork 1
/
spec-html-report.py
executable file
·201 lines (171 loc) · 8.35 KB
/
spec-html-report.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
#!/usr/bin/env python3
import argparse
import datetime
import os
import re
import subprocess
import sys
from html import escape
perf_record_prefix = 'perf record --call-graph dwarf'
perf_record_regex = re.compile(r'\s+([^\s]*)%\s+(?P<percent>[^\s]*)%\s*(?P<samples>[\d]+)'
r'\s*([^\s]*)\s*(?P<shobj>[^\s]*)\s*\[.\]\s(?P<function>.*)')
perf_annotate_regex = re.compile(r'\s+(?P<percent>[0-9]+\.[0-9]+)\s+:.*')
context_size = 15
threshold_percent = 1
perf_annotate_threshold = 0.3
output_folder = 'html'
int_benchmarks = ['500.perlbench_r', '502.gcc_r', '505.mcf_r', '520.omnetpp_r', '523.xalancbmk_r',
'525.x264_r', '531.deepsjeng_r', '541.leela_r', '548.exchange2_r', '557.xz_r']
fp_benchmarks = ['503.bwaves_r', '507.cactuBSSN_r', '508.namd_r', '510.parest_r', '511.povray_r', '519.lbm_r',
'521.wrf_r', '526.blender_r', '527.cam4_r', '538.imagick_r', '544.nab_r', '549.fotonik3d_r',
'554.roms_r']
HTML_HEADER = """
<html><head>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css"
rel="stylesheet" integrity="sha384-BmbxuPwQa2lc/FVzBcNJ7UAyJxM6wuqIj61tLrc4wSX0szH/Ev+nYRRuWlolflfl"
crossorigin="anonymous">
<title>%s</title>
</head>
<body>
<main>
<div class="container">
<h2>%s</h2>
"""
HTML_FOOTER = """
</main></div><footer><div class="container">%s</div></footer></body></html>
"""
parser = argparse.ArgumentParser(description='SPEC perf analysis HTML report generator')
parser.add_argument('machine', help='Machine name')
parser.add_argument('compiler', help='Compiler name')
parser.add_argument('options', help='Compiler options')
parser.add_argument('spec_config', help='SPEC configuration file')
parser.add_argument('--benchmarks', help='List of benchmarks to run (all by default)')
args = parser.parse_args()
spec_script = f'runcpu --config={args.spec_config} --size=ref --iterations=1 --no-reportable --tune=peak'
benchmarks = int_benchmarks + fp_benchmarks
if args.benchmarks:
benchmarks = args.benchmarks.split(',')
def skip_binary(binary):
return binary.startswith('spec') or binary in ('runcpu', 'sh')
def parse_spec_report(lines):
functions = {}
for line in lines:
m = perf_record_regex.match(line)
if m:
percent = float(m.group('percent'))
samples = int(m.group('samples'))
shobj = m.group('shobj')
if not skip_binary(shobj) and percent >= threshold_percent:
functions[m.group('function')] = (percent, samples)
return functions
def decode_perf_annotate(data):
try:
return data.decode('utf8')
except Exception:
# Some Fortran SPEC benchmarks have latin1 encoding
return data.decode('latin1')
def demangle(function):
return subprocess.check_output(f'c++filt "{function}"', shell=True, encoding='utf8').strip()
def filter_perf_script():
output = []
data = subprocess.check_output('perf script', shell=True, encoding='utf8')
records = data.strip().split('\n\n')
for record in records:
binary = record.split('\n')[0].split(' ')[0]
if not skip_binary(binary):
output.append(record)
return '\n\n'.join(output).encode()
def write_hot_perf_annotate_hunks(data, data_nocolor):
color_lines = data.split('\n')
nocolor_lines = data_nocolor.split('\n')
assert len(color_lines) == len(nocolor_lines)
# add header to interesting spots
lines_to_output = set(range(30))
# investigate interesting spots
for i, line in enumerate(nocolor_lines):
m = perf_annotate_regex.match(line)
if m and float(m.group('percent')) >= perf_annotate_threshold:
for x in range(-context_size, context_size):
lines_to_output.add(i + x)
# output interesting hunks
output = []
last_line = -1
for i, line in enumerate(color_lines):
if i in lines_to_output:
if last_line + 1 != i:
output.append(f'... ({i - last_line - 1} lines skipped) ...')
output.append(line)
last_line = i
# transform colored output to HTML
return subprocess.check_output('aha --no-header', input='\n'.join(output), shell=True, encoding='utf8')
def check_dependencies():
# check ulimit -s
r = subprocess.check_output('ulimit -s', shell=True, encoding='utf8')
if 'unlimited' not in r:
print("Please set 'ulimit -s unlimited'")
sys.exit(1)
# check perf features
lines = subprocess.check_output('perf version --build-options', shell=True, encoding='utf8').splitlines()[1:]
features = set()
for line in lines:
parts = line.split(':')
feature = parts[0].strip()
if 'on' in parts[1]:
features.add(feature)
diff = {'dwarf', 'libelf', 'libunwind'} - features
if diff:
print(f'Missing perf features: {diff}')
sys.exit(2)
check_dependencies()
os.chdir(os.path.expanduser('~/Programming/cpu2017'))
if not os.path.exists(output_folder):
os.mkdir(output_folder)
for i, benchmark in enumerate(benchmarks):
title = f'{benchmark} - {args.machine} - {args.compiler} {args.options}'
print(f'== {i + 1}/{len(benchmarks)}: {title} ==')
subprocess.check_output(f'source ./shrc && {spec_script} --action build -D {benchmark}', shell=True)
print(' ... build done')
cmd = f'source ./shrc && perf stat -- {spec_script} --action run {benchmark}'
r = subprocess.run(cmd, shell=True, encoding='utf8', stderr=subprocess.PIPE, stdout=subprocess.DEVNULL)
assert r.returncode == 0
stats = r.stderr.strip()
print(' ... perf stat done')
cmd = f'source ./shrc && perf record -F150 -o perf.data --call-graph dwarf {spec_script} --action run {benchmark}'
subprocess.check_output(cmd, shell=True, stderr=subprocess.DEVNULL)
r = subprocess.check_output('perf report --no-demangle --stdio -g none --show-nr-samples',
shell=True, encoding='utf8')
print(' ... perf report done')
report = parse_spec_report(r.splitlines())
filename = f'{benchmark}-{args.machine}-{args.compiler}-{args.options}'
filename = filename.replace(' ', '_').replace('-', '_')
flamegraph = os.path.join(output_folder, f'{filename}.svg')
perf_script_output = filter_perf_script()
cmd = f'~/Programming/FlameGraph/stackcollapse-perf.pl --context > tmp.txt ' \
f'&& ~/Programming/FlameGraph/flamegraph.pl --title {benchmark} --minwidth 3 tmp.txt > {flamegraph}'
subprocess.check_output(cmd, input=perf_script_output, shell=True)
with open(os.path.join(output_folder, filename + '.html'), 'w+') as f:
f.write(HTML_HEADER % (title, title))
f.write('<h3>Flame graph</h3>')
f.write(f'<object class="p" data="{filename}.svg" type="image/svg+xml">'
f'<img src="{filename}.svg" \\></object>')
f.write('<h3>Perf stat</h3>')
f.write(f'<pre style="font-size: 8pt;">{stats}</pre>')
f.write('<h3>Perf annotate</h3>')
f.write('<table class="table"><thead><th>Function</th><th class="text-end">Samples</th>'
'<th class="text-end">Percentage</th></thead><tbody>')
for mangled_function, (percentage, samples) in sorted(report.items(), key=lambda x: x[1], reverse=True):
function = demangle(mangled_function)
f.write(f'<tr><td><a href="#{mangled_function}">{escape(function)}</a></td>'
f'<td class="text-end">{samples}</td><td class="text-end">{percentage:.2f} %</td></tr>')
f.write('<tbody></table>')
for mangled_function, (percentage, samples) in sorted(report.items(), key=lambda x: x[1], reverse=True):
function = demangle(mangled_function)
f.write(f'<h5 id="{mangled_function}">{percentage:.2f}% ({samples} samples) - {escape(function)}</h5>')
cmd = f'perf --buildid-dir none annotate --no-demangle --symbol={mangled_function} ' \
f'--stdio --stdio-color=always -l'
data = decode_perf_annotate(subprocess.check_output(cmd, shell=True))
data_nocolor = decode_perf_annotate(subprocess.check_output(cmd + ' --stdio-color=never', shell=True))
f.write('<pre style="font-size: 8pt;">')
f.write(write_hot_perf_annotate_hunks(data, data_nocolor))
f.write('</pre>')
f.write(HTML_FOOTER % f'Generated {datetime.datetime.now()}')