1 use crate::runtime::metrics::Histogram; 2 use crate::runtime::Config; 3 use crate::util::metric_atomics::{MetricAtomicU64, MetricAtomicUsize}; 4 use std::sync::atomic::Ordering::Relaxed; 5 use std::sync::Mutex; 6 use std::thread::ThreadId; 7 8 /// Retrieve runtime worker metrics. 9 /// 10 /// **Note**: This is an [unstable API][unstable]. The public API of this type 11 /// may break in 1.x releases. See [the documentation on unstable 12 /// features][unstable] for details. 13 /// 14 /// [unstable]: crate#unstable-features 15 #[derive(Debug, Default)] 16 #[repr(align(128))] 17 pub(crate) struct WorkerMetrics { 18 /// Number of times the worker parked. 19 pub(crate) park_count: MetricAtomicU64, 20 21 /// Number of times the worker parked and unparked. 22 pub(crate) park_unpark_count: MetricAtomicU64, 23 24 /// Number of times the worker woke then parked again without doing work. 25 pub(crate) noop_count: MetricAtomicU64, 26 27 /// Number of tasks the worker stole. 28 pub(crate) steal_count: MetricAtomicU64, 29 30 /// Number of times the worker stole 31 pub(crate) steal_operations: MetricAtomicU64, 32 33 /// Number of tasks the worker polled. 34 pub(crate) poll_count: MetricAtomicU64, 35 36 /// EWMA task poll time, in nanoseconds. 37 pub(crate) mean_poll_time: MetricAtomicU64, 38 39 /// Amount of time the worker spent doing work vs. parking. 40 pub(crate) busy_duration_total: MetricAtomicU64, 41 42 /// Number of tasks scheduled for execution on the worker's local queue. 43 pub(crate) local_schedule_count: MetricAtomicU64, 44 45 /// Number of tasks moved from the local queue to the global queue to free space. 46 pub(crate) overflow_count: MetricAtomicU64, 47 48 /// Number of tasks currently in the local queue. Used only by the 49 /// current-thread scheduler. 50 pub(crate) queue_depth: MetricAtomicUsize, 51 52 /// If `Some`, tracks the number of polls by duration range. 53 pub(super) poll_count_histogram: Option<Histogram>, 54 55 /// Thread id of worker thread. 56 thread_id: Mutex<Option<ThreadId>>, 57 } 58 59 impl WorkerMetrics { from_config(config: &Config) -> WorkerMetrics60 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 new() -> WorkerMetrics69 pub(crate) fn new() -> WorkerMetrics { 70 WorkerMetrics::default() 71 } 72 queue_depth(&self) -> usize73 pub(crate) fn queue_depth(&self) -> usize { 74 self.queue_depth.load(Relaxed) 75 } 76 set_queue_depth(&self, len: usize)77 pub(crate) fn set_queue_depth(&self, len: usize) { 78 self.queue_depth.store(len, Relaxed); 79 } 80 thread_id(&self) -> Option<ThreadId>81 pub(crate) fn thread_id(&self) -> Option<ThreadId> { 82 *self.thread_id.lock().unwrap() 83 } 84 set_thread_id(&self, thread_id: ThreadId)85 pub(crate) fn set_thread_id(&self, thread_id: ThreadId) { 86 *self.thread_id.lock().unwrap() = Some(thread_id); 87 } 88 } 89