Skip to content

Commit

Permalink
Merge pull request #6 from pyroscope-io/fix-sample-time-calculation
Browse files Browse the repository at this point in the history
Refine sample CPU time calculation
  • Loading branch information
kolesnikovae authored Jun 21, 2021
2 parents c22867d + cd65098 commit 75d6658
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
9 changes: 7 additions & 2 deletions nettrace/profiler/profiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func (s *SampleProfiler) Samples() map[string]time.Duration {
for i := range x.stack {
name[i] = s.sym.resolve(x.stack[i])
}
samples[strings.Join(name, ";")] += -time.Millisecond * time.Duration(x.value)
samples[strings.Join(name, ";")] += time.Duration(x.value)
}
return samples
}
Expand Down Expand Up @@ -162,17 +162,22 @@ func (s *SampleProfiler) SequencePointBlockHandler(*nettrace.SequencePointBlock)
return nil
}

// https://github.com/microsoft/perfview/blob/8a34d2d64bc958902b2fa8ea5799437df57d8de2/src/TraceEvent/TraceEvent.cs#L440-L460
func (s *SampleProfiler) addSample(e *nettrace.Blob) error {
var d clrThreadSampleTraceData
if err := binary.Read(e.Payload, binary.LittleEndian, &d); err != nil {
return err
}
rel := e.Header.TimeStamp - s.trace.SyncTimeQPC
if rel < 0 {
return nil
}
heap.Push(&s.events, &event{
typ: d.Type,
threadID: e.Header.ThreadID,
stackID: e.Header.StackID,
timestamp: e.Header.TimeStamp,
relativeTime: (e.Header.TimeStamp - s.trace.SyncTimeQPC) * 1000 / s.trace.QPCFrequency,
relativeTime: rel * (int64(time.Second) / s.trace.QPCFrequency),
})
return nil
}
Expand Down
4 changes: 2 additions & 2 deletions nettrace/profiler/thread.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,12 @@ func (t *thread) addSample(sampleType clrThreadSampleType, relativeTime int64, s

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

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

0 comments on commit 75d6658

Please sign in to comment.