Skip to main content

qubit_thread_pool/thread_pool/
thread_pool_stats.rs

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