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