Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle NativeOverlapped* coming from both the Windows or Portable thread pool in NativeRuntimeEventSource #97365

Merged
merged 9 commits into from
Feb 9, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ public unsafe void ThreadPoolWorkerThreadAdjustmentStats(
[Event(63, Level = EventLevel.Verbose, Message = Messages.IOEnqueue, Task = Tasks.ThreadPool, Opcode = Opcodes.IOEnqueue, Version = 0, Keywords = Keywords.ThreadingKeyword | Keywords.ThreadTransferKeyword)]
private unsafe void ThreadPoolIOEnqueue(
IntPtr NativeOverlapped,
IntPtr Overlapped,
IntPtr Overlapped, // 0 if the Windows thread pool is used, the relevant info could be obtained from the NativeOverlapped* if necessary
bool MultiDequeues,
ushort ClrInstanceID = DefaultClrInstanceId)
{
Expand All @@ -230,9 +230,14 @@ public unsafe void ThreadPoolIOEnqueue(NativeOverlapped* nativeOverlapped)
{
if (IsEnabled(EventLevel.Verbose, Keywords.ThreadingKeyword | Keywords.ThreadTransferKeyword))
{
#if TARGET_WINDOWS
eduardo-vp marked this conversation as resolved.
Show resolved Hide resolved
IntPtr overlapped = ThreadPool.UseWindowsThreadPool ? 0 : (IntPtr)Overlapped.GetOverlappedFromNative(nativeOverlapped).GetHashCode();
#else
IntPtr overlapped = (IntPtr)Overlapped.GetOverlappedFromNative(nativeOverlapped).GetHashCode();
#endif
ThreadPoolIOEnqueue(
(IntPtr)nativeOverlapped,
(IntPtr)Overlapped.GetOverlappedFromNative(nativeOverlapped).GetHashCode(),
overlapped,
false);
}
}
Expand All @@ -254,7 +259,7 @@ public void ThreadPoolIOEnqueue(RegisteredWaitHandle registeredWaitHandle)
[Event(64, Level = EventLevel.Verbose, Message = Messages.IO, Task = Tasks.ThreadPool, Opcode = Opcodes.IODequeue, Version = 0, Keywords = Keywords.ThreadingKeyword | Keywords.ThreadTransferKeyword)]
private unsafe void ThreadPoolIODequeue(
IntPtr NativeOverlapped,
IntPtr Overlapped,
IntPtr Overlapped, // 0 if the Windows thread pool is used, the relevant info could be obtained from the NativeOverlapped* if necessary
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How do diagnostic tools use this value? Is 0 the best option to make them work seamlessly?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As far as I know it's not used for anything specific, just additional info. We used to pass in the actual Overlapped object pointer and later switched to a hash code since that's not a stable value. I'm not sure how useful it is actually, since the NativeOverlapped* is stable, maybe a hash code is not very useful. For the Windows thread pool implementation there is an OverlappedData object that could be used, though it could also be obtained from the NativeOverlapped* if debugging side-by-side with a heap dump.

ushort ClrInstanceID = DefaultClrInstanceId)
{
LogThreadPoolIODequeue(NativeOverlapped, Overlapped, ClrInstanceID);
Expand All @@ -266,9 +271,14 @@ public unsafe void ThreadPoolIODequeue(NativeOverlapped* nativeOverlapped)
{
if (IsEnabled(EventLevel.Verbose, Keywords.ThreadingKeyword | Keywords.ThreadTransferKeyword))
{
#if TARGET_WINDOWS
IntPtr overlapped = ThreadPool.UseWindowsThreadPool ? 0 : (IntPtr)Overlapped.GetOverlappedFromNative(nativeOverlapped).GetHashCode();
#else
IntPtr overlapped = (IntPtr)Overlapped.GetOverlappedFromNative(nativeOverlapped).GetHashCode();
#endif
ThreadPoolIODequeue(
(IntPtr)nativeOverlapped,
(IntPtr)Overlapped.GetOverlappedFromNative(nativeOverlapped).GetHashCode());
overlapped);
}
}

Expand Down Expand Up @@ -300,16 +310,21 @@ public unsafe void ThreadPoolIOPack(NativeOverlapped* nativeOverlapped)
{
if (IsEnabled(EventLevel.Verbose, Keywords.ThreadingKeyword))
{
#if TARGET_WINDOWS
IntPtr overlapped = ThreadPool.UseWindowsThreadPool ? 0 : (IntPtr)Overlapped.GetOverlappedFromNative(nativeOverlapped).GetHashCode();
#else
IntPtr overlapped = (IntPtr)Overlapped.GetOverlappedFromNative(nativeOverlapped).GetHashCode();
#endif
ThreadPoolIOPack(
(IntPtr)nativeOverlapped,
(IntPtr)Overlapped.GetOverlappedFromNative(nativeOverlapped).GetHashCode());
overlapped);
}
}

[Event(65, Level = EventLevel.Verbose, Message = Messages.IO, Task = Tasks.ThreadPool, Opcode = Opcodes.IOPack, Version = 0, Keywords = Keywords.ThreadingKeyword)]
private unsafe void ThreadPoolIOPack(
IntPtr NativeOverlapped,
IntPtr Overlapped,
IntPtr Overlapped, // 0 if the Windows thread pool is used, the relevant info could be obtained from the NativeOverlapped* if necessary
ushort ClrInstanceID = DefaultClrInstanceId)
{
LogThreadPoolIOPack(NativeOverlapped, Overlapped, ClrInstanceID);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ public unsafe void ThreadPoolWorkerThreadAdjustmentStats(
[Event(63, Level = EventLevel.Verbose, Message = Messages.IOEnqueue, Task = Tasks.ThreadPool, Opcode = Opcodes.IOEnqueue, Version = 0, Keywords = Keywords.ThreadingKeyword | Keywords.ThreadTransferKeyword)]
private unsafe void ThreadPoolIOEnqueue(
IntPtr NativeOverlapped,
IntPtr Overlapped,
IntPtr Overlapped, // 0 if the Windows thread pool is used, the relevant info could be obtained from the NativeOverlapped* if necessary
bool MultiDequeues,
ushort ClrInstanceID = DefaultClrInstanceId)
{
Expand All @@ -367,9 +367,14 @@ public unsafe void ThreadPoolIOEnqueue(NativeOverlapped* nativeOverlapped)
{
if (IsEnabled(EventLevel.Verbose, Keywords.ThreadingKeyword | Keywords.ThreadTransferKeyword))
{
#if TARGET_WINDOWS
IntPtr overlapped = ThreadPool.UseWindowsThreadPool ? 0 : (IntPtr)Overlapped.GetOverlappedFromNative(nativeOverlapped).GetHashCode();
#else
IntPtr overlapped = (IntPtr)Overlapped.GetOverlappedFromNative(nativeOverlapped).GetHashCode();
#endif
ThreadPoolIOEnqueue(
(IntPtr)nativeOverlapped,
(IntPtr)Overlapped.GetOverlappedFromNative(nativeOverlapped).GetHashCode(),
overlapped,
false);
}
}
eduardo-vp marked this conversation as resolved.
Show resolved Hide resolved
Expand All @@ -392,7 +397,7 @@ public void ThreadPoolIOEnqueue(RegisteredWaitHandle registeredWaitHandle)
[Event(64, Level = EventLevel.Verbose, Message = Messages.IO, Task = Tasks.ThreadPool, Opcode = Opcodes.IODequeue, Version = 0, Keywords = Keywords.ThreadingKeyword | Keywords.ThreadTransferKeyword)]
private unsafe void ThreadPoolIODequeue(
IntPtr NativeOverlapped,
IntPtr Overlapped,
IntPtr Overlapped, // 0 if the Windows thread pool is used, the relevant info could be obtained from the NativeOverlapped* if necessary
ushort ClrInstanceID = DefaultClrInstanceId)
{
EventData* data = stackalloc EventData[3];
Expand All @@ -414,9 +419,14 @@ public unsafe void ThreadPoolIODequeue(NativeOverlapped* nativeOverlapped)
{
if (IsEnabled(EventLevel.Verbose, Keywords.ThreadingKeyword | Keywords.ThreadTransferKeyword))
{
#if TARGET_WINDOWS
IntPtr overlapped = ThreadPool.UseWindowsThreadPool ? 0 : (IntPtr)Overlapped.GetOverlappedFromNative(nativeOverlapped).GetHashCode();
#else
IntPtr overlapped = (IntPtr)Overlapped.GetOverlappedFromNative(nativeOverlapped).GetHashCode();
#endif
ThreadPoolIODequeue(
(IntPtr)nativeOverlapped,
(IntPtr)Overlapped.GetOverlappedFromNative(nativeOverlapped).GetHashCode());
overlapped);
}
}

Expand Down Expand Up @@ -456,17 +466,22 @@ public unsafe void ThreadPoolIOPack(NativeOverlapped* nativeOverlapped)
{
if (IsEnabled(EventLevel.Verbose, Keywords.ThreadingKeyword))
{
#if TARGET_WINDOWS
IntPtr overlapped = ThreadPool.UseWindowsThreadPool ? 0 : (IntPtr)Overlapped.GetOverlappedFromNative(nativeOverlapped).GetHashCode();
#else
IntPtr overlapped = (IntPtr)Overlapped.GetOverlappedFromNative(nativeOverlapped).GetHashCode();
#endif
ThreadPoolIOPack(
(IntPtr)nativeOverlapped,
(IntPtr)Overlapped.GetOverlappedFromNative(nativeOverlapped).GetHashCode());
overlapped);
}
}

[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026:UnrecognizedReflectionPattern", Justification = "Parameters to this method are primitive and are trimmer safe")]
[Event(65, Level = EventLevel.Verbose, Message = Messages.IO, Task = Tasks.ThreadPool, Opcode = Opcodes.IOPack, Version = 0, Keywords = Keywords.ThreadingKeyword)]
private unsafe void ThreadPoolIOPack(
IntPtr NativeOverlapped,
IntPtr Overlapped,
IntPtr Overlapped, // 0 if the Windows thread pool is used, the relevant info could be obtained from the NativeOverlapped* if necessary
ushort ClrInstanceID = DefaultClrInstanceId)
{
EventData* data = stackalloc EventData[3];
Expand Down
Loading