-
Notifications
You must be signed in to change notification settings - Fork 105
/
embench_core.py
291 lines (219 loc) · 8.54 KB
/
embench_core.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
#!/usr/bin/env python3
# Common python procedures for use across Embench.
# 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
"""
Embench common procedures.
This version is suitable when using a version of GDB which can launch a GDB
server to use as a target.
"""
import logging
import math
import os
import re
import sys
import time
from enum import Enum
# What we export
__all__ = [
'check_python_version',
'log',
'gp',
'output_format',
'setup_logging',
'log_args',
'log_benchmarks',
'embench_stats',
'arglist_to_str',
]
# Handle for the logger
log = logging.getLogger()
# All the global parameters
gp = dict()
# Different formats of output that can be generated by the benchmarking scripts
class output_format(Enum):
JSON = 1
TEXT = 2
BASELINE = 3
# Make sure we have new enough python
def check_python_version(major, minor):
"""Check the python version is at least {major}.{minor}."""
if ((sys.version_info[0] < major)
or ((sys.version_info[0] == major) and (sys.version_info[1] < minor))):
log.error('ERROR: Requires Python {mjr}.{mnr} or later'.format(mjr=major, mnr=minor))
sys.exit(1)
def create_logdir(logdir):
"""Create the log directory, which can be relative to the root directory
or absolute"""
if not os.path.isabs(logdir):
logdir = os.path.join(gp['rootdir'], logdir)
if not os.path.isdir(logdir):
try:
os.makedirs(logdir)
except PermissionError:
raise PermissionError('Unable to create log directory {dir}'.format(dir=logdir))
if not os.access(logdir, os.W_OK):
raise PermissionError('Unable to write to log directory {dir}'.format(dir=logdir))
return logdir
def setup_logging(logdir, prefix):
"""Set up logging in the directory specified by "logdir".
The log file name is the "prefix" argument followed by a timestamp.
Debug messages only go to file, everything else also goes to the
console."""
# Create the log directory first if necessary.
logdir_abs = create_logdir(logdir)
logfile = os.path.join(
logdir_abs, time.strftime('{pref}-%Y-%m-%d-%H%M%S.log'.format(pref=prefix))
)
# Set up logging
log.setLevel(logging.DEBUG)
cons_h = logging.StreamHandler(sys.stdout)
cons_h.setLevel(logging.INFO)
log.addHandler(cons_h)
file_h = logging.FileHandler(logfile)
file_h.setLevel(logging.DEBUG)
log.addHandler(file_h)
# Log where the log file is
log.debug('Log file: {log}\n'.format(log=logfile))
log.debug('')
def log_args(args):
"""Record all the argument values"""
log.debug('Supplied arguments')
log.debug('==================')
for arg in vars(args):
realarg = re.sub('_', '-', arg)
val = getattr(args, arg)
log.debug('--{arg:20}: {val}'.format(arg=realarg, val=val))
log.debug('')
def find_benchmarks():
"""Enumerate all the benchmarks in alphabetical order. The benchmarks are
found in the 'src' subdirectory of the root directory. Set up global
parameters for the source and build benchmark directories.
Return the list of benchmarks."""
gp['benchdir'] = os.path.join(gp['rootdir'], 'src')
gp['bd_benchdir'] = os.path.join(gp['bd'], 'src')
dirlist = os.listdir(gp['benchdir'])
benchmarks = []
for bench in dirlist:
abs_b = os.path.join(gp['benchdir'], bench)
if os.path.isdir(abs_b):
benchmarks.append(bench)
benchmarks.sort()
return benchmarks
def log_benchmarks(benchmarks):
"""Record all the benchmarks in the log"""
log.debug('Benchmarks')
log.debug('==========')
for bench in benchmarks:
log.debug(bench)
log.debug('')
def compute_geomean(benchmarks, raw_data, rel_data):
"""Compute the geometric mean and count the number of data points for the
supplied benchmarks, raw and optionally relative data. Return a
list of geometric mean and count of data, with a."""
geomean = 1.0
count = 0.0
for bench in benchmarks:
if gp['absolute']:
# Want absolute results. Ignore zero values
if bench in raw_data:
if raw_data[bench] > 0:
count += 1
geomean *= raw_data[bench]
else:
# Want relative results (the default). Ignore zero value
if bench in rel_data:
if rel_data[bench] > 0:
count += 1
geomean *= rel_data[bench]
if count > 0.0:
geomean = pow(geomean, 1.0 / count)
return geomean, count
def compute_geosd(benchmarks, raw_data, rel_data, geomean, count):
"""Compute geometric standard deviation for the given set of benchmarks,
using the supplied raw and optinally relative data. This draws on the
previously computed geometric mean and count for each benchmark.
Return geometric standard deviation."""
lnsize = 0.0
geosd = 0.0
for bench in benchmarks:
if gp['absolute']:
# Want absolute results
if raw_data[bench] > 0.0:
lnsize += math.pow(math.log(raw_data[bench] / geomean), 2)
else:
# Want relative results (the default).
if rel_data[bench] > 0.0:
lnsize += math.pow(math.log(rel_data[bench] / geomean), 2)
# Compute the standard deviation using the lnsize data for each benchmark.
if count > 0.0:
geosd = math.exp(math.sqrt(lnsize / count))
return geosd
def compute_georange(geomean, geosd, count):
"""Compute the geometric range of one geometric standard deviation around
the geometric mean. Return the geometric range."""
georange = 0.0
if count > 0:
if geosd > 0.0:
georange = geomean * geosd - geomean / geosd
else:
georange = 0.0
return georange
def output_stats(geomean, geosd, georange, count, bm_type, opt_comma):
"""Output the statistical summary.
Note that we manually generate the JSON output, rather than using the
dumps method, because the result will be manually edited, and we want
to guarantee the layout."""
geomean_op = ''
geosd_op = ''
georange_op = ''
if count > 0:
if gp['absolute']:
if gp['output_format'] == output_format.JSON:
geomean_op = '{gm}'.format(gm=round(geomean))
geosd_op = '{gs:.2f}'.format(gs=geosd)
elif gp['output_format'] == output_format.TEXT:
geomean_op = '{gm:8,}'.format(gm=round(geomean))
geosd_op = ' {gs:6.2f}'.format(gs=geosd)
georange_op = '{gr:8,}'.format(gr=georange)
else:
if gp['output_format'] == output_format.JSON:
geomean_op = '{gm:.2f}'.format(gm=geomean)
geosd_op = '{gs:.2f}'.format(gs=geosd)
elif gp['output_format'] == output_format.TEXT:
geomean_op = ' {gm:6.2f}'.format(gm=geomean)
geosd_op = ' {gs:6.2f}'.format(gs=geosd)
georange_op = ' {gr:6.2f}'.format(gr=georange)
else:
geomean_op = ' - '
geosd_op = ' - '
georange_op = ' - '
# Output the results
if gp['output_format'] == output_format.JSON:
log.info(' "{bm} geometric mean" : {gmo},'.format(bm=bm_type, gmo=geomean_op))
log.info(' "{bm} geometric standard deviation" : {gso}'.format(bm=bm_type, gso=geosd_op))
log.info(' }' + '{oc}'.format(oc=opt_comma))
elif gp['output_format'] == output_format.TEXT:
log.info('--------- -----')
log.info('Geometric mean {gmo:8}'.format(gmo=geomean_op))
log.info('Geometric SD {gso:8}'.format(gso=geosd_op))
log.info('Geometric range {gro:8}'.format(gro=georange_op))
def embench_stats(benchmarks, raw_data, rel_data, bm_type, opt_comma):
"""Output statistics summary for Embench."""
geomean, count = compute_geomean(benchmarks, raw_data, rel_data)
geosd = compute_geosd(benchmarks, raw_data, rel_data, geomean, count)
georange = compute_georange(geomean, geosd, count)
output_stats(geomean, geosd, georange, count, bm_type, opt_comma)
def arglist_to_str(arglist):
"""Make arglist into a string"""
for arg in arglist:
if arg == arglist[0]:
str = arg
else:
str = str + ' ' + arg
return str