blob: 3e558f7387acfe8021d2e9d77118b65a582bc0a3 [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"
Ramy Medhatbbf25672019-07-17 12:30:04 +000022
23 "android/soong/ui/metrics"
24)
25
Patrice Arruda62f1bf22020-07-07 12:48:26 +000026const (
27 rbeLeastNProcs = 2500
28 rbeLeastNFiles = 16000
29
30 // prebuilt RBE binaries
31 bootstrapCmd = "bootstrap"
32
33 // RBE metrics proto buffer file
34 rbeMetricsPBFilename = "rbe_metrics.pb"
Kousik Kumara0a44a82020-10-08 02:33:29 -040035
36 defaultOutDir = "out"
Patrice Arruda62f1bf22020-07-07 12:48:26 +000037)
38
39func rbeCommand(ctx Context, config Config, rbeCmd string) string {
40 var cmdPath string
Ramy Medhat0fc67eb2020-08-12 01:26:23 -040041 if rbeDir := config.rbeDir(); rbeDir != "" {
Patrice Arruda62f1bf22020-07-07 12:48:26 +000042 cmdPath = filepath.Join(rbeDir, rbeCmd)
Patrice Arruda62f1bf22020-07-07 12:48:26 +000043 } else {
44 ctx.Fatalf("rbe command path not found")
45 }
46
47 if _, err := os.Stat(cmdPath); err != nil && os.IsNotExist(err) {
48 ctx.Fatalf("rbe command %q not found", rbeCmd)
49 }
50
51 return cmdPath
52}
Ramy Medhatbbf25672019-07-17 12:30:04 +000053
Ramy Medhat81b3a832020-08-28 23:53:02 -040054func getRBEVars(ctx Context, config Config) map[string]string {
Ramy Medhat0fc67eb2020-08-12 01:26:23 -040055 vars := map[string]string{
Kousik Kumar4c180ad2022-05-27 07:48:37 -040056 "RBE_log_dir": config.rbeProxyLogsDir(),
Ramy Medhata958d352020-08-13 22:53:42 -040057 "RBE_re_proxy": config.rbeReproxy(),
58 "RBE_exec_root": config.rbeExecRoot(),
Kousik Kumar4c180ad2022-05-27 07:48:37 -040059 "RBE_output_dir": config.rbeProxyLogsDir(),
60 "RBE_proxy_log_dir": config.rbeProxyLogsDir(),
Ramy Medhata958d352020-08-13 22:53:42 -040061 }
62 if config.StartRBE() {
Kousik Kumar4c180ad2022-05-27 07:48:37 -040063 name, err := config.rbeSockAddr(absPath(ctx, config.TempDir()))
Ramy Medhat81b3a832020-08-28 23:53:02 -040064 if err != nil {
65 ctx.Fatalf("Error retrieving socket address: %v", err)
66 return nil
67 }
68 vars["RBE_server_address"] = fmt.Sprintf("unix://%v", name)
Ramy Medhat0fc67eb2020-08-12 01:26:23 -040069 }
Kousik Kumar35faaef2022-01-13 02:13:10 -050070
71 rf := 1.0
72 if config.Parallel() < runtime.NumCPU() {
73 rf = float64(config.Parallel()) / float64(runtime.NumCPU())
74 }
75 vars["RBE_local_resource_fraction"] = fmt.Sprintf("%.2f", rf)
76
Ramy Medhat0fc67eb2020-08-12 01:26:23 -040077 k, v := config.rbeAuth()
78 vars[k] = v
79 return vars
Ramy Medhatca1e44c2020-07-16 12:18:37 -040080}
81
Kousik Kumar4c180ad2022-05-27 07:48:37 -040082func cleanupRBELogsDir(ctx Context, config Config) {
83 if !config.shouldCleanupRBELogsDir() {
84 return
85 }
86
87 rbeTmpDir := config.rbeProxyLogsDir()
88 if err := os.RemoveAll(rbeTmpDir); err != nil {
89 fmt.Fprintln(ctx.Writer, "\033[33mUnable to remove RBE log directory: ", err, "\033[0m")
90 }
91}
92
Ramy Medhatbbf25672019-07-17 12:30:04 +000093func startRBE(ctx Context, config Config) {
94 ctx.BeginTrace(metrics.RunSetupTool, "rbe_bootstrap")
95 defer ctx.EndTrace()
96
97 if u := ulimitOrFatal(ctx, config, "-u"); u < rbeLeastNProcs {
98 ctx.Fatalf("max user processes is insufficient: %d; want >= %d.\n", u, rbeLeastNProcs)
99 }
100 if n := ulimitOrFatal(ctx, config, "-n"); n < rbeLeastNFiles {
101 ctx.Fatalf("max open files is insufficient: %d; want >= %d.\n", n, rbeLeastNFiles)
102 }
Kousik Kumar4c180ad2022-05-27 07:48:37 -0400103 if _, err := os.Stat(config.rbeProxyLogsDir()); os.IsNotExist(err) {
104 if err := os.MkdirAll(config.rbeProxyLogsDir(), 0744); err != nil {
105 ctx.Fatalf("Unable to create logs dir (%v) for RBE: %v", config.rbeProxyLogsDir, err)
106 }
107 }
Ramy Medhatbbf25672019-07-17 12:30:04 +0000108
Patrice Arruda62f1bf22020-07-07 12:48:26 +0000109 cmd := Command(ctx, config, "startRBE bootstrap", rbeCommand(ctx, config, bootstrapCmd))
Ramy Medhatbbf25672019-07-17 12:30:04 +0000110
111 if output, err := cmd.CombinedOutput(); err != nil {
Kousik Kumar1e4d5f32021-01-26 14:30:53 -0500112 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 +0000113 }
114}
Patrice Arruda62f1bf22020-07-07 12:48:26 +0000115
116func stopRBE(ctx Context, config Config) {
117 cmd := Command(ctx, config, "stopRBE bootstrap", rbeCommand(ctx, config, bootstrapCmd), "-shutdown")
Kousik Kumar20810522021-03-21 22:35:26 -0400118 output, err := cmd.CombinedOutput()
119 if err != nil {
Patrice Arruda62f1bf22020-07-07 12:48:26 +0000120 ctx.Fatalf("rbe bootstrap with shutdown failed with: %v\n%s\n", err, output)
121 }
Kousik Kumar20810522021-03-21 22:35:26 -0400122
Peter Collingbournef4d9bd22021-04-20 20:58:19 -0700123 if !config.Environment().IsEnvTrue("ANDROID_QUIET_BUILD") && len(output) > 0 {
Kousik Kumar20810522021-03-21 22:35:26 -0400124 fmt.Fprintln(ctx.Writer, "")
125 fmt.Fprintln(ctx.Writer, fmt.Sprintf("%s", output))
126 }
Patrice Arruda62f1bf22020-07-07 12:48:26 +0000127}
128
129// DumpRBEMetrics creates a metrics protobuf file containing RBE related metrics.
130// The protobuf file is created if RBE is enabled and the proxy service has
131// started. The proxy service is shutdown in order to dump the RBE metrics to the
132// protobuf file.
133func DumpRBEMetrics(ctx Context, config Config, filename string) {
134 ctx.BeginTrace(metrics.RunShutdownTool, "dump_rbe_metrics")
135 defer ctx.EndTrace()
136
137 // Remove the previous metrics file in case there is a failure or RBE has been
138 // disable for this run.
139 os.Remove(filename)
140
141 // If RBE is not enabled then there are no metrics to generate.
142 // If RBE does not require to start, the RBE proxy maybe started
143 // manually for debugging purpose and can generate the metrics
144 // afterwards.
145 if !config.StartRBE() {
146 return
147 }
148
Kousik Kumar4c180ad2022-05-27 07:48:37 -0400149 outputDir := config.rbeProxyLogsDir()
Patrice Arruda62f1bf22020-07-07 12:48:26 +0000150 if outputDir == "" {
151 ctx.Fatal("RBE output dir variable not defined. Aborting metrics dumping.")
152 }
153 metricsFile := filepath.Join(outputDir, rbeMetricsPBFilename)
154
155 // Stop the proxy first in order to generate the RBE metrics protobuf file.
156 stopRBE(ctx, config)
157
Ramy Medhat0fc67eb2020-08-12 01:26:23 -0400158 if metricsFile == filename {
159 return
160 }
Patrice Arruda62f1bf22020-07-07 12:48:26 +0000161 if _, err := copyFile(metricsFile, filename); err != nil {
162 ctx.Fatalf("failed to copy %q to %q: %v\n", metricsFile, filename, err)
163 }
164}
Kousik Kumara0a44a82020-10-08 02:33:29 -0400165
166// PrintOutDirWarning prints a warning to indicate to the user that
167// setting output directory to a path other than "out" in an RBE enabled
168// build can cause slow builds.
169func PrintOutDirWarning(ctx Context, config Config) {
170 if config.UseRBE() && config.OutDir() != defaultOutDir {
171 fmt.Fprintln(ctx.Writer, "")
172 fmt.Fprintln(ctx.Writer, "\033[33mWARNING:\033[0m")
173 fmt.Fprintln(ctx.Writer, fmt.Sprintf("Setting OUT_DIR to a path other than %v may result in slow RBE builds.", defaultOutDir))
174 fmt.Fprintln(ctx.Writer, "See http://go/android_rbe_out_dir for a workaround.")
175 fmt.Fprintln(ctx.Writer, "")
176 }
177}