2019-01-10 09:14:16 +08:00
|
|
|
// Copyright 2018 Google Inc. All rights reserved.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package build
|
|
|
|
|
|
|
|
import (
|
2019-01-11 11:30:37 +08:00
|
|
|
"fmt"
|
|
|
|
"math"
|
2019-01-10 09:14:16 +08:00
|
|
|
"path/filepath"
|
2019-01-11 11:30:37 +08:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
2019-01-10 09:14:16 +08:00
|
|
|
|
|
|
|
"android/soong/ui/metrics"
|
|
|
|
)
|
|
|
|
|
|
|
|
const gomaCtlScript = "goma_ctl.py"
|
2019-01-11 11:30:37 +08:00
|
|
|
const gomaLeastNProcs = 2500
|
|
|
|
const gomaLeastNFiles = 16000
|
2019-01-10 09:14:16 +08:00
|
|
|
|
2019-01-11 11:30:37 +08:00
|
|
|
// ulimit returns ulimit result for |opt|.
|
|
|
|
// if the resource is unlimited, it returns math.MaxInt32 so that a caller do
|
|
|
|
// not need special handling of the returned value.
|
|
|
|
//
|
|
|
|
// Note that since go syscall package do not have RLIMIT_NPROC constant,
|
|
|
|
// we use bash ulimit instead.
|
|
|
|
func ulimitOrFatal(ctx Context, config Config, opt string) int {
|
|
|
|
commandText := fmt.Sprintf("ulimit %s", opt)
|
|
|
|
cmd := Command(ctx, config, commandText, "bash", "-c", commandText)
|
|
|
|
output := strings.TrimRight(string(cmd.CombinedOutputOrFatal()), "\n")
|
|
|
|
ctx.Verbose(output + "\n")
|
|
|
|
ctx.Verbose("done\n")
|
|
|
|
|
|
|
|
if output == "unlimited" {
|
|
|
|
return math.MaxInt32
|
|
|
|
}
|
|
|
|
num, err := strconv.Atoi(output)
|
|
|
|
if err != nil {
|
|
|
|
ctx.Fatalf("ulimit returned unexpected value: %s: %v\n", opt, err)
|
|
|
|
}
|
|
|
|
return num
|
|
|
|
}
|
2019-01-10 09:14:16 +08:00
|
|
|
|
2019-01-11 11:30:37 +08:00
|
|
|
func startGoma(ctx Context, config Config) {
|
2019-01-10 09:14:16 +08:00
|
|
|
ctx.BeginTrace(metrics.RunSetupTool, "goma_ctl")
|
|
|
|
defer ctx.EndTrace()
|
|
|
|
|
2019-01-11 11:30:37 +08:00
|
|
|
if u := ulimitOrFatal(ctx, config, "-u"); u < gomaLeastNProcs {
|
|
|
|
ctx.Fatalf("max user processes is insufficient: %d; want >= %d.\n", u, gomaLeastNProcs)
|
|
|
|
}
|
|
|
|
if n := ulimitOrFatal(ctx, config, "-n"); n < gomaLeastNFiles {
|
|
|
|
ctx.Fatalf("max open files is insufficient: %d; want >= %d.\n", n, gomaLeastNFiles)
|
|
|
|
}
|
|
|
|
|
2019-01-10 09:14:16 +08:00
|
|
|
var gomaCtl string
|
|
|
|
if gomaDir, ok := config.Environment().Get("GOMA_DIR"); ok {
|
|
|
|
gomaCtl = filepath.Join(gomaDir, gomaCtlScript)
|
|
|
|
} else if home, ok := config.Environment().Get("HOME"); ok {
|
|
|
|
gomaCtl = filepath.Join(home, "goma", gomaCtlScript)
|
|
|
|
} else {
|
2019-01-11 11:30:37 +08:00
|
|
|
ctx.Fatalln("goma_ctl.py not found")
|
2019-01-10 09:14:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
cmd := Command(ctx, config, "goma_ctl.py ensure_start", gomaCtl, "ensure_start")
|
2019-07-24 04:16:15 +08:00
|
|
|
cmd.Environment.Set("DIST_DIR", config.DistDir())
|
2019-01-10 09:14:16 +08:00
|
|
|
|
2019-02-13 09:47:32 +08:00
|
|
|
if output, err := cmd.CombinedOutput(); err != nil {
|
|
|
|
ctx.Fatalf("goma_ctl.py ensure_start failed with: %v\n%s\n", err, output)
|
2019-01-10 09:14:16 +08:00
|
|
|
}
|
|
|
|
}
|