Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 7bbfcc4

Browse files
authored
Add EventSource event for WaitHandle waits (#94737)
Add start/stop events for WaitHandle waits Context: #94264
1 parent f1d2587 commit 7bbfcc4

35 files changed

+648
-27
lines changed

src/coreclr/System.Private.CoreLib/src/System/Diagnostics/Eventing/NativeRuntimeEventSource.Threading.NativeSinks.CoreCLR.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,5 +97,18 @@ private static partial void LogThreadPoolIOPack(
9797
IntPtr NativeOverlapped,
9898
IntPtr Overlapped,
9999
ushort ClrInstanceID);
100+
101+
#pragma warning disable IDE0060 // Remove unused parameter
102+
[NonEvent]
103+
private static void LogWaitHandleWaitStart(
104+
WaitHandleWaitSourceMap WaitSource,
105+
IntPtr AssociatedObjectID,
106+
ushort ClrInstanceID) =>
107+
Debug.Fail("This event is currently not expected to be raised by managed code in CoreCLR.");
108+
109+
[NonEvent]
110+
private static void LogWaitHandleWaitStop(ushort ClrInstanceID) =>
111+
Debug.Fail("This event is currently not expected to be raised by managed code in CoreCLR.");
112+
#pragma warning restore IDE0060
100113
}
101114
}

src/coreclr/System.Private.CoreLib/src/System/Threading/WaitHandle.CoreCLR.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public abstract partial class WaitHandle
1111
[MethodImpl(MethodImplOptions.InternalCall)]
1212
private static extern int WaitOneCore(IntPtr waitHandle, int millisecondsTimeout);
1313

14-
internal static unsafe int WaitMultipleIgnoringSyncContext(Span<IntPtr> waitHandles, bool waitAll, int millisecondsTimeout)
14+
private static unsafe int WaitMultipleIgnoringSyncContextCore(Span<IntPtr> waitHandles, bool waitAll, int millisecondsTimeout)
1515
{
1616
fixed (IntPtr* pWaitHandles = &MemoryMarshal.GetReference(waitHandles))
1717
{

src/coreclr/gc/env/etmdummy.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -417,3 +417,5 @@
417417
#define FireEtwAppDomainAssemblyResolveHandlerInvoked(ClrInstanceId, assemblyName, handlerName, resultAssemblyName, resultAssemblyPath) 0
418418
#define FireEtwAssemblyLoadFromResolveHandlerInvoked(ClrInstanceId, assemblyName, isTrackedAssembly, requestingAssemblyPath, requestedAssemblyPath) 0
419419
#define FireEtwEventSource(eventID, eventName, eventSourceName, payload) 0
420+
#define FireEtwWaitHandleWaitStart(WaitSource, AssociatedObjectID, ClrInstanceID) 0
421+
#define FireEtwWaitHandleWaitStop(ClrInstanceID) 0

src/coreclr/inc/eventtracebase.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1153,6 +1153,19 @@ namespace ETW
11531153
} ContentionFlags;
11541154
} ContentionStructs;
11551155
};
1156+
1157+
class WaitHandleLog
1158+
{
1159+
public:
1160+
typedef union _WaitHandleStructs
1161+
{
1162+
typedef enum _WaitSource {
1163+
Unknown=0,
1164+
MonitorWait=1
1165+
} WaitSource;
1166+
} WaitHandleStructs;
1167+
};
1168+
11561169
// Class to wrap all Interop logic for ETW
11571170
class InteropLog
11581171
{

src/coreclr/nativeaot/Runtime/disabledruntimeeventinternal.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,4 +84,12 @@ EXTERN_C NATIVEAOT_API void __cdecl NativeRuntimeEventSource_LogExceptionThrown(
8484
{
8585
}
8686

87+
EXTERN_C NATIVEAOT_API void __cdecl NativeRuntimeEventSource_LogWaitHandleWaitStart(uint8_t WaitSource, intptr_t AssociatedObjectID, uint16_t ClrInstanceID)
88+
{
89+
}
90+
91+
EXTERN_C NATIVEAOT_API void __cdecl NativeRuntimeEventSource_LogWaitHandleWaitStop(uint16_t ClrInstanceID)
92+
{
93+
}
94+
8795
#endif // FEATURE_PERFTRACING

src/coreclr/nativeaot/Runtime/eventpipe/gen-eventing-event-inc.lst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,3 +110,5 @@ ThreadPoolWorkerThreadStop
110110
ThreadPoolWorkerThreadWait
111111
ThreadPoolWorkingThreadCount
112112
ThreadRunning
113+
WaitHandleWaitStart
114+
WaitHandleWaitStop

src/coreclr/nativeaot/Runtime/runtimeeventinternal.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,4 +100,14 @@ EXTERN_C NATIVEAOT_API void __cdecl NativeRuntimeEventSource_LogExceptionThrown(
100100
GetClrInstanceId());
101101
}
102102

103+
EXTERN_C NATIVEAOT_API void __cdecl NativeRuntimeEventSource_LogWaitHandleWaitStart(uint8_t WaitSource, intptr_t AssociatedObjectID, uint16_t ClrInstanceID)
104+
{
105+
FireEtwWaitHandleWaitStart(WaitSource, reinterpret_cast<const void*>(AssociatedObjectID), ClrInstanceID);
106+
}
107+
108+
EXTERN_C NATIVEAOT_API void __cdecl NativeRuntimeEventSource_LogWaitHandleWaitStop(uint16_t ClrInstanceID)
109+
{
110+
FireEtwWaitHandleWaitStop(ClrInstanceID);
111+
}
112+
103113
#endif // FEATURE_PERFTRACING

src/coreclr/nativeaot/System.Private.CoreLib/src/System/Diagnostics/Eventing/NativeRuntimeEventSource.Threading.NativeSinks.NativeAot.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ public static partial class Keywords
2222
public const EventKeywords ContentionKeyword = (EventKeywords)0x4000;
2323
public const EventKeywords ThreadingKeyword = (EventKeywords)0x10000;
2424
public const EventKeywords ThreadTransferKeyword = (EventKeywords)0x80000000;
25+
public const EventKeywords WaitHandleKeyword = (EventKeywords)0x40000000000;
2526
}
2627

2728
[NonEvent]
@@ -132,5 +133,20 @@ internal static void LogThreadPoolIOPack(
132133
{
133134
RuntimeImports.NativeRuntimeEventSource_LogThreadPoolIOPack(NativeOverlapped, Overlapped, ClrInstanceID);
134135
}
136+
137+
[NonEvent]
138+
internal static void LogWaitHandleWaitStart(
139+
WaitHandleWaitSourceMap WaitSource,
140+
IntPtr AssociatedObjectID,
141+
ushort ClrInstanceID)
142+
{
143+
RuntimeImports.NativeRuntimeEventSource_LogWaitHandleWaitStart((byte)WaitSource, AssociatedObjectID, ClrInstanceID);
144+
}
145+
146+
[NonEvent]
147+
internal static void LogWaitHandleWaitStop(ushort ClrInstanceID)
148+
{
149+
RuntimeImports.NativeRuntimeEventSource_LogWaitHandleWaitStop(ClrInstanceID);
150+
}
135151
}
136152
}

src/coreclr/nativeaot/System.Private.CoreLib/src/System/Runtime/RuntimeImports.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -826,6 +826,17 @@ internal static partial void NativeRuntimeEventSource_LogThreadPoolIOPack(
826826
[LibraryImport(RuntimeLibrary)]
827827
[UnmanagedCallConv(CallConvs = new Type[] { typeof(CallConvCdecl) })]
828828
internal static unsafe partial void NativeRuntimeEventSource_LogExceptionThrown(char* exceptionTypeName, char* exceptionMessage, IntPtr faultingIP, long hresult);
829+
830+
[LibraryImport(RuntimeLibrary)]
831+
[UnmanagedCallConv(CallConvs = new Type[] { typeof(CallConvCdecl) })]
832+
internal static partial void NativeRuntimeEventSource_LogWaitHandleWaitStart(
833+
byte WaitSource,
834+
IntPtr AssociatedObjectID,
835+
ushort ClrInstanceID);
836+
837+
[LibraryImport(RuntimeLibrary)]
838+
[UnmanagedCallConv(CallConvs = new Type[] { typeof(CallConvCdecl) })]
839+
internal static partial void NativeRuntimeEventSource_LogWaitHandleWaitStop(ushort ClrInstanceID);
829840
#endif // FEATURE_PERFTRACING
830841

831842
//

src/coreclr/nativeaot/System.Private.CoreLib/src/System/Threading/Condition.cs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33

44
#pragma warning disable 0420 //passing volatile fields by ref
55

6-
76
using System.Diagnostics;
7+
using System.Diagnostics.Tracing;
88

99
namespace System.Threading
1010
{
@@ -97,7 +97,7 @@ public Condition(Lock @lock)
9797

9898
public bool Wait(TimeSpan timeout) => Wait(WaitHandle.ToTimeoutMilliseconds(timeout));
9999

100-
public unsafe bool Wait(int millisecondsTimeout)
100+
public unsafe bool Wait(int millisecondsTimeout, object? associatedObjectForMonitorWait = null)
101101
{
102102
ArgumentOutOfRangeException.ThrowIfLessThan(millisecondsTimeout, -1);
103103

@@ -111,7 +111,13 @@ public unsafe bool Wait(int millisecondsTimeout)
111111
bool success = false;
112112
try
113113
{
114-
success = waiter.ev.WaitOne(millisecondsTimeout);
114+
success =
115+
waiter.ev.WaitOneNoCheck(
116+
millisecondsTimeout,
117+
associatedObjectForMonitorWait,
118+
associatedObjectForMonitorWait != null
119+
? NativeRuntimeEventSource.WaitHandleWaitSourceMap.MonitorWait
120+
: NativeRuntimeEventSource.WaitHandleWaitSourceMap.Unknown);
115121
}
116122
finally
117123
{

0 commit comments

Comments
 (0)