Skip to content

Commit 75d6658

Browse files
authored
Merge pull request #6 from pyroscope-io/fix-sample-time-calculation
Refine sample CPU time calculation
2 parents c22867d + cd65098 commit 75d6658

File tree

2 files changed

+9
-4
lines changed

2 files changed

+9
-4
lines changed

nettrace/profiler/profiler.go

+7-2
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ func (s *SampleProfiler) Samples() map[string]time.Duration {
104104
for i := range x.stack {
105105
name[i] = s.sym.resolve(x.stack[i])
106106
}
107-
samples[strings.Join(name, ";")] += -time.Millisecond * time.Duration(x.value)
107+
samples[strings.Join(name, ";")] += time.Duration(x.value)
108108
}
109109
return samples
110110
}
@@ -162,17 +162,22 @@ func (s *SampleProfiler) SequencePointBlockHandler(*nettrace.SequencePointBlock)
162162
return nil
163163
}
164164

165+
// https://github.com/microsoft/perfview/blob/8a34d2d64bc958902b2fa8ea5799437df57d8de2/src/TraceEvent/TraceEvent.cs#L440-L460
165166
func (s *SampleProfiler) addSample(e *nettrace.Blob) error {
166167
var d clrThreadSampleTraceData
167168
if err := binary.Read(e.Payload, binary.LittleEndian, &d); err != nil {
168169
return err
169170
}
171+
rel := e.Header.TimeStamp - s.trace.SyncTimeQPC
172+
if rel < 0 {
173+
return nil
174+
}
170175
heap.Push(&s.events, &event{
171176
typ: d.Type,
172177
threadID: e.Header.ThreadID,
173178
stackID: e.Header.StackID,
174179
timestamp: e.Header.TimeStamp,
175-
relativeTime: (e.Header.TimeStamp - s.trace.SyncTimeQPC) * 1000 / s.trace.QPCFrequency,
180+
relativeTime: rel * (int64(time.Second) / s.trace.QPCFrequency),
176181
})
177182
return nil
178183
}

nettrace/profiler/thread.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,12 @@ func (t *thread) addSample(sampleType clrThreadSampleType, relativeTime int64, s
5959

6060
func (t *thread) managedSample(stackID int32, rt int64) {
6161
if t.lastManagedTime > 0 {
62-
t.samples[stackID] += t.lastManagedTime - rt
62+
t.samples[stackID] += rt - t.lastManagedTime
6363
}
6464
}
6565

6666
func (t *thread) externalSample(stackID int32, rt int64) {
6767
if t.lastExternalTime > 0 && !t.managedOnly {
68-
t.samples[stackID] += t.lastExternalTime - rt
68+
t.samples[stackID] += rt - t.lastExternalTime
6969
}
7070
}

0 commit comments

Comments
 (0)