-
Notifications
You must be signed in to change notification settings - Fork 5.5k
[Profiler] Fix DoStackSnapshot calls in ELT hooks #132301
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -123,6 +123,7 @@ | |
| #include "generics.h" | ||
| #include "gcinfo.h" | ||
| #include "safemath.h" | ||
| #include "stacktrace.h" | ||
| #include "threadsuspend.h" | ||
| #include "inlinetracking.h" | ||
| #include "frozenobjectheap.h" | ||
|
|
@@ -8578,6 +8579,8 @@ HRESULT ProfToEEInterfaceImpl::DoStackSnapshot(ThreadID thread, | |
| CONTEXT ctxCurrent; | ||
| memset(&ctxCurrent, 0, sizeof(ctxCurrent)); | ||
|
|
||
| CONTEXT ctxELT; | ||
|
|
||
| REGDISPLAY rd; | ||
|
|
||
| PROFILER_STACK_WALK_DATA data; | ||
|
|
@@ -8716,6 +8719,17 @@ HRESULT ProfToEEInterfaceImpl::DoStackSnapshot(ThreadID thread, | |
| #endif // !PLATFORM_SUPPORTS_SAFE_THREADSUSPEND | ||
| } | ||
|
|
||
| if ((pctxSeed == nullptr) && (pThreadToSnapshot == pCurrentThread)) | ||
| { | ||
| T_CONTEXT *pELTContext = pCurrentThread->GetProfilerELTContext(); | ||
| if (pELTContext != nullptr) | ||
| { | ||
| CopyOSContext(&ctxELT, pELTContext); | ||
| Thread::VirtualUnwindToFirstManagedCallFrame(&ctxELT); | ||
| pctxSeed = &ctxELT; | ||
| } | ||
| } | ||
|
Comment on lines
+8722
to
+8731
|
||
|
|
||
| // If target thread is in pre-emptive mode, the profiler's seed context is unnecessary | ||
| // because our frame chain is good enough: it will give us at least as accurate a | ||
| // starting point as the profiler could. Also, since profiler contexts cannot be | ||
|
|
@@ -10694,6 +10708,21 @@ void __stdcall ProfilerUnmanagedToManagedTransitionMD(MethodDesc *pMD, | |
| // These do a lot of work for us, setting up Frames, gathering arg info and resolving generics. | ||
| //******************************************************************************************* | ||
|
|
||
| #ifdef PROFILING_SUPPORTED | ||
| static void CaptureProfilerELTContext(T_CONTEXT *pContext) | ||
| { | ||
| CONTRACTL | ||
| { | ||
| NOTHROW; | ||
| GC_NOTRIGGER; | ||
| MODE_COOPERATIVE; | ||
| } | ||
| CONTRACTL_END; | ||
|
|
||
| ClrCaptureContext(pContext); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is very expensive. What is the performance impact of this change when the ETL hooks are enabled? I suspect that it will extremely slow to the point of being unusuable.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see you have some benchmark results in the PR description. What was the benchmark source?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I used an adhoc BenchmarkDotNet microbenchmark in my local repo in conjunction with a profiler with a barebones EnterCallback. Since the context saving and restoration surround the callback in ProfilerEnter, I ran the benchmark against the baseline repo and changed corerun builds to measure the overhead introduced in this PR. |
||
| } | ||
| #endif // PROFILING_SUPPORTED | ||
|
|
||
| HCIMPL2(EXTERN_C void, ProfileEnter, UINT_PTR clientData, void * platformSpecificHandle) | ||
| { | ||
| FCALL_CONTRACT; | ||
|
|
@@ -10741,6 +10770,14 @@ HCIMPL2(EXTERN_C void, ProfileEnter, UINT_PTR clientData, void * platformSpecifi | |
| SetCallbackStateFlagsHolder csf( | ||
| COR_PRF_CALLBACKSTATE_INCALLBACK); | ||
|
|
||
| T_CONTEXT *pProfilerELTContext = nullptr; | ||
| if (CORProfilerStackSnapshotEnabled()) | ||
| { | ||
| pProfilerELTContext = static_cast<T_CONTEXT *>(_alloca(sizeof(T_CONTEXT))); | ||
| CaptureProfilerELTContext(pProfilerELTContext); | ||
| } | ||
| ProfilerELTContextHolder contextHolder(pProfilerELTContext); | ||
|
|
||
| COR_PRF_ELT_INFO_INTERNAL eltInfo; | ||
| eltInfo.platformSpecificHandle = platformSpecificHandle; | ||
|
|
||
|
|
@@ -10908,6 +10945,14 @@ HCIMPL2(EXTERN_C void, ProfileLeave, UINT_PTR clientData, void * platformSpecifi | |
| SetCallbackStateFlagsHolder csf( | ||
| COR_PRF_CALLBACKSTATE_INCALLBACK); | ||
|
|
||
| T_CONTEXT *pProfilerELTContext = nullptr; | ||
| if (CORProfilerStackSnapshotEnabled()) | ||
| { | ||
| pProfilerELTContext = static_cast<T_CONTEXT *>(_alloca(sizeof(T_CONTEXT))); | ||
| CaptureProfilerELTContext(pProfilerELTContext); | ||
| } | ||
| ProfilerELTContextHolder contextHolder(pProfilerELTContext); | ||
|
|
||
| COR_PRF_ELT_INFO_INTERNAL eltInfo; | ||
| eltInfo.platformSpecificHandle = platformSpecificHandle; | ||
|
|
||
|
|
@@ -11033,6 +11078,14 @@ HCIMPL2(EXTERN_C void, ProfileTailcall, UINT_PTR clientData, void * platformSpec | |
| SetCallbackStateFlagsHolder csf( | ||
| COR_PRF_CALLBACKSTATE_INCALLBACK); | ||
|
|
||
| T_CONTEXT *pProfilerELTContext = nullptr; | ||
| if (CORProfilerStackSnapshotEnabled()) | ||
| { | ||
| pProfilerELTContext = static_cast<T_CONTEXT *>(_alloca(sizeof(T_CONTEXT))); | ||
| CaptureProfilerELTContext(pProfilerELTContext); | ||
| } | ||
| ProfilerELTContextHolder contextHolder(pProfilerELTContext); | ||
|
|
||
| COR_PRF_ELT_INFO_INTERNAL eltInfo; | ||
| eltInfo.platformSpecificHandle = platformSpecificHandle; | ||
|
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is indirectly introducing new libunwind dependency. It is factory for problems. For example, this won't work on Windows x86 at all.
I think if we wanted to fix this, it would be better to fix the codegen for these hooks so that there is proper managed->unmanaged transition.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure DoStackSnapshot works within ELT hooks on Windows x86, as it returned
CORPROF_E_STACKSNAPSHOT_UNSAFEon Windows x64 both in .NET 9 and .NET 10.It seemed like the scenario only worked on Linux x64 in .NET 9.
Thanks, I'll look into fixing the codegen if this overhead is going in the wrong direction.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If this never worked on Windows x64, was it actually working reliably on Linux? If it never worked well, can we block it everywhere instead like it is blocked on Windows?
I do not think it is worth it to try to make these callbacks worth better. They are slow, and I believe most profilers moved away from them.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a particular reason for DoStackSnapshot to not be supported within these callbacks?
I don't fully understand what DoStackSnapshot within these callbacks would achieve, but for the user of the original issue, what would an alternative be?
It sounds like the scenario was working on Linux until #107152, from the issue author it worked on .NET 8 and .NET 9. I confirmed it worked on .NET 9, didn't try .NET 8 yet.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
https://learn.microsoft.com/en-us/dotnet/framework/unmanaged-api/profiling/icorprofilerinfo2-dostacksnapshot-method documentation says that the synchronous DoStackSnapshot stackwalk only works from ICorProfilerCallback callbacks. ETL hook is not ICorProfilerCallback callback. I do not think this was ever meant to work.