Skip to content

Commit

Permalink
metrics: rename num_active_tasks to num_alive_tasks (#6667)
Browse files Browse the repository at this point in the history
  • Loading branch information
Darksonn committed Jun 30, 2024
1 parent 65d0e08 commit 68d0e3c
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 26 deletions.
19 changes: 10 additions & 9 deletions tokio/src/runtime/metrics/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,15 +75,16 @@ impl RuntimeMetrics {
self.handle.inner.num_blocking_threads()
}

#[deprecated = "Renamed to num_active_tasks"]
/// Renamed to [`RuntimeMetrics::num_active_tasks`]
#[deprecated = "Renamed to num_alive_tasks"]
/// Renamed to [`RuntimeMetrics::num_alive_tasks`]
pub fn active_tasks_count(&self) -> usize {
self.num_active_tasks()
self.num_alive_tasks()
}

/// Returns the current number of active tasks in the runtime.
/// Returns the current number of alive tasks in the runtime.
///
/// This value increases and decreases over time as tasks are spawned and as they are completed or cancelled.
/// This counter increases when a task is spawned and decreases when a
/// task exits.
///
/// # Examples
///
Expand All @@ -94,12 +95,12 @@ impl RuntimeMetrics {
/// async fn main() {
/// let metrics = Handle::current().metrics();
///
/// let n = metrics.num_active_tasks();
/// println!("Runtime has {} active tasks", n);
/// let n = metrics.num_alive_tasks();
/// println!("Runtime has {} alive tasks", n);
/// }
/// ```
pub fn num_active_tasks(&self) -> usize {
self.handle.inner.active_tasks_count()
pub fn num_alive_tasks(&self) -> usize {
self.handle.inner.alive_tasks_count()
}

/// Returns the number of idle threads, which have spawned by the runtime
Expand Down
4 changes: 2 additions & 2 deletions tokio/src/runtime/scheduler/current_thread/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -533,8 +533,8 @@ 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 alive_tasks_count(&self) -> usize {
self.shared.owned.alive_tasks_count()
}

cfg_64bit_metrics! {
Expand Down
4 changes: 2 additions & 2 deletions tokio/src/runtime/scheduler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,8 +193,8 @@ 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 alive_tasks_count(&self) -> usize {
match_flavor!(self, Handle(handle) => handle.alive_tasks_count())
}

pub(crate) fn scheduler_metrics(&self) -> &SchedulerMetrics {
Expand Down
4 changes: 2 additions & 2 deletions tokio/src/runtime/scheduler/multi_thread/handle/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,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 alive_tasks_count(&self) -> usize {
self.shared.owned.alive_tasks_count()
}

pub(crate) fn scheduler_metrics(&self) -> &SchedulerMetrics {
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 alive_tasks_count(&self) -> usize {
self.shared.owned.alive_tasks_count()
}

cfg_64bit_metrics! {
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 alive_tasks_count(&self) -> usize {
self.list.len()
}

Expand Down
16 changes: 8 additions & 8 deletions tokio/tests/rt_metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,35 +93,35 @@ fn blocking_queue_depth() {
}

#[test]
fn num_active_tasks() {
fn num_alive_tasks() {
let rt = current_thread();
let metrics = rt.metrics();
assert_eq!(0, metrics.num_active_tasks());
assert_eq!(0, metrics.num_alive_tasks());
rt.block_on(rt.spawn(async move {
assert_eq!(1, metrics.num_active_tasks());
assert_eq!(1, metrics.num_alive_tasks());
}))
.unwrap();

assert_eq!(0, rt.metrics().num_active_tasks());
assert_eq!(0, rt.metrics().num_alive_tasks());

let rt = threaded();
let metrics = rt.metrics();
assert_eq!(0, metrics.num_active_tasks());
assert_eq!(0, metrics.num_alive_tasks());
rt.block_on(rt.spawn(async move {
assert_eq!(1, metrics.num_active_tasks());
assert_eq!(1, metrics.num_alive_tasks());
}))
.unwrap();

// try for 10 seconds to see if this eventually succeeds.
// wake_join() is called before the task is released, so in multithreaded
// code, this means we sometimes exit the block_on before the counter decrements.
for _ in 0..100 {
if rt.metrics().num_active_tasks() == 0 {
if rt.metrics().num_alive_tasks() == 0 {
break;
}
std::thread::sleep(std::time::Duration::from_millis(100));
}
assert_eq!(0, rt.metrics().num_active_tasks());
assert_eq!(0, rt.metrics().num_alive_tasks());
}

#[test]
Expand Down

0 comments on commit 68d0e3c

Please sign in to comment.