Skip to content

Commit

Permalink
rename to 'alive_tasks'
Browse files Browse the repository at this point in the history
  • Loading branch information
rcoh committed Jul 18, 2024
1 parent 8dff8c1 commit 0fd010f
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 104 deletions.
39 changes: 8 additions & 31 deletions tokio/src/runtime/metrics/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,10 @@ impl RuntimeMetrics {
self.handle.inner.num_workers()
}

/// 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.
///
/// To see the total number of spawned tasks, see `spawned_tasks_count`.
/// This counter increases when a task is spawned and decreases when a
/// task exits.
///
/// # Examples
///
Expand All @@ -62,12 +61,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.num_active_tasks()
pub fn num_alive_tasks(&self) -> usize {
self.handle.inner.num_alive_tasks()
}

cfg_unstable_metrics! {
Expand Down Expand Up @@ -101,29 +100,7 @@ impl RuntimeMetrics {
#[deprecated = "Renamed to num_alive_tasks"]
/// Renamed to [`RuntimeMetrics::num_alive_tasks`]
pub fn active_tasks_count(&self) -> usize {
self.num_active_tasks()
}

/// Returns the current number of alive tasks in the runtime.
///
/// This counter increases when a task is spawned and decreases when a
/// task exits.
///
/// # Examples
///
/// ```
/// use tokio::runtime::Handle;
///
/// #[tokio::main]
/// async fn main() {
/// let metrics = Handle::current().metrics();
///
/// let n = metrics.num_alive_tasks();
/// println!("Runtime has {} alive tasks", n);
/// }
/// ```
pub fn num_alive_tasks(&self) -> usize {
self.handle.inner.alive_tasks_count()
self.num_alive_tasks()
}

/// 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 @@ -501,8 +501,8 @@ impl Handle {
self.shared.woken.swap(false, AcqRel)
}

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

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 @@ -174,8 +174,8 @@ cfg_rt! {
}
}

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

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 @@ -9,8 +9,8 @@ impl Handle {
self.shared.worker_metrics.len()
}

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

cfg_unstable_metrics! {
Expand Down
67 changes: 0 additions & 67 deletions tokio/tests/rt_metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,73 +13,6 @@ fn num_workers() {
assert_eq!(2, rt.metrics().num_workers());
}

#[test]
fn num_blocking_threads() {
let rt = current_thread();
assert_eq!(0, rt.metrics().num_blocking_threads());
let _ = rt.block_on(rt.spawn_blocking(move || {}));
assert_eq!(1, rt.metrics().num_blocking_threads());

let rt = threaded();
assert_eq!(0, rt.metrics().num_blocking_threads());
let _ = rt.block_on(rt.spawn_blocking(move || {}));
assert_eq!(1, rt.metrics().num_blocking_threads());
}

#[test]
fn num_idle_blocking_threads() {
let rt = current_thread();
assert_eq!(0, rt.metrics().num_idle_blocking_threads());
let _ = rt.block_on(rt.spawn_blocking(move || {}));
rt.block_on(async {
time::sleep(Duration::from_millis(5)).await;
});

// We need to wait until the blocking thread has become idle. Usually 5ms is
// enough for this to happen, but not always. When it isn't enough, sleep
// for another second. We don't always wait for a whole second since we want
// the test suite to finish quickly.
//
// Note that the timeout for idle threads to be killed is 10 seconds.
if 0 == rt.metrics().num_idle_blocking_threads() {
rt.block_on(async {
time::sleep(Duration::from_secs(1)).await;
});
}

assert_eq!(1, rt.metrics().num_idle_blocking_threads());
}

#[test]
fn blocking_queue_depth() {
let rt = tokio::runtime::Builder::new_current_thread()
.enable_all()
.max_blocking_threads(1)
.build()
.unwrap();

assert_eq!(0, rt.metrics().blocking_queue_depth());

let ready = Arc::new(Mutex::new(()));
let guard = ready.lock().unwrap();

let ready_cloned = ready.clone();
let wait_until_ready = move || {
let _unused = ready_cloned.lock().unwrap();
};

let h1 = rt.spawn_blocking(wait_until_ready.clone());
let h2 = rt.spawn_blocking(wait_until_ready);
assert!(rt.metrics().blocking_queue_depth() > 0);

drop(guard);

let _ = rt.block_on(h1);
let _ = rt.block_on(h2);

assert_eq!(0, rt.metrics().blocking_queue_depth());
}

#[test]
fn num_alive_tasks() {
let rt = current_thread();
Expand Down

0 comments on commit 0fd010f

Please sign in to comment.