This repository has been archived by the owner on Mar 18, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbenchmark_size.py
executable file
·282 lines (232 loc) · 8.23 KB
/
benchmark_size.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
#!/usr/bin/env python3
# Script to benchmark size
# Copyright (C) 2017, 2019 Embecosm Limited
#
# Contributor: Graham Markall <[email protected]>
# Contributor: Jeremy Bennett <[email protected]>
#
# This file is part of Embench.
# SPDX-License-Identifier: GPL-3.0-or-later
"""
Compute the size benchmark for a set of compiled Embench programs.
"""
import argparse
import codecs
import os
import sys
from json import loads
from elftools.elf.elffile import ELFFile
sys.path.append(
os.path.join(os.path.abspath(os.path.dirname(__file__)), 'pylib')
)
from embench_core import check_python_version
from embench_core import log
from embench_core import gp
from embench_core import setup_logging
from embench_core import log_args
from embench_core import find_benchmarks
from embench_core import log_benchmarks
from embench_core import embench_stats
def build_parser():
"""Build a parser for all the arguments"""
parser = argparse.ArgumentParser(description='Compute the size benchmark')
parser.add_argument(
'--builddir',
type=str,
default='bd',
help='Directory holding all the binaries',
)
parser.add_argument(
'--logdir',
type=str,
default='logs',
help='Directory in which to store logs',
)
parser.add_argument(
'--absolute',
action='store_true',
help='Specify to show absolute results',
)
parser.add_argument(
'--relative',
dest='absolute',
action='store_false',
help='Specify to show relative results (the default)',
)
# List arguments are empty by default, a user specified value then takes
# precedence. If the list is empty after parsing, then we can install a
# default value.
parser.add_argument(
'--text',
type=str,
default=[],
action='append',
help='Section name(s) containing code'
)
parser.add_argument(
'--data',
type=str,
default=[],
action='append',
help='Section name(s) containing non-zero initialized writable data'
)
parser.add_argument(
'--rodata',
type=str,
default=[],
action='append',
help='Section name(s) containing read only data'
)
parser.add_argument(
'--bss',
type=str,
default=[],
action='append',
help='Section name(s) containing zero initialized writable data'
)
parser.add_argument(
'--vectors',
type=str,
default=[],
action='append',
help='Section name(s) containing interrupt vectors(text)'
)
parser.add_argument(
'--metric',
type=str,
default=[],
action='append',
choices=['text', 'rodata', 'data', 'bss', 'vectors'],
help='Sections to include in metric: one or more of "text", "rodata", '
+ '"data" or "bss". Default "text"',
)
return parser
def validate_args(args):
"""Check that supplied args are all valid. By definition logging is
working when we get here.
Update the gp dictionary with all the useful info"""
if os.path.isabs(args.builddir):
gp['bd'] = args.builddir
else:
gp['bd'] = os.path.join(gp['rootdir'], args.builddir)
if not os.path.isdir(gp['bd']):
log.error(f'ERROR: build directory {gp["bd"]} not found: exiting')
sys.exit(1)
if not os.access(gp['bd'], os.R_OK):
log.error(f'ERROR: Unable to read build directory {gp["bd"]}: exiting')
sys.exit(1)
gp['absolute'] = args.absolute
# Sort out the list of section names to use
gp['secnames'] = dict()
for argname in ['text', 'rodata', 'data', 'bss', 'vectors']:
secnames = getattr(args, argname)
if secnames:
gp['secnames'][argname] = secnames
else:
gp['secnames'][argname] = ['.' + argname]
# If no sections are specified, we just use .text
if args.metric:
gp['metric'] = args.metric
else:
gp['metric'] = ['text']
def get_section(elf, section_name):
"""Workaround to use get_section_by_name on pyelftools both pre- and post-
version 0.24 - section names are always decoded in 0.24 onwards"""
section = elf.get_section_by_name(section_name)
if section:
return section
encoded_name = codecs.encode(section_name, 'utf-8')
return elf.get_section_by_name(encoded_name)
def benchmark_size(bench):
"""Compute the total size of the desired sections in a benchmark. Returns
the size in bytes, which may be zero if the section wasn't found."""
appexe = os.path.join(gp['bd_benchdir'], bench, bench)
sec_size = 0
with open(appexe, 'rb') as fileh:
elf = ELFFile(fileh)
for metric in gp['metric']:
for secname in gp['secnames'][metric]:
sec = get_section(elf, secname)
if sec:
sec_size += sec['sh_size']
# Return the section size
return sec_size
def collect_data(benchmarks):
"""Collect and log all the raw and optionally relative data associated with
the list of benchmarks supplied in the "benchmarks" argument. Return
the raw data and relative data as a list. The raw data may be empty if
there is a failure. The relative data will be empty if only absolute
results have been requested."""
# Baseline data is held external to the script. Import it here.
gp['baseline_dir'] = os.path.join(
gp['rootdir'], 'baseline-data', 'size.json')
with open(gp['baseline_dir']) as fileh:
baseline_all = loads(fileh.read())
# Compute the baseline data we need
baseline = {}
for bench, data in baseline_all.items():
baseline[bench] = 0
for sec in gp['metric']:
if sec in data:
baseline[bench] += data[sec]
# Collect data and output it
successful = True
raw_data = {}
rel_data = {}
log.info('Benchmark size')
log.info('--------- ----')
for bench in benchmarks:
raw_data[bench] = benchmark_size(bench)
rel_data[bench] = {}
output = {}
# Zero is a valid section size, although empty .text should be a
# concern.
if gp['absolute']:
# Want absolute results. Only include non-zero values
output = f'{raw_data[bench]:8,}'
else:
# Want relative results (the default). If baseline is zero, just
# use 0.0 as the value.
if baseline[bench] > 0:
rel_data[bench] = raw_data[bench] / baseline[bench]
else:
rel_data[bench] = 0.0
output = f' {rel_data[bench]:6.2f}'
log.info(f'{bench:15} {output:8}')
if successful:
return raw_data, rel_data
# Otherwise failure return
return [], []
def main():
"""Main program driving measurement of benchmark size"""
# Establish the root directory of the repository, since we know this file is
# in that directory.
gp['rootdir'] = os.path.abspath(os.path.dirname(__file__))
# Parse arguments using standard technology
parser = build_parser()
args = parser.parse_args()
# Establish logging
setup_logging(args.logdir, 'size')
log_args(args)
# Check args are OK (have to have logging and build directory set up first)
validate_args(args)
# Find the benchmarks
benchmarks = find_benchmarks()
log_benchmarks(benchmarks)
# Collect the size data for the benchmarks
raw_data, rel_data = collect_data(benchmarks)
# We can't compute geometric SD on the fly, so we need to collect all the
# data and then process it in two passes. We could do the first processing
# as we collect the data, but it is clearer to do the three things
# separately. Given the size of datasets with which we are concerned the
# compute overhead is not significant.
if raw_data:
embench_stats(benchmarks, raw_data, rel_data)
log.info('All benchmarks sized successfully')
else:
log.info('ERROR: Failed to compute size benchmarks')
sys.exit(1)
# Make sure we have new enough Python and only run if this is the main package
check_python_version(3, 6)
if __name__ == '__main__':
sys.exit(main())