1- using System . Collections . Generic ;
1+ using System ;
2+ using System . Collections . Generic ;
23using System . Diagnostics . CodeAnalysis ;
34using System . Linq ;
45using BenchmarkDotNet . Columns ;
56using BenchmarkDotNet . Diagnosers ;
7+ using BenchmarkDotNet . Engines ;
68using BenchmarkDotNet . Loggers ;
79using BenchmarkDotNet . Reports ;
810using BenchmarkDotNet . Running ;
911using Microsoft . Diagnostics . Tracing . Etlx ;
1012using Microsoft . Diagnostics . Tracing . Parsers . Kernel ;
13+ using Microsoft . Diagnostics . Tracing . Stacks ;
1114using Address = System . UInt64 ;
1215
1316namespace BenchmarkDotNet . Diagnostics . Windows . Tracing
@@ -23,18 +26,27 @@ public class NativeMemoryLogParser
2326
2427 private readonly ILogger logger ;
2528
26- public NativeMemoryLogParser ( string etlFilePath , BenchmarkCase benchmarkCase , ILogger logger )
29+ private readonly string moduleName ;
30+
31+ private readonly string functionName ;
32+
33+ public NativeMemoryLogParser ( string etlFilePath , BenchmarkCase benchmarkCase , ILogger logger ,
34+ string programName )
2735 {
2836 this . etlFilePath = etlFilePath ;
2937 this . benchmarkCase = benchmarkCase ;
3038 this . logger = logger ;
39+
40+ moduleName = programName ;
41+ functionName = nameof ( EngineParameters . WorkloadActionUnroll ) ;
3142 }
3243
3344 //Code is inspired by https://github.com/Microsoft/perfview/blob/master/src/PerfView/PerfViewData.cs#L5719-L5944
3445 public IEnumerable < Metric > Parse ( )
3546 {
3647 using ( var traceLog = new TraceLog ( TraceLog . CreateFromEventTraceLogFile ( etlFilePath ) ) )
3748 {
49+ var stackSource = new MutableTraceEventStackSource ( traceLog ) ;
3850 var eventSource = traceLog . Events . GetSource ( ) ;
3951
4052 var bdnEventsParser = new EngineEventLogParser ( eventSource ) ;
@@ -77,6 +89,14 @@ public IEnumerable<Metric> Parse()
7789 return ;
7890 }
7991
92+ var call = data . CallStackIndex ( ) ;
93+ var frameIndex = stackSource . GetCallStack ( call , data ) ;
94+
95+ if ( ! IsCallStackIn ( frameIndex ) )
96+ {
97+ return ;
98+ }
99+
80100 var allocs = lastHeapAllocs ;
81101 if ( data . HeapHandle != lastHeapHandle )
82102 {
@@ -91,7 +111,27 @@ public IEnumerable<Metric> Parse()
91111 nativeLeakSize += data . AllocSize ;
92112 totalAllocation += data . AllocSize ;
93113 }
114+
115+ bool IsCallStackIn ( StackSourceCallStackIndex index )
116+ {
117+ while ( index != StackSourceCallStackIndex . Invalid )
118+ {
119+ var frame = stackSource . GetFrameIndex ( index ) ;
120+ var name = stackSource . GetFrameName ( frame , false ) ;
121+
122+ if ( name . StartsWith ( moduleName , StringComparison . Ordinal ) &&
123+ name . IndexOf ( functionName , StringComparison . Ordinal ) > 0 )
124+ {
125+ return true ;
126+ }
127+
128+ index = stackSource . GetCallerIndex ( index ) ;
129+ }
130+
131+ return false ;
132+ }
94133 } ;
134+
95135 heapParser . HeapTraceFree += delegate ( HeapFreeTraceData data )
96136 {
97137 if ( ! start )
@@ -112,6 +152,7 @@ public IEnumerable<Metric> Parse()
112152 allocs . Remove ( data . FreeAddress ) ;
113153 }
114154 } ;
155+
115156 heapParser . HeapTraceReAlloc += delegate ( HeapReallocTraceData data )
116157 {
117158 if ( ! start )
@@ -132,22 +173,23 @@ public IEnumerable<Metric> Parse()
132173 allocs = CreateHeapCache ( data . HeapHandle , heaps , ref lastHeapAllocs , ref lastHeapHandle ) ;
133174 }
134175
135- // This is a clone of the Free code
136176 if ( allocs . TryGetValue ( data . OldAllocAddress , out long alloc ) )
137177 {
178+ // Free
138179 nativeLeakSize -= alloc ;
139180
140181 allocs . Remove ( data . OldAllocAddress ) ;
141- }
142182
143- // This is a clone of the Alloc code (sigh don't clone code)
144- allocs [ data . NewAllocAddress ] = data . NewAllocSize ;
183+ // Alloc
184+ allocs [ data . NewAllocAddress ] = data . NewAllocSize ;
145185
146- checked
147- {
148- nativeLeakSize += data . NewAllocSize ;
186+ checked
187+ {
188+ nativeLeakSize += data . NewAllocSize ;
189+ }
149190 }
150191 } ;
192+
151193 heapParser . HeapTraceDestroy += delegate ( HeapTraceData data )
152194 {
153195 if ( ! start )
0 commit comments