source: benchmark/rmit.py@ 2af1943

ADT ast-experimental pthread-emulation
Last change on this file since 2af1943 was 1b97cc87, checked in by Thierry Delisle <tdelisle@…>, 3 years ago

Minor fixes to rmit for nasus

  • Property mode set to 100755
File size: 9.4 KB
Line 
1#!/usr/bin/python3
2"""
3Python Script to implement R.M.I.T. testing : Randomized Multiple Interleaved Trials
4
5./rmit.py run COMMAND CANDIDATES
6-t trials
7-o option:values
8"""
9
10
11import argparse
12import datetime
13import itertools
14import json
15import os
16import random
17import re
18import socket
19import subprocess
20import sys
21
22
23def parse_range(x):
24 result = []
25 for part in x.split(','):
26 if '-' in part:
27 a, b = part.split('-')
28 a, b = int(a), int(b)
29 result.extend(range(a, b + 1))
30 else:
31 a = int(part)
32 result.append(a)
33 return result
34
35class DependentOpt:
36 def __init__(self, key, value):
37 self.key = key
38 self.value = value
39 self.vars = re.findall("[a-zA-Z]", value)
40
41def parse_option(key, values):
42 try:
43 num = int(values)
44 return key, [num]
45 except:
46 pass
47
48 if values.startswith('\\'):
49 return key, values[1:].split(',')
50 elif re.search("^[0-9-,]+$", values):
51 values = parse_range(values)
52 return key, [v for v in values]
53 else:
54 return key, DependentOpt(key, values)
55
56def eval_one(fmt, vals):
57 orig = fmt
58 for k, v in vals:
59 fmt = fmt.replace(k, str(v))
60
61 if not re.search("^[0-9-/*+ ]+$", fmt):
62 print('ERROR: pattern option {} (interpreted as {}) could not be evaluated'.format(orig, fmt), file=sys.stderr)
63 sys.exit(1)
64
65 return eval(fmt)
66
67# Evaluate all the options
68# options can be of the for key = val or key = some_math(other_key)
69# produce a list of all the options to replace some_math(other_key) with actual value
70def eval_options(opts):
71 # Find all the options with dependencies
72 dependents = [d for d in opts.values() if type(d) is DependentOpt]
73
74 # we need to find all the straglers
75 processed = []
76
77 # extract all the necessary inputs
78 input_keys = {}
79 for d in dependents:
80 # Mark the dependent as seen
81 processed.append(d.key)
82
83 # process each of the dependencies
84 for dvar in d.vars:
85 # Check that it depends on something that exists
86 if not dvar in opts.keys():
87 print('ERROR: extra pattern option {}:{} uses unknown key {}'.format(d.key,d.value,dvar), file=sys.stderr)
88 sys.exit(1)
89
90 # Check that it's not nested
91 if type(dvar) is DependentOpt:
92 print('ERROR: dependent options cannot be nested {}:{} uses key {}'.format(d.key,d.value,dvar), file=sys.stderr)
93 sys.exit(1)
94
95 # Add the values to the input keys
96 if dvar not in input_keys:
97 input_keys[dvar] = opts[dvar]
98 else :
99 if input_keys[dvar] != opts[dvar]:
100 print('INTERNAL ERROR: repeat input do not match {}:{} vs {}'.format(dvar,opts[dvar],input_keys[dvar]), file=sys.stderr)
101 sys.exit(1)
102
103 # Mark the input as seen
104 processed.append(dvar)
105
106 # add in all the straglers they should cause too many problems
107 for k, v in opts.items():
108 if type(v) is DependentOpt:
109 continue
110
111 if k in processed:
112 # consistency check
113 if k not in input_keys:
114 print('INTERNAL ERROR: key \'{}\' marked as processed but not in input_keys'.format(k), file=sys.stderr)
115 sys.exit(1)
116 continue
117
118 # consistency check
119 if k in input_keys:
120 print('INTERNAL ERROR: key \'{}\' in input_keys but not marked as processed'.format(k), file=sys.stderr)
121 sys.exit(1)
122
123 # add the straggler
124 input_keys[k] = v
125
126 # flatten the dict into a list of pairs so it's easier to work with
127 input_list = []
128 for k, v in input_keys.items():
129 input_list.append([(k, o) for o in v])
130
131 # evaluate all the dependents
132 # they are not allowed to produce new values so it's a one-to-one mapping from here
133 evaluated = []
134 for inputs in list(itertools.product(*input_list)):
135 this_eval = list(inputs)
136 for d in dependents:
137 this_eval.append((d.key, eval_one(d.value, inputs)))
138
139 evaluated.append(this_eval)
140
141 # reformat everything to a list of arguments
142 formated = []
143 for o in evaluated:
144 inner = []
145 for k,v in o:
146 inner.append("-{}".format(k))
147 inner.append("{}".format(v))
148
149 # print(inner)
150 formated.append(inner)
151
152 return formated
153
154# returns the first option with key 'opt'
155def search_option(action, opt):
156 i = 0
157 while i < len(action):
158 if action[i] == opt:
159 i += 1
160 if i != len(action):
161 return action[i]
162 i += 1
163
164 return None
165
166def actions_eta(actions):
167 time = 0
168 for a in actions:
169 o = search_option(a, '-d')
170 if o :
171 time += int(o)
172 return time
173
174taskset_maps = None
175
176def init_taskset_maps():
177 global taskset_maps
178 known_hosts = {
179 "jax": {
180 range( 1, 25) : "48-71",
181 range( 25, 49) : "48-71,144-167",
182 range( 49, 97) : "48-95,144-191",
183 range( 97, 145) : "24-95,120-191",
184 range(145, 193) : "0-95,96-191",
185 },
186 "nasus": {
187 range( 1, 65) : "64-127",
188 range( 65, 129) : "64-127,192-255",
189 range(129, 193) : "64-255",
190 range(193, 257) : "0-255",
191 },
192 }
193
194 if (host := socket.gethostname()) in known_hosts:
195 taskset_maps = known_hosts[host]
196 return True
197
198 print("Warning unknown host '{}', disable taskset usage".format(host), file=sys.stderr)
199 return False
200
201
202def settaskset_one(action):
203 o = search_option(action, '-p')
204 if not o:
205 return action
206 try:
207 oi = int(o)
208 except ValueError:
209 return action
210
211 m = "Not found"
212 for key in taskset_maps:
213 if oi in key:
214 return ['taskset', '-c', taskset_maps[key], *action]
215
216 print("Warning no mapping for {} cores".format(oi), file=sys.stderr)
217 return action
218
219def settaskset(actions):
220 return [settaskset_one(a) for a in actions]
221
222if __name__ == "__main__":
223 # ================================================================================
224 # parse command line arguments
225 formats = ['raw', 'csv', 'json']
226 parser = argparse.ArgumentParser(description='Python Script to implement R.M.I.T. testing : Randomized Multiple Interleaved Trials')
227 parser.add_argument('--list', help='List all the commands that would be run', action='store_true')
228 parser.add_argument('--file', nargs='?', type=argparse.FileType('w'), default=sys.stdout)
229 parser.add_argument('--trials', help='Number of trials to run per combinaison', type=int, default=3)
230 parser.add_argument('--notaskset', help='If specified, the trial will not use taskset to match the -p option', action='store_true')
231 parser.add_argument('command', metavar='command', type=str, nargs=1, help='the command prefix to run')
232 parser.add_argument('candidates', metavar='candidates', type=str, nargs='*', help='the candidate suffix to run')
233
234 try:
235 options, unknown = parser.parse_known_args()
236
237 options.option = []
238 while unknown:
239 key = unknown.pop(0)
240 val = unknown.pop(0)
241
242 if key[0] != '-':
243 raise ValueError
244
245 options.option.append((key[1:], val))
246
247 except:
248 sys.exit(1)
249
250 # ================================================================================
251 # Identify the commands to run
252 command = './' + options.command[0]
253 if options.candidates:
254 commands = [command + "-" + c for c in options.candidates]
255 else:
256 commands = [command]
257 for c in commands:
258 if not os.path.isfile(c):
259 print('ERROR: invalid command {}, file does not exist'.format(c), file=sys.stderr)
260 sys.exit(1)
261
262 if not os.access(c, os.X_OK):
263 print('ERROR: invalid command {}, file not executable'.format(c), file=sys.stderr)
264 sys.exit(1)
265
266
267 # ================================================================================
268 # Identify the options to run
269 opts = dict([parse_option(k, v) for k, v in options.option])
270
271 # Evaluate the options (options can depend on the value of other options)
272 opts = eval_options(opts)
273
274 # ================================================================================
275 # Figure out all the combinations to run
276 actions = []
277 for p in itertools.product(range(options.trials), commands, opts):
278 act = [p[1]]
279 for o in p[2:]:
280 act.extend(o)
281 actions.append(act)
282
283 # ================================================================================
284 # Fixup the different commands
285
286 # Add tasksets
287 withtaskset = False
288 if not options.notaskset and init_taskset_maps():
289 withtaskset = True
290 actions = settaskset(actions)
291
292 # ================================================================================
293 # Now that we know what to run, print it.
294 # find expected time
295 time = actions_eta(actions)
296 print("Running {} trials{}".format(len(actions), "" if time == 0 else " (expecting to take {})".format(str(datetime.timedelta(seconds=int(time)))) ))
297
298 # dry run if options ask for it
299 if options.list:
300 for a in actions:
301 print(" ".join(a))
302 sys.exit(0)
303
304
305 # ================================================================================
306 # Prepare to run
307
308 random.shuffle(actions)
309
310 # ================================================================================
311 # Run
312 options.file.write("[")
313 first = True
314 for i, a in enumerate(actions):
315 sa = " ".join(a[3:] if withtaskset else a)
316 if first:
317 first = False
318 else:
319 options.file.write(",")
320 if options.file != sys.stdout:
321 print("{}/{} : {} \r".format(i, len(actions), sa), end = '')
322 fields = {}
323 with subprocess.Popen( a, stdout = subprocess.PIPE, stderr = subprocess.PIPE) as proc:
324 out, err = proc.communicate()
325 if proc.returncode != 0:
326 print("ERROR: command '{}' encountered error, returned code {}".format(sa, proc.returncode), file=sys.stderr)
327 print(err.decode("utf-8"))
328 sys.exit(1)
329 for s in out.decode("utf-8").splitlines():
330 match = re.search("^(.*):(.*)$", s)
331 if match:
332 try:
333 fields[match.group(1).strip()] = float(match.group(2).strip().replace(',',''))
334 except:
335 pass
336
337 options.file.write(json.dumps([a[3 if withtaskset else 0][2:], sa, fields]))
338 options.file.flush()
339
340 options.file.write("]\n")
341
342 if options.file != sys.stdout:
343 print("Done ")
Note: See TracBrowser for help on using the repository browser.