bisect-skip-count 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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
  4. #
  5. # Typical usage:
  6. #
  7. # bisect-skip-count bisect-command.sh "%(skip)d" "%(count)d" 2>&1 | tee bisect.out
  8. #
  9. # bisect-command.sh is something like this:
  10. # #! /bin/bash
  11. #
  12. # skip=$1
  13. # count=$2
  14. #
  15. # opt -debug-counter=my-counter-skip=${skip},my-counter-count=${count}
  16. # ... Test output of opt and exit zero for pass, non-zero for fail
  17. #
  18. # Examine bisect.out to look for "Last good skip" and "Last good
  19. # count" to find the values of the counter that produce a passing
  20. # result. Incrementing the last good count by one or decrementing the
  21. # last good skip by one should produce a failure.
  22. #
  23. from __future__ import print_function
  24. import os
  25. import sys
  26. import argparse
  27. # This is for timeout support. Use the recommended way of import.
  28. # We do timeouts because when doing, execution testing, we have a habit
  29. # of finding variants that infinite loop
  30. if os.name == 'posix' and sys.version_info[0] < 3:
  31. import subprocess32 as subprocess
  32. else:
  33. import subprocess
  34. parser = argparse.ArgumentParser()
  35. parser.add_argument('--skipstart', type=int, default=0)
  36. parser.add_argument('--skipend', type=int, default=(1 << 32))
  37. parser.add_argument('--countstart', type=int, default=0)
  38. parser.add_argument('--countend', type=int, default=(1 << 32))
  39. parser.add_argument('--timeout', type=int, default=None)
  40. # Use shell support if you need to use complex shell expressions in your command
  41. parser.add_argument('--shell', type=bool, default=False)
  42. parser.add_argument('command', nargs='+')
  43. args = parser.parse_args()
  44. start = args.skipstart
  45. end = args.skipend
  46. print("Bisect of Skip starting!")
  47. print("Start: %d" % start)
  48. print("End: %d" % end)
  49. last = None
  50. while start != end and start != end-1:
  51. count = start + (end - start)//2
  52. print("Visiting Skip: %d with (Start, End) = (%d,%d)" % (count, start, end))
  53. cmd = [x % {'skip':count, 'count':-1} for x in args.command]
  54. print(cmd)
  55. try:
  56. result = subprocess.call(cmd, shell=args.shell, timeout=args.timeout)
  57. if result == 0:
  58. print(" PASSES! Setting end to count")
  59. end = count
  60. else:
  61. print(" FAILS! Setting start to count")
  62. start = count
  63. except:
  64. print(" TIMEOUT, setting end to count")
  65. end = count
  66. firstcount = start
  67. print("Last good skip: %d" % start)
  68. start = args.countstart
  69. end = args.countend
  70. print("Bisect of Count starting!")
  71. print("Start: %d" % start)
  72. print("End: %d" % end)
  73. while start != end and start != end-1:
  74. count = start + (end - start)//2
  75. print("Visiting Count: %d with (Start, End) = (%d,%d)" % (count, start, end))
  76. cmd = [x % {'count':count, 'skip':firstcount } for x in args.command]
  77. print(cmd)
  78. try:
  79. result = subprocess.call(cmd, shell=args.shell, timeout=args.timeout)
  80. if result == 0:
  81. print(" PASSES! Setting start to count")
  82. start = count
  83. else:
  84. print(" FAILS! Setting end to count")
  85. end = count
  86. except:
  87. print(" TIMEOUT, setting start to count")
  88. start = count
  89. print("Last good count: %d" % start)