Skip to content

Commit

Permalink
Merge pull request #67 from yonderblue/changes
Browse files Browse the repository at this point in the history
Add clock (fgprof) profile
  • Loading branch information
davecheney authored Oct 20, 2022
2 parents 3704c8d + e103051 commit 1ac3e9a
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 0 deletions.
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
module github.com/pkg/profile

go 1.13

require github.com/felixge/fgprof v0.9.3
21 changes: 21 additions & 0 deletions profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import (
"runtime/pprof"
"runtime/trace"
"sync/atomic"

"github.com/felixge/fgprof"
)

const (
Expand All @@ -22,6 +24,7 @@ const (
traceMode
threadCreateMode
goroutineMode
clockMode
)

// Profile represents an active profiling session.
Expand Down Expand Up @@ -122,6 +125,10 @@ func ThreadcreationProfile(p *Profile) { p.mode = threadCreateMode }
// It disables any previous profiling settings.
func GoroutineProfile(p *Profile) { p.mode = goroutineMode }

// ClockProfile enables wall clock (fgprof) profiling.
// It disables any previous profiling settings.
func ClockProfile(p *Profile) { p.mode = clockMode }

// ProfilePath controls the base path where various profiling
// files are written. If blank, the base path will be generated
// by ioutil.TempDir.
Expand Down Expand Up @@ -287,6 +294,20 @@ func Start(options ...func(*Profile)) interface {
f.Close()
logf("profile: goroutine profiling disabled, %s", fn)
}

case clockMode:
fn := filepath.Join(path, "clock.pprof")
f, err := os.Create(fn)
if err != nil {
log.Fatalf("profile: could not create clock profile %q: %v", fn, err)
}
logf("profile: clock profiling enabled, %s", fn)
stop := fgprof.Start(f, fgprof.FormatPprof)
prof.closer = func() {
stop()
f.Close()
logf("profile: clock profiling disabled, %s", fn)
}
}

if !prof.noShutdownHook {
Expand Down
16 changes: 16 additions & 0 deletions profile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,22 @@ func main() {
Stderr("profile: mutex profiling enabled"),
NoErr,
},
}, {
name: "clock profile",
code: `
package main
import "github.com/pkg/profile"
func main() {
defer profile.Start(profile.ClockProfile).Stop()
}
`,
checks: []checkFn{
NoStdout,
Stderr("profile: clock profiling enabled"),
NoErr,
},
}, {
name: "profile path",
code: `
Expand Down

0 comments on commit 1ac3e9a

Please sign in to comment.