Skip to content

feat(nvtx): add thread_id to RangePush/RangePop and stamp OS thread id in callbacks - #474

Merged
rapids-bot[bot] merged 5 commits into
rapidsai:mainfrom
9prady9:nvtx-thread-id-pr
Jul 29, 2026
Merged

feat(nvtx): add thread_id to RangePush/RangePop and stamp OS thread id in callbacks#474
rapids-bot[bot] merged 5 commits into
rapidsai:mainfrom
9prady9:nvtx-thread-id-pr

Conversation

@9prady9

@9prady9 9prady9 commented Jul 28, 2026

Copy link
Copy Markdown
Contributor

Prerequisite for the NVTX tolerant analyzer (#473 ).

Adds thread_id: u32 to the RangePush and RangePop variants in nvtx-events so the analyzer can reconstruct per-thread nested stacks. Without a stamped thread id, Push/Pop reconstruction would require a global (incorrect) stack or best-effort heuristics.

Changes:

  • nvtx-events: RangePush and RangePop gain a thread_id: u32 field
  • nvtx-injection: on_range_push / on_range_pop callbacks stamp the OS thread id via gettid (Linux); non-Linux falls back to a hash of std::thread::current().id() truncated to u32
  • nvtx-injection: on_domain_range_push_ex / on_domain_range_pop (CORE2 path) updated identically
  • nvtx-example: new tests/thread_id.rs — captures a two-thread interleaved Push/Pop session and asserts each event carries the expected thread id

…d in callbacks

Adds thread_id: u32 to the RangePush and RangePop event variants so the
analyzer can reconstruct per-thread nested stacks. Stamps the OS thread id
via gettid (Linux) in the injection callbacks and proves it end-to-end in a
new thread_id capture test.

Signed-off-by: Pradeep Garigipati <pgarigipati@nvidia.com>
@9prady9
9prady9 marked this pull request as ready for review July 28, 2026 10:56
@coderabbitai

coderabbitai Bot commented Jul 28, 2026

Copy link
Copy Markdown

Review Change Stack

Note

Reviews paused

It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the reviews.auto_review.auto_pause_after_reviewed_commits setting.

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • ▶️ Resume reviews
  • 🔍 Trigger review

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: QUIET

Plan: Enterprise

Run ID: d852406b-c118-4272-b8a7-883e163c4170

📥 Commits

Reviewing files that changed from the base of the PR and between 4f27845 and 8fab534.

📒 Files selected for processing (2)
  • integrations/nvtx/injection/build.rs
  • integrations/nvtx/injection/src/init.rs

📝 Walkthrough

Walkthrough

NVTX range push and pop events now carry per-thread identifiers. Injection callbacks capture identifiers through a Linux-specific helper, conversion functions preserve them, and the example adds synchronized multithreaded capture with integration coverage.

Changes

NVTX thread ID propagation

Layer / File(s) Summary
Thread ID generation and platform guard
integrations/nvtx/injection/Cargo.toml, integrations/nvtx/injection/build.rs, integrations/nvtx/injection/src/init.rs
Adds the libc dependency, restricts builds to Linux 64-bit targets, and provides cached current_thread_id() generation with stability tests.
Event payload and conversion updates
integrations/nvtx/events/src/lib.rs, integrations/nvtx/injection/src/convert.rs
Adds thread_id to range event payloads, threads it through conversion functions, and updates conversion tests.
Callback propagation
integrations/nvtx/injection/src/callbacks.rs
Captures the current thread identifier inside NVTX callback guards and passes it to range conversion functions.
Multithreaded capture validation
integrations/nvtx/example/src/lib.rs, integrations/nvtx/example/tests/thread_id.rs
Adds synchronized multi-thread capture and verifies distinct, nonzero, matching push/pop thread identifiers.

Estimated code review effort: 3 (Moderate) | ~25 minutes

Possibly related PRs

Suggested labels: feature request

Suggested reviewers: mbrobbel, dhruv9vats

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Description check ⚠️ Warning The PR description explains the change, but it does not follow the required template or include Testing, Related Issues, and Screenshots sections. Rewrite the description using the repository template and add Description, Related Issues, Testing commands, and Screenshots or a note that none are needed.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly summarizes the main change: adding thread_id to NVTX push/pop events and stamping OS thread IDs in callbacks.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@integrations/nvtx/example/tests/thread_id.rs`:
- Around line 67-72: Expand the test around the single-thread Push/Pop sequence
to coordinate two threads and interleave their operations. Record each thread’s
IDs separately, assert that its matching Push/Pop pairs share one thread_id, and
assert the two threads’ IDs differ; retain the existing consistency checks while
exercising the per-thread reconstruction behavior.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: QUIET

Plan: Enterprise

Run ID: dcf42e59-a934-4fa3-8f80-bcaaba3459d3

📥 Commits

Reviewing files that changed from the base of the PR and between 3b7085c and 5a95a0e.

⛔ Files ignored due to path filters (1)
  • Cargo.lock is excluded by !**/*.lock, !Cargo.lock
📒 Files selected for processing (7)
  • Cargo.toml
  • integrations/nvtx/events/src/lib.rs
  • integrations/nvtx/example/tests/thread_id.rs
  • integrations/nvtx/injection/Cargo.toml
  • integrations/nvtx/injection/src/callbacks.rs
  • integrations/nvtx/injection/src/convert.rs
  • integrations/nvtx/injection/src/init.rs

Comment thread integrations/nvtx/example/tests/thread_id.rs Outdated
9prady9 added 2 commits July 28, 2026 16:38
Replace the single-thread consistency check with a 4-thread test that
also asserts cross-thread uniqueness — the property CodeRabbit flagged as
missing. Adds run_capture_n_threads to nvtx-example: spawns N threads behind
a Barrier so their push/pop events interleave in real time, then asserts
N distinct thread_ids across Push events and N across Pop events, and that
the two sets are identical (Push and Pop on the same thread share one id).

Signed-off-by: Pradeep Garigipati <pgarigipati@nvidia.com>
Signed-off-by: Pradeep Garigipati <pgarigipati@nvidia.com>

@johanpel johanpel left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Looks good overall, just two minor things we can improve:

Comment thread integrations/nvtx/injection/src/init.rs
Comment thread Cargo.toml Outdated
Cache the gettid result (or hash fallback) in a thread_local OnceCell so
the syscall is paid once per thread rather than on every push/pop callback.

Move libc from [workspace.dependencies] to a direct version pin in
nvtx-injection's own Cargo.toml — no other crate uses it.

Signed-off-by: Pradeep Garigipati <pgarigipati@nvidia.com>

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
integrations/nvtx/injection/src/init.rs (1)

71-76: 🎯 Functional Correctness | 🟠 Major | ⚡ Quick win

Use collision-free IDs for the non-Linux fallback.

Hashing ThreadId and truncating the result to u32 does not guarantee distinct identifiers for live threads. A collision can merge separate threads’ push/pop stacks during reconstruction; .max(1) only prevents zero. Allocate a process-local nonzero ID (for example, with an atomic counter cached by the existing OnceCell) and add a cross-thread distinctness test.

Also applies to: 578-588

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@integrations/nvtx/injection/src/init.rs` around lines 71 - 76, Replace the
hashed ThreadId logic in compute_thread_id with a process-local atomic ID
allocator cached through the existing OnceCell, ensuring each live thread
receives a distinct nonzero u32 identifier. Reuse the per-thread cached value so
repeated calls for one thread remain stable, and add a cross-thread test that
verifies the generated IDs are distinct.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Outside diff comments:
In `@integrations/nvtx/injection/src/init.rs`:
- Around line 71-76: Replace the hashed ThreadId logic in compute_thread_id with
a process-local atomic ID allocator cached through the existing OnceCell,
ensuring each live thread receives a distinct nonzero u32 identifier. Reuse the
per-thread cached value so repeated calls for one thread remain stable, and add
a cross-thread test that verifies the generated IDs are distinct.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: QUIET

Plan: Enterprise

Run ID: 36f40791-324d-47f7-a1b8-ca46d14edf64

📥 Commits

Reviewing files that changed from the base of the PR and between 05077b6 and 4f27845.

📒 Files selected for processing (2)
  • integrations/nvtx/injection/Cargo.toml
  • integrations/nvtx/injection/src/init.rs

Remove the non-Linux compute_thread_id hash fallback — the crate already
emits a compile_error! in lib.rs for non-Linux-64 targets, so the fallback
was unreachable dead code and the hash collision risk (WR-03 / CodeRabbit)
was only theoretical.

Add an early platform check in build.rs so cargo reports the unsupported
target before bindgen or rustc run, with the actual os and pointer_width
values in the error message. Linux aarch64 is explicitly supported — gettid
and the ELF injection mechanism both work there.

Signed-off-by: Pradeep Garigipati <pgarigipati@nvidia.com>
@9prady9
9prady9 requested a review from johanpel July 29, 2026 04:57
@9prady9

9prady9 commented Jul 29, 2026

Copy link
Copy Markdown
Contributor Author

/merge

@rapids-bot
rapids-bot Bot merged commit 459af01 into rapidsai:main Jul 29, 2026
20 checks passed
@9prady9
9prady9 deleted the nvtx-thread-id-pr branch July 29, 2026 11:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants