koder aka kdanilov | 4af1c1d | 2015-05-18 15:48:58 +0300 | [diff] [blame] | 1 | import os |
| 2 | import sys |
| 3 | import copy |
| 4 | import os.path |
| 5 | import argparse |
| 6 | import itertools |
| 7 | from collections import OrderedDict, namedtuple |
| 8 | |
| 9 | |
koder aka kdanilov | 88407ff | 2015-05-26 15:35:57 +0300 | [diff] [blame] | 10 | from wally.utils import sec_to_str, ssize2b |
koder aka kdanilov | 4af1c1d | 2015-05-18 15:48:58 +0300 | [diff] [blame] | 11 | |
| 12 | |
| 13 | SECTION = 0 |
| 14 | SETTING = 1 |
| 15 | INCLUDE = 2 |
| 16 | |
| 17 | |
| 18 | Var = namedtuple('Var', ('name',)) |
| 19 | CfgLine = namedtuple('CfgLine', ('fname', 'lineno', 'oline', |
| 20 | 'tp', 'name', 'val')) |
| 21 | |
| 22 | |
| 23 | class FioJobSection(object): |
| 24 | def __init__(self, name): |
| 25 | self.name = name |
| 26 | self.vals = OrderedDict() |
| 27 | |
| 28 | def copy(self): |
| 29 | return copy.deepcopy(self) |
| 30 | |
| 31 | def required_vars(self): |
| 32 | for name, val in self.vals.items(): |
| 33 | if isinstance(val, Var): |
| 34 | yield name, val |
| 35 | |
| 36 | def is_free(self): |
| 37 | return len(list(self.required_vars())) == 0 |
| 38 | |
| 39 | def __str__(self): |
| 40 | res = "[{0}]\n".format(self.name) |
| 41 | |
| 42 | for name, val in self.vals.items(): |
| 43 | if name.startswith('_') or name == name.upper(): |
| 44 | continue |
| 45 | if isinstance(val, Var): |
| 46 | res += "{0}={{{1}}}\n".format(name, val.name) |
| 47 | else: |
| 48 | res += "{0}={1}\n".format(name, val) |
| 49 | |
| 50 | return res |
| 51 | |
| 52 | |
koder aka kdanilov | 4af1c1d | 2015-05-18 15:48:58 +0300 | [diff] [blame] | 53 | class ParseError(ValueError): |
| 54 | def __init__(self, msg, fname, lineno, line_cont=""): |
| 55 | ValueError.__init__(self, msg) |
| 56 | self.file_name = fname |
| 57 | self.lineno = lineno |
| 58 | self.line_cont = line_cont |
| 59 | |
| 60 | def __str__(self): |
| 61 | msg = "In {0}:{1} ({2}) : {3}" |
| 62 | return msg.format(self.file_name, |
| 63 | self.lineno, |
| 64 | self.line_cont, |
| 65 | super(ParseError, self).__str__()) |
| 66 | |
| 67 | |
| 68 | def is_name(name): |
| 69 | if len(name) == 0: |
| 70 | return False |
| 71 | |
| 72 | if name[0] != '_' and not name[0].isalpha(): |
| 73 | return False |
| 74 | |
| 75 | for ch in name[1:]: |
| 76 | if name[0] != '_' and not name[0].isalnum(): |
| 77 | return False |
| 78 | |
| 79 | return True |
| 80 | |
| 81 | |
| 82 | def parse_value(val): |
| 83 | try: |
| 84 | return int(val) |
| 85 | except ValueError: |
| 86 | pass |
| 87 | |
| 88 | try: |
| 89 | return float(val) |
| 90 | except ValueError: |
| 91 | pass |
| 92 | |
| 93 | if val.startswith('{%'): |
| 94 | assert val.endswith("%}") |
| 95 | content = val[2:-2] |
| 96 | vals = list(i.strip() for i in content.split(',')) |
| 97 | return map(parse_value, vals) |
| 98 | |
| 99 | if val.startswith('{'): |
| 100 | assert val.endswith("}") |
| 101 | assert is_name(val[1:-1]) |
| 102 | return Var(val[1:-1]) |
| 103 | return val |
| 104 | |
| 105 | |
| 106 | def fio_config_lexer(fio_cfg, fname): |
| 107 | for lineno, oline in enumerate(fio_cfg.split("\n")): |
| 108 | try: |
| 109 | line = oline.strip() |
| 110 | |
| 111 | if line.startswith("#") or line.startswith(";"): |
| 112 | continue |
| 113 | |
| 114 | if line == "": |
| 115 | continue |
| 116 | |
| 117 | if '#' in line: |
| 118 | raise ParseError("# isn't allowed inside line", |
| 119 | fname, lineno, oline) |
| 120 | |
| 121 | if line.startswith('['): |
| 122 | yield CfgLine(fname, lineno, oline, SECTION, |
| 123 | line[1:-1].strip(), None) |
| 124 | elif '=' in line: |
| 125 | opt_name, opt_val = line.split('=', 1) |
| 126 | yield CfgLine(fname, lineno, oline, SETTING, |
| 127 | opt_name.strip(), |
| 128 | parse_value(opt_val.strip())) |
| 129 | elif line.startswith("include "): |
| 130 | yield CfgLine(fname, lineno, oline, INCLUDE, |
| 131 | line.split(" ", 1)[1], None) |
| 132 | else: |
| 133 | yield CfgLine(fname, lineno, oline, SETTING, line, '1') |
| 134 | |
| 135 | except Exception as exc: |
| 136 | raise ParseError(str(exc), fname, lineno, oline) |
| 137 | |
| 138 | |
| 139 | def fio_config_parse(lexer_iter): |
| 140 | in_globals = False |
| 141 | curr_section = None |
| 142 | glob_vals = OrderedDict() |
| 143 | sections_count = 0 |
| 144 | |
| 145 | lexed_lines = list(lexer_iter) |
| 146 | one_more = True |
| 147 | includes = {} |
| 148 | |
| 149 | while one_more: |
| 150 | new_lines = [] |
| 151 | one_more = False |
| 152 | for line in lexed_lines: |
| 153 | fname, lineno, oline, tp, name, val = line |
| 154 | |
| 155 | if INCLUDE == tp: |
| 156 | if not os.path.exists(fname): |
| 157 | dirname = '.' |
| 158 | else: |
| 159 | dirname = os.path.dirname(fname) |
| 160 | |
| 161 | new_fname = os.path.join(dirname, name) |
| 162 | includes[new_fname] = (fname, lineno) |
| 163 | |
| 164 | try: |
| 165 | cont = open(new_fname).read() |
| 166 | except IOError as err: |
| 167 | msg = "Error while including file {0}: {1}" |
| 168 | raise ParseError(msg.format(new_fname, err), |
| 169 | fname, lineno, oline) |
| 170 | |
| 171 | new_lines.extend(fio_config_lexer(cont, new_fname)) |
| 172 | one_more = True |
| 173 | else: |
| 174 | new_lines.append(line) |
| 175 | |
| 176 | lexed_lines = new_lines |
| 177 | |
| 178 | for fname, lineno, oline, tp, name, val in lexed_lines: |
| 179 | if tp == SECTION: |
| 180 | if curr_section is not None: |
| 181 | yield curr_section |
| 182 | curr_section = None |
| 183 | |
| 184 | if name == 'global': |
| 185 | if sections_count != 0: |
| 186 | raise ParseError("[global] section should" + |
| 187 | " be only one and first", |
| 188 | fname, lineno, oline) |
| 189 | in_globals = True |
| 190 | else: |
| 191 | in_globals = False |
| 192 | curr_section = FioJobSection(name) |
| 193 | curr_section.vals = glob_vals.copy() |
| 194 | sections_count += 1 |
| 195 | else: |
| 196 | assert tp == SETTING |
| 197 | if in_globals: |
| 198 | glob_vals[name] = val |
| 199 | elif name == name.upper(): |
| 200 | raise ParseError("Param '" + name + |
| 201 | "' not in [global] section", |
| 202 | fname, lineno, oline) |
| 203 | elif curr_section is None: |
| 204 | raise ParseError("Data outside section", |
| 205 | fname, lineno, oline) |
| 206 | else: |
| 207 | curr_section.vals[name] = val |
| 208 | |
| 209 | if curr_section is not None: |
| 210 | yield curr_section |
| 211 | |
| 212 | |
| 213 | def process_repeats(sec): |
| 214 | sec = sec.copy() |
| 215 | count = sec.vals.pop('NUM_ROUNDS', 1) |
| 216 | assert isinstance(count, (int, long)) |
| 217 | |
| 218 | for _ in range(count): |
| 219 | yield sec.copy() |
| 220 | |
| 221 | if 'ramp_time' in sec.vals: |
| 222 | sec.vals['_ramp_time'] = sec.vals.pop('ramp_time') |
| 223 | |
| 224 | |
| 225 | def process_cycles(sec): |
| 226 | cycles = OrderedDict() |
| 227 | |
| 228 | for name, val in sec.vals.items(): |
| 229 | if isinstance(val, list) and name.upper() != name: |
| 230 | cycles[name] = val |
| 231 | |
| 232 | if len(cycles) == 0: |
| 233 | yield sec |
| 234 | else: |
| 235 | for combination in itertools.product(*cycles.values()): |
| 236 | new_sec = sec.copy() |
| 237 | new_sec.vals.update(zip(cycles.keys(), combination)) |
| 238 | yield new_sec |
| 239 | |
| 240 | |
| 241 | def apply_params(sec, params): |
| 242 | processed_vals = OrderedDict() |
| 243 | processed_vals.update(params) |
| 244 | for name, val in sec.vals.items(): |
| 245 | if name in params: |
| 246 | continue |
| 247 | |
| 248 | if isinstance(val, Var): |
| 249 | if val.name in params: |
| 250 | val = params[val.name] |
| 251 | elif val.name in processed_vals: |
| 252 | val = processed_vals[val.name] |
| 253 | processed_vals[name] = val |
koder aka kdanilov | 88407ff | 2015-05-26 15:35:57 +0300 | [diff] [blame] | 254 | |
koder aka kdanilov | 4af1c1d | 2015-05-18 15:48:58 +0300 | [diff] [blame] | 255 | sec = sec.copy() |
| 256 | sec.vals = processed_vals |
| 257 | return sec |
| 258 | |
| 259 | |
koder aka kdanilov | 88407ff | 2015-05-26 15:35:57 +0300 | [diff] [blame] | 260 | MAGIC_OFFSET = 0.1885 |
| 261 | |
| 262 | |
| 263 | def abbv_name_to_full(name): |
| 264 | assert len(name) == 3 |
| 265 | |
| 266 | smode = { |
| 267 | 'a': 'async', |
| 268 | 's': 'sync', |
| 269 | 'd': 'direct', |
| 270 | 'x': 'sync direct' |
| 271 | } |
| 272 | off_mode = {'s': 'sequential', 'r': 'random'} |
koder aka kdanilov | 7248c7b | 2015-05-31 22:53:03 +0300 | [diff] [blame^] | 273 | oper = {'r': 'read', 'w': 'write', 'm': 'mixed'} |
koder aka kdanilov | 88407ff | 2015-05-26 15:35:57 +0300 | [diff] [blame] | 274 | return smode[name[2]] + " " + \ |
| 275 | off_mode[name[0]] + " " + oper[name[1]] |
| 276 | |
| 277 | |
koder aka kdanilov | 4af1c1d | 2015-05-18 15:48:58 +0300 | [diff] [blame] | 278 | def finall_process(sec, counter=[0]): |
| 279 | sec = sec.copy() |
| 280 | |
| 281 | if sec.vals.get('numjobs', '1') != 1: |
| 282 | msg = "Group reporting should be set if numjobs != 1" |
| 283 | assert 'group_reporting' in sec.vals, msg |
| 284 | |
| 285 | sec.vals['unified_rw_reporting'] = '1' |
| 286 | |
koder aka kdanilov | 88407ff | 2015-05-26 15:35:57 +0300 | [diff] [blame] | 287 | sz = ssize2b(sec.vals['size']) |
| 288 | offset = sz * ((MAGIC_OFFSET * counter[0]) % 1.0) |
| 289 | offset = int(offset) // 1024 ** 2 |
| 290 | new_vars = {'UNIQ_OFFSET': str(offset) + "m"} |
| 291 | |
| 292 | for name, val in sec.vals.items(): |
| 293 | if isinstance(val, Var): |
| 294 | if val.name in new_vars: |
| 295 | sec.vals[name] = new_vars[val.name] |
| 296 | |
koder aka kdanilov | 4af1c1d | 2015-05-18 15:48:58 +0300 | [diff] [blame] | 297 | params = sec.vals.copy() |
| 298 | params['UNIQ'] = 'UN{0}'.format(counter[0]) |
| 299 | params['COUNTER'] = str(counter[0]) |
| 300 | params['TEST_SUMM'] = get_test_summary(sec) |
| 301 | sec.name = sec.name.format(**params) |
| 302 | counter[0] += 1 |
| 303 | |
| 304 | return sec |
| 305 | |
| 306 | |
| 307 | def get_test_sync_mode(sec): |
| 308 | is_sync = str(sec.vals.get("sync", "0")) == "1" |
| 309 | is_direct = str(sec.vals.get("direct", "0")) == "1" |
| 310 | |
| 311 | if is_sync and is_direct: |
| 312 | return 'x' |
| 313 | elif is_sync: |
| 314 | return 's' |
| 315 | elif is_direct: |
| 316 | return 'd' |
| 317 | else: |
| 318 | return 'a' |
| 319 | |
| 320 | |
| 321 | def get_test_summary(sec): |
| 322 | rw = {"randread": "rr", |
| 323 | "randwrite": "rw", |
| 324 | "read": "sr", |
koder aka kdanilov | 7248c7b | 2015-05-31 22:53:03 +0300 | [diff] [blame^] | 325 | "write": "sw", |
| 326 | "randrw": "rm", |
| 327 | "rw": "sm", |
| 328 | "readwrite": "sm"}[sec.vals["rw"]] |
koder aka kdanilov | 4af1c1d | 2015-05-18 15:48:58 +0300 | [diff] [blame] | 329 | |
| 330 | sync_mode = get_test_sync_mode(sec) |
| 331 | th_count = sec.vals.get('numjobs') |
| 332 | |
| 333 | if th_count is None: |
| 334 | th_count = sec.vals.get('concurence', 1) |
| 335 | |
| 336 | return "{0}{1}{2}th{3}".format(rw, |
| 337 | sync_mode, |
| 338 | sec.vals['blocksize'], |
| 339 | th_count) |
| 340 | |
| 341 | |
| 342 | def execution_time(sec): |
| 343 | return sec.vals.get('ramp_time', 0) + sec.vals.get('runtime', 0) |
| 344 | |
| 345 | |
| 346 | def slice_config(sec_iter, runcycle=None, max_jobs=1000, split_on_names=False): |
| 347 | jcount = 0 |
| 348 | runtime = 0 |
| 349 | curr_slice = [] |
| 350 | prev_name = None |
| 351 | |
| 352 | for pos, sec in enumerate(sec_iter): |
| 353 | |
| 354 | if prev_name is not None: |
| 355 | split_here = False |
| 356 | |
| 357 | if split_on_names and prev_name != sec.name: |
| 358 | split_here = True |
| 359 | |
| 360 | if split_here: |
| 361 | yield curr_slice |
| 362 | curr_slice = [] |
| 363 | runtime = 0 |
| 364 | jcount = 0 |
| 365 | |
| 366 | prev_name = sec.name |
| 367 | |
| 368 | jc = sec.vals.get('numjobs', 1) |
| 369 | msg = "numjobs should be integer, not {0!r}".format(jc) |
| 370 | assert isinstance(jc, int), msg |
| 371 | |
| 372 | curr_task_time = execution_time(sec) |
| 373 | |
| 374 | if jc > max_jobs: |
| 375 | err_templ = "Can't process job {0!r} - too large numjobs" |
| 376 | raise ValueError(err_templ.format(sec.name)) |
| 377 | |
| 378 | if runcycle is not None and len(curr_slice) != 0: |
| 379 | rc_ok = curr_task_time + runtime <= runcycle |
| 380 | else: |
| 381 | rc_ok = True |
| 382 | |
| 383 | if jc + jcount <= max_jobs and rc_ok: |
| 384 | runtime += curr_task_time |
| 385 | jcount += jc |
| 386 | curr_slice.append(sec) |
| 387 | continue |
| 388 | |
| 389 | assert len(curr_slice) != 0 |
| 390 | yield curr_slice |
| 391 | |
| 392 | if '_ramp_time' in sec.vals: |
| 393 | sec.vals['ramp_time'] = sec.vals.pop('_ramp_time') |
| 394 | curr_task_time = execution_time(sec) |
| 395 | |
| 396 | runtime = curr_task_time |
| 397 | jcount = jc |
| 398 | curr_slice = [sec] |
| 399 | prev_name = None |
| 400 | |
| 401 | if curr_slice != []: |
| 402 | yield curr_slice |
| 403 | |
| 404 | |
| 405 | def parse_all_in_1(source, fname=None): |
| 406 | return fio_config_parse(fio_config_lexer(source, fname)) |
| 407 | |
| 408 | |
| 409 | def flatmap(func, inp_iter): |
| 410 | for val in inp_iter: |
| 411 | for res in func(val): |
| 412 | yield res |
| 413 | |
| 414 | |
| 415 | def fio_cfg_compile(source, fname, test_params, **slice_params): |
| 416 | it = parse_all_in_1(source, fname) |
| 417 | it = (apply_params(sec, test_params) for sec in it) |
| 418 | it = flatmap(process_cycles, it) |
| 419 | it = flatmap(process_repeats, it) |
| 420 | it = itertools.imap(finall_process, it) |
| 421 | return slice_config(it, **slice_params) |
| 422 | |
| 423 | |
| 424 | def parse_args(argv): |
| 425 | parser = argparse.ArgumentParser( |
| 426 | description="Run fio' and return result") |
| 427 | parser.add_argument("--runcycle", type=int, default=None, |
| 428 | metavar="MAX_CYCLE_SECONDS", |
| 429 | help="Max cycle length in seconds") |
| 430 | parser.add_argument("-p", "--params", nargs="*", metavar="PARAM=VAL", |
| 431 | default=[], |
| 432 | help="Provide set of pairs PARAM=VAL to" + |
| 433 | "format into job description") |
| 434 | parser.add_argument("action", choices=['estimate', 'compile', 'num_tests']) |
| 435 | parser.add_argument("jobfile") |
| 436 | return parser.parse_args(argv) |
| 437 | |
| 438 | |
| 439 | def main(argv): |
| 440 | argv_obj = parse_args(argv) |
| 441 | |
| 442 | if argv_obj.jobfile == '-': |
| 443 | job_cfg = sys.stdin.read() |
| 444 | else: |
| 445 | job_cfg = open(argv_obj.jobfile).read() |
| 446 | |
| 447 | params = {} |
| 448 | for param_val in argv_obj.params: |
| 449 | assert '=' in param_val |
| 450 | name, val = param_val.split("=", 1) |
| 451 | params[name] = parse_value(val) |
| 452 | |
| 453 | slice_params = { |
| 454 | 'runcycle': argv_obj.runcycle, |
| 455 | } |
| 456 | |
| 457 | sliced_it = fio_cfg_compile(job_cfg, argv_obj.jobfile, |
| 458 | params, **slice_params) |
| 459 | |
| 460 | if argv_obj.action == 'estimate': |
| 461 | sum_time = 0 |
| 462 | for cfg_slice in sliced_it: |
| 463 | sum_time += sum(map(execution_time, cfg_slice)) |
| 464 | print sec_to_str(sum_time) |
| 465 | elif argv_obj.action == 'num_tests': |
| 466 | print sum(map(len, map(list, sliced_it))) |
| 467 | elif argv_obj.action == 'compile': |
| 468 | splitter = "\n#" + "-" * 70 + "\n\n" |
| 469 | for cfg_slice in sliced_it: |
| 470 | print splitter.join(map(str, cfg_slice)) |
| 471 | |
| 472 | return 0 |
| 473 | |
| 474 | |
| 475 | if __name__ == '__main__': |
| 476 | exit(main(sys.argv[1:])) |