Skip to content

Commit 1be8a8e

Browse files
authored
metrics: stabilize num_alive_tasks (#6619)
1 parent da17c61 commit 1be8a8e

File tree

8 files changed

+789
-760
lines changed

8 files changed

+789
-760
lines changed

tokio/src/runtime/metrics/runtime.rs

+22-22
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,28 @@ impl RuntimeMetrics {
4747
self.handle.inner.num_workers()
4848
}
4949

50+
/// Returns the current number of alive tasks in the runtime.
51+
///
52+
/// This counter increases when a task is spawned and decreases when a
53+
/// task exits.
54+
///
55+
/// # Examples
56+
///
57+
/// ```
58+
/// use tokio::runtime::Handle;
59+
///
60+
/// #[tokio::main]
61+
/// async fn main() {
62+
/// let metrics = Handle::current().metrics();
63+
///
64+
/// let n = metrics.num_alive_tasks();
65+
/// println!("Runtime has {} alive tasks", n);
66+
/// }
67+
/// ```
68+
pub fn num_alive_tasks(&self) -> usize {
69+
self.handle.inner.num_alive_tasks()
70+
}
71+
5072
cfg_unstable_metrics! {
5173

5274
/// Returns the number of additional threads spawned by the runtime.
@@ -81,28 +103,6 @@ impl RuntimeMetrics {
81103
self.num_alive_tasks()
82104
}
83105

84-
/// Returns the current number of alive tasks in the runtime.
85-
///
86-
/// This counter increases when a task is spawned and decreases when a
87-
/// task exits.
88-
///
89-
/// # Examples
90-
///
91-
/// ```
92-
/// use tokio::runtime::Handle;
93-
///
94-
/// #[tokio::main]
95-
/// async fn main() {
96-
/// let metrics = Handle::current().metrics();
97-
///
98-
/// let n = metrics.num_alive_tasks();
99-
/// println!("Runtime has {} alive tasks", n);
100-
/// }
101-
/// ```
102-
pub fn num_alive_tasks(&self) -> usize {
103-
self.handle.inner.alive_tasks_count()
104-
}
105-
106106
/// Returns the number of idle threads, which have spawned by the runtime
107107
/// for `spawn_blocking` calls.
108108
///

tokio/src/runtime/scheduler/current_thread/mod.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -500,6 +500,10 @@ impl Handle {
500500
pub(crate) fn reset_woken(&self) -> bool {
501501
self.shared.woken.swap(false, AcqRel)
502502
}
503+
504+
pub(crate) fn num_alive_tasks(&self) -> usize {
505+
self.shared.owned.num_alive_tasks()
506+
}
503507
}
504508

505509
cfg_unstable_metrics! {
@@ -533,10 +537,6 @@ cfg_unstable_metrics! {
533537
self.blocking_spawner.queue_depth()
534538
}
535539

536-
pub(crate) fn alive_tasks_count(&self) -> usize {
537-
self.shared.owned.alive_tasks_count()
538-
}
539-
540540
cfg_64bit_metrics! {
541541
pub(crate) fn spawned_tasks_count(&self) -> u64 {
542542
self.shared.owned.spawned_tasks_count()

tokio/src/runtime/scheduler/mod.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,10 @@ cfg_rt! {
173173
Handle::MultiThreadAlt(handle) => handle.num_workers(),
174174
}
175175
}
176+
177+
pub(crate) fn num_alive_tasks(&self) -> usize {
178+
match_flavor!(self, Handle(handle) => handle.num_alive_tasks())
179+
}
176180
}
177181

178182
cfg_unstable_metrics! {
@@ -193,10 +197,6 @@ cfg_rt! {
193197
match_flavor!(self, Handle(handle) => handle.num_idle_blocking_threads())
194198
}
195199

196-
pub(crate) fn alive_tasks_count(&self) -> usize {
197-
match_flavor!(self, Handle(handle) => handle.alive_tasks_count())
198-
}
199-
200200
pub(crate) fn scheduler_metrics(&self) -> &SchedulerMetrics {
201201
match_flavor!(self, Handle(handle) => handle.scheduler_metrics())
202202
}

tokio/src/runtime/scheduler/multi_thread/handle/metrics.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ impl Handle {
99
self.shared.worker_metrics.len()
1010
}
1111

12+
pub(crate) fn num_alive_tasks(&self) -> usize {
13+
self.shared.owned.num_alive_tasks()
14+
}
15+
1216
cfg_unstable_metrics! {
1317
cfg_64bit_metrics! {
1418
pub(crate) fn spawned_tasks_count(&self) -> u64 {
@@ -27,10 +31,6 @@ impl Handle {
2731
self.blocking_spawner.num_idle_threads()
2832
}
2933

30-
pub(crate) fn alive_tasks_count(&self) -> usize {
31-
self.shared.owned.alive_tasks_count()
32-
}
33-
3434
pub(crate) fn scheduler_metrics(&self) -> &SchedulerMetrics {
3535
&self.shared.scheduler_metrics
3636
}

tokio/src/runtime/scheduler/multi_thread_alt/handle/metrics.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ impl Handle {
1818
self.blocking_spawner.num_idle_threads()
1919
}
2020

21-
pub(crate) fn alive_tasks_count(&self) -> usize {
22-
self.shared.owned.alive_tasks_count()
21+
pub(crate) fn num_alive_tasks(&self) -> usize {
22+
self.shared.owned.num_alive_tasks()
2323
}
2424

2525
cfg_64bit_metrics! {

tokio/src/runtime/task/list.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ impl<S: 'static> OwnedTasks<S> {
166166
self.list.shard_size()
167167
}
168168

169-
pub(crate) fn alive_tasks_count(&self) -> usize {
169+
pub(crate) fn num_alive_tasks(&self) -> usize {
170170
self.list.len()
171171
}
172172

0 commit comments

Comments
 (0)