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

Runtime identifier and name #5545

Open
hds opened this issue Mar 14, 2023 · 2 comments
Open

Runtime identifier and name #5545

hds opened this issue Mar 14, 2023 · 2 comments
Labels
A-tokio Area: The main tokio crate C-feature-request Category: A feature request.

Comments

@hds
Copy link
Contributor

hds commented Mar 14, 2023

Is your feature request related to a problem? Please describe.

In certain circumstances, it would be useful for introspection of Tokio if it was possible to identify a runtime.

This would unlock new features and analyses in tokio-console (some described in console#130, and another in console#229).

I believe it would also add value to tokio-metrics.

There are two different forms of identification that would be useful:

  1. Identifier: unique identifier for the runtime per process. Ideally for the lifetime of the process (although guaranteed to be unique for all concurrently existing runtimes would be better than nothing).
  2. Name: human readable (and probably human created) string which describes the runtime. No need for uniqueness.

In a sense these are two features, but to me it made more sense to present them together.

Describe the solution you'd like

Foe the identifier, an opaque struct that can produce a Sized value would be ideal for the public API.

Internally a (thread-safe) counter could be used. Each new runtime is assigned the next value. A u64 will provide more values than can be used during the lifetime of a process (or ever). This aligns with the task Id currently available in unstable.

The identifier would be available from the runtime Handle.

The runtime name could be optionally provided to the runtime builder.

It would then be available on the Handle.

Describe alternatives you've considered

Foe the tokio-console use-case alone, the console-subscriber could keep track of runtime creation and assign each an Id. However linking this back to the tasks executing on the runtime and the resources associated with the runtime would be much more complicated (maybe not possible?).

If an identifier is available in the tokio API, bit no name, then the console-subscriber or tokio-console could apply the mapping based on user input. However unless the runtime identifier would be stable, this mapping would need to be provided on every run of the final application.

Additional context

Both tokio-console (really console-subscriber) and tokio-metrics already require that tokio be built with the tokio_unstable cfg option set, as such it would be sense to put this feature in unstable as well, especially initially.

As well as use in tokio-console and tokio-metrics, a name or a way to distinguish Runtimes could be useful in other contexts.

Any application which runs multiple Runtimes, or frequently creates new runtimes (from memory reqwest does this for the sync API) may like to be able to distinguish between them at execution time.

@hds hds added A-tokio Area: The main tokio crate C-feature-request Category: A feature request. labels Mar 14, 2023
hds added a commit that referenced this issue 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.
hds added a commit that referenced this issue 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.
hds added a commit that referenced this issue 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.
hds added a commit that referenced this issue Jul 19, 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. 

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.
@j-baker
Copy link

j-baker commented Dec 5, 2023

Would a PR be accepted which stabilises this feature? It would be extremely useful for us.

@Darksonn
Copy link
Contributor

Darksonn commented Dec 5, 2023

Hmm. We should definitely stabilize task ids soon, and I guess this could go with it. It doesn't seem like there is an issue for stabilizing task ids ...

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 C-feature-request Category: A feature request.
Projects
None yet
Development

No branches or pull requests

3 participants