goma.go 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. // Copyright 2018 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. "math"
  18. "path/filepath"
  19. "strconv"
  20. "strings"
  21. "android/soong/ui/metrics"
  22. )
  23. const gomaCtlScript = "goma_ctl.py"
  24. const gomaLeastNProcs = 2500
  25. const gomaLeastNFiles = 16000
  26. // ulimit returns ulimit result for |opt|.
  27. // if the resource is unlimited, it returns math.MaxInt32 so that a caller do
  28. // not need special handling of the returned value.
  29. //
  30. // Note that since go syscall package do not have RLIMIT_NPROC constant,
  31. // we use bash ulimit instead.
  32. func ulimitOrFatal(ctx Context, config Config, opt string) int {
  33. commandText := fmt.Sprintf("ulimit %s", opt)
  34. cmd := Command(ctx, config, commandText, "bash", "-c", commandText)
  35. output := strings.TrimRight(string(cmd.CombinedOutputOrFatal()), "\n")
  36. ctx.Verbose(output + "\n")
  37. ctx.Verbose("done\n")
  38. if output == "unlimited" {
  39. return math.MaxInt32
  40. }
  41. num, err := strconv.Atoi(output)
  42. if err != nil {
  43. ctx.Fatalf("ulimit returned unexpected value: %s: %v\n", opt, err)
  44. }
  45. return num
  46. }
  47. func startGoma(ctx Context, config Config) {
  48. ctx.BeginTrace(metrics.RunSetupTool, "goma_ctl")
  49. defer ctx.EndTrace()
  50. if u := ulimitOrFatal(ctx, config, "-u"); u < gomaLeastNProcs {
  51. ctx.Fatalf("max user processes is insufficient: %d; want >= %d.\n", u, gomaLeastNProcs)
  52. }
  53. if n := ulimitOrFatal(ctx, config, "-n"); n < gomaLeastNFiles {
  54. ctx.Fatalf("max open files is insufficient: %d; want >= %d.\n", n, gomaLeastNFiles)
  55. }
  56. var gomaCtl string
  57. if gomaDir, ok := config.Environment().Get("GOMA_DIR"); ok {
  58. gomaCtl = filepath.Join(gomaDir, gomaCtlScript)
  59. } else if home, ok := config.Environment().Get("HOME"); ok {
  60. gomaCtl = filepath.Join(home, "goma", gomaCtlScript)
  61. } else {
  62. ctx.Fatalln("goma_ctl.py not found")
  63. }
  64. cmd := Command(ctx, config, "goma_ctl.py ensure_start", gomaCtl, "ensure_start")
  65. cmd.Environment.Set("DIST_DIR", config.DistDir())
  66. if output, err := cmd.CombinedOutput(); err != nil {
  67. ctx.Fatalf("goma_ctl.py ensure_start failed with: %v\n%s\n", err, output)
  68. }
  69. }