Bruce Dawson | ebebd95 | 2017-05-31 14:24:38 -0700 | [diff] [blame] | 1 | # Copyright (c) 2017 The Chromium Authors. All rights reserved. |
| 2 | # Use of this source code is governed by a BSD-style license that can be |
| 3 | # found in the LICENSE file. |
| 4 | |
| 5 | """ |
| 6 | This script (intended to be invoked by autoninja or autoninja.bat) detects |
| 7 | whether a build is using goma. If so it runs with a large -j value, and |
| 8 | otherwise it chooses a small one. This auto-adjustment makes using goma simpler |
| 9 | and safer, and avoids errors that can cause slow goma builds or swap-storms |
| 10 | on non-goma builds. |
| 11 | """ |
| 12 | |
| 13 | import multiprocessing |
| 14 | import os |
| 15 | import re |
| 16 | import sys |
| 17 | |
Bruce Dawson | 1f767e1 | 2017-07-07 15:10:37 -0700 | [diff] [blame^] | 18 | # The -t tools are incompatible with -j and -l |
| 19 | t_specified = False |
Bruce Dawson | ebebd95 | 2017-05-31 14:24:38 -0700 | [diff] [blame] | 20 | j_specified = False |
| 21 | output_dir = '.' |
| 22 | for index, arg in enumerate(sys.argv[1:]): |
| 23 | if arg == '-j': |
| 24 | j_specified = True |
Bruce Dawson | 1f767e1 | 2017-07-07 15:10:37 -0700 | [diff] [blame^] | 25 | if arg == '-t': |
| 26 | t_specified = True |
Bruce Dawson | ebebd95 | 2017-05-31 14:24:38 -0700 | [diff] [blame] | 27 | if arg == '-C': |
| 28 | # + 1 to get the next argument and +1 because we trimmed off sys.argv[0] |
| 29 | output_dir = sys.argv[index + 2] |
| 30 | |
| 31 | use_goma = False |
| 32 | try: |
| 33 | with open(os.path.join(output_dir, 'args.gn')) as file_handle: |
| 34 | for line in file_handle: |
| 35 | # This regex pattern copied from create_installer_archive.py |
| 36 | m = re.match('^\s*use_goma\s*=\s*true(\s*$|\s*#.*$)', line) |
| 37 | if m: |
| 38 | use_goma = True |
| 39 | except IOError: |
| 40 | pass |
| 41 | |
| 42 | if sys.platform.startswith('win'): |
| 43 | # Specify ninja.exe on Windows so that ninja.bat can call autoninja and not |
| 44 | # be called back. |
| 45 | args = ['ninja.exe'] + sys.argv[1:] |
| 46 | else: |
| 47 | args = ['ninja'] + sys.argv[1:] |
| 48 | |
| 49 | num_cores = multiprocessing.cpu_count() |
Bruce Dawson | 1f767e1 | 2017-07-07 15:10:37 -0700 | [diff] [blame^] | 50 | if not j_specified and not t_specified: |
Bruce Dawson | ebebd95 | 2017-05-31 14:24:38 -0700 | [diff] [blame] | 51 | if use_goma: |
| 52 | args.append('-j') |
| 53 | core_multiplier = int(os.environ.get("NINJA_CORE_MULTIPLIER", "20")) |
| 54 | args.append('%d' % (num_cores * core_multiplier)) |
| 55 | else: |
| 56 | core_addition = os.environ.get("NINJA_CORE_ADDITION") |
| 57 | if core_addition: |
| 58 | core_addition = int(core_addition) |
| 59 | args.append('-j') |
| 60 | args.append('%d' % (num_cores + core_addition)) |
| 61 | |
Bruce Dawson | 1f767e1 | 2017-07-07 15:10:37 -0700 | [diff] [blame^] | 62 | if not t_specified: |
| 63 | # Specify a maximum CPU load so that running builds in two different command |
| 64 | # prompts won't overload the system too much. This is not reliable enough to |
| 65 | # be used to auto-adjust between goma/non-goma loads, but it is a nice |
| 66 | # fallback load balancer. |
| 67 | args.append('-l') |
| 68 | args.append('%d' % num_cores) |
Bruce Dawson | ebebd95 | 2017-05-31 14:24:38 -0700 | [diff] [blame] | 69 | |
| 70 | print ' '.join(args) |