tokio/runtime/metrics/
worker.rs1use crate::runtime::metrics::Histogram;
2use crate::runtime::Config;
3use crate::util::metric_atomics::{MetricAtomicU64, MetricAtomicUsize};
4use std::sync::atomic::Ordering::Relaxed;
5use std::sync::Mutex;
6use std::thread::ThreadId;
7
8#[derive(Debug, Default)]
16#[repr(align(128))]
17pub(crate) struct WorkerMetrics {
18 pub(crate) park_count: MetricAtomicU64,
20
21 pub(crate) park_unpark_count: MetricAtomicU64,
23
24 pub(crate) noop_count: MetricAtomicU64,
26
27 pub(crate) steal_count: MetricAtomicU64,
29
30 pub(crate) steal_operations: MetricAtomicU64,
32
33 pub(crate) poll_count: MetricAtomicU64,
35
36 pub(crate) mean_poll_time: MetricAtomicU64,
38
39 pub(crate) busy_duration_total: MetricAtomicU64,
41
42 pub(crate) local_schedule_count: MetricAtomicU64,
44
45 pub(crate) overflow_count: MetricAtomicU64,
47
48 pub(crate) queue_depth: MetricAtomicUsize,
51
52 pub(super) poll_count_histogram: Option<Histogram>,
54
55 thread_id: Mutex<Option<ThreadId>>,
57}
58
59impl WorkerMetrics {
60 pub(crate) fn from_config(config: &Config) -> WorkerMetrics {
61 let mut worker_metrics = WorkerMetrics::new();
62 worker_metrics.poll_count_histogram = config
63 .metrics_poll_count_histogram
64 .as_ref()
65 .map(|histogram_builder| histogram_builder.build());
66 worker_metrics
67 }
68
69 pub(crate) fn new() -> WorkerMetrics {
70 WorkerMetrics::default()
71 }
72
73 pub(crate) fn queue_depth(&self) -> usize {
74 self.queue_depth.load(Relaxed)
75 }
76
77 pub(crate) fn set_queue_depth(&self, len: usize) {
78 self.queue_depth.store(len, Relaxed);
79 }
80
81 pub(crate) fn thread_id(&self) -> Option<ThreadId> {
82 *self.thread_id.lock().unwrap()
83 }
84
85 pub(crate) fn set_thread_id(&self, thread_id: ThreadId) {
86 *self.thread_id.lock().unwrap() = Some(thread_id);
87 }
88}