blob: d069452ddc54d75a29fb3fadcd519a55273d28de [file] [log] [blame]
Bruce Dawsonebebd952017-05-31 14:24:38 -07001# 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"""
6This script (intended to be invoked by autoninja or autoninja.bat) detects
7whether a build is using goma. If so it runs with a large -j value, and
8otherwise it chooses a small one. This auto-adjustment makes using goma simpler
9and safer, and avoids errors that can cause slow goma builds or swap-storms
10on non-goma builds.
11"""
12
13import multiprocessing
14import os
15import re
16import sys
17
Bruce Dawson1f767e12017-07-07 15:10:37 -070018# The -t tools are incompatible with -j and -l
19t_specified = False
Bruce Dawsonebebd952017-05-31 14:24:38 -070020j_specified = False
21output_dir = '.'
22for index, arg in enumerate(sys.argv[1:]):
23 if arg == '-j':
24 j_specified = True
Bruce Dawson1f767e12017-07-07 15:10:37 -070025 if arg == '-t':
26 t_specified = True
Bruce Dawsonebebd952017-05-31 14:24:38 -070027 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
31use_goma = False
32try:
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
39except IOError:
40 pass
41
42if 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:]
46else:
47 args = ['ninja'] + sys.argv[1:]
48
49num_cores = multiprocessing.cpu_count()
Bruce Dawson1f767e12017-07-07 15:10:37 -070050if not j_specified and not t_specified:
Bruce Dawsonebebd952017-05-31 14:24:38 -070051 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 Dawson1f767e12017-07-07 15:10:37 -070062if 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 Dawsonebebd952017-05-31 14:24:38 -070069
70print ' '.join(args)