|
| 1 | +using System.Collections.Generic; |
| 2 | +using System.Threading; |
| 3 | +using BenchmarkDotNet.Columns; |
| 4 | +using BenchmarkDotNet.Diagnosers; |
| 5 | +using BenchmarkDotNet.Reports; |
| 6 | +using BenchmarkDotNet.Running; |
| 7 | +using Microsoft.Diagnostics.Tracing.Parsers; |
| 8 | +using Microsoft.Diagnostics.Tracing.Session; |
| 9 | + |
| 10 | +namespace BenchmarkDotNet.Diagnostics.Windows |
| 11 | +{ |
| 12 | + public class JitStatsDiagnoser : JitDiagnoser<JitStats>, IProfiler |
| 13 | + { |
| 14 | + public override IEnumerable<string> Ids => new[] { nameof(JitStatsDiagnoser) }; |
| 15 | + |
| 16 | + public string ShortName => "jit"; |
| 17 | + |
| 18 | + protected override ulong EventType => (ulong)(ClrTraceEventParser.Keywords.Jit | ClrTraceEventParser.Keywords.Compilation); |
| 19 | + |
| 20 | + public override IEnumerable<Metric> ProcessResults(DiagnoserResults results) |
| 21 | + { |
| 22 | + if (BenchmarkToProcess.TryGetValue(results.BenchmarkCase, out int pid)) |
| 23 | + { |
| 24 | + if (StatsPerProcess.TryGetValue(pid, out JitStats jitStats)) |
| 25 | + { |
| 26 | + yield return new Metric(MethodsJittedDescriptor.Instance, jitStats.MethodsCompiled); |
| 27 | + yield return new Metric(MethodsTieredDescriptor.Instance, jitStats.MethodsTiered); |
| 28 | + yield return new Metric(JitAllocatedMemoryDescriptor.Instance, jitStats.MemoryAllocated); |
| 29 | + } |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + protected override void AttachToEvents(TraceEventSession session, BenchmarkCase benchmarkCase) |
| 34 | + { |
| 35 | + session.Source.Clr.MethodJittingStarted += methodData => |
| 36 | + { |
| 37 | + if (StatsPerProcess.TryGetValue(methodData.ProcessID, out JitStats jitStats)) |
| 38 | + { |
| 39 | + Interlocked.Increment(ref jitStats.MethodsCompiled); |
| 40 | + } |
| 41 | + }; |
| 42 | + |
| 43 | + session.Source.Clr.MethodMemoryAllocatedForJitCode += memoryAllocated => |
| 44 | + { |
| 45 | + if (StatsPerProcess.TryGetValue(memoryAllocated.ProcessID, out JitStats jitStats)) |
| 46 | + { |
| 47 | + Interlocked.Add(ref jitStats.MemoryAllocated, memoryAllocated.AllocatedSizeForJitCode); |
| 48 | + } |
| 49 | + }; |
| 50 | + |
| 51 | + session.Source.Clr.TieredCompilationBackgroundJitStart += tieredData => |
| 52 | + { |
| 53 | + if (StatsPerProcess.TryGetValue(tieredData.ProcessID, out JitStats jitStats)) |
| 54 | + { |
| 55 | + Interlocked.Increment(ref jitStats.MethodsTiered); |
| 56 | + } |
| 57 | + }; |
| 58 | + } |
| 59 | + |
| 60 | + private sealed class MethodsJittedDescriptor : IMetricDescriptor |
| 61 | + { |
| 62 | + internal static readonly MethodsJittedDescriptor Instance = new (); |
| 63 | + |
| 64 | + public string Id => nameof(MethodsJittedDescriptor); |
| 65 | + public string DisplayName => "Methods JITted"; |
| 66 | + public string Legend => "Total number of methods JITted during entire benchmark execution (including warmup)."; |
| 67 | + public bool TheGreaterTheBetter => false; |
| 68 | + public string NumberFormat => "N0"; |
| 69 | + public UnitType UnitType => UnitType.Dimensionless; |
| 70 | + public string Unit => "Count"; |
| 71 | + public int PriorityInCategory => 0; |
| 72 | + } |
| 73 | + |
| 74 | + private sealed class MethodsTieredDescriptor : IMetricDescriptor |
| 75 | + { |
| 76 | + internal static readonly MethodsTieredDescriptor Instance = new (); |
| 77 | + |
| 78 | + public string Id => nameof(MethodsTieredDescriptor); |
| 79 | + public string DisplayName => "Methods Tiered"; |
| 80 | + public string Legend => "Total number of methods re-compiled by Tiered JIT during entire benchmark execution (including warmup)."; |
| 81 | + public bool TheGreaterTheBetter => false; |
| 82 | + public string NumberFormat => "N0"; |
| 83 | + public UnitType UnitType => UnitType.Dimensionless; |
| 84 | + public string Unit => "Count"; |
| 85 | + public int PriorityInCategory => 0; |
| 86 | + } |
| 87 | + |
| 88 | + private sealed class JitAllocatedMemoryDescriptor : IMetricDescriptor |
| 89 | + { |
| 90 | + internal static readonly JitAllocatedMemoryDescriptor Instance = new (); |
| 91 | + |
| 92 | + public string Id => nameof(JitAllocatedMemoryDescriptor); |
| 93 | + public string DisplayName => "JIT allocated memory"; |
| 94 | + public string Legend => "Total memory allocated by the JIT during entire benchmark execution (including warmup)."; |
| 95 | + public bool TheGreaterTheBetter => false; |
| 96 | + public string NumberFormat => "N0"; |
| 97 | + public UnitType UnitType => UnitType.Size; |
| 98 | + public string Unit => SizeUnit.B.Name; |
| 99 | + public int PriorityInCategory => 0; |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + public sealed class JitStats |
| 104 | + { |
| 105 | + public long MethodsCompiled; |
| 106 | + public long MethodsTiered; |
| 107 | + public long MemoryAllocated; |
| 108 | + } |
| 109 | +} |
0 commit comments