1#!/usr/bin/env python
2# This script is used to bisect skip and count arguments for --debug-counter.
3# It is similar to bisect, except it understands how to increase skip and decrease count
4import os
5import sys
6import argparse
7# This is for timeout support. Use the recommended way of import.
8# We do timeouts because when doing, execution testing, we have a habit
9# of finding variants that infinite loop
10if os.name == 'posix' and sys.version_info[0] < 3:
11  import subprocess32 as subprocess
12else:
13  import subprocess
14parser = argparse.ArgumentParser()
15
16parser.add_argument('--skipstart', type=int, default=0)
17parser.add_argument('--skipend', type=int, default=(1 << 32))
18parser.add_argument('--countstart', type=int, default=0)
19parser.add_argument('--countend', type=int, default=(1 << 32))
20parser.add_argument('--timeout', type=int, default=None)
21# Use shell support if you need to use complex shell expressions in your command
22parser.add_argument('--shell', type=bool, default=False)
23parser.add_argument('command', nargs='+')
24
25args = parser.parse_args()
26
27start = args.skipstart
28end = args.skipend
29
30print("Bisect of Skip starting!")
31print("Start: %d" % start)
32print("End: %d" % end)
33
34last = None
35while start != end and start != end-1:
36    count = start + (end - start)/2
37    print("Visiting Skip: %d with (Start, End) = (%d,%d)" % (count, start, end))
38    cmd = [x % {'skip':count, 'count':-1} for x in args.command]
39    print cmd
40    try:
41        result = subprocess.call(cmd, shell=args.shell, timeout=args.timeout)
42        if result == 0:
43           print("    PASSES! Setting end to count")
44           end = count
45        else:
46           print("    FAILS! Setting start to count")
47           start = count
48    except:
49        print(" TIMEOUT, setting end to count")
50        end = count
51firstcount = start
52print("Last good skip: %d" % start)
53start = args.countstart
54end = args.countend
55print("Bisect of Count starting!")
56print("Start: %d" % start)
57print("End: %d" % end)
58while start != end and start != end-1:
59    count = start + (end - start)/2
60    print("Visiting Count: %d with (Start, End) = (%d,%d)" % (count, start, end))
61    cmd = [x % {'count':count, 'skip':firstcount } for x in args.command]
62    print cmd
63    try:
64        result = subprocess.call(cmd, shell=args.shell, timeout=args.timeout)
65        if result == 0:
66           print("    PASSES! Setting start to count")
67           start = count
68        else:
69           print("    FAILS! Setting end to count")
70           end = count
71    except:
72        print(" TIMEOUT, setting start to count")
73        start = count
74
75print("Last good count: %d" % start)
76