Skip to content

runtime: add worker_index() getter#7921

Merged
ADD-SP merged 5 commits intotokio-rs:masterfrom
rcoh:master
Mar 15, 2026
Merged

runtime: add worker_index() getter#7921
ADD-SP merged 5 commits intotokio-rs:masterfrom
rcoh:master

Conversation

@rcoh
Copy link
Copy Markdown
Contributor

@rcoh rcoh commented Feb 19, 2026

Motivation

I am working on some runtime tracing instrumentation and one of the current pain points is being able to figure out what worker is currently running so that it can be properly correlated with the runtime metrics. I am working around this currently by enumerating RuntimeMetrics::worker_thread_id but that's obviously not ideal.

Solution

Add tokio::runtime::worker_id() which returns the 0-based index of the current runtime worker thread. The index matches the worker indices used by RuntimeMetrics (e.g. worker_total_busy_duration), making it possible to correlate per-worker metrics with specific tasks.

Returns None from blocking threads, the block_on caller thread (on the multi-thread runtime), and non-Tokio threads. For the current-thread runtime, always returns Some(0).

@github-actions github-actions Bot added R-loom-current-thread Run loom current-thread tests on this PR R-loom-multi-thread Run loom multi-thread tests on this PR labels Feb 19, 2026
@rcoh rcoh force-pushed the master branch 4 times, most recently from 519b1f2 to ec5c64a Compare February 19, 2026 20:46
@ADD-SP ADD-SP added A-tokio Area: The main tokio crate M-metrics Module: tokio/runtime/metrics M-runtime Module: tokio/runtime and removed M-metrics Module: tokio/runtime/metrics labels Feb 19, 2026
Copy link
Copy Markdown
Member

@ADD-SP ADD-SP left a comment

Choose a reason for hiding this comment

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

Shall we use worker_index() instead of worker_id()? It looks like methods like worker_local_schedule_count are using the term "worker index".

@ADD-SP ADD-SP added the S-waiting-on-author Status: awaiting some action (such as code changes) from the PR or issue author. label Feb 20, 2026
@rcoh
Copy link
Copy Markdown
Contributor Author

rcoh commented Feb 20, 2026

Yes I like that:

  • Id => opaque id
  • Index => numeric monotonic etc.

Will change, thanks for review!

@rcoh rcoh changed the title runtime: add worker_id() getter runtime: add worker_index() getter Feb 22, 2026
@rcoh rcoh requested a review from ADD-SP February 22, 2026 11:31
@ADD-SP ADD-SP removed the S-waiting-on-author Status: awaiting some action (such as code changes) from the PR or issue author. label Feb 24, 2026
Copy link
Copy Markdown
Member

@ADD-SP ADD-SP left a comment

Choose a reason for hiding this comment

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

Overall looks good to me, just one question.

By the way, please sync the changes from the base branch as we have recently fixed a bug which may causes CI failure.

Comment thread tokio/tests/rt_worker_index.rs Outdated
@ADD-SP ADD-SP added the S-waiting-on-author Status: awaiting some action (such as code changes) from the PR or issue author. label Feb 24, 2026
@rcoh rcoh force-pushed the master branch 2 times, most recently from 9b760e7 to 88f3ca9 Compare February 25, 2026 14:42
@ADD-SP ADD-SP removed S-waiting-on-author Status: awaiting some action (such as code changes) from the PR or issue author. labels Feb 26, 2026
Comment thread tokio/src/runtime/mod.rs Outdated
Comment thread tokio/tests/rt_worker_index.rs Outdated
Comment thread tokio/tests/rt_worker_index.rs
Comment thread tokio/src/runtime/mod.rs Outdated
rcoh and others added 4 commits March 4, 2026 15:23
Add `tokio::runtime::worker_id()` which returns the 0-based index of
the current runtime worker thread. The index matches the worker indices
used by `RuntimeMetrics` (e.g. `worker_total_busy_duration`), making
it possible to correlate per-worker metrics with specific tasks.

Returns `None` from blocking threads, the `block_on` caller thread
(on the multi-thread runtime), and non-Tokio threads. For the
current-thread runtime, always returns `Some(0)`.
rcoh added a commit to rcoh/tokio that referenced this pull request Mar 5, 2026
Add a `tokio::runtime::worker_index()` function that returns the index
of the current runtime worker thread. This is useful for per-worker
sharding and diagnostics.

Squashed from tokio-rs#7921
rcoh added a commit to rcoh/tokio that referenced this pull request Mar 5, 2026
Add a `tokio::runtime::worker_index()` function that returns the index
of the current runtime worker thread. This is useful for per-worker
sharding and diagnostics.

Squashed from tokio-rs#7921
Copy link
Copy Markdown
Member

@ADD-SP ADD-SP left a comment

Choose a reason for hiding this comment

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

Looks good to me. Synced with the base branch via the GitHub web UI—let’s see if CI is green.

@ADD-SP ADD-SP merged commit 26de318 into tokio-rs:master Mar 15, 2026
92 checks passed
@ADD-SP
Copy link
Copy Markdown
Member

ADD-SP commented Mar 15, 2026

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-tokio Area: The main tokio crate M-runtime Module: tokio/runtime R-loom-current-thread Run loom current-thread tests on this PR R-loom-multi-thread Run loom multi-thread tests on this PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants