blob: 1d172162391d4a02d4fc72092afa9183688a541b [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
David Srbecky2285cff2022-07-12 16:26:37 +010024 "android/soong/remoteexec"
Ramy Medhatbbf25672019-07-17 12:30:04 +000025 "android/soong/ui/metrics"
26)
27
Patrice Arruda62f1bf22020-07-07 12:48:26 +000028const (
29 rbeLeastNProcs = 2500
30 rbeLeastNFiles = 16000
31
32 // prebuilt RBE binaries
33 bootstrapCmd = "bootstrap"
34
35 // RBE metrics proto buffer file
36 rbeMetricsPBFilename = "rbe_metrics.pb"
Kousik Kumara0a44a82020-10-08 02:33:29 -040037
38 defaultOutDir = "out"
Patrice Arruda62f1bf22020-07-07 12:48:26 +000039)
40
41func rbeCommand(ctx Context, config Config, rbeCmd string) string {
42 var cmdPath string
Ramy Medhat0fc67eb2020-08-12 01:26:23 -040043 if rbeDir := config.rbeDir(); rbeDir != "" {
Patrice Arruda62f1bf22020-07-07 12:48:26 +000044 cmdPath = filepath.Join(rbeDir, rbeCmd)
Patrice Arruda62f1bf22020-07-07 12:48:26 +000045 } else {
46 ctx.Fatalf("rbe command path not found")
47 }
48
49 if _, err := os.Stat(cmdPath); err != nil && os.IsNotExist(err) {
50 ctx.Fatalf("rbe command %q not found", rbeCmd)
51 }
52
53 return cmdPath
54}
Ramy Medhatbbf25672019-07-17 12:30:04 +000055
Ramy Medhat81b3a832020-08-28 23:53:02 -040056func getRBEVars(ctx Context, config Config) map[string]string {
Ramy Medhat0fc67eb2020-08-12 01:26:23 -040057 vars := map[string]string{
David Srbecky2285cff2022-07-12 16:26:37 +010058 "RBE_log_dir": config.rbeProxyLogsDir(),
59 "RBE_re_proxy": config.rbeReproxy(),
60 "RBE_exec_root": config.rbeExecRoot(),
61 "RBE_output_dir": config.rbeProxyLogsDir(),
Kousik Kumar4c180ad2022-05-27 07:48:37 -040062 "RBE_proxy_log_dir": config.rbeProxyLogsDir(),
David Srbecky2285cff2022-07-12 16:26:37 +010063 "RBE_platform": "container-image=" + remoteexec.DefaultImage,
Ramy Medhata958d352020-08-13 22:53:42 -040064 }
65 if config.StartRBE() {
Kousik Kumar4c180ad2022-05-27 07:48:37 -040066 name, err := config.rbeSockAddr(absPath(ctx, config.TempDir()))
Ramy Medhat81b3a832020-08-28 23:53:02 -040067 if err != nil {
68 ctx.Fatalf("Error retrieving socket address: %v", err)
69 return nil
70 }
71 vars["RBE_server_address"] = fmt.Sprintf("unix://%v", name)
Ramy Medhat0fc67eb2020-08-12 01:26:23 -040072 }
Kousik Kumar35faaef2022-01-13 02:13:10 -050073
74 rf := 1.0
75 if config.Parallel() < runtime.NumCPU() {
76 rf = float64(config.Parallel()) / float64(runtime.NumCPU())
77 }
78 vars["RBE_local_resource_fraction"] = fmt.Sprintf("%.2f", rf)
79
Ramy Medhat0fc67eb2020-08-12 01:26:23 -040080 k, v := config.rbeAuth()
81 vars[k] = v
82 return vars
Ramy Medhatca1e44c2020-07-16 12:18:37 -040083}
84
Kousik Kumar4c180ad2022-05-27 07:48:37 -040085func cleanupRBELogsDir(ctx Context, config Config) {
86 if !config.shouldCleanupRBELogsDir() {
87 return
88 }
89
90 rbeTmpDir := config.rbeProxyLogsDir()
91 if err := os.RemoveAll(rbeTmpDir); err != nil {
92 fmt.Fprintln(ctx.Writer, "\033[33mUnable to remove RBE log directory: ", err, "\033[0m")
93 }
94}
95
Ramy Medhatbbf25672019-07-17 12:30:04 +000096func startRBE(ctx Context, config Config) {
andusyu838086f2022-10-13 18:34:08 -040097 if !config.GoogleProdCredsExist() && prodCredsAuthType(config) {
98 ctx.Fatalf("Unable to start RBE reproxy\nFAILED: Missing LOAS credentials.")
99 }
Ramy Medhatbbf25672019-07-17 12:30:04 +0000100 ctx.BeginTrace(metrics.RunSetupTool, "rbe_bootstrap")
101 defer ctx.EndTrace()
102
Cole Faust64955b32023-02-16 12:53:32 -0800103 ctx.Status.Status("Starting rbe...")
104
Ramy Medhatbbf25672019-07-17 12:30:04 +0000105 if u := ulimitOrFatal(ctx, config, "-u"); u < rbeLeastNProcs {
106 ctx.Fatalf("max user processes is insufficient: %d; want >= %d.\n", u, rbeLeastNProcs)
107 }
108 if n := ulimitOrFatal(ctx, config, "-n"); n < rbeLeastNFiles {
109 ctx.Fatalf("max open files is insufficient: %d; want >= %d.\n", n, rbeLeastNFiles)
110 }
Kousik Kumar4c180ad2022-05-27 07:48:37 -0400111 if _, err := os.Stat(config.rbeProxyLogsDir()); os.IsNotExist(err) {
112 if err := os.MkdirAll(config.rbeProxyLogsDir(), 0744); err != nil {
113 ctx.Fatalf("Unable to create logs dir (%v) for RBE: %v", config.rbeProxyLogsDir, err)
114 }
115 }
Ramy Medhatbbf25672019-07-17 12:30:04 +0000116
Patrice Arruda62f1bf22020-07-07 12:48:26 +0000117 cmd := Command(ctx, config, "startRBE bootstrap", rbeCommand(ctx, config, bootstrapCmd))
Ramy Medhatbbf25672019-07-17 12:30:04 +0000118
119 if output, err := cmd.CombinedOutput(); err != nil {
Kousik Kumar1e4d5f32021-01-26 14:30:53 -0500120 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 +0000121 }
122}
Patrice Arruda62f1bf22020-07-07 12:48:26 +0000123
124func stopRBE(ctx Context, config Config) {
125 cmd := Command(ctx, config, "stopRBE bootstrap", rbeCommand(ctx, config, bootstrapCmd), "-shutdown")
Kousik Kumar20810522021-03-21 22:35:26 -0400126 output, err := cmd.CombinedOutput()
127 if err != nil {
Patrice Arruda62f1bf22020-07-07 12:48:26 +0000128 ctx.Fatalf("rbe bootstrap with shutdown failed with: %v\n%s\n", err, output)
129 }
Kousik Kumar20810522021-03-21 22:35:26 -0400130
Peter Collingbournef4d9bd22021-04-20 20:58:19 -0700131 if !config.Environment().IsEnvTrue("ANDROID_QUIET_BUILD") && len(output) > 0 {
Kousik Kumar20810522021-03-21 22:35:26 -0400132 fmt.Fprintln(ctx.Writer, "")
133 fmt.Fprintln(ctx.Writer, fmt.Sprintf("%s", output))
134 }
Patrice Arruda62f1bf22020-07-07 12:48:26 +0000135}
136
Kousik Kumar7bc78192022-04-27 14:52:56 -0400137func prodCredsAuthType(config Config) bool {
138 authVar, val := config.rbeAuth()
139 if strings.Contains(authVar, "use_google_prod_creds") && val != "" && val != "false" {
140 return true
141 }
142 return false
143}
144
145// Check whether proper auth exists for RBE builds run within a
146// Google dev environment.
147func CheckProdCreds(ctx Context, config Config) {
148 if !config.IsGooglerEnvironment() {
149 return
150 }
151 if !config.StubbyExists() && prodCredsAuthType(config) {
152 fmt.Fprintln(ctx.Writer, "")
Paul Crowley4965dc02022-11-29 13:08:06 -0800153 fmt.Fprintln(ctx.Writer, fmt.Sprintf("\033[33mWARNING: %q binary not found in $PATH, follow go/build-fast-without-stubby instead for authenticating with RBE.\033[0m", "stubby"))
Kousik Kumar7bc78192022-04-27 14:52:56 -0400154 fmt.Fprintln(ctx.Writer, "")
155 return
156 }
157 if config.GoogleProdCredsExist() {
158 return
159 }
160 fmt.Fprintln(ctx.Writer, "")
161 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")
162 fmt.Fprintln(ctx.Writer, "")
163}
164
Patrice Arruda62f1bf22020-07-07 12:48:26 +0000165// DumpRBEMetrics creates a metrics protobuf file containing RBE related metrics.
166// The protobuf file is created if RBE is enabled and the proxy service has
167// started. The proxy service is shutdown in order to dump the RBE metrics to the
168// protobuf file.
169func DumpRBEMetrics(ctx Context, config Config, filename string) {
170 ctx.BeginTrace(metrics.RunShutdownTool, "dump_rbe_metrics")
171 defer ctx.EndTrace()
172
173 // Remove the previous metrics file in case there is a failure or RBE has been
174 // disable for this run.
175 os.Remove(filename)
176
177 // If RBE is not enabled then there are no metrics to generate.
178 // If RBE does not require to start, the RBE proxy maybe started
179 // manually for debugging purpose and can generate the metrics
180 // afterwards.
181 if !config.StartRBE() {
182 return
183 }
184
Cole Faust64955b32023-02-16 12:53:32 -0800185 ctx.Status.Status("Dumping rbe metrics...")
186
Kousik Kumar4c180ad2022-05-27 07:48:37 -0400187 outputDir := config.rbeProxyLogsDir()
Patrice Arruda62f1bf22020-07-07 12:48:26 +0000188 if outputDir == "" {
189 ctx.Fatal("RBE output dir variable not defined. Aborting metrics dumping.")
190 }
191 metricsFile := filepath.Join(outputDir, rbeMetricsPBFilename)
192
193 // Stop the proxy first in order to generate the RBE metrics protobuf file.
194 stopRBE(ctx, config)
195
Ramy Medhat0fc67eb2020-08-12 01:26:23 -0400196 if metricsFile == filename {
197 return
198 }
Patrice Arruda62f1bf22020-07-07 12:48:26 +0000199 if _, err := copyFile(metricsFile, filename); err != nil {
200 ctx.Fatalf("failed to copy %q to %q: %v\n", metricsFile, filename, err)
201 }
202}
Kousik Kumara0a44a82020-10-08 02:33:29 -0400203
204// PrintOutDirWarning prints a warning to indicate to the user that
205// setting output directory to a path other than "out" in an RBE enabled
206// build can cause slow builds.
207func PrintOutDirWarning(ctx Context, config Config) {
208 if config.UseRBE() && config.OutDir() != defaultOutDir {
209 fmt.Fprintln(ctx.Writer, "")
210 fmt.Fprintln(ctx.Writer, "\033[33mWARNING:\033[0m")
211 fmt.Fprintln(ctx.Writer, fmt.Sprintf("Setting OUT_DIR to a path other than %v may result in slow RBE builds.", defaultOutDir))
212 fmt.Fprintln(ctx.Writer, "See http://go/android_rbe_out_dir for a workaround.")
213 fmt.Fprintln(ctx.Writer, "")
214 }
215}