rbe.go 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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. package build
  15. import (
  16. "fmt"
  17. "os"
  18. "path/filepath"
  19. "runtime"
  20. "strings"
  21. "android/soong/remoteexec"
  22. "android/soong/ui/metrics"
  23. )
  24. const (
  25. rbeLeastNProcs = 2500
  26. rbeLeastNFiles = 16000
  27. // prebuilt RBE binaries
  28. bootstrapCmd = "bootstrap"
  29. // RBE metrics proto buffer file
  30. rbeMetricsPBFilename = "rbe_metrics.pb"
  31. defaultOutDir = "out"
  32. )
  33. func rbeCommand(ctx Context, config Config, rbeCmd string) string {
  34. var cmdPath string
  35. if rbeDir := config.rbeDir(); rbeDir != "" {
  36. cmdPath = filepath.Join(rbeDir, rbeCmd)
  37. } else {
  38. ctx.Fatalf("rbe command path not found")
  39. }
  40. if _, err := os.Stat(cmdPath); err != nil && os.IsNotExist(err) {
  41. ctx.Fatalf("rbe command %q not found", rbeCmd)
  42. }
  43. return cmdPath
  44. }
  45. func getRBEVars(ctx Context, config Config) map[string]string {
  46. vars := map[string]string{
  47. "RBE_log_dir": config.rbeProxyLogsDir(),
  48. "RBE_re_proxy": config.rbeReproxy(),
  49. "RBE_exec_root": config.rbeExecRoot(),
  50. "RBE_output_dir": config.rbeProxyLogsDir(),
  51. "RBE_proxy_log_dir": config.rbeProxyLogsDir(),
  52. "RBE_cache_dir": config.rbeCacheDir(),
  53. "RBE_platform": "container-image=" + remoteexec.DefaultImage,
  54. }
  55. if config.StartRBE() {
  56. name, err := config.rbeSockAddr(absPath(ctx, config.TempDir()))
  57. if err != nil {
  58. ctx.Fatalf("Error retrieving socket address: %v", err)
  59. return nil
  60. }
  61. vars["RBE_server_address"] = fmt.Sprintf("unix://%v", name)
  62. }
  63. rf := 1.0
  64. if config.Parallel() < runtime.NumCPU() {
  65. rf = float64(config.Parallel()) / float64(runtime.NumCPU())
  66. }
  67. vars["RBE_local_resource_fraction"] = fmt.Sprintf("%.2f", rf)
  68. k, v := config.rbeAuth()
  69. vars[k] = v
  70. return vars
  71. }
  72. func cleanupRBELogsDir(ctx Context, config Config) {
  73. if !config.shouldCleanupRBELogsDir() {
  74. return
  75. }
  76. rbeTmpDir := config.rbeProxyLogsDir()
  77. if err := os.RemoveAll(rbeTmpDir); err != nil {
  78. fmt.Fprintln(ctx.Writer, "\033[33mUnable to remove RBE log directory: ", err, "\033[0m")
  79. }
  80. }
  81. func startRBE(ctx Context, config Config) {
  82. if !config.GoogleProdCredsExist() && prodCredsAuthType(config) {
  83. ctx.Fatalf("Unable to start RBE reproxy\nFAILED: Missing LOAS credentials.")
  84. }
  85. ctx.BeginTrace(metrics.RunSetupTool, "rbe_bootstrap")
  86. defer ctx.EndTrace()
  87. ctx.Status.Status("Starting rbe...")
  88. if u := ulimitOrFatal(ctx, config, "-u"); u < rbeLeastNProcs {
  89. ctx.Fatalf("max user processes is insufficient: %d; want >= %d.\n", u, rbeLeastNProcs)
  90. }
  91. if n := ulimitOrFatal(ctx, config, "-n"); n < rbeLeastNFiles {
  92. ctx.Fatalf("max open files is insufficient: %d; want >= %d.\n", n, rbeLeastNFiles)
  93. }
  94. if _, err := os.Stat(config.rbeProxyLogsDir()); os.IsNotExist(err) {
  95. if err := os.MkdirAll(config.rbeProxyLogsDir(), 0744); err != nil {
  96. ctx.Fatalf("Unable to create logs dir (%v) for RBE: %v", config.rbeProxyLogsDir, err)
  97. }
  98. }
  99. cmd := Command(ctx, config, "startRBE bootstrap", rbeCommand(ctx, config, bootstrapCmd))
  100. if output, err := cmd.CombinedOutput(); err != nil {
  101. ctx.Fatalf("Unable to start RBE reproxy\nFAILED: RBE bootstrap failed with: %v\n%s\n", err, output)
  102. }
  103. }
  104. func stopRBE(ctx Context, config Config) {
  105. cmd := Command(ctx, config, "stopRBE bootstrap", rbeCommand(ctx, config, bootstrapCmd), "-shutdown")
  106. output, err := cmd.CombinedOutput()
  107. if err != nil {
  108. ctx.Fatalf("rbe bootstrap with shutdown failed with: %v\n%s\n", err, output)
  109. }
  110. if !config.Environment().IsEnvTrue("ANDROID_QUIET_BUILD") && len(output) > 0 {
  111. fmt.Fprintln(ctx.Writer, "")
  112. fmt.Fprintln(ctx.Writer, fmt.Sprintf("%s", output))
  113. }
  114. }
  115. func prodCredsAuthType(config Config) bool {
  116. authVar, val := config.rbeAuth()
  117. if strings.Contains(authVar, "use_google_prod_creds") && val != "" && val != "false" {
  118. return true
  119. }
  120. return false
  121. }
  122. // Check whether proper auth exists for RBE builds run within a
  123. // Google dev environment.
  124. func CheckProdCreds(ctx Context, config Config) {
  125. if !config.IsGooglerEnvironment() {
  126. return
  127. }
  128. if !config.StubbyExists() && prodCredsAuthType(config) {
  129. fmt.Fprintln(ctx.Writer, "")
  130. fmt.Fprintln(ctx.Writer, fmt.Sprintf("\033[33mWARNING: %q binary not found in $PATH, follow go/build-fast-without-stubby instead for authenticating with RBE.\033[0m", "stubby"))
  131. fmt.Fprintln(ctx.Writer, "")
  132. return
  133. }
  134. if config.GoogleProdCredsExist() {
  135. return
  136. }
  137. fmt.Fprintln(ctx.Writer, "")
  138. fmt.Fprintln(ctx.Writer, "\033[33mWARNING: Missing LOAS credentials, please run `gcert`. This will result in failing builds in the future, see go/rbe-android-default-announcement.\033[0m")
  139. fmt.Fprintln(ctx.Writer, "")
  140. }
  141. // DumpRBEMetrics creates a metrics protobuf file containing RBE related metrics.
  142. // The protobuf file is created if RBE is enabled and the proxy service has
  143. // started. The proxy service is shutdown in order to dump the RBE metrics to the
  144. // protobuf file.
  145. func DumpRBEMetrics(ctx Context, config Config, filename string) {
  146. ctx.BeginTrace(metrics.RunShutdownTool, "dump_rbe_metrics")
  147. defer ctx.EndTrace()
  148. // Remove the previous metrics file in case there is a failure or RBE has been
  149. // disable for this run.
  150. os.Remove(filename)
  151. // If RBE is not enabled then there are no metrics to generate.
  152. // If RBE does not require to start, the RBE proxy maybe started
  153. // manually for debugging purpose and can generate the metrics
  154. // afterwards.
  155. if !config.StartRBE() {
  156. return
  157. }
  158. outputDir := config.rbeProxyLogsDir()
  159. if outputDir == "" {
  160. ctx.Fatal("RBE output dir variable not defined. Aborting metrics dumping.")
  161. }
  162. metricsFile := filepath.Join(outputDir, rbeMetricsPBFilename)
  163. // Stop the proxy first in order to generate the RBE metrics protobuf file.
  164. stopRBE(ctx, config)
  165. if metricsFile == filename {
  166. return
  167. }
  168. if _, err := copyFile(metricsFile, filename); err != nil {
  169. ctx.Fatalf("failed to copy %q to %q: %v\n", metricsFile, filename, err)
  170. }
  171. }
  172. // PrintOutDirWarning prints a warning to indicate to the user that
  173. // setting output directory to a path other than "out" in an RBE enabled
  174. // build can cause slow builds.
  175. func PrintOutDirWarning(ctx Context, config Config) {
  176. if config.UseRBE() && config.OutDir() != defaultOutDir {
  177. fmt.Fprintln(ctx.Writer, "")
  178. fmt.Fprintln(ctx.Writer, "\033[33mWARNING:\033[0m")
  179. fmt.Fprintln(ctx.Writer, fmt.Sprintf("Setting OUT_DIR to a path other than %v may result in slow RBE builds.", defaultOutDir))
  180. fmt.Fprintln(ctx.Writer, "See http://go/android_rbe_out_dir for a workaround.")
  181. fmt.Fprintln(ctx.Writer, "")
  182. }
  183. }