Gabriel Charette | 125f7cc | 2019-06-20 19:19:35 +0000 | [diff] [blame] | 1 | #!/usr/bin/env vpython |
Bruce Dawson | ebebd95 | 2017-05-31 14:24:38 -0700 | [diff] [blame] | 2 | # Copyright (c) 2017 The Chromium 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 | """ |
| 7 | This script (intended to be invoked by autoninja or autoninja.bat) detects |
| 8 | whether a build is using goma. If so it runs with a large -j value, and |
| 9 | otherwise it chooses a small one. This auto-adjustment makes using goma simpler |
| 10 | and safer, and avoids errors that can cause slow goma builds or swap-storms |
| 11 | on non-goma builds. |
| 12 | """ |
| 13 | |
Gabriel Charette | 125f7cc | 2019-06-20 19:19:35 +0000 | [diff] [blame] | 14 | # [VPYTHON:BEGIN] |
| 15 | # wheel: < |
| 16 | # name: "infra/python/wheels/psutil/${vpython_platform}" |
Jesse McKenna | 3b07526 | 2019-07-09 00:07:49 +0000 | [diff] [blame] | 17 | # version: "version:5.6.2" |
Gabriel Charette | 125f7cc | 2019-06-20 19:19:35 +0000 | [diff] [blame] | 18 | # > |
| 19 | # [VPYTHON:END] |
| 20 | |
Raul Tambre | 80ee78e | 2019-05-06 22:41:05 +0000 | [diff] [blame] | 21 | from __future__ import print_function |
| 22 | |
Bruce Dawson | ebebd95 | 2017-05-31 14:24:38 -0700 | [diff] [blame] | 23 | import os |
Gabriel Charette | 125f7cc | 2019-06-20 19:19:35 +0000 | [diff] [blame] | 24 | import psutil |
Bruce Dawson | ebebd95 | 2017-05-31 14:24:38 -0700 | [diff] [blame] | 25 | import re |
| 26 | import sys |
| 27 | |
Yoshisato Yanagisawa | 4b49707 | 2018-11-07 02:52:33 +0000 | [diff] [blame] | 28 | SCRIPT_DIR = os.path.dirname(os.path.realpath(__file__)) |
| 29 | |
Yoshisato Yanagisawa | 0db62fc | 2018-11-01 01:09:53 +0000 | [diff] [blame] | 30 | # The -t tools are incompatible with -j |
Bruce Dawson | 1f767e1 | 2017-07-07 15:10:37 -0700 | [diff] [blame] | 31 | t_specified = False |
Bruce Dawson | ebebd95 | 2017-05-31 14:24:38 -0700 | [diff] [blame] | 32 | j_specified = False |
| 33 | output_dir = '.' |
Bruce Dawson | f3b4f06 | 2017-09-07 17:22:26 -0700 | [diff] [blame] | 34 | input_args = sys.argv |
| 35 | # On Windows the autoninja.bat script passes along the arguments enclosed in |
| 36 | # double quotes. This prevents multiple levels of parsing of the special '^' |
| 37 | # characters needed when compiling a single file but means that this script gets |
| 38 | # called with a single argument containing all of the actual arguments, |
| 39 | # separated by spaces. When this case is detected we need to do argument |
| 40 | # splitting ourselves. This means that arguments containing actual spaces are |
| 41 | # not supported by autoninja, but that is not a real limitation. |
| 42 | if (sys.platform.startswith('win') and len(sys.argv) == 2 and |
| 43 | input_args[1].count(' ') > 0): |
| 44 | input_args = sys.argv[:1] + sys.argv[1].split() |
Yoshisato Yanagisawa | f66e551 | 2018-11-15 00:40:39 +0000 | [diff] [blame] | 45 | |
| 46 | # Ninja uses getopt_long, which allow to intermix non-option arguments. |
| 47 | # To leave non supported parameters untouched, we do not use getopt. |
Bruce Dawson | f3b4f06 | 2017-09-07 17:22:26 -0700 | [diff] [blame] | 48 | for index, arg in enumerate(input_args[1:]): |
Yoshisato Yanagisawa | f66e551 | 2018-11-15 00:40:39 +0000 | [diff] [blame] | 49 | if arg.startswith('-j'): |
Bruce Dawson | ebebd95 | 2017-05-31 14:24:38 -0700 | [diff] [blame] | 50 | j_specified = True |
Yoshisato Yanagisawa | f66e551 | 2018-11-15 00:40:39 +0000 | [diff] [blame] | 51 | if arg.startswith('-t'): |
Bruce Dawson | 1f767e1 | 2017-07-07 15:10:37 -0700 | [diff] [blame] | 52 | t_specified = True |
Bruce Dawson | ebebd95 | 2017-05-31 14:24:38 -0700 | [diff] [blame] | 53 | if arg == '-C': |
Bruce Dawson | f3b4f06 | 2017-09-07 17:22:26 -0700 | [diff] [blame] | 54 | # + 1 to get the next argument and +1 because we trimmed off input_args[0] |
| 55 | output_dir = input_args[index + 2] |
Bruce Dawson | 6af3aa8 | 2018-10-03 16:39:42 +0000 | [diff] [blame] | 56 | elif arg.startswith('-C'): |
| 57 | # Support -Cout/Default |
| 58 | output_dir = arg[2:] |
Bruce Dawson | ebebd95 | 2017-05-31 14:24:38 -0700 | [diff] [blame] | 59 | |
| 60 | use_goma = False |
Gabriel Charette | 125f7cc | 2019-06-20 19:19:35 +0000 | [diff] [blame] | 61 | use_jumbo_build = False |
Sam Clegg | 3a96d62 | 2019-08-22 21:22:04 +0000 | [diff] [blame] | 62 | |
| 63 | # Attempt to auto-detect goma usage. We support gn-based builds, where we |
| 64 | # look for args.gn in the build tree, and cmake-based builds where we look for |
| 65 | # rules.ninja. |
| 66 | if os.path.exists(os.path.join(output_dir, 'args.gn')): |
Gabriel Charette | 125f7cc | 2019-06-20 19:19:35 +0000 | [diff] [blame] | 67 | with open(os.path.join(output_dir, 'args.gn')) as file_handle: |
| 68 | for line in file_handle: |
| 69 | # This regex pattern copied from create_installer_archive.py |
Sam Clegg | 3a96d62 | 2019-08-22 21:22:04 +0000 | [diff] [blame] | 70 | if re.match(r'^\s*use_goma\s*=\s*true(\s*$|\s*#.*$)', line): |
Gabriel Charette | 125f7cc | 2019-06-20 19:19:35 +0000 | [diff] [blame] | 71 | use_goma = True |
| 72 | continue |
| 73 | match_use_jumbo_build = re.match( |
| 74 | r'^\s*use_jumbo_build\s*=\s*true(\s*$|\s*#.*$)', line) |
| 75 | if match_use_jumbo_build: |
| 76 | use_jumbo_build = True |
| 77 | continue |
Sam Clegg | 3a96d62 | 2019-08-22 21:22:04 +0000 | [diff] [blame] | 78 | elif os.path.exists(os.path.join(output_dir, 'rules.ninja')): |
| 79 | with open(os.path.join(output_dir, 'rules.ninja')) as file_handle: |
| 80 | for line in file_handle: |
| 81 | if re.match(r'^\s*command\s*=\s*\S+gomacc', line): |
| 82 | use_goma = True |
| 83 | break |
| 84 | |
Bruce Dawson | 6a86032 | 2019-09-09 18:03:40 +0000 | [diff] [blame] | 85 | # If GOMA_DISABLED is set to "true", "t", "yes", "y", or "1" (case-insensitive) |
| 86 | # then gomacc will use the local compiler instead of doing a goma compile. This |
| 87 | # is convenient if you want to briefly disable goma. It avoids having to rebuild |
| 88 | # the world when transitioning between goma/non-goma builds. However, it is not |
| 89 | # as fast as doing a "normal" non-goma build because an extra process is created |
| 90 | # for each compile step. Checking this environment variable ensures that |
| 91 | # autoninja uses an appropriate -j value in this situation. |
| 92 | goma_disabled_env = os.environ.get('GOMA_DISABLED', '0').lower() |
| 93 | if goma_disabled_env in ['true', 't', 'yes', 'y', '1']: |
Sam Clegg | 3a96d62 | 2019-08-22 21:22:04 +0000 | [diff] [blame] | 94 | use_goma = False |
Bruce Dawson | ebebd95 | 2017-05-31 14:24:38 -0700 | [diff] [blame] | 95 | |
Yoshisato Yanagisawa | 4b49707 | 2018-11-07 02:52:33 +0000 | [diff] [blame] | 96 | # Specify ninja.exe on Windows so that ninja.bat can call autoninja and not |
| 97 | # be called back. |
| 98 | ninja_exe = 'ninja.exe' if sys.platform.startswith('win') else 'ninja' |
Allen Bauer | 75fa855 | 2018-11-07 22:43:39 +0000 | [diff] [blame] | 99 | ninja_exe_path = os.path.join(SCRIPT_DIR, ninja_exe) |
| 100 | |
Yoshisato Yanagisawa | 4b49707 | 2018-11-07 02:52:33 +0000 | [diff] [blame] | 101 | # Use absolute path for ninja path, |
| 102 | # or fail to execute ninja if depot_tools is not in PATH. |
Allen Bauer | 75fa855 | 2018-11-07 22:43:39 +0000 | [diff] [blame] | 103 | args = [ninja_exe_path] + input_args[1:] |
Bruce Dawson | ebebd95 | 2017-05-31 14:24:38 -0700 | [diff] [blame] | 104 | |
Gabriel Charette | 125f7cc | 2019-06-20 19:19:35 +0000 | [diff] [blame] | 105 | num_cores = psutil.cpu_count() |
Bruce Dawson | 1f767e1 | 2017-07-07 15:10:37 -0700 | [diff] [blame] | 106 | if not j_specified and not t_specified: |
Bruce Dawson | ebebd95 | 2017-05-31 14:24:38 -0700 | [diff] [blame] | 107 | if use_goma: |
| 108 | args.append('-j') |
Gabriel Charette | 125f7cc | 2019-06-20 19:19:35 +0000 | [diff] [blame] | 109 | core_multiplier = int(os.environ.get('NINJA_CORE_MULTIPLIER', '40')) |
Takuto Ikuta | 1206a35 | 2019-02-07 22:18:08 +0000 | [diff] [blame] | 110 | j_value = num_cores * core_multiplier |
| 111 | |
| 112 | if sys.platform.startswith('win'): |
| 113 | # On windows, j value higher than 1000 does not improve build performance. |
| 114 | j_value = min(j_value, 1000) |
Takuto Ikuta | da4dbf8 | 2019-03-04 03:21:58 +0000 | [diff] [blame] | 115 | elif sys.platform == 'darwin': |
| 116 | # On Mac, j value higher than 500 causes 'Too many open files' error |
| 117 | # (crbug.com/936864). |
| 118 | j_value = min(j_value, 500) |
Takuto Ikuta | 1206a35 | 2019-02-07 22:18:08 +0000 | [diff] [blame] | 119 | |
| 120 | args.append('%d' % j_value) |
Bruce Dawson | ebebd95 | 2017-05-31 14:24:38 -0700 | [diff] [blame] | 121 | else: |
Gabriel Charette | 125f7cc | 2019-06-20 19:19:35 +0000 | [diff] [blame] | 122 | j_value = num_cores |
| 123 | # Ninja defaults to |num_cores + 2| |
| 124 | j_value += int(os.environ.get('NINJA_CORE_ADDITION', '2')) |
| 125 | if use_jumbo_build: |
| 126 | # Compiling a jumbo .o can easily use 1-2GB of memory. Leaving 2GB per |
| 127 | # process avoids memory swap/compression storms when also considering |
| 128 | # already in-use memory. |
| 129 | physical_ram = psutil.virtual_memory().total |
| 130 | GB = 1024 * 1024 * 1024 |
| 131 | j_value = min(j_value, physical_ram / (2 * GB)) |
| 132 | args.append('-j') |
| 133 | args.append('%d' % j_value) |
Bruce Dawson | ebebd95 | 2017-05-31 14:24:38 -0700 | [diff] [blame] | 134 | |
Yoshisato Yanagisawa | 43a35d2 | 2018-11-15 03:00:51 +0000 | [diff] [blame] | 135 | # On Windows, fully quote the path so that the command processor doesn't think |
| 136 | # the whole output is the command. |
| 137 | # On Linux and Mac, if people put depot_tools in directories with ' ', |
| 138 | # shell would misunderstand ' ' as a path separation. |
| 139 | # TODO(yyanagisawa): provide proper quating for Windows. |
| 140 | # see https://cs.chromium.org/chromium/src/tools/mb/mb.py |
| 141 | for i in range(len(args)): |
| 142 | if (i == 0 and sys.platform.startswith('win')) or ' ' in args[i]: |
| 143 | args[i] = '"%s"' % args[i].replace('"', '\\"') |
| 144 | |
Bruce Dawson | b3b46a2 | 2019-09-06 15:57:52 +0000 | [diff] [blame] | 145 | if os.environ.get('NINJA_SUMMARIZE_BUILD', '0') == '1': |
| 146 | args += ['-d', 'stats'] |
| 147 | |
Raul Tambre | 80ee78e | 2019-05-06 22:41:05 +0000 | [diff] [blame] | 148 | print(' '.join(args)) |