-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathSessions.cs
More file actions
137 lines (110 loc) · 4.89 KB
/
Sessions.cs
File metadata and controls
137 lines (110 loc) · 4.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
using System;
using System.ComponentModel;
using System.IO;
using System.Linq;
using BenchmarkDotNet.Diagnosers;
using BenchmarkDotNet.Engines;
using BenchmarkDotNet.Exporters;
using BenchmarkDotNet.Extensions;
using BenchmarkDotNet.Helpers;
using BenchmarkDotNet.Loggers;
using BenchmarkDotNet.Running;
using Microsoft.Diagnostics.Tracing;
using Microsoft.Diagnostics.Tracing.Parsers;
using Microsoft.Diagnostics.Tracing.Session;
namespace BenchmarkDotNet.Diagnostics.Windows
{
internal class HeapSession : Session
{
public HeapSession(DiagnoserActionParameters details, EtwProfilerConfig config, DateTime creationTime)
: base(GetSessionName(details.BenchmarkCase) + "Heap", details, config, creationTime)
{
}
protected override string FileExtension => "userheap.etl";
internal override Session EnableProviders()
{
var osHeapExe = Path.GetFileName(Path.ChangeExtension(Details.Process.StartInfo.FileName, ".exe"));
TraceEventSession.EnableWindowsHeapProvider(osHeapExe);
return this;
}
}
internal class UserSession : Session
{
public UserSession(DiagnoserActionParameters details, EtwProfilerConfig config, DateTime creationTime)
: base(GetSessionName(details.BenchmarkCase), details, config, creationTime)
{
}
protected override string FileExtension => "etl";
internal override Session EnableProviders()
{
TraceEventSession.EnableProvider(EngineEventSource.Log.Name, TraceEventLevel.Informational); // mandatory provider to enable Engine events
foreach (var provider in Config.Providers)
{
TraceEventSession.EnableProvider(provider.providerGuid, provider.providerLevel, provider.keywords, provider.options);
}
return this;
}
}
internal class KernelSession : Session
{
public KernelSession(DiagnoserActionParameters details, EtwProfilerConfig config, DateTime creationTime)
: base(KernelTraceEventParser.KernelSessionName, details, config, creationTime)
{
}
protected override string FileExtension => "kernel.etl";
internal override Session EnableProviders()
{
var keywords = Config.KernelKeywords
| KernelTraceEventParser.Keywords.ImageLoad // handles stack frames from native modules, SUPER IMPORTANT!
| KernelTraceEventParser.Keywords.Profile; // CPU stacks
if (Details.Config.GetHardwareCounters().Any() || Details.Config.GetCustomCounters().Any())
keywords |= KernelTraceEventParser.Keywords.PMCProfile; // Precise Machine Counters
TraceEventSession.StackCompression = true;
try
{
TraceEventSession.EnableKernelProvider(keywords, Config.KernelStackKeywords);
}
catch (Win32Exception)
{
Details.Config.GetCompositeLogger().WriteLineError(
"Please install the latest Microsoft.Diagnostics.Tracing.TraceEvent package in the project with benchmarks so MSBuild can copy the native dependencies of TraceEvent to the output folder.");
throw;
}
return this;
}
}
internal abstract class Session : DisposeAtProcessTermination
{
private const int MaxSessionNameLength = 128;
protected abstract string FileExtension { get; }
protected TraceEventSession TraceEventSession { get; }
protected DiagnoserActionParameters Details { get; }
protected EtwProfilerConfig Config { get; }
internal string FilePath { get; }
protected Session(string sessionName, DiagnoserActionParameters details, EtwProfilerConfig config, DateTime creationTime)
{
Details = details;
Config = config;
FilePath = ArtifactFileNameHelper.GetTraceFilePath(details, creationTime, FileExtension).EnsureFolderExists();
TraceEventSession = new TraceEventSession(sessionName, FilePath)
{
BufferSizeMB = config.BufferSizeInMb,
CpuSampleIntervalMSec = config.CpuSampleIntervalInMilliseconds,
};
}
public override void Dispose()
{
TraceEventSession.Dispose();
base.Dispose();
}
internal abstract Session EnableProviders();
protected static string GetSessionName(BenchmarkCase benchmarkCase)
{
string benchmarkName = FullNameProvider.GetBenchmarkName(benchmarkCase);
if (benchmarkName.Length <= MaxSessionNameLength)
return benchmarkName;
// session name is not really used by humans, we can just give it the hashcode value
return $"BenchmarkDotNet.EtwProfiler.Session_{Hashing.HashString(benchmarkName)}";
}
}
}