blob: 67bcebb754553146873e17ccee9e43bb6210da92 [file] [log] [blame]
Ramy Medhatbbf25672019-07-17 12:30:04 +00001// 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 (
Ramy Medhatca1e44c2020-07-16 12:18:37 -040018 "fmt"
19 "math/rand"
Patrice Arruda62f1bf22020-07-07 12:48:26 +000020 "os"
Ramy Medhatbbf25672019-07-17 12:30:04 +000021 "path/filepath"
Ramy Medhatca1e44c2020-07-16 12:18:37 -040022 "time"
Ramy Medhatbbf25672019-07-17 12:30:04 +000023
24 "android/soong/ui/metrics"
25)
26
Patrice Arruda62f1bf22020-07-07 12:48:26 +000027const (
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
38func rbeCommand(ctx Context, config Config, rbeCmd string) string {
39 var cmdPath string
Ramy Medhat0fc67eb2020-08-12 01:26:23 -040040 if rbeDir := config.rbeDir(); rbeDir != "" {
Patrice Arruda62f1bf22020-07-07 12:48:26 +000041 cmdPath = filepath.Join(rbeDir, rbeCmd)
Patrice Arruda62f1bf22020-07-07 12:48:26 +000042 } 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 Medhatbbf25672019-07-17 12:30:04 +000052
Ramy Medhat0fc67eb2020-08-12 01:26:23 -040053func getRBEVars(ctx Context, config Config) map[string]string {
Ramy Medhatca1e44c2020-07-16 12:18:37 -040054 rand.Seed(time.Now().UnixNano())
Ramy Medhat0fc67eb2020-08-12 01:26:23 -040055 vars := map[string]string{
Ramy Medhata958d352020-08-13 22:53:42 -040056 "RBE_log_path": config.rbeLogPath(),
Ramy Medhat8c56bc22020-08-13 14:51:51 -040057 "RBE_log_dir": config.logDir(),
Ramy Medhata958d352020-08-13 22:53:42 -040058 "RBE_re_proxy": config.rbeReproxy(),
59 "RBE_exec_root": config.rbeExecRoot(),
60 "RBE_output_dir": config.rbeStatsOutputDir(),
61 }
62 if config.StartRBE() {
63 vars["RBE_server_address"] = fmt.Sprintf("unix://%v/reproxy_%v.sock", absPath(ctx, config.TempDir()), rand.Intn(1000))
Ramy Medhat0fc67eb2020-08-12 01:26:23 -040064 }
65 k, v := config.rbeAuth()
66 vars[k] = v
67 return vars
Ramy Medhatca1e44c2020-07-16 12:18:37 -040068}
69
Ramy Medhatbbf25672019-07-17 12:30:04 +000070func startRBE(ctx Context, config Config) {
71 ctx.BeginTrace(metrics.RunSetupTool, "rbe_bootstrap")
72 defer ctx.EndTrace()
73
74 if u := ulimitOrFatal(ctx, config, "-u"); u < rbeLeastNProcs {
75 ctx.Fatalf("max user processes is insufficient: %d; want >= %d.\n", u, rbeLeastNProcs)
76 }
77 if n := ulimitOrFatal(ctx, config, "-n"); n < rbeLeastNFiles {
78 ctx.Fatalf("max open files is insufficient: %d; want >= %d.\n", n, rbeLeastNFiles)
79 }
80
Patrice Arruda62f1bf22020-07-07 12:48:26 +000081 cmd := Command(ctx, config, "startRBE bootstrap", rbeCommand(ctx, config, bootstrapCmd))
Ramy Medhatbbf25672019-07-17 12:30:04 +000082
83 if output, err := cmd.CombinedOutput(); err != nil {
84 ctx.Fatalf("rbe bootstrap failed with: %v\n%s\n", err, output)
85 }
86}
Patrice Arruda62f1bf22020-07-07 12:48:26 +000087
88func stopRBE(ctx Context, config Config) {
89 cmd := Command(ctx, config, "stopRBE bootstrap", rbeCommand(ctx, config, bootstrapCmd), "-shutdown")
90 if output, err := cmd.CombinedOutput(); err != nil {
91 ctx.Fatalf("rbe bootstrap with shutdown failed with: %v\n%s\n", err, output)
92 }
93}
94
95// DumpRBEMetrics creates a metrics protobuf file containing RBE related metrics.
96// The protobuf file is created if RBE is enabled and the proxy service has
97// started. The proxy service is shutdown in order to dump the RBE metrics to the
98// protobuf file.
99func DumpRBEMetrics(ctx Context, config Config, filename string) {
100 ctx.BeginTrace(metrics.RunShutdownTool, "dump_rbe_metrics")
101 defer ctx.EndTrace()
102
103 // Remove the previous metrics file in case there is a failure or RBE has been
104 // disable for this run.
105 os.Remove(filename)
106
107 // If RBE is not enabled then there are no metrics to generate.
108 // If RBE does not require to start, the RBE proxy maybe started
109 // manually for debugging purpose and can generate the metrics
110 // afterwards.
111 if !config.StartRBE() {
112 return
113 }
114
Ramy Medhat0fc67eb2020-08-12 01:26:23 -0400115 outputDir := config.rbeStatsOutputDir()
Patrice Arruda62f1bf22020-07-07 12:48:26 +0000116 if outputDir == "" {
117 ctx.Fatal("RBE output dir variable not defined. Aborting metrics dumping.")
118 }
119 metricsFile := filepath.Join(outputDir, rbeMetricsPBFilename)
120
121 // Stop the proxy first in order to generate the RBE metrics protobuf file.
122 stopRBE(ctx, config)
123
Ramy Medhat0fc67eb2020-08-12 01:26:23 -0400124 if metricsFile == filename {
125 return
126 }
Patrice Arruda62f1bf22020-07-07 12:48:26 +0000127 if _, err := copyFile(metricsFile, filename); err != nil {
128 ctx.Fatalf("failed to copy %q to %q: %v\n", metricsFile, filename, err)
129 }
130}
Ramy Medhatf6689172020-08-18 17:29:48 -0400131
132// PrintGomaDeprecation prints a PSA on the deprecation of Goma if it is set for the build.
133func PrintGomaDeprecation(ctx Context, config Config) {
134 if config.UseGoma() {
135 fmt.Fprintln(ctx.Writer, "")
136 fmt.Fprintln(ctx.Writer, "Goma for Android is being deprecated and replaced with RBE.")
137 fmt.Fprintln(ctx.Writer, "See go/goma_android_deprecation for more details.")
138 fmt.Fprintln(ctx.Writer, "")
139 }
140}