predictable_wrapper.py 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. #!/usr/bin/env python3
  2. # Copyright 2017 the V8 project authors. All rights reserved.
  3. # Use of this source code is governed by a BSD-style license that can be
  4. # found in the LICENSE file.
  5. """
  6. Wrapper script for verify-predictable mode. D8 is expected to be compiled with
  7. v8_enable_verify_predictable.
  8. The actual test command is expected to be passed to this wraper as is. E.g.:
  9. predictable_wrapper.py path/to/d8 --test --predictable --flag1 --flag2
  10. The command is run up to three times and the printed allocation hash is
  11. compared. Differences are reported as errors.
  12. """
  13. # for py2/py3 compatibility
  14. from __future__ import absolute_import
  15. from __future__ import print_function
  16. import sys
  17. from testrunner.local import command
  18. from testrunner.local import utils
  19. MAX_TRIES = 3
  20. TIMEOUT = 120
  21. # Predictable mode works only when run on the host os.
  22. command.setup(utils.GuessOS(), None)
  23. def maybe_decode(message):
  24. if not isinstance(message, str):
  25. return message.decode()
  26. return message
  27. def main(args):
  28. def allocation_str(stdout):
  29. for line in reversed((stdout or '').splitlines()):
  30. if maybe_decode(line).startswith('### Allocations = '):
  31. return line
  32. return None
  33. cmd = command.Command(
  34. args[0], args[1:], timeout=TIMEOUT, handle_sigterm=True)
  35. previous_allocations = None
  36. for run in range(1, MAX_TRIES + 1):
  37. print('### Predictable run #%d' % run)
  38. output = cmd.execute()
  39. if output.stdout:
  40. print('### Stdout:')
  41. print(output.stdout)
  42. if output.stderr:
  43. print('### Stderr:')
  44. print(output.stderr)
  45. print('### Return code: %s' % output.exit_code)
  46. if output.HasTimedOut():
  47. # If we get a timeout in any run, we are in an unpredictable state. Just
  48. # report it as a failure and don't rerun.
  49. print('### Test timed out')
  50. return 1
  51. allocations = allocation_str(output.stdout)
  52. if not allocations:
  53. print ('### Test had no allocation output. Ensure this is built '
  54. 'with v8_enable_verify_predictable and that '
  55. '--verify-predictable is passed at the cmd line.')
  56. return 2
  57. if previous_allocations and previous_allocations != allocations:
  58. print('### Allocations differ')
  59. return 3
  60. if run >= MAX_TRIES:
  61. # No difference on the last run -> report a success.
  62. return 0
  63. previous_allocations = allocations
  64. # Unreachable.
  65. assert False
  66. if __name__ == '__main__':
  67. sys.exit(main(sys.argv[1:]))