Chromium Code Reviews| Index: pydir/bisection-tool.py |
| diff --git a/pydir/bisection-tool.py b/pydir/bisection-tool.py |
| new file mode 100755 |
| index 0000000000000000000000000000000000000000..d2ca546d3ce8d86321cbab47ef6da9780c5b782d |
| --- /dev/null |
| +++ b/pydir/bisection-tool.py |
| @@ -0,0 +1,141 @@ |
| +#!/usr/bin/env python2 |
| +import argparse |
| +import os |
| +import signal |
| +import subprocess |
| + |
| +class Runner(object): |
| + def __init__(self, input_cmd, timeout, comma_join = False): |
| + self._input_cmd = input_cmd |
| + self._timeout = timeout |
| + self._num_tries = 0 |
| + self._comma_join = comma_join |
| + |
| + def Run(self, included_ranges): |
| + def timeout_handler(signum, frame): |
| + raise RuntimeError("Timeout") |
| + |
| + self._num_tries += 1 |
| + cmd = self._input_cmd |
| + for i in included_ranges: |
| + if isinstance(i, int): |
|
John
2016/07/20 22:23:05
I would always pass a range. If you want a single
|
| + range_str = str(i) |
| + else: |
| + range_str = "{start}:{end}".format(start=i[0], end=i[1]) |
| + if self._comma_join: |
| + cmd += "," + range_str |
| + else: |
| + cmd += " -i " + range_str |
| + print cmd |
| + p = subprocess.Popen(cmd, shell = True, cwd = None, |
| + stdout = subprocess.PIPE, stderr = subprocess.PIPE, env = None) |
| + if self._timeout != -1: |
| + signal.signal(signal.SIGALRM, timeout_handler) |
| + signal.alarm(self._timeout) |
| + |
| + try: |
| + _, _ = p.communicate() |
| + if self._timeout != -1: |
| + signal.alarm(0) |
| + except: |
| + try: |
| + os.kill(p.pid, signal.SIGKILL) |
| + except OSError: |
| + pass |
| + print "Timeout" |
| + return -9 |
| + print "Return Code: " + str(p.returncode) |
| + return p.returncode |
| + |
| +def flatten(tree): |
| + if isinstance(tree, list): |
| + result = [] |
| + for node in tree: |
| + result.extend(flatten(node)) |
| + return result |
| + else: |
| + return [tree] # leaf |
| + |
| +def find_crashes(runner, current_interval, include_ranges, find_all): |
| + if current_interval[0] == current_interval[1]: |
| + return [] |
| + mid = (current_interval[0] + current_interval[1])/2 |
| + |
| + first_half = (current_interval[0], mid) |
| + second_half = (mid, current_interval[1]) |
| + |
| + exit_code_2 = 0 |
| + |
| + exit_code_1 = runner.Run([first_half] + include_ranges) |
| + if find_all or exit_code_1 == 0: |
| + exit_code_2 = runner.Run([second_half] + include_ranges) |
| + |
| + if exit_code_1 == 0 and exit_code_2 == 0: |
| + # Whole range fails but both halves pass |
| + # So, some conjunction of functions cause a failure, but none individually. |
| + partial_result = flatten(find_crashes(runner, first_half, [second_half] |
|
John
2016/07/20 22:23:05
you **REALLY** shouldn't need to flatten your resu
|
| + + include_ranges, find_all)) |
| + # Heavy list concatenation, but this is insignificant compared to the |
| + # process run times |
| + partial_result.extend(flatten(find_crashes(runner, second_half, |
| + partial_result + include_ranges, find_all))) |
| + return [partial_result] |
|
John
2016/07/20 22:23:05
why do you return partial result wrapped in a list
|
| + else: |
| + result = [] |
| + if exit_code_1 != 0: |
|
John
2016/07/20 22:23:05
I don't see you using "find_all" here, so you migh
|
| + if first_half[1] == first_half[0] + 1: |
| + result.append(first_half[0]) |
| + else: |
| + result.extend(find_crashes(runner, first_half, |
| + include_ranges, find_all)) |
| + if exit_code_2 != 0: |
| + if second_half[1] == second_half[0] + 1: |
| + result.append(second_half[0]) |
| + else: |
| + result.extend(find_crashes(runner, second_half, |
| + include_ranges, find_all)) |
| + return result |
| + |
| + |
| +def main(): |
| + desc = 'Bisection debugging helper script' |
| + argparser = argparse.ArgumentParser(description=desc) |
| + argparser.add_argument('--cmd', required=True, |
| + dest='cmd', |
| + help='Runnable command') |
| + argparser.add_argument('--start', dest='start', default=0, |
| + help='Start of initial range') |
| + argparser.add_argument('--end', dest='end', default=50000, |
| + help='End of initial range') |
| + argparser.add_argument('--timeout', dest='timeout', default=60, |
| + help='Timeout for each invocation of the input') |
| + |
| + argparser.add_argument('--all', dest='all', action='store_true') |
| + argparser.add_argument('--no-all', dest='all', action='store_false') |
| + argparser.set_defaults(all=True) |
| + |
| + argparser.add_argument('--comma-join', dest='comma_join', action='store_true') |
| + argparser.add_argument('--separate', dest='comma_join', action='store_false') |
| + argparser.set_defaults(all=True) |
| + |
| + args = argparser.parse_args() |
| + |
| + fail_list = [] |
| + |
| + initial_range = (int(args.start), int(args.end)) |
| + timeout = int(args.timeout) |
| + runner = Runner(args.cmd, timeout, args.comma_join) |
| + if runner.Run([initial_range]) != 0: |
|
John
2016/07/20 22:23:05
why not
crashes = find_crashes(runner, initial_ra
|
| + fail_list = find_crashes(runner, initial_range, [], args.all) |
| + else: |
| + print "Pass" # The whole input range works, maybe check subzero build flags? |
| + |
| + if fail_list: |
| + print "Failing Functions:" |
| + for fail in fail_list: |
| + print fail |
| + print "Number of tries: " + str(runner._num_tries) |
| + # TODO(manasijm): Pretty print, associate these numbers with filenames |
| + |
| +if __name__ == '__main__': |
| + main() |