diff --git a/profile.go b/profile.go index d7a5d2c..e0a2ff3 100644 --- a/profile.go +++ b/profile.go @@ -17,6 +17,7 @@ const ( cpuMode = iota memMode blockMode + traceMode ) type profile struct { @@ -181,6 +182,21 @@ func Start(options ...func(*profile)) interface { runtime.SetBlockProfileRate(0) logf("profile: block profiling disabled, %s", fn) } + + case traceMode: + fn := filepath.Join(path, "trace.out") + f, err := os.Create(fn) + if err != nil { + log.Fatalf("profile: could not create trace output file %q: %v", fn, err) + } + if err := startTrace(f); err != nil { + log.Fatalf("profile: could not start trace: %v", err) + } + logf("profile: trace enabled, %s", fn) + prof.closer = func() { + stopTrace() + logf("profile: trace disabled, %s", fn) + } } if !prof.noShutdownHook { diff --git a/trace.go b/trace.go new file mode 100644 index 0000000..ad2ccf7 --- /dev/null +++ b/trace.go @@ -0,0 +1,11 @@ +// +build go1.7 + +package profile + +import "runtime/trace" + +// Trace profile controls if execution tracing will be enabled. It disables any previous profiling settings. +func TraceProfile(p *profile) { p.mode = traceMode } + +var startTrace = trace.Start +var stopTrace = trace.Stop diff --git a/trace16.go b/trace16.go new file mode 100644 index 0000000..6aa6566 --- /dev/null +++ b/trace16.go @@ -0,0 +1,10 @@ +// +build !go1.7 + +package profile + +import "io" + +// mock trace support for Go 1.6 and earlier. + +func startTrace(w io.Writer) error { return nil } +func stopTrace() {} diff --git a/trace_test.go b/trace_test.go new file mode 100644 index 0000000..1063f4d --- /dev/null +++ b/trace_test.go @@ -0,0 +1,10 @@ +// +build go1.7 + +package profile_test + +import "github.com/pkg/profile" + +func ExampleTraceProfile() { + // use execution tracing, rather than the default cpu profiling. + defer profile.Start(profile.TraceProfile).Stop() +}