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 | ca1e44c | 2020-07-16 12:18:37 -0400 | [diff] [blame] | 22 | "time" |
Ramy Medhat | bbf2567 | 2019-07-17 12:30:04 +0000 | [diff] [blame] | 23 | |
| 24 | "android/soong/ui/metrics" |
| 25 | ) |
| 26 | |
Patrice Arruda | 62f1bf2 | 2020-07-07 12:48:26 +0000 | [diff] [blame] | 27 | const ( |
| 28 | rbeLeastNProcs = 2500 |
| 29 | rbeLeastNFiles = 16000 |
| 30 | |
| 31 | // prebuilt RBE binaries |
| 32 | bootstrapCmd = "bootstrap" |
| 33 | |
| 34 | // RBE metrics proto buffer file |
| 35 | rbeMetricsPBFilename = "rbe_metrics.pb" |
| 36 | ) |
| 37 | |
| 38 | func rbeCommand(ctx Context, config Config, rbeCmd string) string { |
| 39 | var cmdPath string |
Ramy Medhat | 0fc67eb | 2020-08-12 01:26:23 -0400 | [diff] [blame^] | 40 | if rbeDir := config.rbeDir(); rbeDir != "" { |
Patrice Arruda | 62f1bf2 | 2020-07-07 12:48:26 +0000 | [diff] [blame] | 41 | cmdPath = filepath.Join(rbeDir, rbeCmd) |
Patrice Arruda | 62f1bf2 | 2020-07-07 12:48:26 +0000 | [diff] [blame] | 42 | } else { |
| 43 | ctx.Fatalf("rbe command path not found") |
| 44 | } |
| 45 | |
| 46 | if _, err := os.Stat(cmdPath); err != nil && os.IsNotExist(err) { |
| 47 | ctx.Fatalf("rbe command %q not found", rbeCmd) |
| 48 | } |
| 49 | |
| 50 | return cmdPath |
| 51 | } |
Ramy Medhat | bbf2567 | 2019-07-17 12:30:04 +0000 | [diff] [blame] | 52 | |
Ramy Medhat | 0fc67eb | 2020-08-12 01:26:23 -0400 | [diff] [blame^] | 53 | func getRBEVars(ctx Context, config Config) map[string]string { |
Ramy Medhat | ca1e44c | 2020-07-16 12:18:37 -0400 | [diff] [blame] | 54 | rand.Seed(time.Now().UnixNano()) |
Ramy Medhat | 0fc67eb | 2020-08-12 01:26:23 -0400 | [diff] [blame^] | 55 | vars := map[string]string{ |
| 56 | "RBE_server_address": fmt.Sprintf("unix://%v/reproxy_%v.sock", absPath(ctx, config.TempDir()), rand.Intn(1000)), |
| 57 | "RBE_log_path": config.rbeLogPath(), |
| 58 | "RBE_re_proxy": config.rbeReproxy(), |
| 59 | "RBE_exec_root": config.rbeExecRoot(), |
| 60 | "RBE_output_dir": config.rbeStatsOutputDir(), |
| 61 | } |
| 62 | k, v := config.rbeAuth() |
| 63 | vars[k] = v |
| 64 | return vars |
Ramy Medhat | ca1e44c | 2020-07-16 12:18:37 -0400 | [diff] [blame] | 65 | } |
| 66 | |
Ramy Medhat | bbf2567 | 2019-07-17 12:30:04 +0000 | [diff] [blame] | 67 | func startRBE(ctx Context, config Config) { |
| 68 | ctx.BeginTrace(metrics.RunSetupTool, "rbe_bootstrap") |
| 69 | defer ctx.EndTrace() |
| 70 | |
| 71 | if u := ulimitOrFatal(ctx, config, "-u"); u < rbeLeastNProcs { |
| 72 | ctx.Fatalf("max user processes is insufficient: %d; want >= %d.\n", u, rbeLeastNProcs) |
| 73 | } |
| 74 | if n := ulimitOrFatal(ctx, config, "-n"); n < rbeLeastNFiles { |
| 75 | ctx.Fatalf("max open files is insufficient: %d; want >= %d.\n", n, rbeLeastNFiles) |
| 76 | } |
| 77 | |
Patrice Arruda | 62f1bf2 | 2020-07-07 12:48:26 +0000 | [diff] [blame] | 78 | cmd := Command(ctx, config, "startRBE bootstrap", rbeCommand(ctx, config, bootstrapCmd)) |
Ramy Medhat | bbf2567 | 2019-07-17 12:30:04 +0000 | [diff] [blame] | 79 | |
| 80 | if output, err := cmd.CombinedOutput(); err != nil { |
| 81 | ctx.Fatalf("rbe bootstrap failed with: %v\n%s\n", err, output) |
| 82 | } |
| 83 | } |
Patrice Arruda | 62f1bf2 | 2020-07-07 12:48:26 +0000 | [diff] [blame] | 84 | |
| 85 | func stopRBE(ctx Context, config Config) { |
| 86 | cmd := Command(ctx, config, "stopRBE bootstrap", rbeCommand(ctx, config, bootstrapCmd), "-shutdown") |
| 87 | if output, err := cmd.CombinedOutput(); err != nil { |
| 88 | ctx.Fatalf("rbe bootstrap with shutdown failed with: %v\n%s\n", err, output) |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | // DumpRBEMetrics creates a metrics protobuf file containing RBE related metrics. |
| 93 | // The protobuf file is created if RBE is enabled and the proxy service has |
| 94 | // started. The proxy service is shutdown in order to dump the RBE metrics to the |
| 95 | // protobuf file. |
| 96 | func DumpRBEMetrics(ctx Context, config Config, filename string) { |
| 97 | ctx.BeginTrace(metrics.RunShutdownTool, "dump_rbe_metrics") |
| 98 | defer ctx.EndTrace() |
| 99 | |
| 100 | // Remove the previous metrics file in case there is a failure or RBE has been |
| 101 | // disable for this run. |
| 102 | os.Remove(filename) |
| 103 | |
| 104 | // If RBE is not enabled then there are no metrics to generate. |
| 105 | // If RBE does not require to start, the RBE proxy maybe started |
| 106 | // manually for debugging purpose and can generate the metrics |
| 107 | // afterwards. |
| 108 | if !config.StartRBE() { |
| 109 | return |
| 110 | } |
| 111 | |
Ramy Medhat | 0fc67eb | 2020-08-12 01:26:23 -0400 | [diff] [blame^] | 112 | outputDir := config.rbeStatsOutputDir() |
Patrice Arruda | 62f1bf2 | 2020-07-07 12:48:26 +0000 | [diff] [blame] | 113 | if outputDir == "" { |
| 114 | ctx.Fatal("RBE output dir variable not defined. Aborting metrics dumping.") |
| 115 | } |
| 116 | metricsFile := filepath.Join(outputDir, rbeMetricsPBFilename) |
| 117 | |
| 118 | // Stop the proxy first in order to generate the RBE metrics protobuf file. |
| 119 | stopRBE(ctx, config) |
| 120 | |
Ramy Medhat | 0fc67eb | 2020-08-12 01:26:23 -0400 | [diff] [blame^] | 121 | if metricsFile == filename { |
| 122 | return |
| 123 | } |
Patrice Arruda | 62f1bf2 | 2020-07-07 12:48:26 +0000 | [diff] [blame] | 124 | if _, err := copyFile(metricsFile, filename); err != nil { |
| 125 | ctx.Fatalf("failed to copy %q to %q: %v\n", metricsFile, filename, err) |
| 126 | } |
| 127 | } |