|
| 1 | +# Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +# A script to bisect the mainline commits and find the culprit commit. |
| 16 | +# The default 'git bisect' checks feature branches, which is not desired |
| 17 | +# because commits in feature branch might not pass tests or compile. |
| 18 | +# |
| 19 | +# Example: |
| 20 | +# python ../bisect.py --git_dir=$PWD/../Paddle --build_dir=$PWD \ |
| 21 | +# --good_commit=3647ed6 --bad_commit=279aa6 \ |
| 22 | +# --test_target=test_rnn_encoder_decoder |
| 23 | + |
| 24 | +import argparse |
| 25 | +import os |
| 26 | +import subprocess |
| 27 | +import sys |
| 28 | + |
| 29 | +parser = argparse.ArgumentParser(description=__doc__) |
| 30 | +parser.add_argument( |
| 31 | + '--git_dir', type=str, default='', help='git repo root directory.') |
| 32 | +parser.add_argument( |
| 33 | + '--build_dir', type=str, default='', help='build directory.') |
| 34 | +parser.add_argument( |
| 35 | + '--good_commit', |
| 36 | + type=str, |
| 37 | + default='', |
| 38 | + help='The old commit known to be good.') |
| 39 | +parser.add_argument( |
| 40 | + '--bad_commit', |
| 41 | + type=str, |
| 42 | + default='', |
| 43 | + help='The new commit known to be bad.') |
| 44 | +parser.add_argument( |
| 45 | + '--test_target', type=str, default='', help='The test target to evaluate.') |
| 46 | +parser.add_argument( |
| 47 | + '--bisect_branch', |
| 48 | + type=str, |
| 49 | + default='develop', |
| 50 | + help='The mainline branch to bisect (feature branch ignored.') |
| 51 | +parser.add_argument( |
| 52 | + '--log_file', type=str, default='', help='The file use to log outputs.') |
| 53 | +parser.add_argument( |
| 54 | + '--test_times', |
| 55 | + type=int, |
| 56 | + default=10, |
| 57 | + help="Number of times to run the test target.") |
| 58 | +parser.add_argument( |
| 59 | + '--build_parallel', type=int, default=32, help="make parallelism.") |
| 60 | +args = parser.parse_args() |
| 61 | + |
| 62 | +if not args.log_file: |
| 63 | + args.log_file = '/tmp/%s...%s.log' % (args.good_commit, args.bad_commit) |
| 64 | + |
| 65 | + |
| 66 | +def print_arguments(): |
| 67 | + print('----------- Configuration Arguments -----------') |
| 68 | + for arg, value in sorted(vars(args).iteritems()): |
| 69 | + print('%s: %s' % (arg, value)) |
| 70 | + print('------------------------------------------------') |
| 71 | + |
| 72 | + |
| 73 | +print_arguments() |
| 74 | + |
| 75 | +# List the commits in mainline branch. |
| 76 | +os.chdir(args.git_dir) |
| 77 | +ret = subprocess.check_output( |
| 78 | + [ |
| 79 | + 'git rev-list --first-parent %s...%s' % (args.good_commit, |
| 80 | + args.bad_commit) |
| 81 | + ], |
| 82 | + shell=True) |
| 83 | +sys.stdout.write('commits found:\n%s\n' % ret) |
| 84 | +commits = ret.strip().split('\n') |
| 85 | +os.chdir(args.build_dir) |
| 86 | +# Clean up previous logs. |
| 87 | +subprocess.check_output(['echo "" > %s' % args.log_file], shell=True) |
| 88 | + |
| 89 | +last_culprit = '' |
| 90 | +while True: |
| 91 | + # Get to the mainline branch and clean up |
| 92 | + os.chdir(args.git_dir) |
| 93 | + subprocess.check_output( |
| 94 | + [ |
| 95 | + 'git checkout %s && git clean -fd && git checkout .' % |
| 96 | + args.bisect_branch |
| 97 | + ], |
| 98 | + shell=True) |
| 99 | + |
| 100 | + if not commits: |
| 101 | + sys.stdout.write('no commits to bisect\n') |
| 102 | + exit() |
| 103 | + # checkout the picked branch. |
| 104 | + pick_idx = len(commits) / 2 |
| 105 | + pick = commits[pick_idx] |
| 106 | + os.chdir(args.git_dir) |
| 107 | + subprocess.check_output(['git checkout %s' % pick], shell=True) |
| 108 | + |
| 109 | + # Clean builds and compile. |
| 110 | + # We assume mainline commits should always compile. |
| 111 | + os.chdir(args.build_dir) |
| 112 | + sys.stdout.write('eval commit %d/%d: %s\n' % (pick_idx, len(commits), pick)) |
| 113 | + # Link error can happen without complete clean up. |
| 114 | + cmd = ('rm -rf * && ' |
| 115 | + 'cmake -DWITH_TESTING=ON %s >> %s && make -j%s >> %s' % |
| 116 | + (args.git_dir, args.log_file, args.build_parallel, args.log_file)) |
| 117 | + sys.stdout.write('cmd: %s\n' % cmd) |
| 118 | + try: |
| 119 | + subprocess.check_output([cmd], shell=True) |
| 120 | + except subprocess.CalledProcessError as e: |
| 121 | + sys.stderr.write('failed to build commit: %s\n%s\n' % (pick, e)) |
| 122 | + exit() |
| 123 | + # test the selected branch. |
| 124 | + passed = True |
| 125 | + try: |
| 126 | + cmd = ('ctest --repeat-until-fail %s -R %s >> %s' % |
| 127 | + (args.test_times, args.test_target, args.log_file)) |
| 128 | + sys.stdout.write('cmd: %s\n' % cmd) |
| 129 | + subprocess.check_output([cmd], shell=True) |
| 130 | + except subprocess.CalledProcessError as e: |
| 131 | + passed = False |
| 132 | + last_culprit = pick |
| 133 | + sys.stdout.write('eval %s passed: %s\n' % (pick, passed)) |
| 134 | + if passed: |
| 135 | + if pick_idx == 0: break |
| 136 | + commits = commits[:pick_idx] |
| 137 | + else: |
| 138 | + if pick_idx + 1 >= len(commits): break |
| 139 | + commits = commits[pick_idx + 1:] |
| 140 | + |
| 141 | +sys.stdout.write('Culprit commit: %s\n' % last_culprit) |
0 commit comments