Skip to content

Commit

Permalink
Set time and duration of profile
Browse files Browse the repository at this point in the history
  • Loading branch information
brancz committed Apr 27, 2022
1 parent 1badc5b commit ec26bfb
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
10 changes: 9 additions & 1 deletion fgprof.go
Expand Up @@ -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
Expand All @@ -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,
)
}
}

Expand Down
7 changes: 4 additions & 3 deletions format.go
Expand Up @@ -5,6 +5,7 @@ import (
"io"
"sort"
"strings"
"time"

"github.com/google/pprof/profile"
)
Expand All @@ -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)
}
Expand All @@ -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)
Expand Down

0 comments on commit ec26bfb

Please sign in to comment.