blob: 82fc15f916ea89185058fb782668eff32dc31ab5 [file] [log] [blame]
Ramy Medhatbbf25672019-07-17 12:30:04 +00001// Copyright 2019 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package build
16
17import (
Ramy Medhatca1e44c2020-07-16 12:18:37 -040018 "fmt"
Patrice Arruda62f1bf22020-07-07 12:48:26 +000019 "os"
Ramy Medhatbbf25672019-07-17 12:30:04 +000020 "path/filepath"
Kousik Kumar35faaef2022-01-13 02:13:10 -050021 "runtime"
Kousik Kumar7bc78192022-04-27 14:52:56 -040022 "strings"
Ramy Medhatbbf25672019-07-17 12:30:04 +000023
24 "android/soong/ui/metrics"
25)
26
Patrice Arruda62f1bf22020-07-07 12:48:26 +000027const (
28 rbeLeastNProcs = 2500
29 rbeLeastNFiles = 16000
30
31 // prebuilt RBE binaries
32 bootstrapCmd = "bootstrap"
33
34 // RBE metrics proto buffer file
35 rbeMetricsPBFilename = "rbe_metrics.pb"
Kousik Kumara0a44a82020-10-08 02:33:29 -040036
37 defaultOutDir = "out"
Patrice Arruda62f1bf22020-07-07 12:48:26 +000038)
39
40func rbeCommand(ctx Context, config Config, rbeCmd string) string {
41 var cmdPath string
Ramy Medhat0fc67eb2020-08-12 01:26:23 -040042 if rbeDir := config.rbeDir(); rbeDir != "" {
Patrice Arruda62f1bf22020-07-07 12:48:26 +000043 cmdPath = filepath.Join(rbeDir, rbeCmd)
Patrice Arruda62f1bf22020-07-07 12:48:26 +000044 } else {
45 ctx.Fatalf("rbe command path not found")
46 }
47
48 if _, err := os.Stat(cmdPath); err != nil && os.IsNotExist(err) {
49 ctx.Fatalf("rbe command %q not found", rbeCmd)
50 }
51
52 return cmdPath
53}
Ramy Medhatbbf25672019-07-17 12:30:04 +000054
Ramy Medhat81b3a832020-08-28 23:53:02 -040055func getRBEVars(ctx Context, config Config) map[string]string {
Ramy Medhat0fc67eb2020-08-12 01:26:23 -040056 vars := map[string]string{
Kousik Kumar4c180ad2022-05-27 07:48:37 -040057 "RBE_log_dir": config.rbeProxyLogsDir(),
Ramy Medhata958d352020-08-13 22:53:42 -040058 "RBE_re_proxy": config.rbeReproxy(),
59 "RBE_exec_root": config.rbeExecRoot(),
Kousik Kumar4c180ad2022-05-27 07:48:37 -040060 "RBE_output_dir": config.rbeProxyLogsDir(),
61 "RBE_proxy_log_dir": config.rbeProxyLogsDir(),
Ramy Medhata958d352020-08-13 22:53:42 -040062 }
63 if config.StartRBE() {
Kousik Kumar4c180ad2022-05-27 07:48:37 -040064 name, err := config.rbeSockAddr(absPath(ctx, config.TempDir()))
Ramy Medhat81b3a832020-08-28 23:53:02 -040065 if err != nil {
66 ctx.Fatalf("Error retrieving socket address: %v", err)
67 return nil
68 }
69 vars["RBE_server_address"] = fmt.Sprintf("unix://%v", name)
Ramy Medhat0fc67eb2020-08-12 01:26:23 -040070 }
Kousik Kumar35faaef2022-01-13 02:13:10 -050071
72 rf := 1.0
73 if config.Parallel() < runtime.NumCPU() {
74 rf = float64(config.Parallel()) / float64(runtime.NumCPU())
75 }
76 vars["RBE_local_resource_fraction"] = fmt.Sprintf("%.2f", rf)
77
Ramy Medhat0fc67eb2020-08-12 01:26:23 -040078 k, v := config.rbeAuth()
79 vars[k] = v
80 return vars
Ramy Medhatca1e44c2020-07-16 12:18:37 -040081}
82
Kousik Kumar4c180ad2022-05-27 07:48:37 -040083func cleanupRBELogsDir(ctx Context, config Config) {
84 if !config.shouldCleanupRBELogsDir() {
85 return
86 }
87
88 rbeTmpDir := config.rbeProxyLogsDir()
89 if err := os.RemoveAll(rbeTmpDir); err != nil {
90 fmt.Fprintln(ctx.Writer, "\033[33mUnable to remove RBE log directory: ", err, "\033[0m")
91 }
92}
93
Ramy Medhatbbf25672019-07-17 12:30:04 +000094func startRBE(ctx Context, config Config) {
95 ctx.BeginTrace(metrics.RunSetupTool, "rbe_bootstrap")
96 defer ctx.EndTrace()
97
98 if u := ulimitOrFatal(ctx, config, "-u"); u < rbeLeastNProcs {
99 ctx.Fatalf("max user processes is insufficient: %d; want >= %d.\n", u, rbeLeastNProcs)
100 }
101 if n := ulimitOrFatal(ctx, config, "-n"); n < rbeLeastNFiles {
102 ctx.Fatalf("max open files is insufficient: %d; want >= %d.\n", n, rbeLeastNFiles)
103 }
Kousik Kumar4c180ad2022-05-27 07:48:37 -0400104 if _, err := os.Stat(config.rbeProxyLogsDir()); os.IsNotExist(err) {
105 if err := os.MkdirAll(config.rbeProxyLogsDir(), 0744); err != nil {
106 ctx.Fatalf("Unable to create logs dir (%v) for RBE: %v", config.rbeProxyLogsDir, err)
107 }
108 }
Ramy Medhatbbf25672019-07-17 12:30:04 +0000109
Patrice Arruda62f1bf22020-07-07 12:48:26 +0000110 cmd := Command(ctx, config, "startRBE bootstrap", rbeCommand(ctx, config, bootstrapCmd))
Ramy Medhatbbf25672019-07-17 12:30:04 +0000111
112 if output, err := cmd.CombinedOutput(); err != nil {
Kousik Kumar1e4d5f32021-01-26 14:30:53 -0500113 ctx.Fatalf("Unable to start RBE reproxy\nFAILED: RBE bootstrap failed with: %v\n%s\n", err, output)
Ramy Medhatbbf25672019-07-17 12:30:04 +0000114 }
115}
Patrice Arruda62f1bf22020-07-07 12:48:26 +0000116
117func stopRBE(ctx Context, config Config) {
118 cmd := Command(ctx, config, "stopRBE bootstrap", rbeCommand(ctx, config, bootstrapCmd), "-shutdown")
Kousik Kumar20810522021-03-21 22:35:26 -0400119 output, err := cmd.CombinedOutput()
120 if err != nil {
Patrice Arruda62f1bf22020-07-07 12:48:26 +0000121 ctx.Fatalf("rbe bootstrap with shutdown failed with: %v\n%s\n", err, output)
122 }
Kousik Kumar20810522021-03-21 22:35:26 -0400123
Peter Collingbournef4d9bd22021-04-20 20:58:19 -0700124 if !config.Environment().IsEnvTrue("ANDROID_QUIET_BUILD") && len(output) > 0 {
Kousik Kumar20810522021-03-21 22:35:26 -0400125 fmt.Fprintln(ctx.Writer, "")
126 fmt.Fprintln(ctx.Writer, fmt.Sprintf("%s", output))
127 }
Patrice Arruda62f1bf22020-07-07 12:48:26 +0000128}
129
Kousik Kumar7bc78192022-04-27 14:52:56 -0400130func prodCredsAuthType(config Config) bool {
131 authVar, val := config.rbeAuth()
132 if strings.Contains(authVar, "use_google_prod_creds") && val != "" && val != "false" {
133 return true
134 }
135 return false
136}
137
138// Check whether proper auth exists for RBE builds run within a
139// Google dev environment.
140func CheckProdCreds(ctx Context, config Config) {
141 if !config.IsGooglerEnvironment() {
142 return
143 }
144 if !config.StubbyExists() && prodCredsAuthType(config) {
145 fmt.Fprintln(ctx.Writer, "")
146 fmt.Fprintln(ctx.Writer, fmt.Sprintf("\033[33mWARNING: %q binary not found in $PATH, follow go/build-fast#opting-out-of-loas-credentials instead for authenticating with RBE.\033[0m", "stubby"))
147 fmt.Fprintln(ctx.Writer, "")
148 return
149 }
150 if config.GoogleProdCredsExist() {
151 return
152 }
153 fmt.Fprintln(ctx.Writer, "")
154 fmt.Fprintln(ctx.Writer, "\033[33mWARNING: Missing LOAS credentials, please run `gcert`. This will result in failing builds in the future, see go/rbe-android-default-announcement.\033[0m")
155 fmt.Fprintln(ctx.Writer, "")
156}
157
Patrice Arruda62f1bf22020-07-07 12:48:26 +0000158// DumpRBEMetrics creates a metrics protobuf file containing RBE related metrics.
159// The protobuf file is created if RBE is enabled and the proxy service has
160// started. The proxy service is shutdown in order to dump the RBE metrics to the
161// protobuf file.
162func DumpRBEMetrics(ctx Context, config Config, filename string) {
163 ctx.BeginTrace(metrics.RunShutdownTool, "dump_rbe_metrics")
164 defer ctx.EndTrace()
165
166 // Remove the previous metrics file in case there is a failure or RBE has been
167 // disable for this run.
168 os.Remove(filename)
169
170 // If RBE is not enabled then there are no metrics to generate.
171 // If RBE does not require to start, the RBE proxy maybe started
172 // manually for debugging purpose and can generate the metrics
173 // afterwards.
174 if !config.StartRBE() {
175 return
176 }
177
Kousik Kumar4c180ad2022-05-27 07:48:37 -0400178 outputDir := config.rbeProxyLogsDir()
Patrice Arruda62f1bf22020-07-07 12:48:26 +0000179 if outputDir == "" {
180 ctx.Fatal("RBE output dir variable not defined. Aborting metrics dumping.")
181 }
182 metricsFile := filepath.Join(outputDir, rbeMetricsPBFilename)
183
184 // Stop the proxy first in order to generate the RBE metrics protobuf file.
185 stopRBE(ctx, config)
186
Ramy Medhat0fc67eb2020-08-12 01:26:23 -0400187 if metricsFile == filename {
188 return
189 }
Patrice Arruda62f1bf22020-07-07 12:48:26 +0000190 if _, err := copyFile(metricsFile, filename); err != nil {
191 ctx.Fatalf("failed to copy %q to %q: %v\n", metricsFile, filename, err)
192 }
193}
Kousik Kumara0a44a82020-10-08 02:33:29 -0400194
195// PrintOutDirWarning prints a warning to indicate to the user that
196// setting output directory to a path other than "out" in an RBE enabled
197// build can cause slow builds.
198func PrintOutDirWarning(ctx Context, config Config) {
199 if config.UseRBE() && config.OutDir() != defaultOutDir {
200 fmt.Fprintln(ctx.Writer, "")
201 fmt.Fprintln(ctx.Writer, "\033[33mWARNING:\033[0m")
202 fmt.Fprintln(ctx.Writer, fmt.Sprintf("Setting OUT_DIR to a path other than %v may result in slow RBE builds.", defaultOutDir))
203 fmt.Fprintln(ctx.Writer, "See http://go/android_rbe_out_dir for a workaround.")
204 fmt.Fprintln(ctx.Writer, "")
205 }
206}