xref: /aosp_15_r20/external/cronet/build/toolchain/concurrent_links.gni (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1# Copyright 2016 The Chromium Authors
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5# This file should only be imported from files that define toolchains.
6# There's no way to enforce this exactly, but all toolchains are processed
7# in the context of the default_toolchain, so we can at least check for that.
8assert(current_toolchain == default_toolchain)
9
10import("//build/config/android/config.gni")
11import("//build/config/apple/symbols.gni")
12import("//build/config/chromeos/ui_mode.gni")
13import("//build/config/compiler/compiler.gni")
14import("//build/config/coverage/coverage.gni")
15import("//build/config/sanitizers/sanitizers.gni")
16import("//build/toolchain/toolchain.gni")
17
18declare_args() {
19  # Limit the number of concurrent links; we often want to run fewer
20  # links at once than we do compiles, because linking is memory-intensive.
21  # The default to use varies by platform and by the amount of memory
22  # available, so we call out to a script to get the right value.
23  concurrent_links = -1
24}
25
26if (concurrent_links == -1) {
27  if (use_thin_lto) {
28    _args = [
29      "--reserve_mem_gb=10",
30      "--thin-lto=local",
31    ]
32    if (is_win) {
33      # Based on measurements of linking chrome.dll and chrome_child.dll, plus
34      # a little padding to account for future growth.
35      _args += [ "--mem_per_link_gb=45" ]
36    } else if (is_android) {
37      # Large solink of Android official builds take 30-60GB.
38      _args += [ "--mem_per_link_gb=30" ]
39    } else {
40      _args += [ "--mem_per_link_gb=20" ]
41    }
42  } else if ((use_clang_coverage &&
43              # When coverage_instrumentation_input_file is not empty it means
44              # we're only instrumenting changed files and not using a lot of
45              # memory. Likewise, when it's empty we're building everything with
46              # coverage, which requires more memory.
47              coverage_instrumentation_input_file == "") ||
48             use_sanitizer_coverage || use_fuzzing_engine) {
49    # Full sanitizer coverage instrumentation increases linker memory consumption
50    # significantly.
51    _args = [ "--mem_per_link_gb=16" ]
52  } else if (is_win && symbol_level == 1 && !is_debug && is_component_build) {
53    _args = [ "--mem_per_link_gb=3" ]
54  } else if (is_win) {
55    _args = [ "--mem_per_link_gb=6" ]
56  } else if (is_mac) {
57    if (enable_dsyms) {
58      _args = [ "--mem_per_link_gb=12" ]
59    } else {
60      _args = [ "--mem_per_link_gb=4" ]
61    }
62  } else if (is_android && !is_component_build && symbol_level == 2) {
63    # Full debug symbols require large memory for link.
64    _args = [ "--mem_per_link_gb=25" ]
65  } else if (is_android && !is_debug && !using_sanitizer && symbol_level < 2) {
66    if (symbol_level == 1) {
67      _args = [ "--mem_per_link_gb=6" ]
68    } else {
69      _args = [ "--mem_per_link_gb=4" ]
70    }
71  } else if ((is_linux || is_chromeos_lacros) && symbol_level == 0) {
72    # Memory consumption on link without debug symbols is low on linux.
73    _args = [ "--mem_per_link_gb=3" ]
74  } else if (current_os == "zos") {
75    _args = [ "--mem_per_link_gb=1" ]
76  } else if (is_fuchsia) {
77    # TODO(crbug.com/1347159): This was defaulting to 8GB. The number of
78    #    linker instances to run in parallel is calculated by diviging
79    #    the available memory by this value. On a 32GB machine with
80    #    roughly 29GB of available memory, this would cause three instances
81    #    to run. This started running out of memory and thrashing. This change
82    #    addresses that issue to get the SDk rollers running again but
83    #    could be optimized (maybe to 12GB or for different configs like
84    #    component build).
85    _args = [ "--mem_per_link_gb=16" ]
86  } else if (is_chromeos && is_msan) {
87    # crbug.com/1505350 - CrOS MSan builder consumes more memory and crushes.
88    # Max 25.2GB, Avg: 9.4GB, Median: 7.9GB
89    _args = [ "--mem_per_link_gb=12" ]
90  } else if (is_chromeos && is_debug) {
91    # b/315102033, b/312072730: Large links use 9GB-13.5GB.
92    _args = [ "--mem_per_link_gb=10" ]
93  } else {
94    _args = []
95  }
96
97  # For Android builds, we also need to be wary of:
98  # * ProGuard / R8
99  # * Android Lint
100  # These both have a peak usage of < 2GB, but that is still large enough for
101  # them to need to use a pool since they both typically happen at the
102  # same time as linking.
103  if (is_android) {
104    _args += [ "--secondary_mem_per_link=2" ]
105  }
106
107  # TODO(crbug.com/617429) Pass more build configuration info to the script
108  # so that we can compute better values.
109  _command_dict = exec_script("get_concurrent_links.py", _args, "scope")
110
111  concurrent_links = _command_dict.primary_pool_size
112  concurrent_links_logs = _command_dict.explanation
113
114  if (_command_dict.secondary_pool_size >= concurrent_links) {
115    # Have R8 / Lint share the link pool unless we would safely get more
116    # concurrency out of using a separate one.
117    # On low-RAM machines, this allows an apk's native library to link at the
118    # same time as its java is optimized with R8.
119    java_cmd_pool_size = _command_dict.secondary_pool_size
120  }
121} else {
122  assert(!use_thin_lto, "can't explicitly set concurrent_links with thinlto")
123  concurrent_links_logs =
124      [ "concurrent_links set by GN arg (value=$concurrent_links)" ]
125}
126