Enable PC sampling with attach/detach: add code_object tracking and dedicated background thread for attach/detach to prevent deadlocks#3921
Draft
julijose-amd wants to merge 2 commits intodevelopfrom
Conversation
meserve-amd
reviewed
Mar 10, 2026
Contributor
meserve-amd
left a comment
There was a problem hiding this comment.
Looks good so far, some minor comments on the draft
Please add tests or add-on to existing tests in the full PR
Comment on lines
+35
to
+39
| #include <condition_variable> | ||
| #include <mutex> | ||
| #include <signal.h> | ||
| #include <thread> | ||
| #include <unistd.h> |
Contributor
There was a problem hiding this comment.
Suggested change
| #include <condition_variable> | |
| #include <mutex> | |
| #include <signal.h> | |
| #include <thread> | |
| #include <unistd.h> | |
| #include <signal.h> | |
| #include <unistd.h> | |
| #include <condition_variable> | |
| #include <mutex> | |
| #include <thread> |
Give system headers their own category separate from C++ library headers
| { | ||
| rocprofiler::common::init_logging("ROCPROFILER_ATTACH"); | ||
|
|
||
| static BackgroundThread bg_thread; |
Contributor
There was a problem hiding this comment.
Prefer to use the rocprofiler common::static_object and hide this in a separate "get" function
See here for an example.
Available in lib/common/static_object.hpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
PC Sampling: Code Object Tracking in Attach/Detach Mode
CodeobjTableTranslatorSynchronizedto map sampled PC addresses to code object IDs. In non-attach mode, this is populated viahsa_executable_freeze/hsa_executable_destroyhooks. In attach/detach mode, these hooks are not available as the profiler attaches after HSA initialization, sohsa_executable_freezehas already been called for all loaded executables. Without the attach table path, the translation table remains empty and PC samples cannot be correlated to code objects.pc_sampling/code_objectthat uses the attach dispatch table to enumerate already‑loaded code objects and register a chained notify callback for newly loaded ones.CodeobjTableTranslatorSynchronizedis correctly populated, enabling PC address → code object correlation when attaching mid‑execution.pc_sampling::code_object::initialize(rocattach_api)and integrated it intoregistration.cppso the PC sampling code object module initializes when rocattach registers.Background Thread to avoid deadlocks during Attach/Detach
librocprofiler-sdk-attachthat:"rocp-bg-attach"viapthread_setname_np/proc/<pid>/task/*/commfor"rocp-bg-attach"and uses its TID for all ptrace operations.Changes
pc_sampling/code_object.cpp
executable_freeze_internal()shared logic for HSA hook path and the attach pathchained_notify_new_code_object()for callback chainingload_attach_code_objects()for attach‑time initializationinitialize(RocAttachDispatchTable*)overloadpc_sampling/code_object.hpp
"lib/rocprofiler-sdk-attach/table.h"initialize(RocAttachDispatchTable*)overloadregistration.cpp
(guarded by
ROCPROFILER_SDK_HSA_PC_SAMPLING)rocprofiler-sdk-attach/attach.cpp
BackgroundThreadwith thread naming, signal blocking, and idle waitrocprofiler-sdk-rocattach/rocattach.cpp
resolve_attach_tid()to scan/proc/<pid>/task/*/commsetup()to use the background thread’s TIDTesting
/proc/<pid>/task/*/comm.