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 | |
| 18 | j_specified = False |
| 19 | output_dir = '.' |
| 20 | for index, arg in enumerate(sys.argv[1:]): |
| 21 | if arg == '-j': |
| 22 | j_specified = True |
| 23 | if arg == '-C': |
| 24 | # + 1 to get the next argument and +1 because we trimmed off sys.argv[0] |
| 25 | output_dir = sys.argv[index + 2] |
| 26 | |
| 27 | use_goma = False |
| 28 | try: |
| 29 | with open(os.path.join(output_dir, 'args.gn')) as file_handle: |
| 30 | for line in file_handle: |
| 31 | # This regex pattern copied from create_installer_archive.py |
| 32 | m = re.match('^\s*use_goma\s*=\s*true(\s*$|\s*#.*$)', line) |
| 33 | if m: |
| 34 | use_goma = True |
| 35 | except IOError: |
| 36 | pass |
| 37 | |
| 38 | if sys.platform.startswith('win'): |
| 39 | # Specify ninja.exe on Windows so that ninja.bat can call autoninja and not |
| 40 | # be called back. |
| 41 | args = ['ninja.exe'] + sys.argv[1:] |
| 42 | else: |
| 43 | args = ['ninja'] + sys.argv[1:] |
| 44 | |
| 45 | num_cores = multiprocessing.cpu_count() |
| 46 | if not j_specified: |
| 47 | if use_goma: |
| 48 | args.append('-j') |
| 49 | core_multiplier = int(os.environ.get("NINJA_CORE_MULTIPLIER", "20")) |
| 50 | args.append('%d' % (num_cores * core_multiplier)) |
| 51 | else: |
| 52 | core_addition = os.environ.get("NINJA_CORE_ADDITION") |
| 53 | if core_addition: |
| 54 | core_addition = int(core_addition) |
| 55 | args.append('-j') |
| 56 | args.append('%d' % (num_cores + core_addition)) |
| 57 | |
| 58 | # Specify a maximum CPU load so that running builds in two different command |
| 59 | # prompts won't overload the system too much. This is not reliable enough to |
| 60 | # be used to auto-adjust between goma/non-goma loads, but it is a nice |
| 61 | # fallback load balancer. |
| 62 | args.append('-l') |
| 63 | args.append('%d' % num_cores) |
| 64 | |
| 65 | print ' '.join(args) |