Skip to content

Commit

Permalink
metrics: Stabilize active_task_count
Browse files Browse the repository at this point in the history
This stabilizes active_task_count. I also updated the metrics
integration test to split unstable/vs. stable metrics so that we
correctly test stable metrics in all cases.

Refs: tokio-rs#6546
  • Loading branch information
rcoh committed Jun 6, 2024
1 parent 8e15c23 commit 894ed4b
Show file tree
Hide file tree
Showing 5 changed files with 608 additions and 604 deletions.
38 changes: 19 additions & 19 deletions tokio/src/runtime/metrics/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,25 @@ impl RuntimeMetrics {
self.handle.inner.num_workers()
}

/// Returns the number of active tasks in the runtime.
///
/// # Examples
///
/// ```
/// use tokio::runtime::Handle;
///
/// #[tokio::main]
/// async fn main() {
/// let metrics = Handle::current().metrics();
///
/// let n = metrics.active_tasks_count();
/// println!("Runtime has {} active tasks", n);
/// }
/// ```
pub fn active_tasks_count(&self) -> usize {
self.handle.inner.active_tasks_count()
}

cfg_unstable_metrics! {

/// Returns the number of additional threads spawned by the runtime.
Expand Down Expand Up @@ -75,25 +94,6 @@ impl RuntimeMetrics {
self.handle.inner.num_blocking_threads()
}

/// Returns the number of active tasks in the runtime.
///
/// # Examples
///
/// ```
/// use tokio::runtime::Handle;
///
/// #[tokio::main]
/// async fn main() {
/// let metrics = Handle::current().metrics();
///
/// let n = metrics.active_tasks_count();
/// println!("Runtime has {} active tasks", n);
/// }
/// ```
pub fn active_tasks_count(&self) -> usize {
self.handle.inner.active_tasks_count()
}

/// Returns the number of idle threads, which have spawned by the runtime
/// for `spawn_blocking` calls.
///
Expand Down
7 changes: 4 additions & 3 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 active_tasks_count(&self) -> usize {
self.shared.owned.active_tasks_count()
}
}

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

pub(crate) fn active_tasks_count(&self) -> usize {
self.shared.owned.active_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 active_tasks_count(&self) -> usize {
match_flavor!(self, Handle(handle) => handle.active_tasks_count())
}
}

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 scheduler_metrics(&self) -> &SchedulerMetrics {
match_flavor!(self, Handle(handle) => handle.scheduler_metrics())
Expand Down
7 changes: 4 additions & 3 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 active_tasks_count(&self) -> usize {
self.shared.owned.active_tasks_count()
}

cfg_unstable_metrics! {
pub(crate) fn num_blocking_threads(&self) -> usize {
// workers are currently spawned using spawn_blocking
Expand All @@ -21,9 +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 scheduler_metrics(&self) -> &SchedulerMetrics {
&self.shared.scheduler_metrics
Expand Down
Loading

0 comments on commit 894ed4b

Please sign in to comment.