Ramy Medhat | bbf2567 | 2019-07-17 12:30:04 +0000 | [diff] [blame] | 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 | |
| 15 | package build |
| 16 | |
| 17 | import ( |
Ramy Medhat | ca1e44c | 2020-07-16 12:18:37 -0400 | [diff] [blame] | 18 | "fmt" |
| 19 | "math/rand" |
Patrice Arruda | 62f1bf2 | 2020-07-07 12:48:26 +0000 | [diff] [blame] | 20 | "os" |
Ramy Medhat | bbf2567 | 2019-07-17 12:30:04 +0000 | [diff] [blame] | 21 | "path/filepath" |
Ramy Medhat | 81b3a83 | 2020-08-28 23:53:02 -0400 | [diff] [blame] | 22 | "syscall" |
Ramy Medhat | ca1e44c | 2020-07-16 12:18:37 -0400 | [diff] [blame] | 23 | "time" |
Ramy Medhat | bbf2567 | 2019-07-17 12:30:04 +0000 | [diff] [blame] | 24 | |
| 25 | "android/soong/ui/metrics" |
| 26 | ) |
| 27 | |
Patrice Arruda | 62f1bf2 | 2020-07-07 12:48:26 +0000 | [diff] [blame] | 28 | const ( |
| 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 Kumar | a0a44a8 | 2020-10-08 02:33:29 -0400 | [diff] [blame] | 37 | |
| 38 | defaultOutDir = "out" |
Patrice Arruda | 62f1bf2 | 2020-07-07 12:48:26 +0000 | [diff] [blame] | 39 | ) |
| 40 | |
| 41 | func rbeCommand(ctx Context, config Config, rbeCmd string) string { |
| 42 | var cmdPath string |
Ramy Medhat | 0fc67eb | 2020-08-12 01:26:23 -0400 | [diff] [blame] | 43 | if rbeDir := config.rbeDir(); rbeDir != "" { |
Patrice Arruda | 62f1bf2 | 2020-07-07 12:48:26 +0000 | [diff] [blame] | 44 | cmdPath = filepath.Join(rbeDir, rbeCmd) |
Patrice Arruda | 62f1bf2 | 2020-07-07 12:48:26 +0000 | [diff] [blame] | 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 | } |
Ramy Medhat | bbf2567 | 2019-07-17 12:30:04 +0000 | [diff] [blame] | 55 | |
Ramy Medhat | 81b3a83 | 2020-08-28 23:53:02 -0400 | [diff] [blame] | 56 | func sockAddr(dir string) (string, error) { |
| 57 | maxNameLen := len(syscall.RawSockaddrUnix{}.Path) |
Ramy Medhat | ca1e44c | 2020-07-16 12:18:37 -0400 | [diff] [blame] | 58 | rand.Seed(time.Now().UnixNano()) |
Ramy Medhat | 81b3a83 | 2020-08-28 23:53:02 -0400 | [diff] [blame] | 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 | |
| 74 | func getRBEVars(ctx Context, config Config) map[string]string { |
Ramy Medhat | 0fc67eb | 2020-08-12 01:26:23 -0400 | [diff] [blame] | 75 | vars := map[string]string{ |
Ramy Medhat | a958d35 | 2020-08-13 22:53:42 -0400 | [diff] [blame] | 76 | "RBE_log_path": config.rbeLogPath(), |
Rupert Shuttleworth | 3c9f5ac | 2020-12-10 11:32:38 +0000 | [diff] [blame] | 77 | "RBE_log_dir": config.rbeLogDir(), |
Ramy Medhat | a958d35 | 2020-08-13 22:53:42 -0400 | [diff] [blame] | 78 | "RBE_re_proxy": config.rbeReproxy(), |
| 79 | "RBE_exec_root": config.rbeExecRoot(), |
| 80 | "RBE_output_dir": config.rbeStatsOutputDir(), |
| 81 | } |
| 82 | if config.StartRBE() { |
Ramy Medhat | 81b3a83 | 2020-08-28 23:53:02 -0400 | [diff] [blame] | 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) |
Ramy Medhat | 0fc67eb | 2020-08-12 01:26:23 -0400 | [diff] [blame] | 89 | } |
| 90 | k, v := config.rbeAuth() |
| 91 | vars[k] = v |
| 92 | return vars |
Ramy Medhat | ca1e44c | 2020-07-16 12:18:37 -0400 | [diff] [blame] | 93 | } |
| 94 | |
Ramy Medhat | bbf2567 | 2019-07-17 12:30:04 +0000 | [diff] [blame] | 95 | func 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 Arruda | 62f1bf2 | 2020-07-07 12:48:26 +0000 | [diff] [blame] | 106 | cmd := Command(ctx, config, "startRBE bootstrap", rbeCommand(ctx, config, bootstrapCmd)) |
Ramy Medhat | bbf2567 | 2019-07-17 12:30:04 +0000 | [diff] [blame] | 107 | |
| 108 | if output, err := cmd.CombinedOutput(); err != nil { |
Kousik Kumar | 1e4d5f3 | 2021-01-26 14:30:53 -0500 | [diff] [blame] | 109 | ctx.Fatalf("Unable to start RBE reproxy\nFAILED: RBE bootstrap failed with: %v\n%s\n", err, output) |
Ramy Medhat | bbf2567 | 2019-07-17 12:30:04 +0000 | [diff] [blame] | 110 | } |
| 111 | } |
Patrice Arruda | 62f1bf2 | 2020-07-07 12:48:26 +0000 | [diff] [blame] | 112 | |
| 113 | func stopRBE(ctx Context, config Config) { |
| 114 | cmd := Command(ctx, config, "stopRBE bootstrap", rbeCommand(ctx, config, bootstrapCmd), "-shutdown") |
Kousik Kumar | 2081052 | 2021-03-21 22:35:26 -0400 | [diff] [blame] | 115 | output, err := cmd.CombinedOutput() |
| 116 | if err != nil { |
Patrice Arruda | 62f1bf2 | 2020-07-07 12:48:26 +0000 | [diff] [blame] | 117 | ctx.Fatalf("rbe bootstrap with shutdown failed with: %v\n%s\n", err, output) |
| 118 | } |
Kousik Kumar | 2081052 | 2021-03-21 22:35:26 -0400 | [diff] [blame] | 119 | |
Peter Collingbourne | f4d9bd2 | 2021-04-20 20:58:19 -0700 | [diff] [blame^] | 120 | if !config.Environment().IsEnvTrue("ANDROID_QUIET_BUILD") && len(output) > 0 { |
Kousik Kumar | 2081052 | 2021-03-21 22:35:26 -0400 | [diff] [blame] | 121 | fmt.Fprintln(ctx.Writer, "") |
| 122 | fmt.Fprintln(ctx.Writer, fmt.Sprintf("%s", output)) |
| 123 | } |
Patrice Arruda | 62f1bf2 | 2020-07-07 12:48:26 +0000 | [diff] [blame] | 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. |
| 130 | func 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 Medhat | 0fc67eb | 2020-08-12 01:26:23 -0400 | [diff] [blame] | 146 | outputDir := config.rbeStatsOutputDir() |
Patrice Arruda | 62f1bf2 | 2020-07-07 12:48:26 +0000 | [diff] [blame] | 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 | |
Ramy Medhat | 0fc67eb | 2020-08-12 01:26:23 -0400 | [diff] [blame] | 155 | if metricsFile == filename { |
| 156 | return |
| 157 | } |
Patrice Arruda | 62f1bf2 | 2020-07-07 12:48:26 +0000 | [diff] [blame] | 158 | if _, err := copyFile(metricsFile, filename); err != nil { |
| 159 | ctx.Fatalf("failed to copy %q to %q: %v\n", metricsFile, filename, err) |
| 160 | } |
| 161 | } |
Kousik Kumar | a0a44a8 | 2020-10-08 02:33:29 -0400 | [diff] [blame] | 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. |
| 166 | func 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 | } |