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

Commit 1cf39a4

Browse files
brianrobvancem
authored andcommitted
Allow coniguration of sampling rate. (#11595)
1 parent f11f3d7 commit 1cf39a4

File tree

5 files changed

+31
-13
lines changed

5 files changed

+31
-13
lines changed

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

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ internal sealed class EventPipeConfiguration
5656
private string m_outputFile;
5757
private uint m_circularBufferSizeInMB;
5858
private List<EventPipeProviderConfiguration> m_providers;
59+
private TimeSpan m_minTimeBetweenSamples = TimeSpan.FromMilliseconds(1);
5960

6061
internal EventPipeConfiguration(
6162
string outputFile,
@@ -89,13 +90,29 @@ internal EventPipeProviderConfiguration[] Providers
8990
get { return m_providers.ToArray(); }
9091
}
9192

93+
internal long ProfilerSamplingRateInNanoseconds
94+
{
95+
// 100 nanoseconds == 1 tick.
96+
get { return m_minTimeBetweenSamples.Ticks * 100; }
97+
}
98+
9299
internal void EnableProvider(string providerName, UInt64 keywords, uint loggingLevel)
93100
{
94101
m_providers.Add(new EventPipeProviderConfiguration(
95102
providerName,
96103
keywords,
97104
loggingLevel));
98105
}
106+
107+
internal void SetProfilerSamplingRate(TimeSpan minTimeBetweenSamples)
108+
{
109+
if(minTimeBetweenSamples.Ticks <= 0)
110+
{
111+
throw new ArgumentOutOfRangeException(nameof(minTimeBetweenSamples));
112+
}
113+
114+
m_minTimeBetweenSamples = minTimeBetweenSamples;
115+
}
99116
}
100117

101118
internal static class EventPipe
@@ -112,6 +129,7 @@ internal static void Enable(EventPipeConfiguration configuration)
112129
EventPipeInternal.Enable(
113130
configuration.OutputFile,
114131
configuration.CircularBufferSizeInMB,
132+
configuration.ProfilerSamplingRateInNanoseconds,
115133
providers,
116134
providers.Length);
117135
}
@@ -129,7 +147,7 @@ internal static class EventPipeInternal
129147
//
130148
[DllImport(JitHelpers.QCall, CharSet = CharSet.Unicode)]
131149
[SuppressUnmanagedCodeSecurity]
132-
internal static extern void Enable(string outputFile, uint circularBufferSizeInMB, EventPipeProviderConfiguration[] providers, int numProviders);
150+
internal static extern void Enable(string outputFile, uint circularBufferSizeInMB, long profilerSamplingRateInNanoseconds, EventPipeProviderConfiguration[] providers, int numProviders);
133151

134152
[DllImport(JitHelpers.QCall, CharSet = CharSet.Unicode)]
135153
[SuppressUnmanagedCodeSecurity]

src/vm/eventpipe.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -446,12 +446,14 @@ CrstStatic* EventPipe::GetLock()
446446
void QCALLTYPE EventPipeInternal::Enable(
447447
__in_z LPCWSTR outputFile,
448448
unsigned int circularBufferSizeInMB,
449+
long profilerSamplingRateInNanoseconds,
449450
EventPipeProviderConfiguration *pProviders,
450451
int numProviders)
451452
{
452453
QCALL_CONTRACT;
453454

454455
BEGIN_QCALL;
456+
SampleProfiler::SetSamplingRate(profilerSamplingRateInNanoseconds);
455457
EventPipe::Enable(outputFile, circularBufferSizeInMB, pProviders, numProviders);
456458
END_QCALL;
457459
}

src/vm/eventpipe.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,7 @@ class EventPipeInternal
272272
static void QCALLTYPE Enable(
273273
__in_z LPCWSTR outputFile,
274274
unsigned int circularBufferSizeInMB,
275+
long profilerSamplingRateInNanoseconds,
275276
EventPipeProviderConfiguration *pProviders,
276277
int numProviders);
277278

src/vm/sampleprofiler.cpp

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,7 @@ const GUID SampleProfiler::s_providerID = {0x3c530d44,0x97ae,0x513a,{0x1e,0x6d,0
1717
EventPipeProvider* SampleProfiler::s_pEventPipeProvider = NULL;
1818
EventPipeEvent* SampleProfiler::s_pThreadTimeEvent = NULL;
1919
CLREventStatic SampleProfiler::s_threadShutdownEvent;
20-
#ifdef FEATURE_PAL
2120
long SampleProfiler::s_samplingRateInNs = 1000000; // 1ms
22-
#endif
2321

2422
void SampleProfiler::Enable()
2523
{
@@ -88,6 +86,12 @@ void SampleProfiler::Disable()
8886
s_threadShutdownEvent.Wait(0, FALSE /* bAlertable */);
8987
}
9088

89+
void SampleProfiler::SetSamplingRate(long nanoseconds)
90+
{
91+
LIMITED_METHOD_CONTRACT;
92+
s_samplingRateInNs = nanoseconds;
93+
}
94+
9195
DWORD WINAPI SampleProfiler::ThreadProc(void *args)
9296
{
9397
CONTRACTL
@@ -111,11 +115,7 @@ DWORD WINAPI SampleProfiler::ThreadProc(void *args)
111115
if(ThreadSuspend::SysIsSuspendInProgress() || (ThreadSuspend::GetSuspensionThread() != 0))
112116
{
113117
// Skip the current sample.
114-
#ifdef FEATURE_PAL
115118
PAL_nanosleep(s_samplingRateInNs);
116-
#else
117-
ClrSleepEx(1, FALSE);
118-
#endif
119119
continue;
120120
}
121121

@@ -129,11 +129,7 @@ DWORD WINAPI SampleProfiler::ThreadProc(void *args)
129129
ThreadSuspend::RestartEE(FALSE /* bFinishedGC */, TRUE /* SuspendSucceeded */);
130130

131131
// Wait until it's time to sample again.
132-
#ifdef FEATURE_PAL
133132
PAL_nanosleep(s_samplingRateInNs);
134-
#else
135-
ClrSleepEx(1, FALSE);
136-
#endif
137133
}
138134
}
139135

src/vm/sampleprofiler.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ class SampleProfiler
2525
// Disable profiling.
2626
static void Disable();
2727

28+
// Set the sampling rate.
29+
static void SetSamplingRate(long nanoseconds);
30+
2831
private:
2932

3033
// Iterate through all managed threads and walk all stacks.
@@ -47,10 +50,8 @@ class SampleProfiler
4750
// Thread shutdown event for synchronization between Disable() and the sampling thread.
4851
static CLREventStatic s_threadShutdownEvent;
4952

50-
#ifdef FEATURE_PAL
5153
// The sampling rate.
5254
static long s_samplingRateInNs;
53-
#endif
5455
};
5556

5657
#endif // FEATURE_PERFTRACING

0 commit comments

Comments
 (0)