~linaro-toolchain-dev/cortex-strings/trunk

« back to all changes in this revision

Viewing changes to scripts/bench.py

  • Committer: Michael Hope
  • Date: 2012-06-12 03:19:48 UTC
  • Revision ID: michael.hope@linaro.org-20120612031948-4ii8jicywtzjprak
Added the C only routines from GLIBC 2.16+20120607~git24a6dbe

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python
 
2
 
 
3
"""Simple harness that benchmarks different variants of the routines,
 
4
caches the results, and emits all of the records at the end.
 
5
 
 
6
Results are generated for different values of:
 
7
 * Source
 
8
 * Routine
 
9
 * Length
 
10
 * Alignment
 
11
"""
 
12
 
 
13
import subprocess
 
14
import math
 
15
import sys
 
16
 
 
17
# Prefix to the executables
 
18
build = '../build/try-'
 
19
 
 
20
DEFAULTS = 'memcpy memset memchr strchr strcmp strcpy strlen'
 
21
 
 
22
HAS = {
 
23
    'this': 'bounce ' + DEFAULTS,
 
24
    'bionic': DEFAULTS,
 
25
    'glibc': DEFAULTS,
 
26
    'newlib': DEFAULTS,
 
27
    'newlib-xscale': DEFAULTS,
 
28
    'plain': 'memset memcpy strcmp strcpy',
 
29
    'csl': 'memcpy memset'
 
30
}
 
31
 
 
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))
 
35
 
 
36
    if key in cache:
 
37
        got = cache[key]
 
38
    else:
 
39
        xbuild = build
 
40
        cmd = '%(xbuild)s%(variant)s -t %(function)s -c %(bytes)s -l %(loops)s -a %(alignment)s' % locals()
 
41
 
 
42
        try:
 
43
            got = subprocess.check_output(cmd.split()).strip()
 
44
        except OSError, ex:
 
45
            assert False, 'Error %s while running %s' % (ex, cmd)
 
46
 
 
47
    parts = got.split(':')
 
48
    took = float(parts[5])
 
49
 
 
50
    cache[key] = got
 
51
 
 
52
    if not quiet:
 
53
        print got
 
54
        sys.stdout.flush()
 
55
 
 
56
    return took
 
57
 
 
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
 
61
    bytes = sorted(bytes)
 
62
    mid = bytes[len(bytes)/2]
 
63
 
 
64
    # Use the ordering in 'this' as the default
 
65
    all_functions = HAS['this'].split()
 
66
 
 
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)
 
72
 
 
73
    for alignment in alignments:
 
74
        for function in all_functions:
 
75
            for variant in variants:
 
76
                if function not in HAS[variant].split():
 
77
                    continue
 
78
 
 
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
 
82
                # do
 
83
                f = 50000000
 
84
                want = 5.0
 
85
 
 
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))
 
90
                f = f * factor
 
91
                
 
92
                # Round f to a few significant figures
 
93
                scale = 10**int(math.log10(f) - 1)
 
94
                f = scale*int(f/scale)
 
95
 
 
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)
 
100
 
 
101
def run_top(cache):
 
102
    variants = sorted(HAS.keys())
 
103
 
 
104
    # Upper limit in bytes to test to
 
105
    top = 512*1024
 
106
    # Test all powers of 2
 
107
    step1 = 2.0
 
108
    # Test intermediate powers of 1.4
 
109
    step2 = 1.4
 
110
 
 
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)))
 
114
    
 
115
    bytes = []
 
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)])
 
118
 
 
119
    alignments = [8, 16, 4, 1, 2, 32]
 
120
 
 
121
    run_many(cache, variants, bytes, alignments)
 
122
 
 
123
def main():
 
124
    cachename = 'cache.txt'
 
125
 
 
126
    cache = {}
 
127
 
 
128
    try:
 
129
        with open(cachename) as f:
 
130
            for line in f:
 
131
                line = line.strip()
 
132
                parts = line.split(':')
 
133
                cache[':'.join(parts[:5])] = line
 
134
    except:
 
135
        pass
 
136
 
 
137
    try:
 
138
        run_top(cache)
 
139
    finally:
 
140
        with open(cachename, 'w') as f:
 
141
            for line in cache.values():
 
142
                print >> f, line
 
143
 
 
144
if __name__ == '__main__':
 
145
    main()