| 1 |
use crate::runtime::Handle; |
| 2 |
|
| 3 |
cfg_unstable_metrics! { |
| 4 |
use std::ops::Range; |
| 5 |
use std::thread::ThreadId; |
| 6 |
cfg_64bit_metrics! { |
| 7 |
use std::sync::atomic::Ordering::Relaxed; |
| 8 |
} |
| 9 |
use std::time::Duration; |
| 10 |
} |
| 11 |
|
| 12 |
/// Handle to the runtime's metrics. |
| 13 |
/// |
| 14 |
/// This handle is internally reference-counted and can be freely cloned. A |
| 15 |
/// `RuntimeMetrics` handle is obtained using the [`Runtime::metrics`] method. |
| 16 |
/// |
| 17 |
/// [`Runtime::metrics`]: crate::runtime::Runtime::metrics() |
| 18 |
#[derive(Clone, Debug)] |
| 19 |
pub struct RuntimeMetrics { |
| 20 |
handle: Handle, |
| 21 |
} |
| 22 |
|
| 23 |
impl RuntimeMetrics { |
| 24 |
pub(crate) fn new(handle: Handle) -> RuntimeMetrics { |
| 25 |
RuntimeMetrics { handle } |
| 26 |
} |
| 27 |
|
| 28 |
/// Returns the number of worker threads used by the runtime. |
| 29 |
/// |
| 30 |
/// The number of workers is set by configuring `worker_threads` on |
| 31 |
/// `runtime::Builder`. When using the `current_thread` runtime, the return |
| 32 |
/// value is always `1`. |
| 33 |
/// |
| 34 |
/// # Examples |
| 35 |
/// |
| 36 |
/// ``` |
| 37 |
/// use tokio::runtime::Handle; |
| 38 |
/// |
| 39 |
/// #[tokio::main] |
| 40 |
/// async fn main() { |
| 41 |
/// let metrics = Handle::current().metrics(); |
| 42 |
/// |
| 43 |
/// let n = metrics.num_workers(); |
| 44 |
/// println!("Runtime is using {} workers", n); |
| 45 |
/// } |
| 46 |
/// ``` |
| 47 |
pub fn num_workers(&self) -> usize { |
| 48 |
self.handle.inner.num_workers() |
| 49 |
} |
| 50 |
|
| 51 |
/// Returns the current number of alive tasks in the runtime. |
| 52 |
/// |
| 53 |
/// This counter increases when a task is spawned and decreases when a |
| 54 |
/// task exits. |
| 55 |
/// |
| 56 |
/// # Examples |
| 57 |
/// |
| 58 |
/// ``` |
| 59 |
/// use tokio::runtime::Handle; |
| 60 |
/// |
| 61 |
/// #[tokio::main] |
| 62 |
/// async fn main() { |
| 63 |
/// let metrics = Handle::current().metrics(); |
| 64 |
/// |
| 65 |
/// let n = metrics.num_alive_tasks(); |
| 66 |
/// println!("Runtime has {} alive tasks", n); |
| 67 |
/// } |
| 68 |
/// ``` |
| 69 |
pub fn num_alive_tasks(&self) -> usize { |
| 70 |
self.handle.inner.num_alive_tasks() |
| 71 |
} |
| 72 |
|
| 73 |
cfg_unstable_metrics! { |
| 74 |
|
| 75 |
/// Returns the number of additional threads spawned by the runtime. |
| 76 |
/// |
| 77 |
/// The number of workers is set by configuring `max_blocking_threads` on |
| 78 |
/// `runtime::Builder`. |
| 79 |
/// |
| 80 |
/// # Examples |
| 81 |
/// |
| 82 |
/// ``` |
| 83 |
/// use tokio::runtime::Handle; |
| 84 |
/// |
| 85 |
/// #[tokio::main] |
| 86 |
/// async fn main() { |
| 87 |
/// let _ = tokio::task::spawn_blocking(move || { |
| 88 |
/// // Stand-in for compute-heavy work or using synchronous APIs |
| 89 |
/// 1 + 1 |
| 90 |
/// }).await; |
| 91 |
/// let metrics = Handle::current().metrics(); |
| 92 |
/// |
| 93 |
/// let n = metrics.num_blocking_threads(); |
| 94 |
/// println!("Runtime has created {} threads", n); |
| 95 |
/// } |
| 96 |
/// ``` |
| 97 |
pub fn num_blocking_threads(&self) -> usize { |
| 98 |
self.handle.inner.num_blocking_threads() |
| 99 |
} |
| 100 |
|
| 101 |
#[deprecated = "Renamed to num_alive_tasks"] |
| 102 |
/// Renamed to [`RuntimeMetrics::num_alive_tasks`] |
| 103 |
pub fn active_tasks_count(&self) -> usize { |
| 104 |
self.num_alive_tasks() |
| 105 |
} |
| 106 |
|
| 107 |
/// Returns the number of idle threads, which have spawned by the runtime |
| 108 |
/// for `spawn_blocking` calls. |
| 109 |
/// |
| 110 |
/// # Examples |
| 111 |
/// |
| 112 |
/// ``` |
| 113 |
/// use tokio::runtime::Handle; |
| 114 |
/// |
| 115 |
/// #[tokio::main] |
| 116 |
/// async fn main() { |
| 117 |
/// let _ = tokio::task::spawn_blocking(move || { |
| 118 |
/// // Stand-in for compute-heavy work or using synchronous APIs |
| 119 |
/// 1 + 1 |
| 120 |
/// }).await; |
| 121 |
/// let metrics = Handle::current().metrics(); |
| 122 |
/// |
| 123 |
/// let n = metrics.num_idle_blocking_threads(); |
| 124 |
/// println!("Runtime has {} idle blocking thread pool threads", n); |
| 125 |
/// } |
| 126 |
/// ``` |
| 127 |
pub fn num_idle_blocking_threads(&self) -> usize { |
| 128 |
self.handle.inner.num_idle_blocking_threads() |
| 129 |
} |
| 130 |
|
| 131 |
/// Returns the thread id of the given worker thread. |
| 132 |
/// |
| 133 |
/// The returned value is `None` if the worker thread has not yet finished |
| 134 |
/// starting up. |
| 135 |
/// |
| 136 |
/// If additional information about the thread, such as its native id, are |
| 137 |
/// required, those can be collected in [`on_thread_start`] and correlated |
| 138 |
/// using the thread id. |
| 139 |
/// |
| 140 |
/// [`on_thread_start`]: crate::runtime::Builder::on_thread_start |
| 141 |
/// |
| 142 |
/// # Arguments |
| 143 |
/// |
| 144 |
/// `worker` is the index of the worker being queried. The given value must |
| 145 |
/// be between 0 and `num_workers()`. The index uniquely identifies a single |
| 146 |
/// worker and will continue to identify the worker throughout the lifetime |
| 147 |
/// of the runtime instance. |
| 148 |
/// |
| 149 |
/// # Panics |
| 150 |
/// |
| 151 |
/// The method panics when `worker` represents an invalid worker, i.e. is |
| 152 |
/// greater than or equal to `num_workers()`. |
| 153 |
/// |
| 154 |
/// # Examples |
| 155 |
/// |
| 156 |
/// ``` |
| 157 |
/// use tokio::runtime::Handle; |
| 158 |
/// |
| 159 |
/// #[tokio::main] |
| 160 |
/// async fn main() { |
| 161 |
/// let metrics = Handle::current().metrics(); |
| 162 |
/// |
| 163 |
/// let id = metrics.worker_thread_id(0); |
| 164 |
/// println!("worker 0 has id {:?}", id); |
| 165 |
/// } |
| 166 |
/// ``` |
| 167 |
pub fn worker_thread_id(&self, worker: usize) -> Option<ThreadId> { |
| 168 |
self.handle |
| 169 |
.inner |
| 170 |
.worker_metrics(worker) |
| 171 |
.thread_id() |
| 172 |
} |
| 173 |
|
| 174 |
cfg_64bit_metrics! { |
| 175 |
/// Returns the number of tasks spawned in this runtime since it was created. |
| 176 |
/// |
| 177 |
/// This count starts at zero when the runtime is created and increases by one each time a task is spawned. |
| 178 |
/// |
| 179 |
/// The counter is monotonically increasing. It is never decremented or |
| 180 |
/// reset to zero. |
| 181 |
/// |
| 182 |
/// # Examples |
| 183 |
/// |
| 184 |
/// ``` |
| 185 |
/// use tokio::runtime::Handle; |
| 186 |
/// |
| 187 |
/// #[tokio::main] |
| 188 |
/// async fn main() { |
| 189 |
/// let metrics = Handle::current().metrics(); |
| 190 |
/// |
| 191 |
/// let n = metrics.spawned_tasks_count(); |
| 192 |
/// println!("Runtime has had {} tasks spawned", n); |
| 193 |
/// } |
| 194 |
/// ``` |
| 195 |
pub fn spawned_tasks_count(&self) -> u64 { |
| 196 |
self.handle.inner.spawned_tasks_count() |
| 197 |
} |
| 198 |
|
| 199 |
/// Returns the number of tasks scheduled from **outside** of the runtime. |
| 200 |
/// |
| 201 |
/// The remote schedule count starts at zero when the runtime is created and |
| 202 |
/// increases by one each time a task is woken from **outside** of the |
| 203 |
/// runtime. This usually means that a task is spawned or notified from a |
| 204 |
/// non-runtime thread and must be queued using the Runtime's injection |
| 205 |
/// queue, which tends to be slower. |
| 206 |
/// |
| 207 |
/// The counter is monotonically increasing. It is never decremented or |
| 208 |
/// reset to zero. |
| 209 |
/// |
| 210 |
/// # Examples |
| 211 |
/// |
| 212 |
/// ``` |
| 213 |
/// use tokio::runtime::Handle; |
| 214 |
/// |
| 215 |
/// #[tokio::main] |
| 216 |
/// async fn main() { |
| 217 |
/// let metrics = Handle::current().metrics(); |
| 218 |
/// |
| 219 |
/// let n = metrics.remote_schedule_count(); |
| 220 |
/// println!("{} tasks were scheduled from outside the runtime", n); |
| 221 |
/// } |
| 222 |
/// ``` |
| 223 |
pub fn remote_schedule_count(&self) -> u64 { |
| 224 |
self.handle |
| 225 |
.inner |
| 226 |
.scheduler_metrics() |
| 227 |
.remote_schedule_count |
| 228 |
.load(Relaxed) |
| 229 |
} |
| 230 |
|
| 231 |
/// Returns the number of times that tasks have been forced to yield back to the scheduler |
| 232 |
/// after exhausting their task budgets. |
| 233 |
/// |
| 234 |
/// This count starts at zero when the runtime is created and increases by one each time a task yields due to exhausting its budget. |
| 235 |
/// |
| 236 |
/// The counter is monotonically increasing. It is never decremented or |
| 237 |
/// reset to zero. |
| 238 |
pub fn budget_forced_yield_count(&self) -> u64 { |
| 239 |
self.handle |
| 240 |
.inner |
| 241 |
.scheduler_metrics() |
| 242 |
.budget_forced_yield_count |
| 243 |
.load(Relaxed) |
| 244 |
} |
| 245 |
|
| 246 |
/// Returns the total number of times the given worker thread has parked. |
| 247 |
/// |
| 248 |
/// The worker park count starts at zero when the runtime is created and |
| 249 |
/// increases by one each time the worker parks the thread waiting for new |
| 250 |
/// inbound events to process. This usually means the worker has processed |
| 251 |
/// all pending work and is currently idle. |
| 252 |
/// |
| 253 |
/// The counter is monotonically increasing. It is never decremented or |
| 254 |
/// reset to zero. |
| 255 |
/// |
| 256 |
/// # Arguments |
| 257 |
/// |
| 258 |
/// `worker` is the index of the worker being queried. The given value must |
| 259 |
/// be between 0 and `num_workers()`. The index uniquely identifies a single |
| 260 |
/// worker and will continue to identify the worker throughout the lifetime |
| 261 |
/// of the runtime instance. |
| 262 |
/// |
| 263 |
/// # Panics |
| 264 |
/// |
| 265 |
/// The method panics when `worker` represents an invalid worker, i.e. is |
| 266 |
/// greater than or equal to `num_workers()`. |
| 267 |
/// |
| 268 |
/// # Examples |
| 269 |
/// |
| 270 |
/// ``` |
| 271 |
/// use tokio::runtime::Handle; |
| 272 |
/// |
| 273 |
/// #[tokio::main] |
| 274 |
/// async fn main() { |
| 275 |
/// let metrics = Handle::current().metrics(); |
| 276 |
/// |
| 277 |
/// let n = metrics.worker_park_count(0); |
| 278 |
/// println!("worker 0 parked {} times", n); |
| 279 |
/// } |
| 280 |
/// ``` |
| 281 |
pub fn worker_park_count(&self, worker: usize) -> u64 { |
| 282 |
self.handle |
| 283 |
.inner |
| 284 |
.worker_metrics(worker) |
| 285 |
.park_count |
| 286 |
.load(Relaxed) |
| 287 |
} |
| 288 |
|
| 289 |
/// Returns the total number of times the given worker thread has parked |
| 290 |
/// and unparked. |
| 291 |
/// |
| 292 |
/// The worker park/unpark count starts at zero when the runtime is created |
| 293 |
/// and increases by one each time the worker parks the thread waiting for |
| 294 |
/// new inbound events to process. This usually means the worker has processed |
| 295 |
/// all pending work and is currently idle. When new work becomes available, |
| 296 |
/// the worker is unparked and the park/unpark count is again increased by one. |
| 297 |
/// |
| 298 |
/// An odd count means that the worker is currently parked. |
| 299 |
/// An even count means that the worker is currently active. |
| 300 |
/// |
| 301 |
/// The counter is monotonically increasing. It is never decremented or |
| 302 |
/// reset to zero. |
| 303 |
/// |
| 304 |
/// # Arguments |
| 305 |
/// |
| 306 |
/// `worker` is the index of the worker being queried. The given value must |
| 307 |
/// be between 0 and `num_workers()`. The index uniquely identifies a single |
| 308 |
/// worker and will continue to identify the worker throughout the lifetime |
| 309 |
/// of the runtime instance. |
| 310 |
/// |
| 311 |
/// # Panics |
| 312 |
/// |
| 313 |
/// The method panics when `worker` represents an invalid worker, i.e. is |
| 314 |
/// greater than or equal to `num_workers()`. |
| 315 |
/// |
| 316 |
/// # Examples |
| 317 |
/// |
| 318 |
/// ``` |
| 319 |
/// use tokio::runtime::Handle; |
| 320 |
/// |
| 321 |
/// #[tokio::main] |
| 322 |
/// async fn main() { |
| 323 |
/// let metrics = Handle::current().metrics(); |
| 324 |
/// let n = metrics.worker_park_unpark_count(0); |
| 325 |
/// |
| 326 |
/// println!("worker 0 parked and unparked {} times", n); |
| 327 |
/// |
| 328 |
/// if n % 2 == 0 { |
| 329 |
/// println!("worker 0 is active"); |
| 330 |
/// } else { |
| 331 |
/// println!("worker 0 is parked"); |
| 332 |
/// } |
| 333 |
/// } |
| 334 |
/// ``` |
| 335 |
pub fn worker_park_unpark_count(&self, worker: usize) -> u64 { |
| 336 |
self.handle |
| 337 |
.inner |
| 338 |
.worker_metrics(worker) |
| 339 |
.park_unpark_count |
| 340 |
.load(Relaxed) |
| 341 |
} |
| 342 |
|
| 343 |
|
| 344 |
/// Returns the number of times the given worker thread unparked but |
| 345 |
/// performed no work before parking again. |
| 346 |
/// |
| 347 |
/// The worker no-op count starts at zero when the runtime is created and |
| 348 |
/// increases by one each time the worker unparks the thread but finds no |
| 349 |
/// new work and goes back to sleep. This indicates a false-positive wake up. |
| 350 |
/// |
| 351 |
/// The counter is monotonically increasing. It is never decremented or |
| 352 |
/// reset to zero. |
| 353 |
/// |
| 354 |
/// # Arguments |
| 355 |
/// |
| 356 |
/// `worker` is the index of the worker being queried. The given value must |
| 357 |
/// be between 0 and `num_workers()`. The index uniquely identifies a single |
| 358 |
/// worker and will continue to identify the worker throughout the lifetime |
| 359 |
/// of the runtime instance. |
| 360 |
/// |
| 361 |
/// # Panics |
| 362 |
/// |
| 363 |
/// The method panics when `worker` represents an invalid worker, i.e. is |
| 364 |
/// greater than or equal to `num_workers()`. |
| 365 |
/// |
| 366 |
/// # Examples |
| 367 |
/// |
| 368 |
/// ``` |
| 369 |
/// use tokio::runtime::Handle; |
| 370 |
/// |
| 371 |
/// #[tokio::main] |
| 372 |
/// async fn main() { |
| 373 |
/// let metrics = Handle::current().metrics(); |
| 374 |
/// |
| 375 |
/// let n = metrics.worker_noop_count(0); |
| 376 |
/// println!("worker 0 had {} no-op unparks", n); |
| 377 |
/// } |
| 378 |
/// ``` |
| 379 |
pub fn worker_noop_count(&self, worker: usize) -> u64 { |
| 380 |
self.handle |
| 381 |
.inner |
| 382 |
.worker_metrics(worker) |
| 383 |
.noop_count |
| 384 |
.load(Relaxed) |
| 385 |
} |
| 386 |
|
| 387 |
/// Returns the number of tasks the given worker thread stole from |
| 388 |
/// another worker thread. |
| 389 |
/// |
| 390 |
/// This metric only applies to the **multi-threaded** runtime and will |
| 391 |
/// always return `0` when using the current thread runtime. |
| 392 |
/// |
| 393 |
/// The worker steal count starts at zero when the runtime is created and |
| 394 |
/// increases by `N` each time the worker has processed its scheduled queue |
| 395 |
/// and successfully steals `N` more pending tasks from another worker. |
| 396 |
/// |
| 397 |
/// The counter is monotonically increasing. It is never decremented or |
| 398 |
/// reset to zero. |
| 399 |
/// |
| 400 |
/// # Arguments |
| 401 |
/// |
| 402 |
/// `worker` is the index of the worker being queried. The given value must |
| 403 |
/// be between 0 and `num_workers()`. The index uniquely identifies a single |
| 404 |
/// worker and will continue to identify the worker throughout the lifetime |
| 405 |
/// of the runtime instance. |
| 406 |
/// |
| 407 |
/// # Panics |
| 408 |
/// |
| 409 |
/// The method panics when `worker` represents an invalid worker, i.e. is |
| 410 |
/// greater than or equal to `num_workers()`. |
| 411 |
/// |
| 412 |
/// # Examples |
| 413 |
/// |
| 414 |
/// ``` |
| 415 |
/// use tokio::runtime::Handle; |
| 416 |
/// |
| 417 |
/// #[tokio::main] |
| 418 |
/// async fn main() { |
| 419 |
/// let metrics = Handle::current().metrics(); |
| 420 |
/// |
| 421 |
/// let n = metrics.worker_steal_count(0); |
| 422 |
/// println!("worker 0 has stolen {} tasks", n); |
| 423 |
/// } |
| 424 |
/// ``` |
| 425 |
pub fn worker_steal_count(&self, worker: usize) -> u64 { |
| 426 |
self.handle |
| 427 |
.inner |
| 428 |
.worker_metrics(worker) |
| 429 |
.steal_count |
| 430 |
.load(Relaxed) |
| 431 |
} |
| 432 |
|
| 433 |
/// Returns the number of times the given worker thread stole tasks from |
| 434 |
/// another worker thread. |
| 435 |
/// |
| 436 |
/// This metric only applies to the **multi-threaded** runtime and will |
| 437 |
/// always return `0` when using the current thread runtime. |
| 438 |
/// |
| 439 |
/// The worker steal count starts at zero when the runtime is created and |
| 440 |
/// increases by one each time the worker has processed its scheduled queue |
| 441 |
/// and successfully steals more pending tasks from another worker. |
| 442 |
/// |
| 443 |
/// The counter is monotonically increasing. It is never decremented or |
| 444 |
/// reset to zero. |
| 445 |
/// |
| 446 |
/// # Arguments |
| 447 |
/// |
| 448 |
/// `worker` is the index of the worker being queried. The given value must |
| 449 |
/// be between 0 and `num_workers()`. The index uniquely identifies a single |
| 450 |
/// worker and will continue to identify the worker throughout the lifetime |
| 451 |
/// of the runtime instance. |
| 452 |
/// |
| 453 |
/// # Panics |
| 454 |
/// |
| 455 |
/// The method panics when `worker` represents an invalid worker, i.e. is |
| 456 |
/// greater than or equal to `num_workers()`. |
| 457 |
/// |
| 458 |
/// # Examples |
| 459 |
/// |
| 460 |
/// ``` |
| 461 |
/// use tokio::runtime::Handle; |
| 462 |
/// |
| 463 |
/// #[tokio::main] |
| 464 |
/// async fn main() { |
| 465 |
/// let metrics = Handle::current().metrics(); |
| 466 |
/// |
| 467 |
/// let n = metrics.worker_steal_operations(0); |
| 468 |
/// println!("worker 0 has stolen tasks {} times", n); |
| 469 |
/// } |
| 470 |
/// ``` |
| 471 |
pub fn worker_steal_operations(&self, worker: usize) -> u64 { |
| 472 |
self.handle |
| 473 |
.inner |
| 474 |
.worker_metrics(worker) |
| 475 |
.steal_operations |
| 476 |
.load(Relaxed) |
| 477 |
} |
| 478 |
|
| 479 |
/// Returns the number of tasks the given worker thread has polled. |
| 480 |
/// |
| 481 |
/// The worker poll count starts at zero when the runtime is created and |
| 482 |
/// increases by one each time the worker polls a scheduled task. |
| 483 |
/// |
| 484 |
/// The counter is monotonically increasing. It is never decremented or |
| 485 |
/// reset to zero. |
| 486 |
/// |
| 487 |
/// # Arguments |
| 488 |
/// |
| 489 |
/// `worker` is the index of the worker being queried. The given value must |
| 490 |
/// be between 0 and `num_workers()`. The index uniquely identifies a single |
| 491 |
/// worker and will continue to identify the worker throughout the lifetime |
| 492 |
/// of the runtime instance. |
| 493 |
/// |
| 494 |
/// # Panics |
| 495 |
/// |
| 496 |
/// The method panics when `worker` represents an invalid worker, i.e. is |
| 497 |
/// greater than or equal to `num_workers()`. |
| 498 |
/// |
| 499 |
/// # Examples |
| 500 |
/// |
| 501 |
/// ``` |
| 502 |
/// use tokio::runtime::Handle; |
| 503 |
/// |
| 504 |
/// #[tokio::main] |
| 505 |
/// async fn main() { |
| 506 |
/// let metrics = Handle::current().metrics(); |
| 507 |
/// |
| 508 |
/// let n = metrics.worker_poll_count(0); |
| 509 |
/// println!("worker 0 has polled {} tasks", n); |
| 510 |
/// } |
| 511 |
/// ``` |
| 512 |
pub fn worker_poll_count(&self, worker: usize) -> u64 { |
| 513 |
self.handle |
| 514 |
.inner |
| 515 |
.worker_metrics(worker) |
| 516 |
.poll_count |
| 517 |
.load(Relaxed) |
| 518 |
} |
| 519 |
|
| 520 |
/// Returns the amount of time the given worker thread has been busy. |
| 521 |
/// |
| 522 |
/// The worker busy duration starts at zero when the runtime is created and |
| 523 |
/// increases whenever the worker is spending time processing work. Using |
| 524 |
/// this value can indicate the load of the given worker. If a lot of time |
| 525 |
/// is spent busy, then the worker is under load and will check for inbound |
| 526 |
/// events less often. |
| 527 |
/// |
| 528 |
/// The timer is monotonically increasing. It is never decremented or reset |
| 529 |
/// to zero. |
| 530 |
/// |
| 531 |
/// # Arguments |
| 532 |
/// |
| 533 |
/// `worker` is the index of the worker being queried. The given value must |
| 534 |
/// be between 0 and `num_workers()`. The index uniquely identifies a single |
| 535 |
/// worker and will continue to identify the worker throughout the lifetime |
| 536 |
/// of the runtime instance. |
| 537 |
/// |
| 538 |
/// # Panics |
| 539 |
/// |
| 540 |
/// The method panics when `worker` represents an invalid worker, i.e. is |
| 541 |
/// greater than or equal to `num_workers()`. |
| 542 |
/// |
| 543 |
/// # Examples |
| 544 |
/// |
| 545 |
/// ``` |
| 546 |
/// use tokio::runtime::Handle; |
| 547 |
/// |
| 548 |
/// #[tokio::main] |
| 549 |
/// async fn main() { |
| 550 |
/// let metrics = Handle::current().metrics(); |
| 551 |
/// |
| 552 |
/// let n = metrics.worker_total_busy_duration(0); |
| 553 |
/// println!("worker 0 was busy for a total of {:?}", n); |
| 554 |
/// } |
| 555 |
/// ``` |
| 556 |
pub fn worker_total_busy_duration(&self, worker: usize) -> Duration { |
| 557 |
let nanos = self |
| 558 |
.handle |
| 559 |
.inner |
| 560 |
.worker_metrics(worker) |
| 561 |
.busy_duration_total |
| 562 |
.load(Relaxed); |
| 563 |
Duration::from_nanos(nanos) |
| 564 |
} |
| 565 |
|
| 566 |
/// Returns the number of tasks scheduled from **within** the runtime on the |
| 567 |
/// given worker's local queue. |
| 568 |
/// |
| 569 |
/// The local schedule count starts at zero when the runtime is created and |
| 570 |
/// increases by one each time a task is woken from **inside** of the |
| 571 |
/// runtime on the given worker. This usually means that a task is spawned |
| 572 |
/// or notified from within a runtime thread and will be queued on the |
| 573 |
/// worker-local queue. |
| 574 |
/// |
| 575 |
/// The counter is monotonically increasing. It is never decremented or |
| 576 |
/// reset to zero. |
| 577 |
/// |
| 578 |
/// # Arguments |
| 579 |
/// |
| 580 |
/// `worker` is the index of the worker being queried. The given value must |
| 581 |
/// be between 0 and `num_workers()`. The index uniquely identifies a single |
| 582 |
/// worker and will continue to identify the worker throughout the lifetime |
| 583 |
/// of the runtime instance. |
| 584 |
/// |
| 585 |
/// # Panics |
| 586 |
/// |
| 587 |
/// The method panics when `worker` represents an invalid worker, i.e. is |
| 588 |
/// greater than or equal to `num_workers()`. |
| 589 |
/// |
| 590 |
/// # Examples |
| 591 |
/// |
| 592 |
/// ``` |
| 593 |
/// use tokio::runtime::Handle; |
| 594 |
/// |
| 595 |
/// #[tokio::main] |
| 596 |
/// async fn main() { |
| 597 |
/// let metrics = Handle::current().metrics(); |
| 598 |
/// |
| 599 |
/// let n = metrics.worker_local_schedule_count(0); |
| 600 |
/// println!("{} tasks were scheduled on the worker's local queue", n); |
| 601 |
/// } |
| 602 |
/// ``` |
| 603 |
pub fn worker_local_schedule_count(&self, worker: usize) -> u64 { |
| 604 |
self.handle |
| 605 |
.inner |
| 606 |
.worker_metrics(worker) |
| 607 |
.local_schedule_count |
| 608 |
.load(Relaxed) |
| 609 |
} |
| 610 |
|
| 611 |
/// Returns the number of times the given worker thread saturated its local |
| 612 |
/// queue. |
| 613 |
/// |
| 614 |
/// This metric only applies to the **multi-threaded** scheduler. |
| 615 |
/// |
| 616 |
/// The worker overflow count starts at zero when the runtime is created and |
| 617 |
/// increases by one each time the worker attempts to schedule a task |
| 618 |
/// locally, but its local queue is full. When this happens, half of the |
| 619 |
/// local queue is moved to the injection queue. |
| 620 |
/// |
| 621 |
/// The counter is monotonically increasing. It is never decremented or |
| 622 |
/// reset to zero. |
| 623 |
/// |
| 624 |
/// # Arguments |
| 625 |
/// |
| 626 |
/// `worker` is the index of the worker being queried. The given value must |
| 627 |
/// be between 0 and `num_workers()`. The index uniquely identifies a single |
| 628 |
/// worker and will continue to identify the worker throughout the lifetime |
| 629 |
/// of the runtime instance. |
| 630 |
/// |
| 631 |
/// # Panics |
| 632 |
/// |
| 633 |
/// The method panics when `worker` represents an invalid worker, i.e. is |
| 634 |
/// greater than or equal to `num_workers()`. |
| 635 |
/// |
| 636 |
/// # Examples |
| 637 |
/// |
| 638 |
/// ``` |
| 639 |
/// use tokio::runtime::Handle; |
| 640 |
/// |
| 641 |
/// #[tokio::main] |
| 642 |
/// async fn main() { |
| 643 |
/// let metrics = Handle::current().metrics(); |
| 644 |
/// |
| 645 |
/// let n = metrics.worker_overflow_count(0); |
| 646 |
/// println!("worker 0 has overflowed its queue {} times", n); |
| 647 |
/// } |
| 648 |
/// ``` |
| 649 |
pub fn worker_overflow_count(&self, worker: usize) -> u64 { |
| 650 |
self.handle |
| 651 |
.inner |
| 652 |
.worker_metrics(worker) |
| 653 |
.overflow_count |
| 654 |
.load(Relaxed) |
| 655 |
} |
| 656 |
} |
| 657 |
|
| 658 |
/// Returns the number of tasks currently scheduled in the runtime's |
| 659 |
/// injection queue. |
| 660 |
/// |
| 661 |
/// Tasks that are spawned or notified from a non-runtime thread are |
| 662 |
/// scheduled using the runtime's injection queue. This metric returns the |
| 663 |
/// **current** number of tasks pending in the injection queue. As such, the |
| 664 |
/// returned value may increase or decrease as new tasks are scheduled and |
| 665 |
/// processed. |
| 666 |
/// |
| 667 |
/// # Examples |
| 668 |
/// |
| 669 |
/// ``` |
| 670 |
/// use tokio::runtime::Handle; |
| 671 |
/// |
| 672 |
/// #[tokio::main] |
| 673 |
/// async fn main() { |
| 674 |
/// let metrics = Handle::current().metrics(); |
| 675 |
/// |
| 676 |
/// let n = metrics.injection_queue_depth(); |
| 677 |
/// println!("{} tasks currently pending in the runtime's injection queue", n); |
| 678 |
/// } |
| 679 |
/// ``` |
| 680 |
pub fn injection_queue_depth(&self) -> usize { |
| 681 |
self.handle.inner.injection_queue_depth() |
| 682 |
} |
| 683 |
|
| 684 |
/// Returns the number of tasks currently scheduled in the given worker's |
| 685 |
/// local queue. |
| 686 |
/// |
| 687 |
/// Tasks that are spawned or notified from within a runtime thread are |
| 688 |
/// scheduled using that worker's local queue. This metric returns the |
| 689 |
/// **current** number of tasks pending in the worker's local queue. As |
| 690 |
/// such, the returned value may increase or decrease as new tasks are |
| 691 |
/// scheduled and processed. |
| 692 |
/// |
| 693 |
/// # Arguments |
| 694 |
/// |
| 695 |
/// `worker` is the index of the worker being queried. The given value must |
| 696 |
/// be between 0 and `num_workers()`. The index uniquely identifies a single |
| 697 |
/// worker and will continue to identify the worker throughout the lifetime |
| 698 |
/// of the runtime instance. |
| 699 |
/// |
| 700 |
/// # Panics |
| 701 |
/// |
| 702 |
/// The method panics when `worker` represents an invalid worker, i.e. is |
| 703 |
/// greater than or equal to `num_workers()`. |
| 704 |
/// |
| 705 |
/// # Examples |
| 706 |
/// |
| 707 |
/// ``` |
| 708 |
/// use tokio::runtime::Handle; |
| 709 |
/// |
| 710 |
/// #[tokio::main] |
| 711 |
/// async fn main() { |
| 712 |
/// let metrics = Handle::current().metrics(); |
| 713 |
/// |
| 714 |
/// let n = metrics.worker_local_queue_depth(0); |
| 715 |
/// println!("{} tasks currently pending in worker 0's local queue", n); |
| 716 |
/// } |
| 717 |
/// ``` |
| 718 |
pub fn worker_local_queue_depth(&self, worker: usize) -> usize { |
| 719 |
self.handle.inner.worker_local_queue_depth(worker) |
| 720 |
} |
| 721 |
|
| 722 |
/// Returns `true` if the runtime is tracking the distribution of task poll |
| 723 |
/// times. |
| 724 |
/// |
| 725 |
/// Task poll times are not instrumented by default as doing so requires |
| 726 |
/// calling [`Instant::now()`] twice per task poll. The feature is enabled |
| 727 |
/// by calling [`enable_metrics_poll_count_histogram()`] when building the |
| 728 |
/// runtime. |
| 729 |
/// |
| 730 |
/// # Examples |
| 731 |
/// |
| 732 |
/// ``` |
| 733 |
/// use tokio::runtime::{self, Handle}; |
| 734 |
/// |
| 735 |
/// fn main() { |
| 736 |
/// runtime::Builder::new_current_thread() |
| 737 |
/// .enable_metrics_poll_count_histogram() |
| 738 |
/// .build() |
| 739 |
/// .unwrap() |
| 740 |
/// .block_on(async { |
| 741 |
/// let metrics = Handle::current().metrics(); |
| 742 |
/// let enabled = metrics.poll_count_histogram_enabled(); |
| 743 |
/// |
| 744 |
/// println!("Tracking task poll time distribution: {:?}", enabled); |
| 745 |
/// }); |
| 746 |
/// } |
| 747 |
/// ``` |
| 748 |
/// |
| 749 |
/// [`enable_metrics_poll_count_histogram()`]: crate::runtime::Builder::enable_metrics_poll_count_histogram |
| 750 |
/// [`Instant::now()`]: std::time::Instant::now |
| 751 |
pub fn poll_count_histogram_enabled(&self) -> bool { |
| 752 |
self.handle |
| 753 |
.inner |
| 754 |
.worker_metrics(0) |
| 755 |
.poll_count_histogram |
| 756 |
.is_some() |
| 757 |
} |
| 758 |
|
| 759 |
/// Returns the number of histogram buckets tracking the distribution of |
| 760 |
/// task poll times. |
| 761 |
/// |
| 762 |
/// This value is configured by calling |
| 763 |
/// [`metrics_poll_count_histogram_buckets()`] when building the runtime. |
| 764 |
/// |
| 765 |
/// # Examples |
| 766 |
/// |
| 767 |
/// ``` |
| 768 |
/// use tokio::runtime::{self, Handle}; |
| 769 |
/// |
| 770 |
/// fn main() { |
| 771 |
/// runtime::Builder::new_current_thread() |
| 772 |
/// .enable_metrics_poll_count_histogram() |
| 773 |
/// .build() |
| 774 |
/// .unwrap() |
| 775 |
/// .block_on(async { |
| 776 |
/// let metrics = Handle::current().metrics(); |
| 777 |
/// let buckets = metrics.poll_count_histogram_num_buckets(); |
| 778 |
/// |
| 779 |
/// println!("Histogram buckets: {:?}", buckets); |
| 780 |
/// }); |
| 781 |
/// } |
| 782 |
/// ``` |
| 783 |
/// |
| 784 |
/// [`metrics_poll_count_histogram_buckets()`]: |
| 785 |
/// crate::runtime::Builder::metrics_poll_count_histogram_buckets |
| 786 |
pub fn poll_count_histogram_num_buckets(&self) -> usize { |
| 787 |
self.handle |
| 788 |
.inner |
| 789 |
.worker_metrics(0) |
| 790 |
.poll_count_histogram |
| 791 |
.as_ref() |
| 792 |
.map(|histogram| histogram.num_buckets()) |
| 793 |
.unwrap_or_default() |
| 794 |
} |
| 795 |
|
| 796 |
/// Returns the range of task poll times tracked by the given bucket. |
| 797 |
/// |
| 798 |
/// This value is configured by calling |
| 799 |
/// [`metrics_poll_count_histogram_resolution()`] when building the runtime. |
| 800 |
/// |
| 801 |
/// # Panics |
| 802 |
/// |
| 803 |
/// The method panics if `bucket` represents an invalid bucket index, i.e. |
| 804 |
/// is greater than or equal to `poll_count_histogram_num_buckets()`. |
| 805 |
/// |
| 806 |
/// # Examples |
| 807 |
/// |
| 808 |
/// ``` |
| 809 |
/// use tokio::runtime::{self, Handle}; |
| 810 |
/// |
| 811 |
/// fn main() { |
| 812 |
/// runtime::Builder::new_current_thread() |
| 813 |
/// .enable_metrics_poll_count_histogram() |
| 814 |
/// .build() |
| 815 |
/// .unwrap() |
| 816 |
/// .block_on(async { |
| 817 |
/// let metrics = Handle::current().metrics(); |
| 818 |
/// let buckets = metrics.poll_count_histogram_num_buckets(); |
| 819 |
/// |
| 820 |
/// for i in 0..buckets { |
| 821 |
/// let range = metrics.poll_count_histogram_bucket_range(i); |
| 822 |
/// println!("Histogram bucket {} range: {:?}", i, range); |
| 823 |
/// } |
| 824 |
/// }); |
| 825 |
/// } |
| 826 |
/// ``` |
| 827 |
/// |
| 828 |
/// [`metrics_poll_count_histogram_resolution()`]: |
| 829 |
/// crate::runtime::Builder::metrics_poll_count_histogram_resolution |
| 830 |
#[track_caller] |
| 831 |
pub fn poll_count_histogram_bucket_range(&self, bucket: usize) -> Range<Duration> { |
| 832 |
self.handle |
| 833 |
.inner |
| 834 |
.worker_metrics(0) |
| 835 |
.poll_count_histogram |
| 836 |
.as_ref() |
| 837 |
.map(|histogram| { |
| 838 |
let range = histogram.bucket_range(bucket); |
| 839 |
std::ops::Range { |
| 840 |
start: Duration::from_nanos(range.start), |
| 841 |
end: Duration::from_nanos(range.end), |
| 842 |
} |
| 843 |
}) |
| 844 |
.unwrap_or_default() |
| 845 |
} |
| 846 |
|
| 847 |
cfg_64bit_metrics! { |
| 848 |
/// Returns the number of times the given worker polled tasks with a poll |
| 849 |
/// duration within the given bucket's range. |
| 850 |
/// |
| 851 |
/// Each worker maintains its own histogram and the counts for each bucket |
| 852 |
/// starts at zero when the runtime is created. Each time the worker polls a |
| 853 |
/// task, it tracks the duration the task poll time took and increments the |
| 854 |
/// associated bucket by 1. |
| 855 |
/// |
| 856 |
/// Each bucket is a monotonically increasing counter. It is never |
| 857 |
/// decremented or reset to zero. |
| 858 |
/// |
| 859 |
/// # Arguments |
| 860 |
/// |
| 861 |
/// `worker` is the index of the worker being queried. The given value must |
| 862 |
/// be between 0 and `num_workers()`. The index uniquely identifies a single |
| 863 |
/// worker and will continue to identify the worker throughout the lifetime |
| 864 |
/// of the runtime instance. |
| 865 |
/// |
| 866 |
/// `bucket` is the index of the bucket being queried. The bucket is scoped |
| 867 |
/// to the worker. The range represented by the bucket can be queried by |
| 868 |
/// calling [`poll_count_histogram_bucket_range()`]. Each worker maintains |
| 869 |
/// identical bucket ranges. |
| 870 |
/// |
| 871 |
/// # Panics |
| 872 |
/// |
| 873 |
/// The method panics when `worker` represents an invalid worker, i.e. is |
| 874 |
/// greater than or equal to `num_workers()` or if `bucket` represents an |
| 875 |
/// invalid bucket. |
| 876 |
/// |
| 877 |
/// # Examples |
| 878 |
/// |
| 879 |
/// ``` |
| 880 |
/// use tokio::runtime::{self, Handle}; |
| 881 |
/// |
| 882 |
/// fn main() { |
| 883 |
/// runtime::Builder::new_current_thread() |
| 884 |
/// .enable_metrics_poll_count_histogram() |
| 885 |
/// .build() |
| 886 |
/// .unwrap() |
| 887 |
/// .block_on(async { |
| 888 |
/// let metrics = Handle::current().metrics(); |
| 889 |
/// let buckets = metrics.poll_count_histogram_num_buckets(); |
| 890 |
/// |
| 891 |
/// for worker in 0..metrics.num_workers() { |
| 892 |
/// for i in 0..buckets { |
| 893 |
/// let count = metrics.poll_count_histogram_bucket_count(worker, i); |
| 894 |
/// println!("Poll count {}", count); |
| 895 |
/// } |
| 896 |
/// } |
| 897 |
/// }); |
| 898 |
/// } |
| 899 |
/// ``` |
| 900 |
/// |
| 901 |
/// [`poll_count_histogram_bucket_range()`]: crate::runtime::RuntimeMetrics::poll_count_histogram_bucket_range |
| 902 |
#[track_caller] |
| 903 |
pub fn poll_count_histogram_bucket_count(&self, worker: usize, bucket: usize) -> u64 { |
| 904 |
self.handle |
| 905 |
.inner |
| 906 |
.worker_metrics(worker) |
| 907 |
.poll_count_histogram |
| 908 |
.as_ref() |
| 909 |
.map(|histogram| histogram.get(bucket)) |
| 910 |
.unwrap_or_default() |
| 911 |
} |
| 912 |
|
| 913 |
/// Returns the mean duration of task polls, in nanoseconds. |
| 914 |
/// |
| 915 |
/// This is an exponentially weighted moving average. Currently, this metric |
| 916 |
/// is only provided by the multi-threaded runtime. |
| 917 |
/// |
| 918 |
/// # Arguments |
| 919 |
/// |
| 920 |
/// `worker` is the index of the worker being queried. The given value must |
| 921 |
/// be between 0 and `num_workers()`. The index uniquely identifies a single |
| 922 |
/// worker and will continue to identify the worker throughout the lifetime |
| 923 |
/// of the runtime instance. |
| 924 |
/// |
| 925 |
/// # Panics |
| 926 |
/// |
| 927 |
/// The method panics when `worker` represents an invalid worker, i.e. is |
| 928 |
/// greater than or equal to `num_workers()`. |
| 929 |
/// |
| 930 |
/// # Examples |
| 931 |
/// |
| 932 |
/// ``` |
| 933 |
/// use tokio::runtime::Handle; |
| 934 |
/// |
| 935 |
/// #[tokio::main] |
| 936 |
/// async fn main() { |
| 937 |
/// let metrics = Handle::current().metrics(); |
| 938 |
/// |
| 939 |
/// let n = metrics.worker_mean_poll_time(0); |
| 940 |
/// println!("worker 0 has a mean poll time of {:?}", n); |
| 941 |
/// } |
| 942 |
/// ``` |
| 943 |
#[track_caller] |
| 944 |
pub fn worker_mean_poll_time(&self, worker: usize) -> Duration { |
| 945 |
let nanos = self |
| 946 |
.handle |
| 947 |
.inner |
| 948 |
.worker_metrics(worker) |
| 949 |
.mean_poll_time |
| 950 |
.load(Relaxed); |
| 951 |
Duration::from_nanos(nanos) |
| 952 |
} |
| 953 |
} |
| 954 |
|
| 955 |
/// Returns the number of tasks currently scheduled in the blocking |
| 956 |
/// thread pool, spawned using `spawn_blocking`. |
| 957 |
/// |
| 958 |
/// This metric returns the **current** number of tasks pending in |
| 959 |
/// blocking thread pool. As such, the returned value may increase |
| 960 |
/// or decrease as new tasks are scheduled and processed. |
| 961 |
/// |
| 962 |
/// # Examples |
| 963 |
/// |
| 964 |
/// ``` |
| 965 |
/// use tokio::runtime::Handle; |
| 966 |
/// |
| 967 |
/// #[tokio::main] |
| 968 |
/// async fn main() { |
| 969 |
/// let metrics = Handle::current().metrics(); |
| 970 |
/// |
| 971 |
/// let n = metrics.blocking_queue_depth(); |
| 972 |
/// println!("{} tasks currently pending in the blocking thread pool", n); |
| 973 |
/// } |
| 974 |
/// ``` |
| 975 |
pub fn blocking_queue_depth(&self) -> usize { |
| 976 |
self.handle.inner.blocking_queue_depth() |
| 977 |
} |
| 978 |
|
| 979 |
cfg_net! { |
| 980 |
cfg_64bit_metrics! { |
| 981 |
/// Returns the number of file descriptors that have been registered with the |
| 982 |
/// runtime's I/O driver. |
| 983 |
/// |
| 984 |
/// # Examples |
| 985 |
/// |
| 986 |
/// ``` |
| 987 |
/// use tokio::runtime::Handle; |
| 988 |
/// |
| 989 |
/// #[tokio::main] |
| 990 |
/// async fn main() { |
| 991 |
/// let metrics = Handle::current().metrics(); |
| 992 |
/// |
| 993 |
/// let registered_fds = metrics.io_driver_fd_registered_count(); |
| 994 |
/// println!("{} fds have been registered with the runtime's I/O driver.", registered_fds); |
| 995 |
/// |
| 996 |
/// let deregistered_fds = metrics.io_driver_fd_deregistered_count(); |
| 997 |
/// |
| 998 |
/// let current_fd_count = registered_fds - deregistered_fds; |
| 999 |
/// println!("{} fds are currently registered by the runtime's I/O driver.", current_fd_count); |
| 1000 |
/// } |
| 1001 |
/// ``` |
| 1002 |
pub fn io_driver_fd_registered_count(&self) -> u64 { |
| 1003 |
self.with_io_driver_metrics(|m| { |
| 1004 |
m.fd_registered_count.load(Relaxed) |
| 1005 |
}) |
| 1006 |
} |
| 1007 |
|
| 1008 |
/// Returns the number of file descriptors that have been deregistered by the |
| 1009 |
/// runtime's I/O driver. |
| 1010 |
/// |
| 1011 |
/// # Examples |
| 1012 |
/// |
| 1013 |
/// ``` |
| 1014 |
/// use tokio::runtime::Handle; |
| 1015 |
/// |
| 1016 |
/// #[tokio::main] |
| 1017 |
/// async fn main() { |
| 1018 |
/// let metrics = Handle::current().metrics(); |
| 1019 |
/// |
| 1020 |
/// let n = metrics.io_driver_fd_deregistered_count(); |
| 1021 |
/// println!("{} fds have been deregistered by the runtime's I/O driver.", n); |
| 1022 |
/// } |
| 1023 |
/// ``` |
| 1024 |
pub fn io_driver_fd_deregistered_count(&self) -> u64 { |
| 1025 |
self.with_io_driver_metrics(|m| { |
| 1026 |
m.fd_deregistered_count.load(Relaxed) |
| 1027 |
}) |
| 1028 |
} |
| 1029 |
|
| 1030 |
/// Returns the number of ready events processed by the runtime's |
| 1031 |
/// I/O driver. |
| 1032 |
/// |
| 1033 |
/// # Examples |
| 1034 |
/// |
| 1035 |
/// ``` |
| 1036 |
/// use tokio::runtime::Handle; |
| 1037 |
/// |
| 1038 |
/// #[tokio::main] |
| 1039 |
/// async fn main() { |
| 1040 |
/// let metrics = Handle::current().metrics(); |
| 1041 |
/// |
| 1042 |
/// let n = metrics.io_driver_ready_count(); |
| 1043 |
/// println!("{} ready events processed by the runtime's I/O driver.", n); |
| 1044 |
/// } |
| 1045 |
/// ``` |
| 1046 |
pub fn io_driver_ready_count(&self) -> u64 { |
| 1047 |
self.with_io_driver_metrics(|m| m.ready_count.load(Relaxed)) |
| 1048 |
} |
| 1049 |
|
| 1050 |
fn with_io_driver_metrics<F>(&self, f: F) -> u64 |
| 1051 |
where |
| 1052 |
F: Fn(&super::IoDriverMetrics) -> u64, |
| 1053 |
{ |
| 1054 |
// TODO: Investigate if this should return 0, most of our metrics always increase |
| 1055 |
// thus this breaks that guarantee. |
| 1056 |
self.handle |
| 1057 |
.inner |
| 1058 |
.driver() |
| 1059 |
.io |
| 1060 |
.as_ref() |
| 1061 |
.map(|h| f(&h.metrics)) |
| 1062 |
.unwrap_or(0) |
| 1063 |
} |
| 1064 |
} |
| 1065 |
} |
| 1066 |
} |
| 1067 |
} |