blob: 8f9a69991ef4238f7a58d8d58490403c2b6ee824 [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"
19 "math/rand"
Patrice Arruda62f1bf22020-07-07 12:48:26 +000020 "os"
Ramy Medhatbbf25672019-07-17 12:30:04 +000021 "path/filepath"
Kousik Kumar35faaef2022-01-13 02:13:10 -050022 "runtime"
Ramy Medhat81b3a832020-08-28 23:53:02 -040023 "syscall"
Ramy Medhatca1e44c2020-07-16 12:18:37 -040024 "time"
Ramy Medhatbbf25672019-07-17 12:30:04 +000025
26 "android/soong/ui/metrics"
27)
28
Patrice Arruda62f1bf22020-07-07 12:48:26 +000029const (
30 rbeLeastNProcs = 2500
31 rbeLeastNFiles = 16000
32
33 // prebuilt RBE binaries
34 bootstrapCmd = "bootstrap"
35
36 // RBE metrics proto buffer file
37 rbeMetricsPBFilename = "rbe_metrics.pb"
Kousik Kumara0a44a82020-10-08 02:33:29 -040038
39 defaultOutDir = "out"
Patrice Arruda62f1bf22020-07-07 12:48:26 +000040)
41
42func rbeCommand(ctx Context, config Config, rbeCmd string) string {
43 var cmdPath string
Ramy Medhat0fc67eb2020-08-12 01:26:23 -040044 if rbeDir := config.rbeDir(); rbeDir != "" {
Patrice Arruda62f1bf22020-07-07 12:48:26 +000045 cmdPath = filepath.Join(rbeDir, rbeCmd)
Patrice Arruda62f1bf22020-07-07 12:48:26 +000046 } else {
47 ctx.Fatalf("rbe command path not found")
48 }
49
50 if _, err := os.Stat(cmdPath); err != nil && os.IsNotExist(err) {
51 ctx.Fatalf("rbe command %q not found", rbeCmd)
52 }
53
54 return cmdPath
55}
Ramy Medhatbbf25672019-07-17 12:30:04 +000056
Ramy Medhat81b3a832020-08-28 23:53:02 -040057func sockAddr(dir string) (string, error) {
58 maxNameLen := len(syscall.RawSockaddrUnix{}.Path)
Ramy Medhatca1e44c2020-07-16 12:18:37 -040059 rand.Seed(time.Now().UnixNano())
Ramy Medhat81b3a832020-08-28 23:53:02 -040060 base := fmt.Sprintf("reproxy_%v.sock", rand.Intn(1000))
61
62 name := filepath.Join(dir, base)
63 if len(name) < maxNameLen {
64 return name, nil
65 }
66
67 name = filepath.Join("/tmp", base)
68 if len(name) < maxNameLen {
69 return name, nil
70 }
71
72 return "", fmt.Errorf("cannot generate a proxy socket address shorter than the limit of %v", maxNameLen)
73}
74
75func getRBEVars(ctx Context, config Config) map[string]string {
Ramy Medhat0fc67eb2020-08-12 01:26:23 -040076 vars := map[string]string{
Ramy Medhata958d352020-08-13 22:53:42 -040077 "RBE_log_path": config.rbeLogPath(),
Rupert Shuttleworth3c9f5ac2020-12-10 11:32:38 +000078 "RBE_log_dir": config.rbeLogDir(),
Ramy Medhata958d352020-08-13 22:53:42 -040079 "RBE_re_proxy": config.rbeReproxy(),
80 "RBE_exec_root": config.rbeExecRoot(),
81 "RBE_output_dir": config.rbeStatsOutputDir(),
82 }
83 if config.StartRBE() {
Ramy Medhat81b3a832020-08-28 23:53:02 -040084 name, err := sockAddr(absPath(ctx, config.TempDir()))
85 if err != nil {
86 ctx.Fatalf("Error retrieving socket address: %v", err)
87 return nil
88 }
89 vars["RBE_server_address"] = fmt.Sprintf("unix://%v", name)
Ramy Medhat0fc67eb2020-08-12 01:26:23 -040090 }
Kousik Kumar35faaef2022-01-13 02:13:10 -050091
92 rf := 1.0
93 if config.Parallel() < runtime.NumCPU() {
94 rf = float64(config.Parallel()) / float64(runtime.NumCPU())
95 }
96 vars["RBE_local_resource_fraction"] = fmt.Sprintf("%.2f", rf)
97
Ramy Medhat0fc67eb2020-08-12 01:26:23 -040098 k, v := config.rbeAuth()
99 vars[k] = v
100 return vars
Ramy Medhatca1e44c2020-07-16 12:18:37 -0400101}
102
Ramy Medhatbbf25672019-07-17 12:30:04 +0000103func startRBE(ctx Context, config Config) {
104 ctx.BeginTrace(metrics.RunSetupTool, "rbe_bootstrap")
105 defer ctx.EndTrace()
106
107 if u := ulimitOrFatal(ctx, config, "-u"); u < rbeLeastNProcs {
108 ctx.Fatalf("max user processes is insufficient: %d; want >= %d.\n", u, rbeLeastNProcs)
109 }
110 if n := ulimitOrFatal(ctx, config, "-n"); n < rbeLeastNFiles {
111 ctx.Fatalf("max open files is insufficient: %d; want >= %d.\n", n, rbeLeastNFiles)
112 }
113
Patrice Arruda62f1bf22020-07-07 12:48:26 +0000114 cmd := Command(ctx, config, "startRBE bootstrap", rbeCommand(ctx, config, bootstrapCmd))
Ramy Medhatbbf25672019-07-17 12:30:04 +0000115
116 if output, err := cmd.CombinedOutput(); err != nil {
Kousik Kumar1e4d5f32021-01-26 14:30:53 -0500117 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 +0000118 }
119}
Patrice Arruda62f1bf22020-07-07 12:48:26 +0000120
121func stopRBE(ctx Context, config Config) {
122 cmd := Command(ctx, config, "stopRBE bootstrap", rbeCommand(ctx, config, bootstrapCmd), "-shutdown")
Kousik Kumar20810522021-03-21 22:35:26 -0400123 output, err := cmd.CombinedOutput()
124 if err != nil {
Patrice Arruda62f1bf22020-07-07 12:48:26 +0000125 ctx.Fatalf("rbe bootstrap with shutdown failed with: %v\n%s\n", err, output)
126 }
Kousik Kumar20810522021-03-21 22:35:26 -0400127
Peter Collingbournef4d9bd22021-04-20 20:58:19 -0700128 if !config.Environment().IsEnvTrue("ANDROID_QUIET_BUILD") && len(output) > 0 {
Kousik Kumar20810522021-03-21 22:35:26 -0400129 fmt.Fprintln(ctx.Writer, "")
130 fmt.Fprintln(ctx.Writer, fmt.Sprintf("%s", output))
131 }
Patrice Arruda62f1bf22020-07-07 12:48:26 +0000132}
133
134// DumpRBEMetrics creates a metrics protobuf file containing RBE related metrics.
135// The protobuf file is created if RBE is enabled and the proxy service has
136// started. The proxy service is shutdown in order to dump the RBE metrics to the
137// protobuf file.
138func DumpRBEMetrics(ctx Context, config Config, filename string) {
139 ctx.BeginTrace(metrics.RunShutdownTool, "dump_rbe_metrics")
140 defer ctx.EndTrace()
141
142 // Remove the previous metrics file in case there is a failure or RBE has been
143 // disable for this run.
144 os.Remove(filename)
145
146 // If RBE is not enabled then there are no metrics to generate.
147 // If RBE does not require to start, the RBE proxy maybe started
148 // manually for debugging purpose and can generate the metrics
149 // afterwards.
150 if !config.StartRBE() {
151 return
152 }
153
Ramy Medhat0fc67eb2020-08-12 01:26:23 -0400154 outputDir := config.rbeStatsOutputDir()
Patrice Arruda62f1bf22020-07-07 12:48:26 +0000155 if outputDir == "" {
156 ctx.Fatal("RBE output dir variable not defined. Aborting metrics dumping.")
157 }
158 metricsFile := filepath.Join(outputDir, rbeMetricsPBFilename)
159
160 // Stop the proxy first in order to generate the RBE metrics protobuf file.
161 stopRBE(ctx, config)
162
Ramy Medhat0fc67eb2020-08-12 01:26:23 -0400163 if metricsFile == filename {
164 return
165 }
Patrice Arruda62f1bf22020-07-07 12:48:26 +0000166 if _, err := copyFile(metricsFile, filename); err != nil {
167 ctx.Fatalf("failed to copy %q to %q: %v\n", metricsFile, filename, err)
168 }
169}
Kousik Kumara0a44a82020-10-08 02:33:29 -0400170
171// PrintOutDirWarning prints a warning to indicate to the user that
172// setting output directory to a path other than "out" in an RBE enabled
173// build can cause slow builds.
174func PrintOutDirWarning(ctx Context, config Config) {
175 if config.UseRBE() && config.OutDir() != defaultOutDir {
176 fmt.Fprintln(ctx.Writer, "")
177 fmt.Fprintln(ctx.Writer, "\033[33mWARNING:\033[0m")
178 fmt.Fprintln(ctx.Writer, fmt.Sprintf("Setting OUT_DIR to a path other than %v may result in slow RBE builds.", defaultOutDir))
179 fmt.Fprintln(ctx.Writer, "See http://go/android_rbe_out_dir for a workaround.")
180 fmt.Fprintln(ctx.Writer, "")
181 }
182}