Skip to content

Commit 293538e

Browse files
committed
Avoid duplication around TraceLoggingEventTypes
1 parent 5ae5982 commit 293538e

File tree

1 file changed

+34
-40
lines changed
  • src/libraries/System.Private.CoreLib/src/System/Diagnostics/Tracing

1 file changed

+34
-40
lines changed

src/libraries/System.Private.CoreLib/src/System/Diagnostics/Tracing/EventSource.cs

Lines changed: 34 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1345,20 +1345,14 @@ protected unsafe void WriteEventWithRelatedActivityIdCore(int eventId, Guid* rel
13451345
#endif // FEATURE_PERFTRACING
13461346
)
13471347
{
1348-
TraceLoggingEventTypes? tlet = metadata.TraceLoggingEventTypes;
1349-
if (tlet == null)
1350-
{
1351-
tlet = new TraceLoggingEventTypes(metadata.Name, metadata.Tags, metadata.Parameters);
1352-
Interlocked.CompareExchange(ref metadata.TraceLoggingEventTypes, tlet, null);
1353-
}
13541348
EventSourceOptions opt = new EventSourceOptions
13551349
{
13561350
Keywords = (EventKeywords)metadata.Descriptor.Keywords,
13571351
Level = (EventLevel)metadata.Descriptor.Level,
13581352
Opcode = (EventOpcode)metadata.Descriptor.Opcode
13591353
};
13601354

1361-
WriteMultiMerge(metadata.Name, ref opt, tlet, pActivityId, relatedActivityId, data);
1355+
WriteMultiMerge(metadata.Name, ref opt, metadata.TraceLoggingEventTypes, pActivityId, relatedActivityId, data);
13621356
}
13631357
#endif // FEATURE_MANAGED_ETW
13641358

@@ -1894,6 +1888,8 @@ private unsafe void WriteEventVarargs(int eventId, Guid* childActivityID, object
18941888
Debug.Assert(m_eventData != null); // You must have initialized this if you enabled the source.
18951889
try
18961890
{
1891+
ref EventMetadata metadata = ref m_eventData[eventId];
1892+
18971893
if (childActivityID != null)
18981894
{
18991895
// If you use WriteEventWithRelatedActivityID you MUST declare the first argument to be a GUID
@@ -1904,7 +1900,7 @@ private unsafe void WriteEventVarargs(int eventId, Guid* childActivityID, object
19041900
// we can end up in a state where the ParameterInfo[] doesn't have its first parameter stripped,
19051901
// and this leads to a mismatch between the number of arguments and the number of ParameterInfos,
19061902
// which would cause a cryptic IndexOutOfRangeException later if we don't catch it here.
1907-
if (!m_eventData[eventId].HasRelatedActivityID)
1903+
if (!metadata.HasRelatedActivityID)
19081904
{
19091905
throw new ArgumentException(SR.EventSource_NoRelatedActivityId);
19101906
}
@@ -1915,19 +1911,19 @@ private unsafe void WriteEventVarargs(int eventId, Guid* childActivityID, object
19151911
Guid* pActivityId = null;
19161912
Guid activityId = Guid.Empty;
19171913
Guid relatedActivityId = Guid.Empty;
1918-
EventOpcode opcode = (EventOpcode)m_eventData[eventId].Descriptor.Opcode;
1919-
EventActivityOptions activityOptions = m_eventData[eventId].ActivityOptions;
1914+
EventOpcode opcode = (EventOpcode)metadata.Descriptor.Opcode;
1915+
EventActivityOptions activityOptions = metadata.ActivityOptions;
19201916

19211917
if (childActivityID == null &&
19221918
((activityOptions & EventActivityOptions.Disable) == 0))
19231919
{
19241920
if (opcode == EventOpcode.Start)
19251921
{
1926-
m_activityTracker.OnStart(m_name, m_eventData[eventId].Name, m_eventData[eventId].Descriptor.Task, ref activityId, ref relatedActivityId, m_eventData[eventId].ActivityOptions);
1922+
m_activityTracker.OnStart(m_name, metadata.Name, metadata.Descriptor.Task, ref activityId, ref relatedActivityId, metadata.ActivityOptions);
19271923
}
19281924
else if (opcode == EventOpcode.Stop)
19291925
{
1930-
m_activityTracker.OnStop(m_name, m_eventData[eventId].Name, m_eventData[eventId].Descriptor.Task, ref activityId);
1926+
m_activityTracker.OnStop(m_name, metadata.Name, metadata.Descriptor.Task, ref activityId);
19311927
}
19321928

19331929
if (activityId != Guid.Empty)
@@ -1937,44 +1933,36 @@ private unsafe void WriteEventVarargs(int eventId, Guid* childActivityID, object
19371933
}
19381934

19391935
#if FEATURE_MANAGED_ETW
1940-
if (m_eventData[eventId].EnabledForETW
1936+
if (metadata.EnabledForETW
19411937
#if FEATURE_PERFTRACING
1942-
|| m_eventData[eventId].EnabledForEventPipe
1938+
|| metadata.EnabledForEventPipe
19431939
#endif // FEATURE_PERFTRACING
19441940
)
19451941
{
19461942
if (!SelfDescribingEvents)
19471943
{
1948-
if (!m_etwProvider.WriteEvent(ref m_eventData[eventId].Descriptor, m_eventData[eventId].EventHandle, pActivityId, childActivityID, args))
1949-
ThrowEventSourceException(m_eventData[eventId].Name);
1944+
if (!m_etwProvider.WriteEvent(ref metadata.Descriptor, metadata.EventHandle, pActivityId, childActivityID, args))
1945+
ThrowEventSourceException(metadata.Name);
19501946
#if FEATURE_PERFTRACING
1951-
if (!m_eventPipeProvider.WriteEvent(ref m_eventData[eventId].Descriptor, m_eventData[eventId].EventHandle, pActivityId, childActivityID, args))
1952-
ThrowEventSourceException(m_eventData[eventId].Name);
1947+
if (!m_eventPipeProvider.WriteEvent(ref metadata.Descriptor, metadata.EventHandle, pActivityId, childActivityID, args))
1948+
ThrowEventSourceException(metadata.Name);
19531949
#endif // FEATURE_PERFTRACING
19541950
}
19551951
else
19561952
{
1957-
TraceLoggingEventTypes? tlet = m_eventData[eventId].TraceLoggingEventTypes;
1958-
if (tlet == null)
1959-
{
1960-
tlet = new TraceLoggingEventTypes(m_eventData[eventId].Name,
1961-
EventTags.None,
1962-
m_eventData[eventId].Parameters);
1963-
Interlocked.CompareExchange(ref m_eventData[eventId].TraceLoggingEventTypes, tlet, null);
1964-
}
19651953
// TODO: activity ID support
19661954
EventSourceOptions opt = new EventSourceOptions
19671955
{
1968-
Keywords = (EventKeywords)m_eventData[eventId].Descriptor.Keywords,
1969-
Level = (EventLevel)m_eventData[eventId].Descriptor.Level,
1970-
Opcode = (EventOpcode)m_eventData[eventId].Descriptor.Opcode
1956+
Keywords = (EventKeywords)metadata.Descriptor.Keywords,
1957+
Level = (EventLevel)metadata.Descriptor.Level,
1958+
Opcode = (EventOpcode)metadata.Descriptor.Opcode
19711959
};
19721960

1973-
WriteMultiMerge(m_eventData[eventId].Name, ref opt, tlet, pActivityId, childActivityID, args);
1961+
WriteMultiMerge(metadata.Name, ref opt, metadata.TraceLoggingEventTypes, pActivityId, childActivityID, args);
19741962
}
19751963
}
19761964
#endif // FEATURE_MANAGED_ETW
1977-
if (m_Dispatchers != null && m_eventData[eventId].EnabledForAnyListener)
1965+
if (m_Dispatchers != null && metadata.EnabledForAnyListener)
19781966
{
19791967
#if !ES_BUILD_STANDALONE
19801968
// Maintain old behavior - object identity is preserved
@@ -2008,14 +1996,7 @@ private unsafe void WriteEventVarargs(int eventId, Guid* childActivityID, object
20081996
private unsafe object?[] SerializeEventArgs(int eventId, object?[] args)
20091997
{
20101998
Debug.Assert(m_eventData != null);
2011-
TraceLoggingEventTypes? eventTypes = m_eventData[eventId].TraceLoggingEventTypes;
2012-
if (eventTypes == null)
2013-
{
2014-
eventTypes = new TraceLoggingEventTypes(m_eventData[eventId].Name,
2015-
EventTags.None,
2016-
m_eventData[eventId].Parameters);
2017-
Interlocked.CompareExchange(ref m_eventData[eventId].TraceLoggingEventTypes, eventTypes, null);
2018-
}
1999+
TraceLoggingEventTypes eventTypes = m_eventData[eventId].TraceLoggingEventTypes;
20192000
int paramCount = Math.Min(eventTypes.typeInfos.Length, args.Length); // parameter count mismatch get logged in LogEventArgsMismatches
20202001
var eventData = new object?[eventTypes.typeInfos.Length];
20212002
for (int i = 0; i < paramCount; i++)
@@ -2466,9 +2447,22 @@ internal partial struct EventMetadata
24662447
public bool AllParametersAreString;
24672448
public bool AllParametersAreInt32;
24682449

2469-
public TraceLoggingEventTypes? TraceLoggingEventTypes;
24702450
public EventActivityOptions ActivityOptions;
24712451

2452+
private TraceLoggingEventTypes _traceLoggingEventTypes;
2453+
public TraceLoggingEventTypes TraceLoggingEventTypes
2454+
{
2455+
get
2456+
{
2457+
if (_traceLoggingEventTypes is null)
2458+
{
2459+
var tlet = new TraceLoggingEventTypes(Name, Tags, Parameters);
2460+
Interlocked.CompareExchange(ref _traceLoggingEventTypes, tlet, null);
2461+
}
2462+
return _traceLoggingEventTypes;
2463+
}
2464+
}
2465+
24722466
private ReadOnlyCollection<string>? _parameterNames;
24732467
public ReadOnlyCollection<string> ParameterNames => _parameterNames ??= GetParameterNames();
24742468

0 commit comments

Comments
 (0)