Skip to content

Commit 1ac3e9a

Browse files
authored
Merge pull request #67 from yonderblue/changes
Add clock (fgprof) profile
2 parents 3704c8d + e103051 commit 1ac3e9a

3 files changed

Lines changed: 39 additions & 0 deletions

File tree

go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
module github.com/pkg/profile
22

33
go 1.13
4+
5+
require github.com/felixge/fgprof v0.9.3

profile.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ import (
1212
"runtime/pprof"
1313
"runtime/trace"
1414
"sync/atomic"
15+
16+
"github.com/felixge/fgprof"
1517
)
1618

1719
const (
@@ -22,6 +24,7 @@ const (
2224
traceMode
2325
threadCreateMode
2426
goroutineMode
27+
clockMode
2528
)
2629

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

128+
// ClockProfile enables wall clock (fgprof) profiling.
129+
// It disables any previous profiling settings.
130+
func ClockProfile(p *Profile) { p.mode = clockMode }
131+
125132
// ProfilePath controls the base path where various profiling
126133
// files are written. If blank, the base path will be generated
127134
// by ioutil.TempDir.
@@ -287,6 +294,20 @@ func Start(options ...func(*Profile)) interface {
287294
f.Close()
288295
logf("profile: goroutine profiling disabled, %s", fn)
289296
}
297+
298+
case clockMode:
299+
fn := filepath.Join(path, "clock.pprof")
300+
f, err := os.Create(fn)
301+
if err != nil {
302+
log.Fatalf("profile: could not create clock profile %q: %v", fn, err)
303+
}
304+
logf("profile: clock profiling enabled, %s", fn)
305+
stop := fgprof.Start(f, fgprof.FormatPprof)
306+
prof.closer = func() {
307+
stop()
308+
f.Close()
309+
logf("profile: clock profiling disabled, %s", fn)
310+
}
290311
}
291312

292313
if !prof.noShutdownHook {

profile_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,22 @@ func main() {
122122
Stderr("profile: mutex profiling enabled"),
123123
NoErr,
124124
},
125+
}, {
126+
name: "clock profile",
127+
code: `
128+
package main
129+
130+
import "github.com/pkg/profile"
131+
132+
func main() {
133+
defer profile.Start(profile.ClockProfile).Stop()
134+
}
135+
`,
136+
checks: []checkFn{
137+
NoStdout,
138+
Stderr("profile: clock profiling enabled"),
139+
NoErr,
140+
},
125141
}, {
126142
name: "profile path",
127143
code: `

0 commit comments

Comments
 (0)