forked from sjp38/masim
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmasim.py
More file actions
executable file
·128 lines (116 loc) · 4.65 KB
/
masim.py
File metadata and controls
executable file
·128 lines (116 loc) · 4.65 KB
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
#!/usr/bin/env python3
import argparse
import subprocess
import masim_config
def parse_bytes(text):
suffix_unit = {
'b': 1,
'k': 1024,
'm': 1024**2,
'g': 1024**3,
't': 1024**4,
}
suffix = text[-1].lower()
if suffix in suffix_unit:
numbers = text[:-1]
else:
numbers = text
try:
numbers = int(numbers)
except Exception as e:
return None, 'number parsing fail (%s)' % e
if suffix in suffix_unit:
numbers = numbers * suffix_unit[suffix]
return numbers, None
def build_regions_phases(args):
regions = []
for name, sz_bytes, data_file in args.region:
sz_bytes, err = parse_bytes(sz_bytes)
if err is not None:
print('wrong region size (%s, %s)' % (sz_bytes, err))
exit(1)
regions.append(masim_config.Region(name, sz_bytes, data_file))
patterns_for_phase = {}
for access_pattern in args.access_pattern:
phase_name, region_name, randomness, stride, probability, rwmode = \
access_pattern
try:
randomness = int(randomness)
except Exception as e:
print('wrong randomness (%s, %s)' % (randomness, e))
exit(1)
if not randomness in [0, 1]:
print('randomness not in [0, 1]: %s' % randomness)
exit(1)
stride, err = parse_bytes(stride)
if err is not None:
print('wrong stride (%s, %s)' % (stride, err))
exit(1)
try:
probability = int(probability)
except Exception as e:
print('wrong pribability (%s, %s)' % (probability, e))
exit(1)
if not rwmode in ['wo', 'ro', 'rw']:
print('wrong rwmode (%s)' % rwmode)
exit(1)
if not phase_name in patterns_for_phase:
patterns_for_phase[phase_name] = []
patterns_for_phase[phase_name].append(
masim_config.AccessPattern(
region_name, randomness, stride, probability, rwmode))
phases = []
for name, runtime_ms in args.phase:
try:
runtime_ms = int(runtime_ms)
except Exception as e:
print('wrong phase runtime (%s, %s)' % (runtime_ms, e))
exit(1)
phases.append(masim_config.Phase(
name, runtime_ms, patterns_for_phase[name]))
return regions, phases
def main():
parser = argparse.ArgumentParser()
parser.add_argument('action', choices=['pr_config', 'run'],
help='what to do')
parser.add_argument(
'--region', nargs=3, action='append',
metavar=('<name>', '<sz_bytes>', '<initial data file>'),
help='memory region of the access pattern')
parser.add_argument('--phase', nargs=2, action='append',
metavar=('<name>', '<runtime_ms>'),
help='access pattern execution phase')
parser.add_argument(
'--access_pattern', nargs=6, action='append',
metavar=('<phase name>', '<region name>', '<randomness>',
'<stride>', '<access_probability>', '<rw_mode>'),
help='per-phase per-region access pattern')
parser.add_argument('--masim_bin', metavar='<file>', default='./masim',
help='path to masim executable file')
parser.add_argument('--config_file', metavar='<file>',
default='masim_py_runconfig',
help='"masim.py run" saves config file here')
parser.add_argument('--log_interval', metavar='<milliseconds>', type=int,
help='periodic access speed logging interval')
parser.add_argument('--accesses_per_region_selection', type=int,
help='number of accesses to make per region selection')
args = parser.parse_args()
if args.region is None or args.phase is None or \
args.access_pattern is None:
print('config is not given')
exit(1)
regions, phases = build_regions_phases(args)
if args.action == 'pr_config':
masim_config.pr_config(regions, phases)
elif args.action == 'run':
with open(args.config_file, 'w') as f:
f.write(masim_config.fmt_config(regions, phases) + '\n')
cmd = [args.masim_bin, args.config_file]
if args.log_interval is not None:
cmd += ['--log_interval=%d' % args.log_interval]
if args.accesses_per_region_selection is not None:
cmd.append('--nr_accesses_per_region=%d' %
args.accesses_per_region_selection)
subprocess.run(cmd)
if __name__ == '__main__':
main()