Skip to main content

qubit_thread_pool/thread_pool/
thread_pool_stats.rs

1/*******************************************************************************
2 *
3 *    Copyright (c) 2025 - 2026 Haixing Hu.
4 *
5 *    SPDX-License-Identifier: Apache-2.0
6 *
7 *    Licensed under the Apache License, Version 2.0.
8 *
9 ******************************************************************************/
10/// Point-in-time counters reported by [`super::thread_pool::ThreadPool`].
11///
12/// The snapshot is intended for monitoring and tests. It is not a stable
13/// synchronization primitive; concurrent submissions and completions may make
14/// the next snapshot different immediately after this one is returned.
15///
16use super::thread_pool_state::ThreadPoolState;
17
18#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
19pub struct ThreadPoolStats {
20    /// Configured core pool size.
21    pub core_pool_size: usize,
22
23    /// Configured maximum pool size.
24    pub maximum_pool_size: usize,
25
26    /// Number of live worker loops.
27    pub live_workers: usize,
28
29    /// Number of workers currently waiting for work.
30    pub idle_workers: usize,
31
32    /// Number of queued tasks waiting for a worker.
33    pub queued_tasks: usize,
34
35    /// Number of tasks currently held by workers.
36    pub running_tasks: usize,
37
38    /// Number of tasks accepted since pool creation.
39    pub submitted_tasks: usize,
40
41    /// Number of worker-held jobs finished since pool creation.
42    pub completed_tasks: usize,
43
44    /// Number of queued jobs cancelled by immediate shutdown.
45    pub cancelled_tasks: usize,
46
47    /// Whether shutdown has been requested.
48    pub shutdown: bool,
49
50    /// Whether the pool has fully terminated.
51    pub terminated: bool,
52}
53
54impl ThreadPoolStats {
55    /// Builds a snapshot from locked pool state.
56    ///
57    /// # Parameters
58    ///
59    /// * `state` - Current [`ThreadPoolState`] while the pool monitor is held.
60    ///
61    /// # Returns
62    ///
63    /// A point-in-time [`ThreadPoolStats`] snapshot.
64    pub(super) fn new(state: &ThreadPoolState) -> Self {
65        Self {
66            core_pool_size: state.core_pool_size,
67            maximum_pool_size: state.maximum_pool_size,
68            live_workers: state.live_workers,
69            idle_workers: state.idle_workers,
70            queued_tasks: state.queued_tasks,
71            running_tasks: state.running_tasks,
72            submitted_tasks: state.submitted_tasks,
73            completed_tasks: state.completed_tasks,
74            cancelled_tasks: state.cancelled_tasks,
75            shutdown: !state.lifecycle.is_running(),
76            terminated: state.is_terminated(),
77        }
78    }
79}