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 | bb6d6cd | 2015-06-20 02:55:07 +0300 | [diff] [blame^] | 287 | if isinstance(sec.vals['size'], Var): |
| 288 | raise ValueError("Variable {0} isn't provided".format( |
| 289 | sec.vals['size'].name)) |
| 290 | |
koder aka kdanilov | 88407ff | 2015-05-26 15:35:57 +0300 | [diff] [blame] | 291 | sz = ssize2b(sec.vals['size']) |
| 292 | offset = sz * ((MAGIC_OFFSET * counter[0]) % 1.0) |
| 293 | offset = int(offset) // 1024 ** 2 |
| 294 | new_vars = {'UNIQ_OFFSET': str(offset) + "m"} |
| 295 | |
| 296 | for name, val in sec.vals.items(): |
| 297 | if isinstance(val, Var): |
| 298 | if val.name in new_vars: |
| 299 | sec.vals[name] = new_vars[val.name] |
| 300 | |
koder aka kdanilov | bb6d6cd | 2015-06-20 02:55:07 +0300 | [diff] [blame^] | 301 | for vl in sec.vals.values(): |
| 302 | if isinstance(vl, Var): |
| 303 | raise ValueError("Variable {0} isn't provided".format(vl.name)) |
| 304 | |
koder aka kdanilov | 4af1c1d | 2015-05-18 15:48:58 +0300 | [diff] [blame] | 305 | params = sec.vals.copy() |
| 306 | params['UNIQ'] = 'UN{0}'.format(counter[0]) |
| 307 | params['COUNTER'] = str(counter[0]) |
| 308 | params['TEST_SUMM'] = get_test_summary(sec) |
| 309 | sec.name = sec.name.format(**params) |
| 310 | counter[0] += 1 |
| 311 | |
| 312 | return sec |
| 313 | |
| 314 | |
| 315 | def get_test_sync_mode(sec): |
koder aka kdanilov | bc2c898 | 2015-06-13 02:50:43 +0300 | [diff] [blame] | 316 | if isinstance(sec, dict): |
| 317 | vals = sec |
| 318 | else: |
| 319 | vals = sec.vals |
| 320 | |
| 321 | is_sync = str(vals.get("sync", "0")) == "1" |
| 322 | is_direct = str(vals.get("direct", "0")) == "1" |
koder aka kdanilov | 4af1c1d | 2015-05-18 15:48:58 +0300 | [diff] [blame] | 323 | |
| 324 | if is_sync and is_direct: |
| 325 | return 'x' |
| 326 | elif is_sync: |
| 327 | return 's' |
| 328 | elif is_direct: |
| 329 | return 'd' |
| 330 | else: |
| 331 | return 'a' |
| 332 | |
| 333 | |
| 334 | def get_test_summary(sec): |
koder aka kdanilov | bc2c898 | 2015-06-13 02:50:43 +0300 | [diff] [blame] | 335 | if isinstance(sec, dict): |
| 336 | vals = sec |
| 337 | else: |
| 338 | vals = sec.vals |
| 339 | |
koder aka kdanilov | 4af1c1d | 2015-05-18 15:48:58 +0300 | [diff] [blame] | 340 | rw = {"randread": "rr", |
| 341 | "randwrite": "rw", |
| 342 | "read": "sr", |
koder aka kdanilov | 7248c7b | 2015-05-31 22:53:03 +0300 | [diff] [blame] | 343 | "write": "sw", |
| 344 | "randrw": "rm", |
| 345 | "rw": "sm", |
koder aka kdanilov | bc2c898 | 2015-06-13 02:50:43 +0300 | [diff] [blame] | 346 | "readwrite": "sm"}[vals["rw"]] |
koder aka kdanilov | 4af1c1d | 2015-05-18 15:48:58 +0300 | [diff] [blame] | 347 | |
| 348 | sync_mode = get_test_sync_mode(sec) |
koder aka kdanilov | bc2c898 | 2015-06-13 02:50:43 +0300 | [diff] [blame] | 349 | th_count = vals.get('numjobs') |
koder aka kdanilov | 4af1c1d | 2015-05-18 15:48:58 +0300 | [diff] [blame] | 350 | |
| 351 | if th_count is None: |
koder aka kdanilov | bc2c898 | 2015-06-13 02:50:43 +0300 | [diff] [blame] | 352 | th_count = vals.get('concurence', 1) |
koder aka kdanilov | 4af1c1d | 2015-05-18 15:48:58 +0300 | [diff] [blame] | 353 | |
| 354 | return "{0}{1}{2}th{3}".format(rw, |
| 355 | sync_mode, |
koder aka kdanilov | bc2c898 | 2015-06-13 02:50:43 +0300 | [diff] [blame] | 356 | vals['blocksize'], |
koder aka kdanilov | 4af1c1d | 2015-05-18 15:48:58 +0300 | [diff] [blame] | 357 | th_count) |
| 358 | |
| 359 | |
| 360 | def execution_time(sec): |
| 361 | return sec.vals.get('ramp_time', 0) + sec.vals.get('runtime', 0) |
| 362 | |
| 363 | |
koder aka kdanilov | 4af1c1d | 2015-05-18 15:48:58 +0300 | [diff] [blame] | 364 | def parse_all_in_1(source, fname=None): |
| 365 | return fio_config_parse(fio_config_lexer(source, fname)) |
| 366 | |
| 367 | |
| 368 | def flatmap(func, inp_iter): |
| 369 | for val in inp_iter: |
| 370 | for res in func(val): |
| 371 | yield res |
| 372 | |
| 373 | |
| 374 | def fio_cfg_compile(source, fname, test_params, **slice_params): |
| 375 | it = parse_all_in_1(source, fname) |
| 376 | it = (apply_params(sec, test_params) for sec in it) |
| 377 | it = flatmap(process_cycles, it) |
| 378 | it = flatmap(process_repeats, it) |
koder aka kdanilov | bc2c898 | 2015-06-13 02:50:43 +0300 | [diff] [blame] | 379 | return itertools.imap(finall_process, it) |
koder aka kdanilov | 4af1c1d | 2015-05-18 15:48:58 +0300 | [diff] [blame] | 380 | |
| 381 | |
| 382 | def parse_args(argv): |
| 383 | parser = argparse.ArgumentParser( |
| 384 | description="Run fio' and return result") |
| 385 | parser.add_argument("--runcycle", type=int, default=None, |
| 386 | metavar="MAX_CYCLE_SECONDS", |
| 387 | help="Max cycle length in seconds") |
| 388 | parser.add_argument("-p", "--params", nargs="*", metavar="PARAM=VAL", |
| 389 | default=[], |
| 390 | help="Provide set of pairs PARAM=VAL to" + |
| 391 | "format into job description") |
| 392 | parser.add_argument("action", choices=['estimate', 'compile', 'num_tests']) |
| 393 | parser.add_argument("jobfile") |
| 394 | return parser.parse_args(argv) |
| 395 | |
| 396 | |
| 397 | def main(argv): |
| 398 | argv_obj = parse_args(argv) |
| 399 | |
| 400 | if argv_obj.jobfile == '-': |
| 401 | job_cfg = sys.stdin.read() |
| 402 | else: |
| 403 | job_cfg = open(argv_obj.jobfile).read() |
| 404 | |
| 405 | params = {} |
| 406 | for param_val in argv_obj.params: |
| 407 | assert '=' in param_val |
| 408 | name, val = param_val.split("=", 1) |
| 409 | params[name] = parse_value(val) |
| 410 | |
| 411 | slice_params = { |
| 412 | 'runcycle': argv_obj.runcycle, |
| 413 | } |
| 414 | |
koder aka kdanilov | bb6d6cd | 2015-06-20 02:55:07 +0300 | [diff] [blame^] | 415 | sec_it = fio_cfg_compile(job_cfg, argv_obj.jobfile, |
| 416 | params, **slice_params) |
koder aka kdanilov | 4af1c1d | 2015-05-18 15:48:58 +0300 | [diff] [blame] | 417 | |
| 418 | if argv_obj.action == 'estimate': |
koder aka kdanilov | bb6d6cd | 2015-06-20 02:55:07 +0300 | [diff] [blame^] | 419 | print sec_to_str(sum(map(execution_time, sec_it))) |
koder aka kdanilov | 4af1c1d | 2015-05-18 15:48:58 +0300 | [diff] [blame] | 420 | elif argv_obj.action == 'num_tests': |
koder aka kdanilov | bb6d6cd | 2015-06-20 02:55:07 +0300 | [diff] [blame^] | 421 | print sum(map(len, map(list, sec_it))) |
koder aka kdanilov | 4af1c1d | 2015-05-18 15:48:58 +0300 | [diff] [blame] | 422 | elif argv_obj.action == 'compile': |
| 423 | splitter = "\n#" + "-" * 70 + "\n\n" |
koder aka kdanilov | bb6d6cd | 2015-06-20 02:55:07 +0300 | [diff] [blame^] | 424 | print splitter.join(map(str, sec_it)) |
koder aka kdanilov | 4af1c1d | 2015-05-18 15:48:58 +0300 | [diff] [blame] | 425 | |
| 426 | return 0 |
| 427 | |
| 428 | |
| 429 | if __name__ == '__main__': |
| 430 | exit(main(sys.argv[1:])) |