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