driver: don't use runtime/trace < Go 1.5
Currently the benchmark driver does not build prior to Go 1.5 because
it uses runtime/trace, which was added in Go 1.5. Fix this by moving
the code to start and stop tracing to a version-gated source file.
Change-Id: I3267e496ad7af80ce4fd31fa653b86206df672e1
Reviewed-on: https://go-review.googlesource.com/18067
Reviewed-by: Dmitry Vyukov <dvyukov@google.com>
diff --git a/driver/driver.go b/driver/driver.go
index b5d307d..2938644 100644
--- a/driver/driver.go
+++ b/driver/driver.go
@@ -22,7 +22,6 @@
"path/filepath"
"runtime"
"runtime/pprof"
- "runtime/trace"
"sort"
"strings"
"sync"
@@ -39,7 +38,6 @@
affinity = flag.Int("affinity", 0, "process affinity (passed to an OS-specific function like sched_setaffinity/SetProcessAffinityMask)")
tmpDir = flag.String("tmpdir", os.TempDir(), "dir for temporary files")
genSvg = flag.Bool("svg", false, "generate svg profiles")
- traceFile = flag.String("trace", "", "write an execution trace to the named file after execution")
BenchNum int
BenchMem int
@@ -47,6 +45,12 @@
WorkDir string
benchmarks = make(map[string]func() Result)
+
+ // startTrace starts runtime tracing if supported and
+ // requested and returns a function to stop tracing.
+ startTrace = func() func() {
+ return func() {}
+ }
)
func Register(name string, f func() Result) {
@@ -82,19 +86,8 @@
return
}
- if *traceFile != "" {
- f, err := os.Create(*traceFile)
- if err != nil {
- fmt.Fprintln(os.Stderr, err)
- os.Exit(1)
- }
- defer f.Close()
- if err := trace.Start(f); err != nil {
- fmt.Fprintf(os.Stderr, "can't start tracing: %s\n", err)
- os.Exit(1)
- }
- defer trace.Stop()
- }
+ stopTrace := startTrace()
+ defer stopTrace()
res := f()
diff --git a/driver/driver_go15.go b/driver/driver_go15.go
new file mode 100644
index 0000000..f6cd9ca
--- /dev/null
+++ b/driver/driver_go15.go
@@ -0,0 +1,41 @@
+// Copyright 2015 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+// +build go1.5
+
+package driver
+
+import (
+ "flag"
+ "fmt"
+ "os"
+ "runtime/trace"
+)
+
+var traceFile = flag.String("trace", "", "write an execution trace to the named file after execution")
+
+func startTraceGo15() func() {
+ // New runtime tracing added in Go 1.5.
+ if *traceFile != "" {
+ f, err := os.Create(*traceFile)
+ if err != nil {
+ fmt.Fprintln(os.Stderr, err)
+ os.Exit(1)
+ }
+ if err := trace.Start(f); err != nil {
+ fmt.Fprintf(os.Stderr, "can't start tracing: %s\n", err)
+ os.Exit(1)
+ }
+
+ return func() {
+ trace.Stop()
+ f.Close()
+ }
+ }
+ return func() {}
+}
+
+func init() {
+ startTrace = startTraceGo15
+}