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" |
| 37 | ) |
| 38 | |
| 39 | func rbeCommand(ctx Context, config Config, rbeCmd string) string { |
| 40 | var cmdPath string |
Ramy Medhat | 0fc67eb | 2020-08-12 01:26:23 -0400 | [diff] [blame] | 41 | if rbeDir := config.rbeDir(); rbeDir != "" { |
Patrice Arruda | 62f1bf2 | 2020-07-07 12:48:26 +0000 | [diff] [blame] | 42 | cmdPath = filepath.Join(rbeDir, rbeCmd) |
Patrice Arruda | 62f1bf2 | 2020-07-07 12:48:26 +0000 | [diff] [blame] | 43 | } else { |
| 44 | ctx.Fatalf("rbe command path not found") |
| 45 | } |
| 46 | |
| 47 | if _, err := os.Stat(cmdPath); err != nil && os.IsNotExist(err) { |
| 48 | ctx.Fatalf("rbe command %q not found", rbeCmd) |
| 49 | } |
| 50 | |
| 51 | return cmdPath |
| 52 | } |
Ramy Medhat | bbf2567 | 2019-07-17 12:30:04 +0000 | [diff] [blame] | 53 | |
Ramy Medhat | 81b3a83 | 2020-08-28 23:53:02 -0400 | [diff] [blame^] | 54 | func sockAddr(dir string) (string, error) { |
| 55 | maxNameLen := len(syscall.RawSockaddrUnix{}.Path) |
Ramy Medhat | ca1e44c | 2020-07-16 12:18:37 -0400 | [diff] [blame] | 56 | rand.Seed(time.Now().UnixNano()) |
Ramy Medhat | 81b3a83 | 2020-08-28 23:53:02 -0400 | [diff] [blame^] | 57 | base := fmt.Sprintf("reproxy_%v.sock", rand.Intn(1000)) |
| 58 | |
| 59 | name := filepath.Join(dir, base) |
| 60 | if len(name) < maxNameLen { |
| 61 | return name, nil |
| 62 | } |
| 63 | |
| 64 | name = filepath.Join("/tmp", base) |
| 65 | if len(name) < maxNameLen { |
| 66 | return name, nil |
| 67 | } |
| 68 | |
| 69 | return "", fmt.Errorf("cannot generate a proxy socket address shorter than the limit of %v", maxNameLen) |
| 70 | } |
| 71 | |
| 72 | func getRBEVars(ctx Context, config Config) map[string]string { |
Ramy Medhat | 0fc67eb | 2020-08-12 01:26:23 -0400 | [diff] [blame] | 73 | vars := map[string]string{ |
Ramy Medhat | a958d35 | 2020-08-13 22:53:42 -0400 | [diff] [blame] | 74 | "RBE_log_path": config.rbeLogPath(), |
Ramy Medhat | 8c56bc2 | 2020-08-13 14:51:51 -0400 | [diff] [blame] | 75 | "RBE_log_dir": config.logDir(), |
Ramy Medhat | a958d35 | 2020-08-13 22:53:42 -0400 | [diff] [blame] | 76 | "RBE_re_proxy": config.rbeReproxy(), |
| 77 | "RBE_exec_root": config.rbeExecRoot(), |
| 78 | "RBE_output_dir": config.rbeStatsOutputDir(), |
| 79 | } |
| 80 | if config.StartRBE() { |
Ramy Medhat | 81b3a83 | 2020-08-28 23:53:02 -0400 | [diff] [blame^] | 81 | name, err := sockAddr(absPath(ctx, config.TempDir())) |
| 82 | if err != nil { |
| 83 | ctx.Fatalf("Error retrieving socket address: %v", err) |
| 84 | return nil |
| 85 | } |
| 86 | vars["RBE_server_address"] = fmt.Sprintf("unix://%v", name) |
Ramy Medhat | 0fc67eb | 2020-08-12 01:26:23 -0400 | [diff] [blame] | 87 | } |
| 88 | k, v := config.rbeAuth() |
| 89 | vars[k] = v |
| 90 | return vars |
Ramy Medhat | ca1e44c | 2020-07-16 12:18:37 -0400 | [diff] [blame] | 91 | } |
| 92 | |
Ramy Medhat | bbf2567 | 2019-07-17 12:30:04 +0000 | [diff] [blame] | 93 | func startRBE(ctx Context, config Config) { |
| 94 | ctx.BeginTrace(metrics.RunSetupTool, "rbe_bootstrap") |
| 95 | defer ctx.EndTrace() |
| 96 | |
| 97 | if u := ulimitOrFatal(ctx, config, "-u"); u < rbeLeastNProcs { |
| 98 | ctx.Fatalf("max user processes is insufficient: %d; want >= %d.\n", u, rbeLeastNProcs) |
| 99 | } |
| 100 | if n := ulimitOrFatal(ctx, config, "-n"); n < rbeLeastNFiles { |
| 101 | ctx.Fatalf("max open files is insufficient: %d; want >= %d.\n", n, rbeLeastNFiles) |
| 102 | } |
| 103 | |
Patrice Arruda | 62f1bf2 | 2020-07-07 12:48:26 +0000 | [diff] [blame] | 104 | cmd := Command(ctx, config, "startRBE bootstrap", rbeCommand(ctx, config, bootstrapCmd)) |
Ramy Medhat | bbf2567 | 2019-07-17 12:30:04 +0000 | [diff] [blame] | 105 | |
| 106 | if output, err := cmd.CombinedOutput(); err != nil { |
| 107 | ctx.Fatalf("rbe bootstrap failed with: %v\n%s\n", err, output) |
| 108 | } |
| 109 | } |
Patrice Arruda | 62f1bf2 | 2020-07-07 12:48:26 +0000 | [diff] [blame] | 110 | |
| 111 | func stopRBE(ctx Context, config Config) { |
| 112 | cmd := Command(ctx, config, "stopRBE bootstrap", rbeCommand(ctx, config, bootstrapCmd), "-shutdown") |
| 113 | if output, err := cmd.CombinedOutput(); err != nil { |
| 114 | ctx.Fatalf("rbe bootstrap with shutdown failed with: %v\n%s\n", err, output) |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | // DumpRBEMetrics creates a metrics protobuf file containing RBE related metrics. |
| 119 | // The protobuf file is created if RBE is enabled and the proxy service has |
| 120 | // started. The proxy service is shutdown in order to dump the RBE metrics to the |
| 121 | // protobuf file. |
| 122 | func DumpRBEMetrics(ctx Context, config Config, filename string) { |
| 123 | ctx.BeginTrace(metrics.RunShutdownTool, "dump_rbe_metrics") |
| 124 | defer ctx.EndTrace() |
| 125 | |
| 126 | // Remove the previous metrics file in case there is a failure or RBE has been |
| 127 | // disable for this run. |
| 128 | os.Remove(filename) |
| 129 | |
| 130 | // If RBE is not enabled then there are no metrics to generate. |
| 131 | // If RBE does not require to start, the RBE proxy maybe started |
| 132 | // manually for debugging purpose and can generate the metrics |
| 133 | // afterwards. |
| 134 | if !config.StartRBE() { |
| 135 | return |
| 136 | } |
| 137 | |
Ramy Medhat | 0fc67eb | 2020-08-12 01:26:23 -0400 | [diff] [blame] | 138 | outputDir := config.rbeStatsOutputDir() |
Patrice Arruda | 62f1bf2 | 2020-07-07 12:48:26 +0000 | [diff] [blame] | 139 | if outputDir == "" { |
| 140 | ctx.Fatal("RBE output dir variable not defined. Aborting metrics dumping.") |
| 141 | } |
| 142 | metricsFile := filepath.Join(outputDir, rbeMetricsPBFilename) |
| 143 | |
| 144 | // Stop the proxy first in order to generate the RBE metrics protobuf file. |
| 145 | stopRBE(ctx, config) |
| 146 | |
Ramy Medhat | 0fc67eb | 2020-08-12 01:26:23 -0400 | [diff] [blame] | 147 | if metricsFile == filename { |
| 148 | return |
| 149 | } |
Patrice Arruda | 62f1bf2 | 2020-07-07 12:48:26 +0000 | [diff] [blame] | 150 | if _, err := copyFile(metricsFile, filename); err != nil { |
| 151 | ctx.Fatalf("failed to copy %q to %q: %v\n", metricsFile, filename, err) |
| 152 | } |
| 153 | } |
Ramy Medhat | f668917 | 2020-08-18 17:29:48 -0400 | [diff] [blame] | 154 | |
| 155 | // PrintGomaDeprecation prints a PSA on the deprecation of Goma if it is set for the build. |
| 156 | func PrintGomaDeprecation(ctx Context, config Config) { |
| 157 | if config.UseGoma() { |
| 158 | fmt.Fprintln(ctx.Writer, "") |
| 159 | fmt.Fprintln(ctx.Writer, "Goma for Android is being deprecated and replaced with RBE.") |
| 160 | fmt.Fprintln(ctx.Writer, "See go/goma_android_deprecation for more details.") |
| 161 | fmt.Fprintln(ctx.Writer, "") |
| 162 | } |
| 163 | } |