summaryrefslogtreecommitdiff
path: root/bin/generate-dfa-benchmark.py
blob: 95fbbb3f191a1103bef421151bef93bdbde0889c (plain)
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
#!/usr/bin/env python3
"""
Generate a driver/library benchmark based on DFA/PTA traces.

Usage:
PYTHONPATH=lib bin/generate-dfa-benchmark.py [options] <pta/dfa definition> [output.cc]

generate-dfa-benchmarks reads in a DFA definition and generates runs
(i.e., all words accepted by the DFA up to a configurable length). Each symbol
corresponds to a function call. If arguments are specified in the DFA
definition, each symbol corresponds to a function call with a specific set of
arguments (so all argument combinations are present in the generated runs).

Options:
--depth=<depth> (default: 3)
    Maximum number of function calls per run

--instance=<name>
    Override the name of the class instance used for benchmarking

--sleep=<ms> (default: 0)
    How long to sleep between function calls.
"""

import getopt
import json
import re
import runner
import sys
import time
import io
import yaml
from automata import PTA
from harness import OnboardTimerHarness

opt = {}

if __name__ == '__main__':

    try:
        optspec = (
            'arch= '
            'app= '
            'depth= '
            'instance= '
            'run= '
            'sleep= '
            'timer-pin= '
        )
        raw_opts, args = getopt.getopt(sys.argv[1:], "", optspec.split(' '))

        for option, parameter in raw_opts:
            optname = re.sub(r'^--', '', option)
            opt[optname] = parameter

        if 'depth' in opt:
            opt['depth'] = int(opt['depth'])
        else:
            opt['depth'] = 3

        if 'sleep' in opt:
            opt['sleep'] = int(opt['sleep'])

    except getopt.GetoptError as err:
        print(err)
        sys.exit(2)

    modelfile = args[0]

    with open(modelfile, 'r') as f:
        if '.json' in modelfile:
            pta = PTA.from_json(json.load(f))
        else:
            pta = PTA.from_yaml(yaml.safe_load(f))

    if 'timer-pin' in opt:
        timer_pin = opt['timer-pin']
    else:
        timer_pin = 'GPIO::p1_0'

    harness = OnboardTimerHarness(timer_pin)

    outbuf = io.StringIO()

    outbuf.write('#include "arch.h"\n')
    if 'includes' in pta.codegen:
        for include in pta.codegen['includes']:
            outbuf.write('#include "{}"\n'.format(include))
    outbuf.write(harness.global_code())

    outbuf.write('void loop(void)\n')
    outbuf.write('{\n')

    outbuf.write(harness.start_benchmark())

    class_prefix = ''
    if 'instance' in opt:
        class_prefix = '{}.'.format(opt['instance'])
    elif 'instance' in pta.codegen:
        class_prefix = '{}.'.format(pta.codegen['instance'])

    num_transitions = 0
    for run in pta.dfs(opt['depth'], with_arguments = True, with_parameters = True):
        outbuf.write(harness.start_run())
        harness.start_trace()
        param = pta.get_initial_param_dict()
        for transition, arguments, parameter in run:
            num_transitions += 1
            harness.append_state(transition.origin.name, param)
            harness.append_transition(transition.name, param)
            outbuf.write('// {} -> {}\n'.format(transition.origin.name, transition.destination.name))
            if transition.is_interrupt:
                outbuf.write('// wait for {} interrupt\n'.format(transition.name))
                transition_code = '// TODO add startTransition / stopTransition calls to interrupt routine'
            else:
                transition_code = '{}{}({});'.format(class_prefix, transition.name, ', '.join(map(str, arguments)))
            outbuf.write(harness.pass_transition(pta.get_transition_id(transition), transition_code, transition = transition, parameter = parameter))

            param = parameter

            if 'sleep' in opt:
                outbuf.write('arch.delay_ms({:d});\n'.format(opt['sleep']))

        outbuf.write(harness.stop_run())
        outbuf.write('\n')

    outbuf.write(harness.stop_benchmark())
    print(harness.traces)
    outbuf.write('}\n')
    outbuf.write('int main(void)\n')
    outbuf.write('{\n')
    for driver in ('arch', 'gpio', 'kout'):
        outbuf.write('{}.setup();\n'.format(driver))
    if 'setup' in pta.codegen:
        for call in pta.codegen['setup']:
            outbuf.write(call)
    outbuf.write('arch.idle_loop();\n')
    outbuf.write('return 0;\n')
    outbuf.write('}\n')

    if len(args) > 1:
        with open(args[1], 'w') as f:
            f.write(outbuf.getvalue())
    else:
        print(outbuf.getvalue())

    if 'run' in opt:
        if 'sleep' in opt:
            run_timeout = num_transitions * opt['sleep'] / 1000
        else:
            run_timeout = num_transitions * 10 / 1000
        monitor = runner.get_monitor(opt['arch'], callback = harness.parser_cb)
        runner.build(opt['arch'], opt['app'], opt['run'].split())
        runner.flash(opt['arch'], opt['app'], opt['run'].split())
        if opt['arch'] != 'posix':
            try:
                slept = 0
                while True:
                    time.sleep(5)
                    slept += 5
                    if slept < run_timeout:
                        print('[MON] approx. {:.0f}% done'.format(slept * 100 / run_timeout))
            except KeyboardInterrupt:
                pass
            lines = monitor.get_lines()
            monitor.close()
        else:
            print('[MON] Will run benchmark for {:.0f} seconds'.format(2 * run_timeout))
            lines = monitor.run(int(2 * run_timeout))
        print(lines)

    sys.exit(0)