3
"""Simple harness that benchmarks different variants of the routines,
4
caches the results, and emits all of the records at the end.
6
Results are generated for different values of:
17
# Prefix to the executables
18
build = '../build/try-'
20
DEFAULTS = 'memcpy memset memchr strchr strcmp strcpy strlen'
23
'this': 'bounce ' + DEFAULTS,
27
'newlib-xscale': DEFAULTS,
28
'plain': 'memset memcpy strcmp strcpy',
29
'csl': 'memcpy memset'
32
def run(cache, variant, function, bytes, loops, alignment=8, quiet=False):
33
"""Perform a single run, exercising the cache as appropriate."""
34
key = ':'.join('%s' % x for x in (variant, function, bytes, loops, alignment))
40
cmd = '%(xbuild)s%(variant)s -t %(function)s -c %(bytes)s -l %(loops)s -a %(alignment)s' % locals()
43
got = subprocess.check_output(cmd.split()).strip()
45
assert False, 'Error %s while running %s' % (ex, cmd)
47
parts = got.split(':')
48
took = float(parts[5])
58
def run_many(cache, variants, bytes, alignments):
59
# We want the data to come out in a useful order. So fix an
60
# alignment and function, and do all sizes for a variant first
62
mid = bytes[len(bytes)/2]
64
# Use the ordering in 'this' as the default
65
all_functions = HAS['this'].split()
67
# Find all other functions
68
for functions in HAS.values():
69
for function in functions.split():
70
if function not in all_functions:
71
all_functions.append(function)
73
for alignment in alignments:
74
for function in all_functions:
75
for variant in variants:
76
if function not in HAS[variant].split():
79
# Run a tracer through and see how long it takes and
80
# adjust the number of loops based on that. Not great
81
# for memchr() and similar which are O(n), but it will
86
loops = int(f / math.sqrt(max(1, mid)))
87
took = run(cache, variant, function, mid, loops, alignment, quiet=True)
88
# Keep it reasonable for silly routines like bounce
89
factor = min(20, max(0.05, want/took))
92
# Round f to a few significant figures
93
scale = 10**int(math.log10(f) - 1)
94
f = scale*int(f/scale)
96
for b in sorted(bytes):
97
# Figure out the number of loops to give a roughly consistent run
98
loops = int(f / math.sqrt(max(1, b)))
99
run(cache, variant, function, b, loops, alignment)
102
variants = sorted(HAS.keys())
104
# Upper limit in bytes to test to
106
# Test all powers of 2
108
# Test intermediate powers of 1.4
111
# Figure out how many steps get us up to the top
112
steps1 = int(round(math.log(top) / math.log(step1)))
113
steps2 = int(round(math.log(top) / math.log(step2)))
116
bytes.extend([int(step1**x) for x in range(0, steps1+1)])
117
bytes.extend([int(step2**x) for x in range(0, steps2+1)])
119
alignments = [8, 16, 4, 1, 2, 32]
121
run_many(cache, variants, bytes, alignments)
124
cachename = 'cache.txt'
129
with open(cachename) as f:
132
parts = line.split(':')
133
cache[':'.join(parts[:5])] = line
140
with open(cachename, 'w') as f:
141
for line in cache.values():
144
if __name__ == '__main__':