1// 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 ( 18 "fmt" 19 "math/rand" 20 "os" 21 "path/filepath" 22 "syscall" 23 "time" 24 25 "android/soong/ui/metrics" 26) 27 28const ( 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" 37 38 defaultOutDir = "out" 39) 40 41func rbeCommand(ctx Context, config Config, rbeCmd string) string { 42 var cmdPath string 43 if rbeDir := config.rbeDir(); rbeDir != "" { 44 cmdPath = filepath.Join(rbeDir, rbeCmd) 45 } 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} 55 56func sockAddr(dir string) (string, error) { 57 maxNameLen := len(syscall.RawSockaddrUnix{}.Path) 58 rand.Seed(time.Now().UnixNano()) 59 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 { 75 vars := map[string]string{ 76 "RBE_log_path": config.rbeLogPath(), 77 "RBE_log_dir": config.rbeLogDir(), 78 "RBE_re_proxy": config.rbeReproxy(), 79 "RBE_exec_root": config.rbeExecRoot(), 80 "RBE_output_dir": config.rbeStatsOutputDir(), 81 } 82 if config.StartRBE() { 83 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) 89 } 90 k, v := config.rbeAuth() 91 vars[k] = v 92 return vars 93} 94 95func 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 106 cmd := Command(ctx, config, "startRBE bootstrap", rbeCommand(ctx, config, bootstrapCmd)) 107 108 if output, err := cmd.CombinedOutput(); err != nil { 109 ctx.Fatalf("Unable to start RBE reproxy\nFAILED: RBE bootstrap failed with: %v\n%s\n", err, output) 110 } 111} 112 113func stopRBE(ctx Context, config Config) { 114 cmd := Command(ctx, config, "stopRBE bootstrap", rbeCommand(ctx, config, bootstrapCmd), "-shutdown") 115 output, err := cmd.CombinedOutput() 116 if err != nil { 117 ctx.Fatalf("rbe bootstrap with shutdown failed with: %v\n%s\n", err, output) 118 } 119 120 if !config.Environment().IsEnvTrue("ANDROID_QUIET_BUILD") && len(output) > 0 { 121 fmt.Fprintln(ctx.Writer, "") 122 fmt.Fprintln(ctx.Writer, fmt.Sprintf("%s", output)) 123 } 124} 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 146 outputDir := config.rbeStatsOutputDir() 147 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 155 if metricsFile == filename { 156 return 157 } 158 if _, err := copyFile(metricsFile, filename); err != nil { 159 ctx.Fatalf("failed to copy %q to %q: %v\n", metricsFile, filename, err) 160 } 161} 162 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} 175