aboutsummaryrefslogtreecommitdiff
path: root/llvm_tools/auto_llvm_bisection.py
blob: 7e8fb1ddfdb5d06e75a615166962e32f4d591c2c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Copyright 2019 The Chromium OS Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

"""Performs bisection on LLVM based off a .JSON file."""

from __future__ import print_function

import enum
import json
import os
import subprocess
import sys
import time
import traceback

import chroot
from llvm_bisection import BisectionExitStatus
import llvm_bisection
import update_tryjob_status

# Used to re-try for 'llvm_bisection.py' to attempt to launch more tryjobs.
BISECTION_RETRY_TIME_SECS = 10 * 60

# Wait time to then poll each tryjob whose 'status' value is 'pending'.
POLL_RETRY_TIME_SECS = 30 * 60

# The number of attempts for 'llvm_bisection.py' to launch more tryjobs.
#
# It is reset (break out of the `for` loop/ exit the program) if successfully
# launched more tryjobs or bisection is finished (no more revisions between
# start and end of the bisection).
BISECTION_ATTEMPTS = 3

# The limit for updating all tryjobs whose 'status' is 'pending'.
#
# If the time that has passed for polling exceeds this value, then the program
# will exit with the appropriate exit code.
POLLING_LIMIT_SECS = 18 * 60 * 60


class BuilderStatus(enum.Enum):
  """Actual values given via 'cros buildresult'."""

  PASS = 'pass'
  FAIL = 'fail'
  RUNNING = 'running'


builder_status_mapping = {
    BuilderStatus.PASS.value: update_tryjob_status.TryjobStatus.GOOD.value,
    BuilderStatus.FAIL.value: update_tryjob_status.TryjobStatus.BAD.value,
    BuilderStatus.RUNNING.value: update_tryjob_status.TryjobStatus.PENDING.value
}


def GetBuildResult(chroot_path, buildbucket_id):
  """Returns the conversion of the result of 'cros buildresult'."""

  # Calls 'cros buildresult' to get the status of the tryjob.
  try:
    tryjob_json = subprocess.check_output(
        [
            'cros_sdk', '--', 'cros', 'buildresult', '--buildbucket-id',
            str(buildbucket_id), '--report', 'json'
        ],
        cwd=chroot_path,
        stderr=subprocess.STDOUT,
        encoding='UTF-8',
    )
  except subprocess.CalledProcessError as err:
    if 'No build found. Perhaps not started' not in err.output:
      raise
    return None

  tryjob_content = json.loads(tryjob_json)

  build_result = str(tryjob_content['%d' % buildbucket_id]['status'])

  # The string returned by 'cros buildresult' might not be in the mapping.
  if build_result not in builder_status_mapping:
    raise ValueError('"cros buildresult" return value is invalid: %s' %
                     build_result)

  return builder_status_mapping[build_result]


def main():
  """Bisects LLVM using the result of `cros buildresult` of each tryjob.

  Raises:
    AssertionError: The script was run inside the chroot.
  """

  chroot.VerifyOutsideChroot()

  args_output = llvm_bisection.GetCommandLineArgs()

  if os.path.isfile(args_output.last_tested):
    print('Resuming bisection for %s' % args_output.last_tested)
  else:
    print('Starting a new bisection for %s' % args_output.last_tested)

  while True:
    # Update the status of existing tryjobs
    if os.path.isfile(args_output.last_tested):
      update_start_time = time.time()
      with open(args_output.last_tested) as json_file:
        json_dict = json.load(json_file)
      while True:
        print('\nAttempting to update all tryjobs whose "status" is '
              '"pending":')
        print('-' * 40)

        completed = True
        for tryjob in json_dict['jobs']:
          if tryjob[
              'status'] == update_tryjob_status.TryjobStatus.PENDING.value:
            status = GetBuildResult(args_output.chroot_path,
                                    tryjob['buildbucket_id'])
            if status:
              tryjob['status'] = status
            else:
              completed = False

        print('-' * 40)

        # Proceed to the next step if all the existing tryjobs have completed.
        if completed:
          break

        delta_time = time.time() - update_start_time

        if delta_time > POLLING_LIMIT_SECS:
          # Something is wrong with updating the tryjobs's 'status' via
          # `cros buildresult` (e.g. network issue, etc.).
          sys.exit('Failed to update pending tryjobs.')

        print('-' * 40)
        print('Sleeping for %d minutes.' % (POLL_RETRY_TIME_SECS // 60))
        time.sleep(POLL_RETRY_TIME_SECS)

      # There should always be update from the tryjobs launched in the
      # last iteration.
      temp_filename = '%s.new' % args_output.last_tested
      with open(temp_filename, 'w') as temp_file:
        json.dump(json_dict, temp_file, indent=4, separators=(',', ': '))
      os.rename(temp_filename, args_output.last_tested)

    # Launch more tryjobs.
    for cur_try in range(1, BISECTION_ATTEMPTS + 1):
      try:
        print('\nAttempting to launch more tryjobs if possible:')
        print('-' * 40)

        bisection_ret = llvm_bisection.main(args_output)

        print('-' * 40)

        # Stop if the bisection has completed.
        if bisection_ret == BisectionExitStatus.BISECTION_COMPLETE.value:
          sys.exit(0)

        # Successfully launched more tryjobs.
        break
      except Exception:
        traceback.print_exc()

        print('-' * 40)

        # Exceeded the number of times to launch more tryjobs.
        if cur_try == BISECTION_ATTEMPTS:
          sys.exit('Unable to continue bisection.')

        num_retries_left = BISECTION_ATTEMPTS - cur_try

        print('Retries left to continue bisection %d.' % num_retries_left)

        print('Sleeping for %d minutes.' % (BISECTION_RETRY_TIME_SECS // 60))
        time.sleep(BISECTION_RETRY_TIME_SECS)


if __name__ == '__main__':
  main()