diff --git a/fgprof.go b/fgprof.go index 31871c8..2716e6c 100644 --- a/fgprof.go +++ b/fgprof.go @@ -14,6 +14,8 @@ import ( // that needs to be invoked by the caller to stop the profiling and write the // results to w using the given format. func Start(w io.Writer, format Format) func() error { + startTime := time.Now() + // Go's CPU profiler uses 100hz, but 99hz might be less likely to result in // accidental synchronization with the program we're profiling. const hz = 99 @@ -39,7 +41,13 @@ func Start(w io.Writer, format Format) func() error { return func() error { stopCh <- struct{}{} - return writeFormat(w, stackCounts.HumanMap(prof.SelfFrame()), format, hz) + return writeFormat( + w, + stackCounts.HumanMap(prof.SelfFrame()), + format, + hz, + startTime, + ) } } diff --git a/format.go b/format.go index c9bfe9f..053d4d5 100644 --- a/format.go +++ b/format.go @@ -5,6 +5,7 @@ import ( "io" "sort" "strings" + "time" "github.com/google/pprof/profile" ) @@ -21,12 +22,12 @@ const ( FormatPprof Format = "pprof" ) -func writeFormat(w io.Writer, s map[string]int, f Format, hz int) error { +func writeFormat(w io.Writer, s map[string]int, f Format, hz int, startTime time.Time) error { switch f { case FormatFolded: return writeFolded(w, s) case FormatPprof: - return toPprof(s, hz).Write(w) + return toPprof(s, hz, startTime).Write(w) default: return fmt.Errorf("unknown format: %q", f) } @@ -42,7 +43,7 @@ func writeFolded(w io.Writer, s map[string]int) error { return nil } -func toPprof(s map[string]int, hz int) *profile.Profile { +func toPprof(s map[string]int, hz int, startTime time.Time) *profile.Profile { functionID := uint64(1) locationID := uint64(1) line := int64(1)