From cd650988894a15e5cc23e341e69c313a78d3bf99 Mon Sep 17 00:00:00 2001 From: Anton Kolesnikov Date: Mon, 21 Jun 2021 17:08:06 +0200 Subject: [PATCH] Refine sample CPU time calculation --- nettrace/profiler/profiler.go | 9 +++++++-- nettrace/profiler/thread.go | 4 ++-- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/nettrace/profiler/profiler.go b/nettrace/profiler/profiler.go index 1b56bf2..94e0232 100644 --- a/nettrace/profiler/profiler.go +++ b/nettrace/profiler/profiler.go @@ -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 } @@ -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 } diff --git a/nettrace/profiler/thread.go b/nettrace/profiler/thread.go index 093fd20..18be719 100644 --- a/nettrace/profiler/thread.go +++ b/nettrace/profiler/thread.go @@ -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 } }