blob: d74f262100037623d76eabb4bf278af6491eaa71 [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"
Ramy Medhat81b3a832020-08-28 23:53:02 -040022 "syscall"
Ramy Medhatca1e44c2020-07-16 12:18:37 -040023 "time"
Ramy Medhatbbf25672019-07-17 12:30:04 +000024
25 "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 sockAddr(dir string) (string, error) {
57 maxNameLen := len(syscall.RawSockaddrUnix{}.Path)
Ramy Medhatca1e44c2020-07-16 12:18:37 -040058 rand.Seed(time.Now().UnixNano())
Ramy Medhat81b3a832020-08-28 23:53:02 -040059 base := fmt.Sprintf("reproxy_%v.sock", rand.Intn(1000))
60
61 name := filepath.Join(dir, base)
62 if len(name) < maxNameLen {
63 return name, nil
64 }
65
66 name = filepath.Join("/tmp", base)
67 if len(name) < maxNameLen {
68 return name, nil
69 }
70
71 return "", fmt.Errorf("cannot generate a proxy socket address shorter than the limit of %v", maxNameLen)
72}
73
74func getRBEVars(ctx Context, config Config) map[string]string {
Ramy Medhat0fc67eb2020-08-12 01:26:23 -040075 vars := map[string]string{
Ramy Medhata958d352020-08-13 22:53:42 -040076 "RBE_log_path": config.rbeLogPath(),
Rupert Shuttleworth3c9f5ac2020-12-10 11:32:38 +000077 "RBE_log_dir": config.rbeLogDir(),
Ramy Medhata958d352020-08-13 22:53:42 -040078 "RBE_re_proxy": config.rbeReproxy(),
79 "RBE_exec_root": config.rbeExecRoot(),
80 "RBE_output_dir": config.rbeStatsOutputDir(),
81 }
82 if config.StartRBE() {
Ramy Medhat81b3a832020-08-28 23:53:02 -040083 name, err := sockAddr(absPath(ctx, config.TempDir()))
84 if err != nil {
85 ctx.Fatalf("Error retrieving socket address: %v", err)
86 return nil
87 }
88 vars["RBE_server_address"] = fmt.Sprintf("unix://%v", name)
Ramy Medhat0fc67eb2020-08-12 01:26:23 -040089 }
90 k, v := config.rbeAuth()
91 vars[k] = v
92 return vars
Ramy Medhatca1e44c2020-07-16 12:18:37 -040093}
94
Ramy Medhatbbf25672019-07-17 12:30:04 +000095func startRBE(ctx Context, config Config) {
96 ctx.BeginTrace(metrics.RunSetupTool, "rbe_bootstrap")
97 defer ctx.EndTrace()
98
99 if u := ulimitOrFatal(ctx, config, "-u"); u < rbeLeastNProcs {
100 ctx.Fatalf("max user processes is insufficient: %d; want >= %d.\n", u, rbeLeastNProcs)
101 }
102 if n := ulimitOrFatal(ctx, config, "-n"); n < rbeLeastNFiles {
103 ctx.Fatalf("max open files is insufficient: %d; want >= %d.\n", n, rbeLeastNFiles)
104 }
105
Patrice Arruda62f1bf22020-07-07 12:48:26 +0000106 cmd := Command(ctx, config, "startRBE bootstrap", rbeCommand(ctx, config, bootstrapCmd))
Ramy Medhatbbf25672019-07-17 12:30:04 +0000107
108 if output, err := cmd.CombinedOutput(); err != nil {
Kousik Kumar1e4d5f32021-01-26 14:30:53 -0500109 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 +0000110 }
111}
Patrice Arruda62f1bf22020-07-07 12:48:26 +0000112
113func stopRBE(ctx Context, config Config) {
114 cmd := Command(ctx, config, "stopRBE bootstrap", rbeCommand(ctx, config, bootstrapCmd), "-shutdown")
Kousik Kumar20810522021-03-21 22:35:26 -0400115 output, err := cmd.CombinedOutput()
116 if err != nil {
Patrice Arruda62f1bf22020-07-07 12:48:26 +0000117 ctx.Fatalf("rbe bootstrap with shutdown failed with: %v\n%s\n", err, output)
118 }
Kousik Kumar20810522021-03-21 22:35:26 -0400119
Peter Collingbournef4d9bd22021-04-20 20:58:19 -0700120 if !config.Environment().IsEnvTrue("ANDROID_QUIET_BUILD") && len(output) > 0 {
Kousik Kumar20810522021-03-21 22:35:26 -0400121 fmt.Fprintln(ctx.Writer, "")
122 fmt.Fprintln(ctx.Writer, fmt.Sprintf("%s", output))
123 }
Patrice Arruda62f1bf22020-07-07 12:48:26 +0000124}
125
126// DumpRBEMetrics creates a metrics protobuf file containing RBE related metrics.
127// The protobuf file is created if RBE is enabled and the proxy service has
128// started. The proxy service is shutdown in order to dump the RBE metrics to the
129// protobuf file.
130func DumpRBEMetrics(ctx Context, config Config, filename string) {
131 ctx.BeginTrace(metrics.RunShutdownTool, "dump_rbe_metrics")
132 defer ctx.EndTrace()
133
134 // Remove the previous metrics file in case there is a failure or RBE has been
135 // disable for this run.
136 os.Remove(filename)
137
138 // If RBE is not enabled then there are no metrics to generate.
139 // If RBE does not require to start, the RBE proxy maybe started
140 // manually for debugging purpose and can generate the metrics
141 // afterwards.
142 if !config.StartRBE() {
143 return
144 }
145
Ramy Medhat0fc67eb2020-08-12 01:26:23 -0400146 outputDir := config.rbeStatsOutputDir()
Patrice Arruda62f1bf22020-07-07 12:48:26 +0000147 if outputDir == "" {
148 ctx.Fatal("RBE output dir variable not defined. Aborting metrics dumping.")
149 }
150 metricsFile := filepath.Join(outputDir, rbeMetricsPBFilename)
151
152 // Stop the proxy first in order to generate the RBE metrics protobuf file.
153 stopRBE(ctx, config)
154
Ramy Medhat0fc67eb2020-08-12 01:26:23 -0400155 if metricsFile == filename {
156 return
157 }
Patrice Arruda62f1bf22020-07-07 12:48:26 +0000158 if _, err := copyFile(metricsFile, filename); err != nil {
159 ctx.Fatalf("failed to copy %q to %q: %v\n", metricsFile, filename, err)
160 }
161}
Kousik Kumara0a44a82020-10-08 02:33:29 -0400162
163// PrintOutDirWarning prints a warning to indicate to the user that
164// setting output directory to a path other than "out" in an RBE enabled
165// build can cause slow builds.
166func PrintOutDirWarning(ctx Context, config Config) {
167 if config.UseRBE() && config.OutDir() != defaultOutDir {
168 fmt.Fprintln(ctx.Writer, "")
169 fmt.Fprintln(ctx.Writer, "\033[33mWARNING:\033[0m")
170 fmt.Fprintln(ctx.Writer, fmt.Sprintf("Setting OUT_DIR to a path other than %v may result in slow RBE builds.", defaultOutDir))
171 fmt.Fprintln(ctx.Writer, "See http://go/android_rbe_out_dir for a workaround.")
172 fmt.Fprintln(ctx.Writer, "")
173 }
174}