blob: 1fabd92621c5788ce15f54d1815d5ada32c30388 [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")
115 if output, err := cmd.CombinedOutput(); err != nil {
116 ctx.Fatalf("rbe bootstrap with shutdown failed with: %v\n%s\n", err, output)
117 }
118}
119
120// DumpRBEMetrics creates a metrics protobuf file containing RBE related metrics.
121// The protobuf file is created if RBE is enabled and the proxy service has
122// started. The proxy service is shutdown in order to dump the RBE metrics to the
123// protobuf file.
124func DumpRBEMetrics(ctx Context, config Config, filename string) {
125 ctx.BeginTrace(metrics.RunShutdownTool, "dump_rbe_metrics")
126 defer ctx.EndTrace()
127
128 // Remove the previous metrics file in case there is a failure or RBE has been
129 // disable for this run.
130 os.Remove(filename)
131
132 // If RBE is not enabled then there are no metrics to generate.
133 // If RBE does not require to start, the RBE proxy maybe started
134 // manually for debugging purpose and can generate the metrics
135 // afterwards.
136 if !config.StartRBE() {
137 return
138 }
139
Ramy Medhat0fc67eb2020-08-12 01:26:23 -0400140 outputDir := config.rbeStatsOutputDir()
Patrice Arruda62f1bf22020-07-07 12:48:26 +0000141 if outputDir == "" {
142 ctx.Fatal("RBE output dir variable not defined. Aborting metrics dumping.")
143 }
144 metricsFile := filepath.Join(outputDir, rbeMetricsPBFilename)
145
146 // Stop the proxy first in order to generate the RBE metrics protobuf file.
147 stopRBE(ctx, config)
148
Ramy Medhat0fc67eb2020-08-12 01:26:23 -0400149 if metricsFile == filename {
150 return
151 }
Patrice Arruda62f1bf22020-07-07 12:48:26 +0000152 if _, err := copyFile(metricsFile, filename); err != nil {
153 ctx.Fatalf("failed to copy %q to %q: %v\n", metricsFile, filename, err)
154 }
155}
Kousik Kumara0a44a82020-10-08 02:33:29 -0400156
157// PrintOutDirWarning prints a warning to indicate to the user that
158// setting output directory to a path other than "out" in an RBE enabled
159// build can cause slow builds.
160func PrintOutDirWarning(ctx Context, config Config) {
161 if config.UseRBE() && config.OutDir() != defaultOutDir {
162 fmt.Fprintln(ctx.Writer, "")
163 fmt.Fprintln(ctx.Writer, "\033[33mWARNING:\033[0m")
164 fmt.Fprintln(ctx.Writer, fmt.Sprintf("Setting OUT_DIR to a path other than %v may result in slow RBE builds.", defaultOutDir))
165 fmt.Fprintln(ctx.Writer, "See http://go/android_rbe_out_dir for a workaround.")
166 fmt.Fprintln(ctx.Writer, "")
167 }
168}