Skip to content

Commit

Permalink
metrics: Stabilize num_active_tasks
Browse files Browse the repository at this point in the history
1. stabilize `num_active_tasks`
2. Rename internal counters to match `num_active_tasks`
3. Split `rt_metrics` into `rt_metrics` and `rt_unstable_metrics`.
  • Loading branch information
rcoh committed Jun 14, 2024
1 parent 8480a18 commit 0d087ef
Show file tree
Hide file tree
Showing 8 changed files with 781 additions and 757 deletions.
43 changes: 23 additions & 20 deletions tokio/src/runtime/metrics/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,29 @@ impl RuntimeMetrics {
self.handle.inner.num_workers()
}

/// Returns the current number of active tasks in the runtime.
///
/// This value increases and decreases over time as tasks are spawned and as they are completed or cancelled.
///
/// To see the total number of spawned tasks, see `spawned_tasks_count`. Note that this API currently requires using `--cfg tokio_unstable`.
///
/// # Examples
///
/// ```
/// use tokio::runtime::Handle;
///
/// #[tokio::main]
/// async fn main() {
/// let metrics = Handle::current().metrics();
///
/// let n = metrics.num_active_tasks();
/// println!("Runtime has {} active tasks", n);
/// }
/// ```
pub fn num_active_tasks(&self) -> usize {
self.handle.inner.num_active_tasks()
}

cfg_unstable_metrics! {

/// Returns the number of additional threads spawned by the runtime.
Expand Down Expand Up @@ -81,26 +104,6 @@ impl RuntimeMetrics {
self.num_active_tasks()
}

/// Returns the current number of active tasks in the runtime.
///
/// This value increases and decreases over time as tasks are spawned and as they are completed or cancelled.
///
/// # Examples
///
/// ```
/// use tokio::runtime::Handle;
///
/// #[tokio::main]
/// async fn main() {
/// let metrics = Handle::current().metrics();
///
/// let n = metrics.num_active_tasks();
/// println!("Runtime has {} active tasks", n);
/// }
/// ```
pub fn num_active_tasks(&self) -> usize {
self.handle.inner.active_tasks_count()
}

/// Returns the number of tasks spawned in this runtime since it was created.
///
Expand Down
8 changes: 4 additions & 4 deletions tokio/src/runtime/scheduler/current_thread/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -500,6 +500,10 @@ impl Handle {
pub(crate) fn reset_woken(&self) -> bool {
self.shared.woken.swap(false, AcqRel)
}

pub(crate) fn num_active_tasks(&self) -> usize {
self.shared.owned.num_active_tasks()
}
}

cfg_unstable_metrics! {
Expand Down Expand Up @@ -533,10 +537,6 @@ cfg_unstable_metrics! {
self.blocking_spawner.queue_depth()
}

pub(crate) fn active_tasks_count(&self) -> usize {
self.shared.owned.active_tasks_count()
}

pub(crate) fn spawned_tasks_count(&self) -> u64 {
self.shared.owned.spawned_tasks_count()
}
Expand Down
7 changes: 4 additions & 3 deletions tokio/src/runtime/scheduler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,10 @@ cfg_rt! {
Handle::MultiThreadAlt(handle) => handle.num_workers(),
}
}

pub(crate) fn num_active_tasks(&self) -> usize {
match_flavor!(self, Handle(handle) => handle.num_active_tasks())
}
}

cfg_unstable_metrics! {
Expand All @@ -187,9 +191,6 @@ cfg_rt! {
match_flavor!(self, Handle(handle) => handle.num_idle_blocking_threads())
}

pub(crate) fn active_tasks_count(&self) -> usize {
match_flavor!(self, Handle(handle) => handle.active_tasks_count())
}

pub(crate) fn spawned_tasks_count(&self) -> u64 {
match_flavor!(self, Handle(handle) => handle.spawned_tasks_count())
Expand Down
8 changes: 4 additions & 4 deletions tokio/src/runtime/scheduler/multi_thread/handle/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ impl Handle {
self.shared.worker_metrics.len()
}

pub(crate) fn num_active_tasks(&self) -> usize {
self.shared.owned.num_active_tasks()
}

cfg_unstable_metrics! {
pub(crate) fn num_blocking_threads(&self) -> usize {
// workers are currently spawned using spawn_blocking
Expand All @@ -21,10 +25,6 @@ impl Handle {
self.blocking_spawner.num_idle_threads()
}

pub(crate) fn active_tasks_count(&self) -> usize {
self.shared.owned.active_tasks_count()
}

pub(crate) fn spawned_tasks_count(&self) -> u64 {
self.shared.owned.spawned_tasks_count()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ impl Handle {
self.blocking_spawner.num_idle_threads()
}

pub(crate) fn active_tasks_count(&self) -> usize {
self.shared.owned.active_tasks_count()
pub(crate) fn num_active_tasks(&self) -> usize {
self.shared.owned.num_active_tasks()
}

pub(crate) fn spawned_tasks_count(&self) -> u64 {
Expand Down
2 changes: 1 addition & 1 deletion tokio/src/runtime/task/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ impl<S: 'static> OwnedTasks<S> {
self.list.shard_size()
}

pub(crate) fn active_tasks_count(&self) -> usize {
pub(crate) fn num_active_tasks(&self) -> usize {
self.list.len()
}

Expand Down
Loading

0 comments on commit 0d087ef

Please sign in to comment.