Skip to content

Commit 1b4aea8

Browse files
committed
Set time and duration of profile
1 parent 3eac545 commit 1b4aea8

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

fgprof.go

+11-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ import (
1414
// that needs to be invoked by the caller to stop the profiling and write the
1515
// results to w using the given format.
1616
func Start(w io.Writer, format Format) func() error {
17+
startTime := time.Now()
18+
1719
// Go's CPU profiler uses 100hz, but 99hz might be less likely to result in
1820
// accidental synchronization with the program we're profiling.
1921
const hz = 99
@@ -39,7 +41,15 @@ func Start(w io.Writer, format Format) func() error {
3941

4042
return func() error {
4143
stopCh <- struct{}{}
42-
return writeFormat(w, stackCounts.HumanMap(prof.SelfFrame()), format, hz)
44+
endTime := time.Now()
45+
return writeFormat(
46+
w,
47+
stackCounts.HumanMap(prof.SelfFrame()),
48+
format,
49+
hz,
50+
startTime,
51+
endTime,
52+
)
4353
}
4454
}
4555

format.go

+6-3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"io"
66
"sort"
77
"strings"
8+
"time"
89

910
"github.com/google/pprof/profile"
1011
)
@@ -21,12 +22,12 @@ const (
2122
FormatPprof Format = "pprof"
2223
)
2324

24-
func writeFormat(w io.Writer, s map[string]int, f Format, hz int) error {
25+
func writeFormat(w io.Writer, s map[string]int, f Format, hz int, startTime, endTime time.Time) error {
2526
switch f {
2627
case FormatFolded:
2728
return writeFolded(w, s)
2829
case FormatPprof:
29-
return toPprof(s, hz).Write(w)
30+
return toPprof(s, hz, startTime, endTime).Write(w)
3031
default:
3132
return fmt.Errorf("unknown format: %q", f)
3233
}
@@ -42,14 +43,16 @@ func writeFolded(w io.Writer, s map[string]int) error {
4243
return nil
4344
}
4445

45-
func toPprof(s map[string]int, hz int) *profile.Profile {
46+
func toPprof(s map[string]int, hz int, startTime, endTime time.Time) *profile.Profile {
4647
functionID := uint64(1)
4748
locationID := uint64(1)
4849
line := int64(1)
4950

5051
p := &profile.Profile{}
5152
m := &profile.Mapping{ID: 1, HasFunctions: true}
5253
p.Period = int64(1e9 / hz) // Number of nanoseconds between samples.
54+
p.TimeNanos = startTime.UnixNano()
55+
p.DurationNanos = endTime.UnixNano() - p.TimeNanos
5356
p.Mapping = []*profile.Mapping{m}
5457
p.SampleType = []*profile.ValueType{
5558
{

0 commit comments

Comments
 (0)