Skip to main content

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    #[cfg(feature = "schedule-latency")]
70    /// If `Some`, tracks the number of times tasks were scheduled by duration range.
71    pub(super) schedule_latency_histogram: Option<Histogram>,
72}
73
74impl WorkerMetrics {
75    pub(crate) fn new() -> WorkerMetrics {
76        WorkerMetrics::default()
77    }
78
79    pub(crate) fn set_queue_depth(&self, len: usize) {
80        self.queue_depth.store(len, Relaxed);
81    }
82
83    pub(crate) fn set_thread_id(&self, thread_id: ThreadId) {
84        *self.thread_id.lock().unwrap() = Some(thread_id);
85    }
86
87    cfg_metrics_variant! {
88        stable: {
89            pub(crate) fn from_config(_: &Config) -> WorkerMetrics {
90                WorkerMetrics::new()
91            }
92        },
93        unstable: {
94            pub(crate) fn from_config(config: &Config) -> WorkerMetrics {
95                let mut worker_metrics = WorkerMetrics::new();
96                worker_metrics.poll_count_histogram = config
97                    .metrics_poll_count_histogram
98                    .as_ref()
99                    .map(|histogram_builder| histogram_builder.build());
100                #[cfg(feature = "schedule-latency")]
101                {
102                    worker_metrics.schedule_latency_histogram = config
103                        .metrics_schedule_latency_histogram
104                        .as_ref()
105                        .map(|histogram_builder| histogram_builder.build());
106                }
107
108                worker_metrics
109            }
110        }
111    }
112
113    cfg_unstable_metrics! {
114        pub(crate) fn queue_depth(&self) -> usize {
115            self.queue_depth.load(Relaxed)
116        }
117
118        pub(crate) fn thread_id(&self) -> Option<ThreadId> {
119            *self.thread_id.lock().unwrap()
120        }
121    }
122}