@@ -12,6 +12,8 @@ import (
1212 "runtime/pprof"
1313 "runtime/trace"
1414 "sync/atomic"
15+
16+ "github.com/felixge/fgprof"
1517)
1618
1719const (
@@ -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.
123126func 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 {
0 commit comments