aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--compiler_wrapper/clang_syntax_flag_test.go4
-rw-r--r--compiler_wrapper/clang_tidy_flag.go2
-rw-r--r--compiler_wrapper/clang_tidy_flag_test.go4
-rw-r--r--compiler_wrapper/compiler_wrapper.go7
-rw-r--r--compiler_wrapper/compiler_wrapper_test.go13
-rw-r--r--compiler_wrapper/oldwrapper.go8
-rw-r--r--compiler_wrapper/oldwrapper_test.go1
-rw-r--r--compiler_wrapper/rusage_flag.go68
-rw-r--r--compiler_wrapper/rusage_flag_test.go153
9 files changed, 247 insertions, 13 deletions
diff --git a/compiler_wrapper/clang_syntax_flag_test.go b/compiler_wrapper/clang_syntax_flag_test.go
index 4f0a077b..d2661812 100644
--- a/compiler_wrapper/clang_syntax_flag_test.go
+++ b/compiler_wrapper/clang_syntax_flag_test.go
@@ -40,8 +40,8 @@ func TestForwardStdOutAndStderrFromClangSyntaxCheck(t *testing.T) {
withTestContext(t, func(ctx *testContext) {
ctx.cmdMock = func(cmd *command, stdout io.Writer, stderr io.Writer) error {
if ctx.cmdCount == 1 {
- fmt.Fprintf(stdout, "somemessage")
- fmt.Fprintf(stderr, "someerror")
+ fmt.Fprint(stdout, "somemessage")
+ fmt.Fprint(stderr, "someerror")
}
return nil
}
diff --git a/compiler_wrapper/clang_tidy_flag.go b/compiler_wrapper/clang_tidy_flag.go
index 4a648ce1..ce53cb51 100644
--- a/compiler_wrapper/clang_tidy_flag.go
+++ b/compiler_wrapper/clang_tidy_flag.go
@@ -78,7 +78,7 @@ func runClangTidy(env env, clangCmd *command, cSrcFile string) error {
if err == nil && exitCode != 0 {
// Note: We continue on purpose when clang-tidy fails
// to maintain compatibility with the previous wrapper.
- fmt.Fprintf(env.stderr(), "clang-tidy failed")
+ fmt.Fprint(env.stderr(), "clang-tidy failed")
}
return err
}
diff --git a/compiler_wrapper/clang_tidy_flag_test.go b/compiler_wrapper/clang_tidy_flag_test.go
index 1cfd40ba..9c0fc928 100644
--- a/compiler_wrapper/clang_tidy_flag_test.go
+++ b/compiler_wrapper/clang_tidy_flag_test.go
@@ -102,8 +102,8 @@ func TestForwardStdOutAndStderrFromClangTidyCall(t *testing.T) {
withClangTidyTestContext(t, func(ctx *testContext) {
ctx.cmdMock = func(cmd *command, stdout io.Writer, stderr io.Writer) error {
if ctx.cmdCount == 2 {
- fmt.Fprintf(stdout, "somemessage")
- fmt.Fprintf(stderr, "someerror")
+ fmt.Fprint(stdout, "somemessage")
+ fmt.Fprint(stderr, "someerror")
}
return nil
}
diff --git a/compiler_wrapper/compiler_wrapper.go b/compiler_wrapper/compiler_wrapper.go
index e24faef3..8c5825e8 100644
--- a/compiler_wrapper/compiler_wrapper.go
+++ b/compiler_wrapper/compiler_wrapper.go
@@ -74,9 +74,16 @@ func callCompilerInternal(env env, cfg *config, inputCmd *command) (exitCode int
}
compilerCmd = calcGccCommand(mainBuilder)
}
+ rusageLogfileName := getRusageLogFilename(env)
if shouldForceDisableWError(env) {
+ if rusageLogfileName != "" {
+ return 0, newUserErrorf("GETRUSAGE is meaningless with FORCE_DISABLE_WERROR")
+ }
return doubleBuildWithWNoError(env, cfg, compilerCmd)
}
+ if rusageLogfileName != "" {
+ return logRusage(env, rusageLogfileName, compilerCmd)
+ }
// Note: We return an exit code only if the underlying env is not
// really doing an exec, e.g. commandRecordingEnv.
return wrapSubprocessErrorWithSourceLoc(compilerCmd, env.exec(compilerCmd))
diff --git a/compiler_wrapper/compiler_wrapper_test.go b/compiler_wrapper/compiler_wrapper_test.go
index 5abf7ce9..0a041fe8 100644
--- a/compiler_wrapper/compiler_wrapper_test.go
+++ b/compiler_wrapper/compiler_wrapper_test.go
@@ -124,6 +124,19 @@ func TestLogExitCodeErrorWhenComparingToOldWrapper(t *testing.T) {
})
}
+func TestErrorOnLogRusageAndForceDisableWError(t *testing.T) {
+ withTestContext(t, func(ctx *testContext) {
+ ctx.env = []string{
+ "FORCE_DISABLE_WERROR=1",
+ "GETRUSAGE=" + filepath.Join(ctx.tempDir, "rusage.log"),
+ }
+ stderr := ctx.mustFail(callCompiler(ctx, ctx.cfg, ctx.newCommand(gccX86_64, mainCc)))
+ if err := verifyNonInternalError(stderr, "GETRUSAGE is meaningless with FORCE_DISABLE_WERROR"); err != nil {
+ t.Error(err)
+ }
+ })
+}
+
func TestPrintUserCompilerError(t *testing.T) {
buffer := bytes.Buffer{}
printCompilerError(&buffer, newUserErrorf("abcd"))
diff --git a/compiler_wrapper/oldwrapper.go b/compiler_wrapper/oldwrapper.go
index f59d0cc2..1b510f6c 100644
--- a/compiler_wrapper/oldwrapper.go
+++ b/compiler_wrapper/oldwrapper.go
@@ -20,13 +20,7 @@ const compareToOldWrapperFilePattern = "old_wrapper_compare"
// Whether the command should be executed by the old wrapper as we don't
// support it yet.
func shouldForwardToOldWrapper(env env, inputCmd *command) bool {
- switch {
- case env.getenv("GETRUSAGE") != "":
- fallthrough
- case env.getenv("BISECT_STAGE") != "":
- return true
- }
- return false
+ return env.getenv("BISECT_STAGE") != ""
}
func forwardToOldWrapper(env env, cfg *config, inputCmd *command) (exitCode int, err error) {
diff --git a/compiler_wrapper/oldwrapper_test.go b/compiler_wrapper/oldwrapper_test.go
index 7c623d6d..5c4b20c3 100644
--- a/compiler_wrapper/oldwrapper_test.go
+++ b/compiler_wrapper/oldwrapper_test.go
@@ -34,7 +34,6 @@ func TestNoForwardToOldWrapperBecauseOfEnv(t *testing.T) {
func TestForwardToOldWrapperBecauseOfEnv(t *testing.T) {
withForwardToOldWrapperTestContext(t, func(ctx *testContext) {
testEnvs := []string{
- "GETRUSAGE=abc",
"BISECT_STAGE=abc",
}
for _, testEnv := range testEnvs {
diff --git a/compiler_wrapper/rusage_flag.go b/compiler_wrapper/rusage_flag.go
new file mode 100644
index 00000000..b103bb38
--- /dev/null
+++ b/compiler_wrapper/rusage_flag.go
@@ -0,0 +1,68 @@
+package main
+
+import (
+ "fmt"
+ "os"
+ "path/filepath"
+ "strings"
+ "syscall"
+ "time"
+)
+
+func getRusageLogFilename(env env) string {
+ return env.getenv("GETRUSAGE")
+}
+
+func logRusage(env env, logFileName string, compilerCmd *command) (exitCode int, err error) {
+ rusageBefore := syscall.Rusage{}
+ if err := syscall.Getrusage(syscall.RUSAGE_CHILDREN, &rusageBefore); err != nil {
+ return 0, err
+ }
+ compilerCmdWithoutRusage := &command{
+ path: compilerCmd.path,
+ args: compilerCmd.args,
+ envUpdates: append(compilerCmd.envUpdates, "GETRUSAGE="),
+ }
+ startTime := time.Now()
+ exitCode, err = wrapSubprocessErrorWithSourceLoc(compilerCmdWithoutRusage,
+ env.run(compilerCmdWithoutRusage, env.stdout(), env.stderr()))
+ if err != nil {
+ return 0, err
+ }
+ elapsedRealTime := time.Since(startTime)
+ rusageAfter := syscall.Rusage{}
+ if err := syscall.Getrusage(syscall.RUSAGE_CHILDREN, &rusageAfter); err != nil {
+ return 0, err
+ }
+ elapsedSysTime := time.Duration(rusageAfter.Stime.Nano()-rusageBefore.Stime.Nano()) * time.Nanosecond
+ elapsedUserTime := time.Duration(rusageAfter.Utime.Nano()-rusageBefore.Utime.Nano()) * time.Nanosecond
+ // Note: We assume that the compiler takes more heap than any other
+ // subcommands that we might have executed before.
+ maxMemUsed := rusageAfter.Maxrss
+ absCompilerPath := compilerCmd.path
+ if !filepath.IsAbs(absCompilerPath) {
+ absCompilerPath = filepath.Join(env.getwd(), absCompilerPath)
+ }
+
+ if err := os.MkdirAll(filepath.Dir(logFileName), 0777); err != nil {
+ return 0, wrapErrorwithSourceLocf(err, "error creating rusage log directory %s", logFileName)
+ }
+ // Note: using file mode 0666 so that a root-created log is writable by others.
+ logFile, err := os.OpenFile(logFileName, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0666)
+ if err != nil {
+ return 0, wrapErrorwithSourceLocf(err, "error creating rusage logfile %s", logFileName)
+ }
+ timeUnit := float64(time.Second)
+ if _, err := fmt.Fprintf(logFile, "%.5f : %.5f : %.5f : %d : %s : %s\n",
+ float64(elapsedRealTime)/timeUnit, float64(elapsedUserTime)/timeUnit, float64(elapsedSysTime)/timeUnit,
+ maxMemUsed, absCompilerPath,
+ strings.Join(append([]string{filepath.Base(absCompilerPath)}, compilerCmd.args...), " ")); err != nil {
+ _ = logFile.Close()
+ return 0, wrapErrorwithSourceLocf(err, "error writing rusage logfile %s", logFileName)
+ }
+ if err := logFile.Close(); err != nil {
+ return 0, wrapErrorwithSourceLocf(err, "error closing rusage logfile %s", logFileName)
+ }
+
+ return exitCode, nil
+}
diff --git a/compiler_wrapper/rusage_flag_test.go b/compiler_wrapper/rusage_flag_test.go
new file mode 100644
index 00000000..4fb3133f
--- /dev/null
+++ b/compiler_wrapper/rusage_flag_test.go
@@ -0,0 +1,153 @@
+package main
+
+import (
+ "errors"
+ "fmt"
+ "io"
+ "io/ioutil"
+ "os"
+ "path/filepath"
+ "regexp"
+ "strconv"
+ "strings"
+ "testing"
+)
+
+func TestForwardStdOutAndStdErrAndExitCodeFromLogRusage(t *testing.T) {
+ withLogRusageTestContext(t, func(ctx *testContext) {
+ ctx.cmdMock = func(cmd *command, stdout io.Writer, stderr io.Writer) error {
+ fmt.Fprint(stdout, "somemessage")
+ fmt.Fprint(stderr, "someerror")
+ return newExitCodeError(23)
+ }
+ exitCode := callCompiler(ctx, ctx.cfg, ctx.newCommand(gccX86_64, mainCc))
+ if exitCode != 23 {
+ t.Errorf("unexpected exit code. Got: %d", exitCode)
+ }
+ if ctx.stdoutString() != "somemessage" {
+ t.Errorf("stdout was not forwarded. Got: %s", ctx.stdoutString())
+ }
+ if ctx.stderrString() != "someerror" {
+ t.Errorf("stderr was not forwarded. Got: %s", ctx.stderrString())
+ }
+ })
+}
+
+func TestReportGeneralErrorsFromLogRusage(t *testing.T) {
+ withLogRusageTestContext(t, func(ctx *testContext) {
+ ctx.cmdMock = func(cmd *command, stdout io.Writer, stderr io.Writer) error {
+ return errors.New("someerror")
+ }
+ stderr := ctx.mustFail(callCompiler(ctx, ctx.cfg,
+ ctx.newCommand(gccX86_64, mainCc)))
+ if err := verifyInternalError(stderr); err != nil {
+ t.Fatal(err)
+ }
+ if !strings.Contains(stderr, "someerror") {
+ t.Errorf("unexpected error. Got: %s", stderr)
+ }
+ })
+}
+
+func TestCreateDirAndFileForLogRusage(t *testing.T) {
+ withLogRusageTestContext(t, func(ctx *testContext) {
+ logFileName := filepath.Join(ctx.tempDir, "somedir", "rusage.log")
+ ctx.env = []string{"GETRUSAGE=" + logFileName}
+ ctx.must(callCompiler(ctx, ctx.cfg, ctx.newCommand(gccX86_64, mainCc)))
+
+ if _, err := os.Stat(logFileName); err != nil {
+ t.Errorf("rusage log file does not exist: %s", err)
+ }
+ })
+}
+
+func TestLogRusageFileContent(t *testing.T) {
+ withLogRusageTestContext(t, func(ctx *testContext) {
+ logFileName := filepath.Join(ctx.tempDir, "rusage.log")
+ ctx.env = []string{"GETRUSAGE=" + logFileName}
+ ctx.must(callCompiler(ctx, ctx.cfg, ctx.newCommand(gccX86_64, mainCc)))
+
+ data, err := ioutil.ReadFile(logFileName)
+ if err != nil {
+ t.Errorf("could not read the rusage log file. Error: %s", err)
+ }
+ // Example output:
+ // 0.100318 : 0.103412 : 0.096386 : 6508 : /tmp/compiler_wrapper036306868/x86_64-cros-linux-gnu-gcc.real : x86_64-cros-linux-gnu-gcc.real --sysroot=/tmp/compiler_wrapper036306868/usr/x86_64-cros-linux-gnu main.cc -mno-movbe
+ logParts := strings.Split(string(data), " : ")
+ if len(logParts) != 6 {
+ t.Errorf("unexpected number of rusage log parts. Got: %s", logParts)
+ }
+
+ // First 3 numbers are times in seconds.
+ for i := 0; i < 3; i++ {
+ if _, err := strconv.ParseFloat(logParts[i], 64); err != nil {
+ t.Errorf("unexpected value for index %d. Got: %s", i, logParts[i])
+ }
+ }
+ // Then an int for the memory usage
+ if _, err := strconv.ParseInt(logParts[3], 10, 64); err != nil {
+ t.Errorf("unexpected mem usage. Got: %s", logParts[3])
+ }
+ // Then the full path of the compiler
+ if logParts[4] != filepath.Join(ctx.tempDir, gccX86_64+".real") {
+ t.Errorf("unexpected compiler path. Got: %s", logParts[4])
+ }
+ // Then the arguments, prefixes with the compiler basename
+ if matched, _ := regexp.MatchString("x86_64-cros-linux-gnu-gcc.real --sysroot=.* main.cc", logParts[5]); !matched {
+ t.Errorf("unexpected compiler args. Got: %s", logParts[5])
+ }
+ })
+}
+
+func TestLogRusageAppendsToFile(t *testing.T) {
+ withLogRusageTestContext(t, func(ctx *testContext) {
+ logFileName := filepath.Join(ctx.tempDir, "rusage.log")
+ ctx.env = []string{"GETRUSAGE=" + logFileName}
+
+ ctx.must(callCompiler(ctx, ctx.cfg, ctx.newCommand(gccX86_64, mainCc)))
+ data, err := ioutil.ReadFile(logFileName)
+ if err != nil {
+ t.Errorf("could not read the rusage log file. Error: %s", err)
+ }
+ firstCallLines := strings.Split(string(data), "\n")
+ if len(firstCallLines) != 2 {
+ t.Errorf("unexpected number of lines. Got: %s", firstCallLines)
+ }
+ if firstCallLines[0] == "" {
+ t.Error("first line was empty")
+ }
+ if firstCallLines[1] != "" {
+ t.Errorf("second line was not empty. Got: %s", firstCallLines[1])
+ }
+
+ ctx.must(callCompiler(ctx, ctx.cfg, ctx.newCommand(gccX86_64, mainCc)))
+ data, err = ioutil.ReadFile(logFileName)
+ if err != nil {
+ t.Errorf("could not read the rusage log file. Error: %s", err)
+ }
+ secondCallLines := strings.Split(string(data), "\n")
+ if len(secondCallLines) != 3 {
+ t.Errorf("unexpected number of lines. Got: %s", secondCallLines)
+ }
+ if secondCallLines[0] != firstCallLines[0] {
+ t.Errorf("first line was changed. Got: %s", secondCallLines[0])
+ }
+ if secondCallLines[1] == "" {
+ t.Error("second line was empty")
+ }
+ if secondCallLines[2] != "" {
+ t.Errorf("third line was not empty. Got: %s", secondCallLines[2])
+ }
+ })
+}
+
+func withLogRusageTestContext(t *testing.T, work func(ctx *testContext)) {
+ withTestContext(t, func(ctx *testContext) {
+ // Disable comparing to the old wrapper as we that uses fork + wait3
+ // to calculate resource usage, and the new wrapper uses the getrusage
+ // syscall.
+ ctx.cfg.oldWrapperPath = ""
+ ctx.env = []string{"GETRUSAGE=" + filepath.Join(ctx.tempDir, "rusage.log")}
+ work(ctx)
+ })
+}