1use crate::runtime::Config;
2use crate::util::metric_atomics::{MetricAtomicU64, MetricAtomicUsize};
3use std::sync::atomic::Ordering::Relaxed;
4use std::sync::Mutex;
5use std::thread::ThreadId;
67cfg_unstable_metrics! {
8use crate::runtime::metrics::Histogram;
9}
1011/// Retrieve runtime worker metrics.
12///
13/// **Note**: This is an [unstable API][unstable]. The public API of this type
14/// may break in 1.x releases. See [the documentation on unstable
15/// features][unstable] for details.
16///
17/// [unstable]: crate#unstable-features
18#[derive(Debug, Default)]
19#[repr(align(128))]
20pub(crate) struct WorkerMetrics {
21/// Amount of time the worker spent doing work vs. parking.
22pub(crate) busy_duration_total: MetricAtomicU64,
2324/// Number of tasks currently in the local queue. Used only by the
25 /// current-thread scheduler.
26pub(crate) queue_depth: MetricAtomicUsize,
2728/// Thread id of worker thread.
29thread_id: Mutex<Option<ThreadId>>,
3031/// Number of times the worker parked.
32pub(crate) park_count: MetricAtomicU64,
3334/// Number of times the worker parked and unparked.
35pub(crate) park_unpark_count: MetricAtomicU64,
3637#[cfg(tokio_unstable)]
38/// Number of times the worker woke then parked again without doing work.
39pub(crate) noop_count: MetricAtomicU64,
4041#[cfg(tokio_unstable)]
42/// Number of tasks the worker stole.
43pub(crate) steal_count: MetricAtomicU64,
4445#[cfg(tokio_unstable)]
46/// Number of times the worker stole
47pub(crate) steal_operations: MetricAtomicU64,
4849#[cfg(tokio_unstable)]
50/// Number of tasks the worker polled.
51pub(crate) poll_count: MetricAtomicU64,
5253#[cfg(tokio_unstable)]
54/// EWMA task poll time, in nanoseconds.
55pub(crate) mean_poll_time: MetricAtomicU64,
5657#[cfg(tokio_unstable)]
58/// Number of tasks scheduled for execution on the worker's local queue.
59pub(crate) local_schedule_count: MetricAtomicU64,
6061#[cfg(tokio_unstable)]
62/// Number of tasks moved from the local queue to the global queue to free space.
63pub(crate) overflow_count: MetricAtomicU64,
6465#[cfg(tokio_unstable)]
66/// If `Some`, tracks the number of polls by duration range.
67pub(super) poll_count_histogram: Option<Histogram>,
68}
6970impl WorkerMetrics {
71pub(crate) fn new() -> WorkerMetrics {
72 WorkerMetrics::default()
73 }
7475pub(crate) fn set_queue_depth(&self, len: usize) {
76self.queue_depth.store(len, Relaxed);
77 }
7879pub(crate) fn set_thread_id(&self, thread_id: ThreadId) {
80*self.thread_id.lock().unwrap() = Some(thread_id);
81 }
8283cfg_metrics_variant! {
84 stable: {
85pub(crate) fn from_config(_: &Config) -> WorkerMetrics {
86 WorkerMetrics::new()
87 }
88 },
89 unstable: {
90pub(crate) fn from_config(config: &Config) -> WorkerMetrics {
91let mut worker_metrics = WorkerMetrics::new();
92 worker_metrics.poll_count_histogram = config
93 .metrics_poll_count_histogram
94 .as_ref()
95 .map(|histogram_builder| histogram_builder.build());
96 worker_metrics
97 }
98 }
99 }
100101cfg_unstable_metrics! {
102pub(crate) fn queue_depth(&self) -> usize {
103self.queue_depth.load(Relaxed)
104 }
105106pub(crate) fn thread_id(&self) -> Option<ThreadId> {
107*self.thread_id.lock().unwrap()
108 }
109 }
110}