Skip to content

Commit

Permalink
Support disabling event tracing
Browse files Browse the repository at this point in the history
  • Loading branch information
cshung committed Jan 5, 2025
1 parent dae8909 commit 8d3e23d
Show file tree
Hide file tree
Showing 18 changed files with 141 additions and 7 deletions.
1 change: 1 addition & 0 deletions src/coreclr/clr.featuredefines.props
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
<DefineConstants Condition="'$(FeatureCominteropApartmentSupport)' == 'true'">$(DefineConstants);FEATURE_COMINTEROP_APARTMENT_SUPPORT</DefineConstants>
<DefineConstants Condition="'$(FeatureIjw)' == 'true'">$(DefineConstants);FEATURE_IJW</DefineConstants>
<DefineConstants Condition="'$(FeatureObjCMarshal)' == 'true'">$(DefineConstants);FEATURE_OBJCMARSHAL</DefineConstants>
<DefineConstants Condition="'$(FeatureEventTrace)' == 'true'">$(DefineConstants);FEATURE_EVENT_TRACE</DefineConstants>
<DefineConstants Condition="'$(FeaturePerfTracing)' == 'true'">$(DefineConstants);FEATURE_PERFTRACING</DefineConstants>
<DefineConstants Condition="'$(FeatureXplatEventSource)' == 'true'">$(DefineConstants);FEATURE_EVENTSOURCE_XPLAT</DefineConstants>
<DefineConstants Condition="'$(FeatureTypeEquivalence)' == 'true'">$(DefineConstants);FEATURE_TYPEEQUIVALENCE</DefineConstants>
Expand Down
1 change: 1 addition & 0 deletions src/coreclr/clrfeatures.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ if(CLR_CMAKE_TARGET_TIZEN_LINUX)
endif()

if(NOT DEFINED FEATURE_EVENT_TRACE)
# To actually disable FEATURE_EVENT_TRACE, also change clr.featuredefines.props
set(FEATURE_EVENT_TRACE 1)
endif(NOT DEFINED FEATURE_EVENT_TRACE)

Expand Down
4 changes: 2 additions & 2 deletions src/coreclr/dlls/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
if(CLR_CMAKE_TARGET_WIN32)
if(CLR_CMAKE_TARGET_WIN32 AND FEATURE_EVENT_TRACE)
add_subdirectory(clretwrc)
endif(CLR_CMAKE_TARGET_WIN32)
endif(CLR_CMAKE_TARGET_WIN32 AND FEATURE_EVENT_TRACE)
if (NOT (CLR_CMAKE_TARGET_WIN32 AND FEATURE_CROSSBITNESS))
add_subdirectory(mscordbi)
add_subdirectory(mscordac)
Expand Down
6 changes: 6 additions & 0 deletions src/coreclr/gc/gc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25904,6 +25904,7 @@ void gc_heap::calculate_new_heap_count ()
mb (total_soh_stable_size), mb (total_bcd), diff_pct, change_int, (change_int * 100.0 / n_heaps)));
}

#ifdef FEATURE_EVENT_TRACE
GCEventFireSizeAdaptationTuning_V1 (
(uint16_t)new_n_heaps,
(uint16_t)max_heap_count_datas,
Expand All @@ -25922,6 +25923,7 @@ void gc_heap::calculate_new_heap_count ()
(uint16_t)hc_change_freq_factor,
(uint16_t)hc_freq_reason,
(uint8_t)adj_metric);
#endif //FEATURE_EVENT_TRACE
}

size_t num_gen2s_since_last_change = 0;
Expand Down Expand Up @@ -25977,6 +25979,7 @@ void gc_heap::calculate_new_heap_count ()
if (process_gen2_samples_p)
{
dynamic_heap_count_data_t::gen2_sample* gen2_samples = dynamic_heap_count_data.gen2_samples;
#ifdef FEATURE_EVENT_TRACE
GCEventFireSizeAdaptationFullGCTuning_V1 (
(uint16_t)dynamic_heap_count_data.new_n_heaps,
(uint64_t)current_gc_index,
Expand All @@ -25988,6 +25991,7 @@ void gc_heap::calculate_new_heap_count ()
(float)gen2_samples[1].gc_percent,
(uint32_t)(current_gc_index - gen2_samples[2].gc_index),
(float)gen2_samples[2].gc_percent);
#endif //FEATURE_EVENT_TRACEs

dprintf (6666, ("processed gen2 samples, updating processed %Id -> %Id", dynamic_heap_count_data.processed_gen2_samples_count, dynamic_heap_count_data.current_gen2_samples_count));
dynamic_heap_count_data.processed_gen2_samples_count = dynamic_heap_count_data.current_gen2_samples_count;
Expand Down Expand Up @@ -26683,12 +26687,14 @@ void gc_heap::process_datas_sample()
(sample.gc_pause_time ? (sample.gc_survived_size / 1000.0 / sample.gc_pause_time) : 0),
(sample.gc_pause_time ? ((float)sample.gc_survived_size / sample.gc_pause_time / n_heaps) : 0)));

#ifdef FEATURE_EVENT_TRACE
GCEventFireSizeAdaptationSample_V1 (
(uint64_t)gc_index,
(uint32_t)sample.elapsed_between_gcs,
(uint32_t)sample.gc_pause_time,
(uint32_t)soh_msl_wait_time, (uint32_t)uoh_msl_wait_time,
(uint64_t)total_soh_stable_size, (uint32_t)sample.gen0_budget_per_heap);
#endif //FEATURE_EVENT_TRACE

dynamic_heap_count_data.sample_index = (dynamic_heap_count_data.sample_index + 1) % dynamic_heap_count_data_t::sample_size;
(dynamic_heap_count_data.current_samples_count)++;
Expand Down
3 changes: 3 additions & 0 deletions src/coreclr/inc/profilepriv.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
#ifndef _ProfilePriv_h_
#define _ProfilePriv_h_

#ifndef FEATURE_PERFTRACING
typedef struct _EventPipeProvider EventPipeProvider;
#endif //FEATURE_PERFTRACING

// Forward declarations
class EEToProfInterfaceImpl;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -583,6 +583,11 @@
<Compile Include="@(EventingSourceFile)" />
</ItemGroup>

<PropertyGroup>
<FeatureEventTrace>true</FeatureEventTrace>
<DefineConstants Condition="'$(FeatureEventTrace)' == 'true'">$(DefineConstants);FEATURE_EVENT_TRACE</DefineConstants>
</PropertyGroup>

<Target Name="GenerateEventingFiles"
Inputs="@(EventingGenerationScript);@(EventManifestFile);@(EventingInclusionList)"
Outputs="@(EventingSourceFile)"
Expand Down
4 changes: 3 additions & 1 deletion src/coreclr/vm/finalizerthread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -294,10 +294,12 @@ VOID FinalizerThread::FinalizerThreadWorker(void *args)
gcGenAnalysisState = GcGenAnalysisState::Disabled;
if (gcGenAnalysisTrace)
{
#ifdef FEATURE_EVENT_TRACE
EventPipeAdapter::Disable(gcGenAnalysisEventPipeSessionId);
#ifdef GEN_ANALYSIS_STRESS
GenAnalysis::EnableGenerationalAwareSession();
#endif
#endif //GEN_ANALYSIS_STRESS
#endif //FEATURE_EVENT_TRACE
}

// Writing an empty file to indicate completion
Expand Down
2 changes: 2 additions & 0 deletions src/coreclr/vm/gcenv.ee.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1724,6 +1724,7 @@ void GCToEEInterface::AnalyzeSurvivorsFinished(size_t gcIndex, int condemnedGene
{
if (gcGenAnalysisTrace)
{
#ifdef FEATURE_EVENT_TRACE
EventPipeAdapter::ResumeSession(gcGenAnalysisEventPipeSession);
FireEtwGenAwareBegin((int)gcIndex, GetClrInstanceId());
s_forcedGCInProgress = true;
Expand All @@ -1732,6 +1733,7 @@ void GCToEEInterface::AnalyzeSurvivorsFinished(size_t gcIndex, int condemnedGene
reportGenerationBounds();
FireEtwGenAwareEnd((int)gcIndex, GetClrInstanceId());
EventPipeAdapter::PauseSession(gcGenAnalysisEventPipeSession);
#endif //FEATURE_EVENT_TRACE
}
if (gcGenAnalysisDump)
{
Expand Down
2 changes: 2 additions & 0 deletions src/coreclr/vm/gchelpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ inline void CheckObjectSize(size_t alloc_size)

void FireAllocationSampled(GC_ALLOC_FLAGS flags, size_t size, size_t samplingBudgetOffset, Object* orObject)
{
#ifdef FEATURE_EVENT_TRACE
// Note: this code is duplicated from GCToCLREventSink::FireGCAllocationTick_V4
void* typeId = nullptr;
const WCHAR* name = nullptr;
Expand Down Expand Up @@ -212,6 +213,7 @@ void FireAllocationSampled(GC_ALLOC_FLAGS flags, size_t size, size_t samplingBud
0; // SOH
FireEtwAllocationSampled(allocKind, GetClrInstanceId(), typeId, name, (BYTE*)orObject, size, samplingBudgetOffset);
}
#endif //FEATURE_EVENT_TRACE
}

inline Object* Alloc(ee_alloc_context* pEEAllocContext, size_t size, GC_ALLOC_FLAGS flags)
Expand Down
4 changes: 4 additions & 0 deletions src/coreclr/vm/genanalysis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ bool gcGenAnalysisDump = false;

/* static */ void GenAnalysis::EnableGenerationalAwareSession()
{
#ifdef FEATURE_EVENT_TRACE
WCHAR outputPath[MAX_PATH];
ReplacePid(GENAWARE_TRACE_FILE_NAME, outputPath, MAX_PATH);

Expand Down Expand Up @@ -115,4 +116,7 @@ bool gcGenAnalysisDump = false;
{
gcGenAnalysisTrace = false;
}
#else
gcGenAnalysisTrace = false;
#endif //FEATURE_EVENT_TRACE
}
4 changes: 4 additions & 0 deletions src/coreclr/vm/genanalysis.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
#ifndef __GENANALYSIS_H__
#define __GENANALYSIS_H__

#ifndef FEATURE_EVENT_TRACE
typedef struct _EventPipeSession EventPipeSession;
#endif //FEATURE_EVENT_TRACE

#ifdef FEATURE_PERFTRACING
#include "eventpipeadaptertypes.h"
#endif // FEATURE_PERFTRACING
Expand Down
6 changes: 5 additions & 1 deletion src/coreclr/vm/prestub.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -830,12 +830,14 @@ PCODE MethodDesc::JitCompileCodeLockedEventWrapper(PrepareCodeConfig* pConfig, J
// for interpreted methods in the first place. The interpreter does generate
// a small stub of native code but no native-IL mapping.
#ifndef FEATURE_INTERPRETER
#ifdef FEATURE_EVENT_TRACE
ETW::MethodLog::MethodJitting(this,
pilHeader,
&namespaceOrClassName,
&methodName,
&methodSignature);
#endif
#endif //FEATURE_EVENT_TRACE
#endif //!FEATURE_INTERPRETER

pCode = JitCompileCodeLocked(pConfig, pilHeader, pEntry, &sizeOfCode);

Expand All @@ -845,12 +847,14 @@ PCODE MethodDesc::JitCompileCodeLockedEventWrapper(PrepareCodeConfig* pConfig, J
#endif
{
// Fire an ETW event to mark the end of JIT'ing
#ifdef FEATURE_EVENT_TRACE
ETW::MethodLog::MethodJitted(this,
&namespaceOrClassName,
&methodName,
&methodSignature,
pCode,
pConfig);
#endif //FEATURE_EVENT_TRACE
}

}
Expand Down
2 changes: 2 additions & 0 deletions src/coreclr/vm/profilinghelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,7 @@ void ProfilingAPIUtility::LogProfEventVA(

AppendSupplementaryInformation(iStringResourceID, &messageToLog);

#ifdef FEATURE_EVENT_TRACE
if (EventEnabledProfilerMessage())
{
StackSString messageToLogUtf16;
Expand All @@ -325,6 +326,7 @@ void ProfilingAPIUtility::LogProfEventVA(
// Write to ETW and EventPipe with the message
FireEtwProfilerMessage(GetClrInstanceId(), messageToLogUtf16.GetUnicode());
}
#endif //FEATURE_EVENT_TRACE

// Output debug strings for diagnostic messages.
OutputDebugStringUtf8(messageToLog.GetUTF8());
Expand Down
6 changes: 4 additions & 2 deletions src/coreclr/vm/yieldprocessornormalizedshared.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,9 @@ void YieldProcessorNormalization::PerformMeasurement()
}
if (i < NsPerYieldMeasurementCount - 1)
{
#ifdef FEATURE_EVENT_TRACE
FireEtwYieldProcessorMeasurement(GetClrInstanceId(), latestNsPerYield, s_establishedNsPerYield);
#endif //FEATURE_EVENT_TRACE
}
}
}
Expand All @@ -204,9 +206,9 @@ void YieldProcessorNormalization::PerformMeasurement()
{
AtomicStore(&s_establishedNsPerYield, establishedNsPerYield);
}

#ifdef FEATURE_EVENT_TRACE
FireEtwYieldProcessorMeasurement(GetClrInstanceId(), latestNsPerYield, s_establishedNsPerYield);

#endif //FEATURE_EVENT_TRACE
// Calculate the number of yields required to span the duration of a normalized yield
unsigned int yieldsPerNormalizedYield = max(1u, (unsigned int)(TargetNsPerNormalizedYield / establishedNsPerYield + 0.5));
_ASSERTE(yieldsPerNormalizedYield <= MaxYieldsPerNormalizedYield);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ namespace System.Diagnostics.Tracing
{
internal static partial class EventPipeInternal
{
#if FEATURE_EVENT_TRACE
//
// These PInvokes are used by the configuration APIs to interact with EventPipe.
//
Expand Down Expand Up @@ -64,5 +65,72 @@ internal static unsafe partial IntPtr CreateProvider(string providerName,
[LibraryImport(RuntimeHelpers.QCall, EntryPoint = "EventPipeInternal_WaitForSessionSignal")]
[return: MarshalAs(UnmanagedType.Bool)]
internal static unsafe partial bool WaitForSessionSignal(ulong sessionID, int timeoutMs);
#else
#pragma warning disable IDE0060
private static unsafe ulong Enable(
char* outputFile,
EventPipeSerializationFormat format,
uint circularBufferSizeInMB,
EventPipeProviderConfigurationNative* providers,
uint numProviders)
{
return 0;
}

internal static void Disable(ulong sessionID)
{
}

internal static unsafe IntPtr CreateProvider(string providerName,
delegate* unmanaged<byte*, int, byte, long, long, Interop.Advapi32.EVENT_FILTER_DESCRIPTOR*, void*, void> callbackFunc,
void* callbackContext)
{
return IntPtr.Zero;
}

internal static unsafe IntPtr DefineEvent(IntPtr provHandle, uint eventID, long keywords, uint eventVersion, uint level, void *pMetadata, uint metadataLength)
{
return IntPtr.Zero;
}

internal static IntPtr GetProvider(string providerName)
{
return IntPtr.Zero;
}

internal static void DeleteProvider(IntPtr provHandle)
{
}

internal static int EventActivityIdControl(uint controlCode, ref Guid activityId)
{
return 0;
}

internal static unsafe void WriteEventData(IntPtr eventHandle, EventProvider.EventData* pEventData, uint dataCount, Guid* activityId, Guid* relatedActivityId)
{
}

internal static unsafe bool GetSessionInfo(ulong sessionID, EventPipeSessionInfo* pSessionInfo)
{
return false;
}

internal static unsafe bool GetNextEvent(ulong sessionID, EventPipeEventInstanceData* pInstance)
{
return false;
}

internal static unsafe bool SignalSession(ulong sessionID)
{
return false;
}

internal static unsafe bool WaitForSessionSignal(ulong sessionID, int timeoutMs)
{
return false;
}
#pragma warning restore IDE0060
#endif //FEATURE_EVENT_TRACE
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ internal sealed class EventPipeWaitHandle : WaitHandle

internal static partial class EventPipeInternal
{
#if FEATURE_EVENT_TRACE
private unsafe struct EventPipeProviderConfigurationNative
{
private char* m_pProviderName;
Expand Down Expand Up @@ -146,5 +147,21 @@ internal static unsafe ulong Enable(
}
}
}
#else
#pragma warning disable IDE0060
private unsafe struct EventPipeProviderConfigurationNative
{
}

internal static unsafe ulong Enable(
string? outputFile,
EventPipeSerializationFormat format,
uint circularBufferSizeInMB,
EventPipeProviderConfiguration[] providers)
{
return 0;
}
#pragma warning restore IDE0060
#endif //FEATURE_EVENT_TRACE
}
}
5 changes: 5 additions & 0 deletions src/mono/System.Private.CoreLib/System.Private.CoreLib.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,11 @@
<Compile Include="@(EventingSourceFile)" />
</ItemGroup>

<PropertyGroup>
<FeatureEventTrace>true</FeatureEventTrace>
<DefineConstants Condition="'$(FeatureEventTrace)' == 'true'">$(DefineConstants);FEATURE_EVENT_TRACE</DefineConstants>
</PropertyGroup>

<Target Name="GenerateEventingFiles"
Inputs="@(EventingGenerationScript);@(EventManifestFile)"
Outputs="@(EventingSourceFile)"
Expand Down
8 changes: 7 additions & 1 deletion src/native/corehost/apphost/static/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,13 @@ else()
coreclrpal_dac
corguids
dbgutil
eventprovider
)
if (FEATURE_EVENT_TRACE)
LIST(APPEND NATIVE_LIBS
eventprovider
)
endif(FEATURE_EVENT_TRACE)
LIST(APPEND NATIVE_LIBS
nativeresourcestring
)

Expand Down

0 comments on commit 8d3e23d

Please sign in to comment.