blob: ae9b7849828210727bc3a25239a9bce6a6e390ad [file] [log] [blame]
Yoshisato Yanagisawa2cb0e5d2019-01-10 10:14:16 +09001// 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
15package build
16
17import (
Yoshisato Yanagisawa0c517bd2019-01-11 12:30:37 +090018 "fmt"
19 "math"
Yoshisato Yanagisawa2cb0e5d2019-01-10 10:14:16 +090020 "path/filepath"
Yoshisato Yanagisawa0c517bd2019-01-11 12:30:37 +090021 "strconv"
22 "strings"
Yoshisato Yanagisawa2cb0e5d2019-01-10 10:14:16 +090023
24 "android/soong/ui/metrics"
25)
26
27const gomaCtlScript = "goma_ctl.py"
Yoshisato Yanagisawa0c517bd2019-01-11 12:30:37 +090028const gomaLeastNProcs = 2500
29const gomaLeastNFiles = 16000
Yoshisato Yanagisawa2cb0e5d2019-01-10 10:14:16 +090030
Yoshisato Yanagisawa0c517bd2019-01-11 12:30:37 +090031// ulimit returns ulimit result for |opt|.
32// if the resource is unlimited, it returns math.MaxInt32 so that a caller do
33// not need special handling of the returned value.
34//
35// Note that since go syscall package do not have RLIMIT_NPROC constant,
36// we use bash ulimit instead.
37func ulimitOrFatal(ctx Context, config Config, opt string) int {
38 commandText := fmt.Sprintf("ulimit %s", opt)
39 cmd := Command(ctx, config, commandText, "bash", "-c", commandText)
40 output := strings.TrimRight(string(cmd.CombinedOutputOrFatal()), "\n")
41 ctx.Verbose(output + "\n")
42 ctx.Verbose("done\n")
Yoshisato Yanagisawa2cb0e5d2019-01-10 10:14:16 +090043
Yoshisato Yanagisawa0c517bd2019-01-11 12:30:37 +090044 if output == "unlimited" {
45 return math.MaxInt32
46 }
47 num, err := strconv.Atoi(output)
48 if err != nil {
49 ctx.Fatalf("ulimit returned unexpected value: %s: %v\n", opt, err)
50 }
51 return num
52}
53
54func startGoma(ctx Context, config Config) {
Yoshisato Yanagisawa2cb0e5d2019-01-10 10:14:16 +090055 ctx.BeginTrace(metrics.RunSetupTool, "goma_ctl")
56 defer ctx.EndTrace()
57
Yoshisato Yanagisawa0c517bd2019-01-11 12:30:37 +090058 if u := ulimitOrFatal(ctx, config, "-u"); u < gomaLeastNProcs {
59 ctx.Fatalf("max user processes is insufficient: %d; want >= %d.\n", u, gomaLeastNProcs)
60 }
61 if n := ulimitOrFatal(ctx, config, "-n"); n < gomaLeastNFiles {
62 ctx.Fatalf("max open files is insufficient: %d; want >= %d.\n", n, gomaLeastNFiles)
63 }
64
Yoshisato Yanagisawa2cb0e5d2019-01-10 10:14:16 +090065 var gomaCtl string
66 if gomaDir, ok := config.Environment().Get("GOMA_DIR"); ok {
67 gomaCtl = filepath.Join(gomaDir, gomaCtlScript)
68 } else if home, ok := config.Environment().Get("HOME"); ok {
69 gomaCtl = filepath.Join(home, "goma", gomaCtlScript)
70 } else {
Yoshisato Yanagisawa0c517bd2019-01-11 12:30:37 +090071 ctx.Fatalln("goma_ctl.py not found")
Yoshisato Yanagisawa2cb0e5d2019-01-10 10:14:16 +090072 }
73
74 cmd := Command(ctx, config, "goma_ctl.py ensure_start", gomaCtl, "ensure_start")
Kousik Kumar61afb562019-07-23 16:16:15 -040075 cmd.Environment.Set("DIST_DIR", config.DistDir())
Yoshisato Yanagisawa2cb0e5d2019-01-10 10:14:16 +090076
Yoshisato Yanagisawa02eacca2019-02-13 10:47:32 +090077 if output, err := cmd.CombinedOutput(); err != nil {
78 ctx.Fatalf("goma_ctl.py ensure_start failed with: %v\n%s\n", err, output)
Yoshisato Yanagisawa2cb0e5d2019-01-10 10:14:16 +090079 }
Yoshisato Yanagisawa2cb0e5d2019-01-10 10:14:16 +090080}