Skip to content
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

rt: add runtime Id #5864

Merged
merged 5 commits into from Jul 19, 2023
Merged

rt: add runtime Id #5864

merged 5 commits into from Jul 19, 2023

Conversation

hds
Copy link
Contributor

@hds hds commented Jul 13, 2023

Motivation

There are a number of cases in which being able to identify a runtime is
useful.

When instrumenting an application, this is particularly true. For
example, we would like to be able to add traces for runtimes so that
tasks can be differentiated (#5792). It would also allow a way to
differentiate runtimes which are have their tasks dumped.

Outside of instrumentation, it may be useful to check whether 2 runtime
handles are pointing to the same runtime.

Solution

This change adds an opaque runtime::Id struct which serves this
purpose, initially behind the tokio_unstable cfg flag.

The inner value of the ID is taken from the OwnedTasks or
LocalOwnedTasks struct which every runtime and local set already
has. This will mean that any use of the ID will align with the task
dump feature.

The Id is added within the scope of working towards closing #5545.

@github-actions github-actions bot added the R-loom Run loom tests on this PR label Jul 13, 2023
There are a number of cases in which being able to identify a runtime is
useful.

When instrumenting an application, this is particularly true. For
example, we would like to be able to add traces for runtimes so that
tasks can be differentiated (#5792). It would also allow a way to
differentiate runtimes which are have their tasks dumped.

Outside of instrumentation, it may be useful to check whether 2 runtime
handles are pointing to the same runtime.

This change adds an opaque `runtime::Id` struct which serves this
purpose, initially behind the `tokio_unstable` cfg flag. It follows the
same pattern as the `task::Id` struct. The Id can be compared for
equality with another `runtime::Id` and implements `Debug` and `Display`
so that it can be output as well.

Internally the Id is a `u64`, but that is an implementation detail.

There is a degree of code duplication, but that is necessary to ensure
that the Ids are not used to compare against one another.

The Id is added within the scope of working towards closing #5545.
@inevity
Copy link
Contributor

inevity commented Jul 14, 2023

The runtime id should also place in the runtime::context thread local?

tokio/src/runtime/id.rs Outdated Show resolved Hide resolved
Copy link
Contributor

Choose a reason for hiding this comment

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

Whether merge all taskid/threadid/runtimeid as one type id?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Keeping a separate type because we don't want users to be able to compare (for example) a task ID to a runtime ID. Also, the runtime::Id implementation is now pretty different as we're using the value from the (Local)OwnedTasks ID.

@Darksonn
Copy link
Contributor

We already have what is effectively a runtime id in the OwnedTasks abstraction. See here.

@hds
Copy link
Contributor Author

hds commented Jul 14, 2023

@Darksonn What do you think about exposing the ID from the OwnedTasks or LocalOwnedTasks in a way similar to what is proposed in this PR?

My use case is that I would like to be able to observe the behavior of a runtime on tokio-console and then find that runtime by ID (probably something that an application would have to keep internally) and get a task dump of it. Right now, I don't think that there's a way to identify the runtime from the external tokio API.

@Darksonn
Copy link
Contributor

Darksonn commented Jul 14, 2023

I don't mind exposing the OwnedTasks id in a similar manner to what this PR currently has. In fact, I think that's simpler than implementing a new separate id.

In fact, spawned tasks already store the id of their OwnedTasks, so you can even access it given a task.

hds added 2 commits July 14, 2023 14:16
Instead of generating our own ID, use the one that has already been
implemented for the owned tasks structs. The u64 value is then returned
as a `runtime::Id` to keep the implementation details hidden.
tokio/src/runtime/id.rs Outdated Show resolved Hide resolved
@hds
Copy link
Contributor Author

hds commented Jul 17, 2023

Waiting to merge #5876, then we'll update this one.

@hds hds merged commit 63577cd into master Jul 19, 2023
75 checks passed
@hds hds deleted the hds/runtime-id branch July 19, 2023 10:53
@Darksonn Darksonn added A-tokio Area: The main tokio crate M-runtime Module: tokio/runtime labels Aug 27, 2023
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 Run loom tests on this PR
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants