Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit fca97d0

Browse files
authored
EventPipe Circular Buffer Support and Ability to Start/Stop Tracing (#11507)
1 parent f706984 commit fca97d0

26 files changed

+2286
-112
lines changed

src/mscorlib/System.Private.CoreLib.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -569,6 +569,7 @@
569569
<Compile Include="$(BclSourcesRoot)\System\Diagnostics\Eventing\EventSource_CoreCLR.cs" />
570570
<Compile Condition="'$(FeatureXplatEventSource)' == 'true'" Include="$(BclSourcesRoot)\System\Diagnostics\Eventing\XplatEventLogger.cs" />
571571
<Compile Include="$(BclSourcesRoot)\System\Diagnostics\Eventing\FrameworkEventSource.cs" />
572+
<Compile Condition="'$(FeaturePerfTracing)' == 'true'" Include="$(BclSourcesRoot)\System\Diagnostics\Eventing\EventPipe.cs" />
572573
<Compile Condition="'$(FeaturePerfTracing)' == 'true'" Include="$(BclSourcesRoot)\System\Diagnostics\Eventing\EventPipeEventProvider.cs" />
573574
</ItemGroup>
574575
<ItemGroup>
@@ -734,4 +735,4 @@
734735
<Win32Resource Condition="'$(GenerateNativeVersionInfo)'=='true'">$(IntermediateOutputPath)\System.Private.CoreLib.res</Win32Resource>
735736
</PropertyGroup>
736737
<Import Project="GenerateCompilerResponseFile.targets" />
737-
</Project>
738+
</Project>
Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
using System.Collections.Generic;
5+
using System.Runtime.CompilerServices;
6+
using System.Runtime.InteropServices;
7+
using System.Security;
8+
using Microsoft.Win32;
9+
10+
namespace System.Diagnostics.Tracing
11+
{
12+
[StructLayout(LayoutKind.Sequential)]
13+
internal struct EventPipeProviderConfiguration
14+
{
15+
[MarshalAs(UnmanagedType.LPWStr)]
16+
private string m_providerName;
17+
private UInt64 m_keywords;
18+
private uint m_loggingLevel;
19+
20+
internal EventPipeProviderConfiguration(
21+
string providerName,
22+
UInt64 keywords,
23+
uint loggingLevel)
24+
{
25+
if(string.IsNullOrEmpty(providerName))
26+
{
27+
throw new ArgumentNullException(nameof(providerName));
28+
}
29+
if(loggingLevel > 5) // 5 == Verbose, the highest value in EventPipeLoggingLevel.
30+
{
31+
throw new ArgumentOutOfRangeException(nameof(loggingLevel));
32+
}
33+
m_providerName = providerName;
34+
m_keywords = keywords;
35+
m_loggingLevel = loggingLevel;
36+
}
37+
38+
internal string ProviderName
39+
{
40+
get { return m_providerName; }
41+
}
42+
43+
internal UInt64 Keywords
44+
{
45+
get { return m_keywords; }
46+
}
47+
48+
internal uint LoggingLevel
49+
{
50+
get { return m_loggingLevel; }
51+
}
52+
}
53+
54+
internal sealed class EventPipeConfiguration
55+
{
56+
private string m_outputFile;
57+
private uint m_circularBufferSizeInMB;
58+
private List<EventPipeProviderConfiguration> m_providers;
59+
60+
internal EventPipeConfiguration(
61+
string outputFile,
62+
uint circularBufferSizeInMB)
63+
{
64+
if(string.IsNullOrEmpty(outputFile))
65+
{
66+
throw new ArgumentNullException(nameof(outputFile));
67+
}
68+
if(circularBufferSizeInMB == 0)
69+
{
70+
throw new ArgumentOutOfRangeException(nameof(circularBufferSizeInMB));
71+
}
72+
m_outputFile = outputFile;
73+
m_circularBufferSizeInMB = circularBufferSizeInMB;
74+
m_providers = new List<EventPipeProviderConfiguration>();
75+
}
76+
77+
internal string OutputFile
78+
{
79+
get { return m_outputFile; }
80+
}
81+
82+
internal uint CircularBufferSizeInMB
83+
{
84+
get { return m_circularBufferSizeInMB; }
85+
}
86+
87+
internal EventPipeProviderConfiguration[] Providers
88+
{
89+
get { return m_providers.ToArray(); }
90+
}
91+
92+
internal void EnableProvider(string providerName, UInt64 keywords, uint loggingLevel)
93+
{
94+
m_providers.Add(new EventPipeProviderConfiguration(
95+
providerName,
96+
keywords,
97+
loggingLevel));
98+
}
99+
}
100+
101+
internal static class EventPipe
102+
{
103+
internal static void Enable(EventPipeConfiguration configuration)
104+
{
105+
if(configuration == null)
106+
{
107+
throw new ArgumentNullException(nameof(configuration));
108+
}
109+
110+
EventPipeProviderConfiguration[] providers = configuration.Providers;
111+
112+
EventPipeInternal.Enable(
113+
configuration.OutputFile,
114+
configuration.CircularBufferSizeInMB,
115+
providers,
116+
providers.Length);
117+
}
118+
119+
internal static void Disable()
120+
{
121+
EventPipeInternal.Disable();
122+
}
123+
}
124+
125+
internal static class EventPipeInternal
126+
{
127+
//
128+
// These PInvokes are used by the configuration APIs to interact with EventPipe.
129+
//
130+
[DllImport(JitHelpers.QCall, CharSet = CharSet.Unicode)]
131+
[SuppressUnmanagedCodeSecurity]
132+
internal static extern void Enable(string outputFile, uint circularBufferSizeInMB, EventPipeProviderConfiguration[] providers, int numProviders);
133+
134+
[DllImport(JitHelpers.QCall, CharSet = CharSet.Unicode)]
135+
[SuppressUnmanagedCodeSecurity]
136+
internal static extern void Disable();
137+
138+
//
139+
// These PInvokes are used by EventSource to interact with the EventPipe.
140+
//
141+
[DllImport(JitHelpers.QCall, CharSet = CharSet.Unicode)]
142+
[SuppressUnmanagedCodeSecurity]
143+
internal static extern IntPtr CreateProvider(Guid providerID, UnsafeNativeMethods.ManifestEtw.EtwEnableCallback callbackFunc);
144+
145+
[DllImport(JitHelpers.QCall, CharSet = CharSet.Unicode)]
146+
[SuppressUnmanagedCodeSecurity]
147+
internal static extern IntPtr AddEvent(IntPtr provHandle, Int64 keywords, uint eventID, uint eventVersion, uint level, bool needStack);
148+
149+
[DllImport(JitHelpers.QCall, CharSet = CharSet.Unicode)]
150+
[SuppressUnmanagedCodeSecurity]
151+
internal static extern void DeleteProvider(IntPtr provHandle);
152+
153+
[DllImport(JitHelpers.QCall, CharSet = CharSet.Unicode)]
154+
[SuppressUnmanagedCodeSecurity]
155+
internal static extern unsafe void WriteEvent(IntPtr eventHandle, void* data, uint length);
156+
}
157+
}

src/mscorlib/src/System/Diagnostics/Eventing/EventPipeEventProvider.cs

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -63,24 +63,4 @@ int IEventProvider.EventActivityIdControl(UnsafeNativeMethods.ManifestEtw.Activi
6363
return 0;
6464
}
6565
}
66-
67-
// PInvokes into the runtime used to interact with the EventPipe.
68-
internal static class EventPipeInternal
69-
{
70-
[DllImport(JitHelpers.QCall, CharSet = CharSet.Unicode)]
71-
[SuppressUnmanagedCodeSecurity]
72-
internal static extern IntPtr CreateProvider(Guid providerID, UnsafeNativeMethods.ManifestEtw.EtwEnableCallback callbackFunc);
73-
74-
[DllImport(JitHelpers.QCall, CharSet = CharSet.Unicode)]
75-
[SuppressUnmanagedCodeSecurity]
76-
internal static extern IntPtr AddEvent(IntPtr provHandle, Int64 keywords, uint eventID, uint eventVersion, uint level, bool needStack);
77-
78-
[DllImport(JitHelpers.QCall, CharSet = CharSet.Unicode)]
79-
[SuppressUnmanagedCodeSecurity]
80-
internal static extern void DeleteProvider(IntPtr provHandle);
81-
82-
[DllImport(JitHelpers.QCall, CharSet = CharSet.Unicode)]
83-
[SuppressUnmanagedCodeSecurity]
84-
internal static extern unsafe void WriteEvent(IntPtr eventHandle, void* data, uint length);
85-
}
8666
}

src/vm/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,8 @@ set(VM_SOURCES_WKS
170170
eventpipefile.cpp
171171
eventpipejsonfile.cpp
172172
eventpipeprovider.cpp
173+
eventpipebuffer.cpp
174+
eventpipebuffermanager.cpp
173175
eventstore.cpp
174176
fastserializer.cpp
175177
fcall.cpp

src/vm/ecalllist.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1273,6 +1273,8 @@ FCFuncEnd()
12731273

12741274
#ifdef FEATURE_PERFTRACING
12751275
FCFuncStart(gEventPipeInternalFuncs)
1276+
QCFuncElement("Enable", EventPipeInternal::Enable)
1277+
QCFuncElement("Disable", EventPipeInternal::Disable)
12761278
QCFuncElement("CreateProvider", EventPipeInternal::CreateProvider)
12771279
QCFuncElement("AddEvent", EventPipeInternal::AddEvent)
12781280
QCFuncElement("DeleteProvider", EventPipeInternal::DeleteProvider)

0 commit comments

Comments
 (0)