[7a2a3af] | 1 | #!/usr/bin/python3
|
---|
| 2 | """
|
---|
| 3 | Python 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 |
|
---|
| 11 | import argparse
|
---|
[dbb1073] | 12 | import datetime
|
---|
[7a2a3af] | 13 | import itertools
|
---|
[883c4d9] | 14 | import json
|
---|
[7a2a3af] | 15 | import os
|
---|
| 16 | import random
|
---|
| 17 | import re
|
---|
[3e9ec44] | 18 | import socket
|
---|
[7a2a3af] | 19 | import subprocess
|
---|
| 20 | import sys
|
---|
| 21 |
|
---|
| 22 |
|
---|
| 23 | def 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 |
|
---|
| 35 | class 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 |
|
---|
[af333e3] | 41 | def parse_option(key, values):
|
---|
[7a2a3af] | 42 | try:
|
---|
| 43 | num = int(values)
|
---|
| 44 | return key, [num]
|
---|
| 45 | except:
|
---|
| 46 | pass
|
---|
| 47 |
|
---|
[3613e25] | 48 | if values.startswith('\\'):
|
---|
| 49 | return key, values[1:].split(',')
|
---|
| 50 | elif re.search("^[0-9-,]+$", values):
|
---|
[7a2a3af] | 51 | values = parse_range(values)
|
---|
| 52 | return key, [v for v in values]
|
---|
| 53 | else:
|
---|
| 54 | return key, DependentOpt(key, values)
|
---|
| 55 |
|
---|
| 56 | def 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 |
|
---|
[f56101f] | 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
|
---|
[7a2a3af] | 70 | def eval_options(opts):
|
---|
[f56101f] | 71 | # Find all the options with dependencies
|
---|
[7a2a3af] | 72 | dependents = [d for d in opts.values() if type(d) is DependentOpt]
|
---|
[f56101f] | 73 |
|
---|
| 74 | # we need to find all the straglers
|
---|
[7a2a3af] | 75 | processed = []
|
---|
[f56101f] | 76 |
|
---|
| 77 | # extract all the necessary inputs
|
---|
| 78 | input_keys = {}
|
---|
[7a2a3af] | 79 | for d in dependents:
|
---|
[f56101f] | 80 | # Mark the dependent as seen
|
---|
[7a2a3af] | 81 | processed.append(d.key)
|
---|
[f56101f] | 82 |
|
---|
| 83 | # process each of the dependencies
|
---|
[7a2a3af] | 84 | for dvar in d.vars:
|
---|
[f56101f] | 85 | # Check that it depends on something that exists
|
---|
[7a2a3af] | 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 |
|
---|
[f56101f] | 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
|
---|
[7a2a3af] | 104 | processed.append(dvar)
|
---|
| 105 |
|
---|
[f56101f] | 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)
|
---|
[7a2a3af] | 140 |
|
---|
[f56101f] | 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))
|
---|
[7a2a3af] | 148 |
|
---|
[f56101f] | 149 | # print(inner)
|
---|
| 150 | formated.append(inner)
|
---|
[7a2a3af] | 151 |
|
---|
[f56101f] | 152 | return formated
|
---|
[7a2a3af] | 153 |
|
---|
[3e9ec44] | 154 | # returns the first option with key 'opt'
|
---|
| 155 | def 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 |
|
---|
[dbb1073] | 166 | def actions_eta(actions):
|
---|
| 167 | time = 0
|
---|
| 168 | for a in actions:
|
---|
[3e9ec44] | 169 | o = search_option(a, '-d')
|
---|
| 170 | if o :
|
---|
| 171 | time += int(o)
|
---|
[dbb1073] | 172 | return time
|
---|
| 173 |
|
---|
[3e9ec44] | 174 | taskset_maps = None
|
---|
| 175 |
|
---|
| 176 | def init_taskset_maps():
|
---|
| 177 | global taskset_maps
|
---|
| 178 | known_hosts = {
|
---|
| 179 | "jax": {
|
---|
[f56101f] | 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",
|
---|
[3e9ec44] | 185 | },
|
---|
[1b97cc87] | 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 | },
|
---|
[3e9ec44] | 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 |
|
---|
| 202 | def 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 |
|
---|
| 219 | def settaskset(actions):
|
---|
| 220 | return [settaskset_one(a) for a in actions]
|
---|
| 221 |
|
---|
[7a2a3af] | 222 | if __name__ == "__main__":
|
---|
| 223 | # ================================================================================
|
---|
| 224 | # parse command line arguments
|
---|
[883c4d9] | 225 | formats = ['raw', 'csv', 'json']
|
---|
[7a2a3af] | 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)
|
---|
[af333e3] | 229 | parser.add_argument('--trials', help='Number of trials to run per combinaison', type=int, default=3)
|
---|
[3e9ec44] | 230 | parser.add_argument('--notaskset', help='If specified, the trial will not use taskset to match the -p option', action='store_true')
|
---|
[7a2a3af] | 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:
|
---|
[af333e3] | 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 |
|
---|
[7a2a3af] | 247 | except:
|
---|
| 248 | sys.exit(1)
|
---|
| 249 |
|
---|
| 250 | # ================================================================================
|
---|
| 251 | # Identify the commands to run
|
---|
[6f27b67] | 252 | command = './' + options.command[0]
|
---|
| 253 | if options.candidates:
|
---|
| 254 | commands = [command + "-" + c for c in options.candidates]
|
---|
| 255 | else:
|
---|
| 256 | commands = [command]
|
---|
[7a2a3af] | 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
|
---|
[af333e3] | 269 | opts = dict([parse_option(k, v) for k, v in options.option])
|
---|
[7a2a3af] | 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 = []
|
---|
[f56101f] | 277 | for p in itertools.product(range(options.trials), commands, opts):
|
---|
[7a2a3af] | 278 | act = [p[1]]
|
---|
| 279 | for o in p[2:]:
|
---|
| 280 | act.extend(o)
|
---|
| 281 | actions.append(act)
|
---|
| 282 |
|
---|
| 283 | # ================================================================================
|
---|
[3e9ec44] | 284 | # Fixup the different commands
|
---|
| 285 |
|
---|
| 286 | # Add tasksets
|
---|
[f46b26b8] | 287 | withtaskset = False
|
---|
[3e9ec44] | 288 | if not options.notaskset and init_taskset_maps():
|
---|
[f46b26b8] | 289 | withtaskset = True
|
---|
[3e9ec44] | 290 | actions = settaskset(actions)
|
---|
| 291 |
|
---|
| 292 | # ================================================================================
|
---|
| 293 | # Now that we know what to run, print it.
|
---|
[83b22b53] | 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
|
---|
[7a2a3af] | 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 |
|
---|
[dbb1073] | 308 | random.shuffle(actions)
|
---|
[7a2a3af] | 309 |
|
---|
| 310 | # ================================================================================
|
---|
| 311 | # Run
|
---|
[af333e3] | 312 | options.file.write("[")
|
---|
| 313 | first = True
|
---|
[7a2a3af] | 314 | for i, a in enumerate(actions):
|
---|
[f46b26b8] | 315 | sa = " ".join(a[3:] if withtaskset else a)
|
---|
[af333e3] | 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 = '')
|
---|
[7a2a3af] | 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:
|
---|
[fa6233a] | 332 | try:
|
---|
| 333 | fields[match.group(1).strip()] = float(match.group(2).strip().replace(',',''))
|
---|
| 334 | except:
|
---|
| 335 | pass
|
---|
[7a2a3af] | 336 |
|
---|
[f46b26b8] | 337 | options.file.write(json.dumps([a[3 if withtaskset else 0][2:], sa, fields]))
|
---|
[af333e3] | 338 | options.file.flush()
|
---|
[7a2a3af] | 339 |
|
---|
[af333e3] | 340 | options.file.write("]\n")
|
---|
[7a2a3af] | 341 |
|
---|
[af333e3] | 342 | if options.file != sys.stdout:
|
---|
[f56101f] | 343 | print("Done ")
|
---|