Skip to content

Add TSF_SuspensionTrapped flag to NativeAOT for accurate sample profiler reporting#125764

Merged
MichalStrehovsky merged 2 commits intodotnet:mainfrom
MichalStrehovsky:fix/nativeaot-suspension-trapped-flag
Mar 19, 2026
Merged

Add TSF_SuspensionTrapped flag to NativeAOT for accurate sample profiler reporting#125764
MichalStrehovsky merged 2 commits intodotnet:mainfrom
MichalStrehovsky:fix/nativeaot-suspension-trapped-flag

Conversation

@MichalStrehovsky
Copy link
Member

Implement TSF_SuspensionTrapped in the NativeAOT runtime to distinguish managed from external code in EventPipe sample profiler events, matching CoreCLR's TS_SuspensionTrapped behavior.

The flag is set in WaitForGC before entering preemptive mode and cleared after the wait loop completes. All suspension paths (DisablePreemptiveMode, InlineSuspend, Redirect, hijack stubs) funnel through WaitForGC, ensuring complete coverage.

Fixes #125217

…ler reporting

Implement TSF_SuspensionTrapped in the NativeAOT runtime to distinguish
managed from external code in EventPipe sample profiler events, matching
CoreCLR's TS_SuspensionTrapped behavior.

The flag is set in WaitForGC before entering preemptive mode and cleared
after the wait loop completes. All suspension paths (DisablePreemptiveMode,
InlineSuspend, Redirect, hijack stubs) funnel through WaitForGC, ensuring
complete coverage.

Fixes dotnet#125217

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings March 19, 2026 05:40
@MichalStrehovsky
Copy link
Member Author

/azp run runtime-nativeaot-outerloop

@dotnet-policy-service
Copy link
Contributor

Tagging subscribers to this area: @agocke, @dotnet/ilc-contrib
See info in area-owners.md if you want to be subscribed.

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Implements a new NativeAOT thread-state flag (TSF_SuspensionTrapped) to let EventPipe SampleProfiler correctly label samples as Managed vs External while threads are trapped during suspension, aligning NativeAOT behavior with CoreCLR and re-enabling the previously skipped validation test.

Changes:

  • Add TSF_SuspensionTrapped thread-state flag and expose Thread::IsSuspensionTrapped().
  • Set/clear the flag around Thread::WaitForGC to mark threads trapped for suspension.
  • Use the flag in NativeAOT EventPipe sample-profiler payload classification and unskip the SampleProfilerSampleType test for NativeAOT.

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
src/tests/tracing/eventpipe/eventsvalidation/SampleProfilerSampleType.cs Removes NativeAOT ActiveIssue skip so the SampleType validation runs on NativeAOT again.
src/coreclr/nativeaot/Runtime/thread.h Adds TSF_SuspensionTrapped flag value and declares IsSuspensionTrapped().
src/coreclr/nativeaot/Runtime/thread.inl Adds inline IsSuspensionTrapped() implementation.
src/coreclr/nativeaot/Runtime/thread.cpp Sets/clears TSF_SuspensionTrapped in WaitForGC.
src/coreclr/nativeaot/Runtime/eventpipe/ep-rt-aot.cpp Uses IsSuspensionTrapped() to set SampleProfiler sample type to Managed vs External.

You can also share your feedback on Copilot code review. Take the survey.

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings March 19, 2026 06:59
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR implements a NativeAOT equivalent of CoreCLR’s TS_SuspensionTrapped behavior to correctly classify EventPipe SampleProfiler ThreadSample events as Managed vs External, enabling accurate reporting and re-enabling the previously skipped validation test.

Changes:

  • Add TSF_SuspensionTrapped thread state flag and accessor in the NativeAOT runtime.
  • Set/clear TSF_SuspensionTrapped around Thread::WaitForGC so suspension/trap paths can be identified as originating from cooperative (managed) execution.
  • Update the NativeAOT EventPipe SampleProfiler payload to emit MANAGED when the target thread is suspension-trapped, and remove the NativeAOT ActiveIssue skip from the test.

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated no comments.

Show a summary per file
File Description
src/tests/tracing/eventpipe/eventsvalidation/SampleProfilerSampleType.cs Removes NativeAOT skip so the test runs now that managed/external sample typing is fixed.
src/coreclr/nativeaot/Runtime/thread.h Introduces TSF_SuspensionTrapped flag and declares IsSuspensionTrapped().
src/coreclr/nativeaot/Runtime/thread.inl Implements Thread::IsSuspensionTrapped() inline via IsStateSet.
src/coreclr/nativeaot/Runtime/thread.cpp Sets TSF_SuspensionTrapped on entry to WaitForGC and clears it after the trap loop completes.
src/coreclr/nativeaot/Runtime/eventpipe/ep-rt-aot.cpp Uses IsSuspensionTrapped() to choose EP_SAMPLE_PROFILER_SAMPLE_TYPE_MANAGED vs EXTERNAL for samples.

You can also share your feedback on Copilot code review. Take the survey.

@MichalStrehovsky
Copy link
Member Author

/azp run runtime-nativeaot-outerloop

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@MichalStrehovsky MichalStrehovsky merged commit cad7369 into dotnet:main Mar 19, 2026
114 of 134 checks passed
@MichalStrehovsky MichalStrehovsky deleted the fix/nativeaot-suspension-trapped-flag branch March 19, 2026 12:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

SampleProfilerSampleType failing with native AOT

3 participants