Skip to content

Commit

Permalink
Add support for execution traces (#23)
Browse files Browse the repository at this point in the history
* Add support for execution traces

- Only available on Go 1.7+

* Fix support for Go 1.6 and earlier
  • Loading branch information
davecheney authored Aug 1, 2016
1 parent 7b053ad commit 1c16f11
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 0 deletions.
16 changes: 16 additions & 0 deletions profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const (
cpuMode = iota
memMode
blockMode
traceMode
)

type profile struct {
Expand Down Expand Up @@ -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 {
Expand Down
11 changes: 11 additions & 0 deletions trace.go
Original file line number Diff line number Diff line change
@@ -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
10 changes: 10 additions & 0 deletions trace16.go
Original file line number Diff line number Diff line change
@@ -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() {}
10 changes: 10 additions & 0 deletions trace_test.go
Original file line number Diff line number Diff line change
@@ -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()
}

0 comments on commit 1c16f11

Please sign in to comment.