tokio/runtime/metrics/
worker.rs

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;
6
7cfg_unstable_metrics! {
8    use crate::runtime::metrics::Histogram;
9}
10
11/// 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.
22    pub(crate) busy_duration_total: MetricAtomicU64,
23
24    /// Number of tasks currently in the local queue. Used only by the
25    /// current-thread scheduler.
26    pub(crate) queue_depth: MetricAtomicUsize,
27
28    /// Thread id of worker thread.
29    thread_id: Mutex<Option<ThreadId>>,
30
31    ///  Number of times the worker parked.
32    pub(crate) park_count: MetricAtomicU64,
33
34    ///  Number of times the worker parked and unparked.
35    pub(crate) park_unpark_count: MetricAtomicU64,
36
37    #[cfg(tokio_unstable)]
38    /// Number of times the worker woke then parked again without doing work.
39    pub(crate) noop_count: MetricAtomicU64,
40
41    #[cfg(tokio_unstable)]
42    /// Number of tasks the worker stole.
43    pub(crate) steal_count: MetricAtomicU64,
44
45    #[cfg(tokio_unstable)]
46    /// Number of times the worker stole
47    pub(crate) steal_operations: MetricAtomicU64,
48
49    #[cfg(tokio_unstable)]
50    /// Number of tasks the worker polled.
51    pub(crate) poll_count: MetricAtomicU64,
52
53    #[cfg(tokio_unstable)]
54    /// EWMA task poll time, in nanoseconds.
55    pub(crate) mean_poll_time: MetricAtomicU64,
56
57    #[cfg(tokio_unstable)]
58    /// Number of tasks scheduled for execution on the worker's local queue.
59    pub(crate) local_schedule_count: MetricAtomicU64,
60
61    #[cfg(tokio_unstable)]
62    /// Number of tasks moved from the local queue to the global queue to free space.
63    pub(crate) overflow_count: MetricAtomicU64,
64
65    #[cfg(tokio_unstable)]
66    /// If `Some`, tracks the number of polls by duration range.
67    pub(super) poll_count_histogram: Option<Histogram>,
68}
69
70impl WorkerMetrics {
71    pub(crate) fn new() -> WorkerMetrics {
72        WorkerMetrics::default()
73    }
74
75    pub(crate) fn set_queue_depth(&self, len: usize) {
76        self.queue_depth.store(len, Relaxed);
77    }
78
79    pub(crate) fn set_thread_id(&self, thread_id: ThreadId) {
80        *self.thread_id.lock().unwrap() = Some(thread_id);
81    }
82
83    cfg_metrics_variant! {
84        stable: {
85            pub(crate) fn from_config(_: &Config) -> WorkerMetrics {
86                WorkerMetrics::new()
87            }
88        },
89        unstable: {
90            pub(crate) fn from_config(config: &Config) -> WorkerMetrics {
91                let 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    }
100
101    cfg_unstable_metrics! {
102        pub(crate) fn queue_depth(&self) -> usize {
103            self.queue_depth.load(Relaxed)
104        }
105
106        pub(crate) fn thread_id(&self) -> Option<ThreadId> {
107            *self.thread_id.lock().unwrap()
108        }
109    }
110}