Thread System 0.3.1
High-performance C++20 thread pool with work stealing and DAG scheduling
Loading...
Searching...
No Matches
kcenon::thread::metrics::EnhancedThreadPoolMetrics Class Reference

Enhanced thread pool metrics with histograms and percentiles. More...

#include <enhanced_metrics.h>

Inheritance diagram for kcenon::thread::metrics::EnhancedThreadPoolMetrics:
Inheritance graph
Collaboration diagram for kcenon::thread::metrics::EnhancedThreadPoolMetrics:
Collaboration graph

Public Member Functions

 EnhancedThreadPoolMetrics (std::size_t worker_count=0)
 Constructs enhanced metrics with the specified worker count.
 
 ~EnhancedThreadPoolMetrics () override=default
 Destructor.
 
 EnhancedThreadPoolMetrics (const EnhancedThreadPoolMetrics &)=delete
 
EnhancedThreadPoolMetricsoperator= (const EnhancedThreadPoolMetrics &)=delete
 
 EnhancedThreadPoolMetrics (EnhancedThreadPoolMetrics &&)=delete
 
EnhancedThreadPoolMetricsoperator= (EnhancedThreadPoolMetrics &&)=delete
 
void record_submission ()
 Record a task submission.
 
void record_enqueue (std::chrono::nanoseconds latency)
 Record enqueue operation latency.
 
void record_execution (std::chrono::nanoseconds latency, bool success)
 Record task execution completion.
 
void record_wait_time (std::chrono::nanoseconds wait)
 Record wait time (time spent in queue).
 
void record_queue_depth (std::size_t depth)
 Record current queue depth.
 
void record_worker_state (std::size_t worker_id, bool busy, std::uint64_t duration_ns=0)
 Update worker state.
 
void set_active_workers (std::size_t count)
 Set the number of active workers.
 
EnhancedSnapshot snapshot () const
 Get a comprehensive snapshot of all metrics.
 
const LatencyHistogramenqueue_latency () const
 Get the enqueue latency histogram (read-only).
 
const LatencyHistogramexecution_latency () const
 Get the execution latency histogram (read-only).
 
const LatencyHistogramwait_time () const
 Get the wait time histogram (read-only).
 
std::vector< WorkerMetricsworker_metrics () const
 Get per-worker metrics.
 
const SlidingWindowCounterthroughput_1s () const
 Get the 1-second throughput counter (read-only).
 
const SlidingWindowCounterthroughput_1m () const
 Get the 1-minute throughput counter (read-only).
 
void reset () override
 Reset all metrics to initial state.
 
void update_worker_count (std::size_t count)
 Update worker count.
 
std::string to_json () const
 Export metrics as JSON string.
 
std::string to_prometheus (const std::string &prefix="thread_pool") const
 Export metrics in Prometheus/OpenMetrics format.
 
- Public Member Functions inherited from kcenon::thread::metrics::MetricsBase
virtual ~MetricsBase ()=default
 Virtual destructor for proper cleanup in derived classes.
 
void record_submission (std::size_t count=1)
 Record task submission(s).
 
void record_execution (std::uint64_t duration_ns, bool success)
 Record task execution completion.
 
void record_idle_time (std::uint64_t duration_ns)
 Record idle time.
 
std::uint64_t tasks_submitted () const
 Get the total number of tasks submitted.
 
std::uint64_t tasks_executed () const
 Get the total number of tasks successfully executed.
 
std::uint64_t tasks_failed () const
 Get the total number of failed tasks.
 
std::uint64_t total_busy_time_ns () const
 Get the total busy time in nanoseconds.
 
std::uint64_t total_idle_time_ns () const
 Get the total idle time in nanoseconds.
 
BaseSnapshot base_snapshot () const
 Get a base snapshot of common metrics.
 
double utilization () const
 Calculate worker utilization ratio.
 
double success_rate () const
 Calculate task success rate.
 

Static Private Member Functions

static double ns_to_us (double ns)
 

Private Attributes

LatencyHistogram enqueue_latency_
 
LatencyHistogram execution_latency_
 
LatencyHistogram wait_time_
 
SlidingWindowCounter throughput_1s_
 
SlidingWindowCounter throughput_1m_
 
std::atomic< std::size_t > current_queue_depth_ {0}
 
std::atomic< std::size_t > peak_queue_depth_ {0}
 
std::atomic< std::uint64_t > queue_depth_sum_ {0}
 
std::atomic< std::uint64_t > queue_depth_samples_ {0}
 
std::atomic< std::size_t > active_workers_ {0}
 
std::mutex workers_mutex_
 
std::vector< WorkerMetricsper_worker_metrics_
 

Additional Inherited Members

- Protected Member Functions inherited from kcenon::thread::metrics::MetricsBase
 MetricsBase ()=default
 Default constructor.
 
 MetricsBase (const MetricsBase &)=delete
 Copy constructor (deleted for thread safety).
 
MetricsBaseoperator= (const MetricsBase &)=delete
 Copy assignment operator (deleted for thread safety).
 
 MetricsBase (MetricsBase &&)=delete
 Move constructor (deleted for thread safety).
 
MetricsBaseoperator= (MetricsBase &&)=delete
 Move assignment operator (deleted for thread safety).
 
- Protected Attributes inherited from kcenon::thread::metrics::MetricsBase
std::atomic< std::uint64_t > tasks_submitted_ {0}
 Counter for submitted tasks.
 
std::atomic< std::uint64_t > tasks_executed_ {0}
 Counter for successfully executed tasks.
 
std::atomic< std::uint64_t > tasks_failed_ {0}
 Counter for failed tasks.
 
std::atomic< std::uint64_t > total_busy_time_ns_ {0}
 Accumulated busy time in nanoseconds.
 
std::atomic< std::uint64_t > total_idle_time_ns_ {0}
 Accumulated idle time in nanoseconds.
 

Detailed Description

Enhanced thread pool metrics with histograms and percentiles.

This class provides production-grade observability for thread pools, including:

  • Latency histograms for enqueue, execution, and wait times
  • Percentile calculations (P50, P90, P99)
  • Sliding window throughput tracking
  • Per-worker utilization metrics
  • Queue depth monitoring

Performance Characteristics

  • record_* overhead: < 100ns
  • snapshot() latency: < 10μs
  • Memory per histogram: < 1KB
  • Memory per counter: < 4KB (for 60s window)

Thread Safety

All methods are thread-safe. Recording methods use lock-free atomics. Snapshot generation acquires a brief mutex for consistency.

Usage Example

auto metrics = std::make_shared<EnhancedThreadPoolMetrics>(8);
// Record metrics (called by thread_pool internally)
metrics->record_enqueue(std::chrono::nanoseconds{1000});
metrics->record_execution(std::chrono::nanoseconds{50000}, true);
metrics->record_wait_time(std::chrono::nanoseconds{5000});
// Get snapshot
auto snap = metrics->snapshot();
LOG_INFO("P99 execution: {:.2f}μs", snap.execution_latency_p99_us);
LOG_INFO("Throughput: {:.1f} ops/sec", snap.throughput_1s);
See also
LatencyHistogram
SlidingWindowCounter
ThreadPoolMetrics
MetricsBase

Definition at line 251 of file enhanced_metrics.h.

Constructor & Destructor Documentation

◆ EnhancedThreadPoolMetrics() [1/3]

kcenon::thread::metrics::EnhancedThreadPoolMetrics::EnhancedThreadPoolMetrics ( std::size_t worker_count = 0)
explicit

Constructs enhanced metrics with the specified worker count.

Parameters
worker_countNumber of workers to track individually.

Definition at line 10 of file enhanced_metrics.cpp.

11 : throughput_1s_(std::chrono::seconds{1}),
12 throughput_1m_(std::chrono::seconds{60}),
13 per_worker_metrics_(worker_count) {
14 for (std::size_t i = 0; i < worker_count; ++i) {
15 per_worker_metrics_[i].worker_id = i;
16 }
17}

References per_worker_metrics_.

◆ ~EnhancedThreadPoolMetrics()

kcenon::thread::metrics::EnhancedThreadPoolMetrics::~EnhancedThreadPoolMetrics ( )
overridedefault

Destructor.

◆ EnhancedThreadPoolMetrics() [2/3]

kcenon::thread::metrics::EnhancedThreadPoolMetrics::EnhancedThreadPoolMetrics ( const EnhancedThreadPoolMetrics & )
delete

◆ EnhancedThreadPoolMetrics() [3/3]

kcenon::thread::metrics::EnhancedThreadPoolMetrics::EnhancedThreadPoolMetrics ( EnhancedThreadPoolMetrics && )
delete

Member Function Documentation

◆ enqueue_latency()

const LatencyHistogram & kcenon::thread::metrics::EnhancedThreadPoolMetrics::enqueue_latency ( ) const
nodiscard

Get the enqueue latency histogram (read-only).

Returns
Reference to the enqueue latency histogram.

Definition at line 166 of file enhanced_metrics.cpp.

References enqueue_latency_.

◆ execution_latency()

const LatencyHistogram & kcenon::thread::metrics::EnhancedThreadPoolMetrics::execution_latency ( ) const
nodiscard

Get the execution latency histogram (read-only).

Returns
Reference to the execution latency histogram.

Definition at line 170 of file enhanced_metrics.cpp.

References execution_latency_.

◆ ns_to_us()

static double kcenon::thread::metrics::EnhancedThreadPoolMetrics::ns_to_us ( double ns)
inlinestaticnodiscardprivate

Definition at line 433 of file enhanced_metrics.h.

433{ return ns / 1000.0; }

Referenced by snapshot().

Here is the caller graph for this function:

◆ operator=() [1/2]

EnhancedThreadPoolMetrics & kcenon::thread::metrics::EnhancedThreadPoolMetrics::operator= ( const EnhancedThreadPoolMetrics & )
delete

◆ operator=() [2/2]

EnhancedThreadPoolMetrics & kcenon::thread::metrics::EnhancedThreadPoolMetrics::operator= ( EnhancedThreadPoolMetrics && )
delete

◆ record_enqueue()

void kcenon::thread::metrics::EnhancedThreadPoolMetrics::record_enqueue ( std::chrono::nanoseconds latency)

Record enqueue operation latency.

Parameters
latencyThe time taken to enqueue a task.

Definition at line 23 of file enhanced_metrics.cpp.

23 {
25}
void record(std::chrono::nanoseconds value)
Record a latency value.
@ latency
Latency threshold exceeded.

References enqueue_latency_, kcenon::thread::latency, and kcenon::thread::metrics::LatencyHistogram::record().

Here is the call graph for this function:

◆ record_execution()

void kcenon::thread::metrics::EnhancedThreadPoolMetrics::record_execution ( std::chrono::nanoseconds latency,
bool success )

Record task execution completion.

Parameters
latencyThe execution duration.
successWhether the task completed successfully.

Definition at line 27 of file enhanced_metrics.cpp.

29 {
31
32 if (success) {
33 tasks_executed_.fetch_add(1, std::memory_order_relaxed);
34 } else {
35 tasks_failed_.fetch_add(1, std::memory_order_relaxed);
36 }
37
38 // Update throughput counters
41}
std::atomic< std::uint64_t > tasks_failed_
Counter for failed tasks.
std::atomic< std::uint64_t > tasks_executed_
Counter for successfully executed tasks.
void increment(std::size_t count=1)
Increment the counter.

References execution_latency_, kcenon::thread::metrics::SlidingWindowCounter::increment(), kcenon::thread::latency, kcenon::thread::metrics::LatencyHistogram::record(), kcenon::thread::success, kcenon::thread::metrics::MetricsBase::tasks_executed_, kcenon::thread::metrics::MetricsBase::tasks_failed_, throughput_1m_, and throughput_1s_.

Here is the call graph for this function:

◆ record_queue_depth()

void kcenon::thread::metrics::EnhancedThreadPoolMetrics::record_queue_depth ( std::size_t depth)

Record current queue depth.

Parameters
depthThe current number of tasks in the queue.

Definition at line 47 of file enhanced_metrics.cpp.

47 {
48 current_queue_depth_.store(depth, std::memory_order_relaxed);
49
50 // Update peak
51 auto current_peak = peak_queue_depth_.load(std::memory_order_relaxed);
52 while (depth > current_peak) {
53 if (peak_queue_depth_.compare_exchange_weak(
54 current_peak, depth,
55 std::memory_order_relaxed,
56 std::memory_order_relaxed)) {
57 break;
58 }
59 }
60
61 // Update average calculation
62 queue_depth_sum_.fetch_add(depth, std::memory_order_relaxed);
63 queue_depth_samples_.fetch_add(1, std::memory_order_relaxed);
64}

References current_queue_depth_, peak_queue_depth_, queue_depth_samples_, and queue_depth_sum_.

◆ record_submission()

void kcenon::thread::metrics::EnhancedThreadPoolMetrics::record_submission ( )

Record a task submission.

Definition at line 19 of file enhanced_metrics.cpp.

19 {
20 tasks_submitted_.fetch_add(1, std::memory_order_relaxed);
21}
std::atomic< std::uint64_t > tasks_submitted_
Counter for submitted tasks.

References kcenon::thread::metrics::MetricsBase::tasks_submitted_.

◆ record_wait_time()

void kcenon::thread::metrics::EnhancedThreadPoolMetrics::record_wait_time ( std::chrono::nanoseconds wait)

Record wait time (time spent in queue).

Parameters
waitThe duration the task waited in the queue.

Definition at line 43 of file enhanced_metrics.cpp.

References kcenon::thread::metrics::LatencyHistogram::record(), and wait_time_.

Here is the call graph for this function:

◆ record_worker_state()

void kcenon::thread::metrics::EnhancedThreadPoolMetrics::record_worker_state ( std::size_t worker_id,
bool busy,
std::uint64_t duration_ns = 0 )

Update worker state.

Parameters
worker_idThe worker's identifier.
busyWhether the worker is currently busy.
duration_nsDuration in the previous state (for utilization calc).

Definition at line 66 of file enhanced_metrics.cpp.

69 {
70 // Update global totals
71 if (busy) {
72 total_busy_time_ns_.fetch_add(duration_ns, std::memory_order_relaxed);
73 } else {
74 total_idle_time_ns_.fetch_add(duration_ns, std::memory_order_relaxed);
75 }
76
77 // Update per-worker metrics
78 std::lock_guard<std::mutex> lock(workers_mutex_);
79 if (worker_id < per_worker_metrics_.size()) {
80 auto& worker = per_worker_metrics_[worker_id];
81 worker.is_busy = busy;
82 if (busy) {
83 worker.busy_time_ns += duration_ns;
84 } else {
85 worker.idle_time_ns += duration_ns;
86 }
87 if (!busy && duration_ns > 0) {
88 // Task completed
89 worker.tasks_executed++;
90 }
91 }
92}
std::atomic< std::uint64_t > total_idle_time_ns_
Accumulated idle time in nanoseconds.
std::atomic< std::uint64_t > total_busy_time_ns_
Accumulated busy time in nanoseconds.

References per_worker_metrics_, kcenon::thread::metrics::MetricsBase::total_busy_time_ns_, kcenon::thread::metrics::MetricsBase::total_idle_time_ns_, and workers_mutex_.

◆ reset()

void kcenon::thread::metrics::EnhancedThreadPoolMetrics::reset ( )
overridevirtual

Reset all metrics to initial state.

Clears all histograms, counters, and per-worker stats.

Reimplemented from kcenon::thread::metrics::MetricsBase.

Definition at line 191 of file enhanced_metrics.cpp.

191 {
192 // Reset base class counters first
194
195 // Reset histograms
199
200 // Reset throughput counters
203
204 // Reset queue depth tracking
205 current_queue_depth_.store(0, std::memory_order_relaxed);
206 peak_queue_depth_.store(0, std::memory_order_relaxed);
207 queue_depth_sum_.store(0, std::memory_order_relaxed);
208 queue_depth_samples_.store(0, std::memory_order_relaxed);
209
210 // Reset per-worker metrics
211 {
212 std::lock_guard<std::mutex> lock(workers_mutex_);
213 for (auto& worker : per_worker_metrics_) {
214 worker.tasks_executed = 0;
215 worker.busy_time_ns = 0;
216 worker.idle_time_ns = 0;
217 worker.is_busy = false;
218 }
219 }
220}
void reset()
Reset all buckets and counters to zero.
virtual void reset()
Reset all metrics to their initial state.

References current_queue_depth_, enqueue_latency_, execution_latency_, peak_queue_depth_, per_worker_metrics_, queue_depth_samples_, queue_depth_sum_, kcenon::thread::metrics::LatencyHistogram::reset(), kcenon::thread::metrics::MetricsBase::reset(), kcenon::thread::metrics::SlidingWindowCounter::reset(), throughput_1m_, throughput_1s_, wait_time_, and workers_mutex_.

Here is the call graph for this function:

◆ set_active_workers()

void kcenon::thread::metrics::EnhancedThreadPoolMetrics::set_active_workers ( std::size_t count)

Set the number of active workers.

Parameters
countNumber of currently active workers.

Definition at line 94 of file enhanced_metrics.cpp.

94 {
95 active_workers_.store(count, std::memory_order_relaxed);
96}

References active_workers_.

◆ snapshot()

EnhancedSnapshot kcenon::thread::metrics::EnhancedThreadPoolMetrics::snapshot ( ) const
nodiscard

Get a comprehensive snapshot of all metrics.

Returns
EnhancedSnapshot with current metric values.

This method is thread-safe and provides a consistent view of all metrics.

Definition at line 98 of file enhanced_metrics.cpp.

98 {
99 EnhancedSnapshot snap;
100 snap.snapshot_time = std::chrono::steady_clock::now();
101
102 // Basic counters (from MetricsBase)
103 snap.tasks_submitted = tasks_submitted();
104 snap.tasks_executed = tasks_executed();
105 snap.tasks_failed = tasks_failed();
106
107 // Latency percentiles (convert ns to μs)
108 snap.enqueue_latency_p50_us = ns_to_us(enqueue_latency_.p50());
109 snap.enqueue_latency_p90_us = ns_to_us(enqueue_latency_.p90());
110 snap.enqueue_latency_p99_us = ns_to_us(enqueue_latency_.p99());
111
112 snap.execution_latency_p50_us = ns_to_us(execution_latency_.p50());
113 snap.execution_latency_p90_us = ns_to_us(execution_latency_.p90());
114 snap.execution_latency_p99_us = ns_to_us(execution_latency_.p99());
115
116 snap.wait_time_p50_us = ns_to_us(wait_time_.p50());
117 snap.wait_time_p90_us = ns_to_us(wait_time_.p90());
118 snap.wait_time_p99_us = ns_to_us(wait_time_.p99());
119
120 // Throughput
121 snap.throughput_1s = throughput_1s_.rate_per_second();
122 snap.throughput_1m = throughput_1m_.rate_per_second();
123
124 // Queue health
125 snap.current_queue_depth = current_queue_depth_.load(std::memory_order_relaxed);
126 snap.peak_queue_depth = peak_queue_depth_.load(std::memory_order_relaxed);
127
128 auto samples = queue_depth_samples_.load(std::memory_order_relaxed);
129 if (samples > 0) {
130 snap.avg_queue_depth =
131 static_cast<double>(queue_depth_sum_.load(std::memory_order_relaxed)) /
132 static_cast<double>(samples);
133 }
134
135 // Worker utilization (from MetricsBase)
136 snap.total_busy_time_ns = total_busy_time_ns();
137 snap.total_idle_time_ns = total_idle_time_ns();
138 snap.active_workers = active_workers_.load(std::memory_order_relaxed);
139
140 auto total_time = snap.total_busy_time_ns + snap.total_idle_time_ns;
141 if (total_time > 0) {
142 snap.worker_utilization =
143 static_cast<double>(snap.total_busy_time_ns) /
144 static_cast<double>(total_time);
145 }
146
147 // Per-worker utilization
148 {
149 std::lock_guard<std::mutex> lock(workers_mutex_);
150 snap.per_worker_utilization.reserve(per_worker_metrics_.size());
151 for (const auto& worker : per_worker_metrics_) {
152 auto worker_total = worker.busy_time_ns + worker.idle_time_ns;
153 if (worker_total > 0) {
154 snap.per_worker_utilization.push_back(
155 static_cast<double>(worker.busy_time_ns) /
156 static_cast<double>(worker_total));
157 } else {
158 snap.per_worker_utilization.push_back(0.0);
159 }
160 }
161 }
162
163 return snap;
164}
double p50() const
Get the 50th percentile (median).
double p90() const
Get the 90th percentile.
double p99() const
Get the 99th percentile.
std::uint64_t total_busy_time_ns() const
Get the total busy time in nanoseconds.
std::uint64_t tasks_executed() const
Get the total number of tasks successfully executed.
std::uint64_t tasks_submitted() const
Get the total number of tasks submitted.
std::uint64_t tasks_failed() const
Get the total number of failed tasks.
std::uint64_t total_idle_time_ns() const
Get the total idle time in nanoseconds.
double rate_per_second() const
Get the current rate per second.

References kcenon::thread::metrics::EnhancedSnapshot::active_workers, active_workers_, kcenon::thread::metrics::EnhancedSnapshot::avg_queue_depth, kcenon::thread::metrics::EnhancedSnapshot::current_queue_depth, current_queue_depth_, enqueue_latency_, kcenon::thread::metrics::EnhancedSnapshot::enqueue_latency_p50_us, kcenon::thread::metrics::EnhancedSnapshot::enqueue_latency_p90_us, kcenon::thread::metrics::EnhancedSnapshot::enqueue_latency_p99_us, execution_latency_, kcenon::thread::metrics::EnhancedSnapshot::execution_latency_p50_us, kcenon::thread::metrics::EnhancedSnapshot::execution_latency_p90_us, kcenon::thread::metrics::EnhancedSnapshot::execution_latency_p99_us, ns_to_us(), kcenon::thread::metrics::LatencyHistogram::p50(), kcenon::thread::metrics::LatencyHistogram::p90(), kcenon::thread::metrics::LatencyHistogram::p99(), kcenon::thread::metrics::EnhancedSnapshot::peak_queue_depth, peak_queue_depth_, per_worker_metrics_, kcenon::thread::metrics::EnhancedSnapshot::per_worker_utilization, queue_depth_samples_, queue_depth_sum_, kcenon::thread::metrics::SlidingWindowCounter::rate_per_second(), kcenon::thread::metrics::EnhancedSnapshot::snapshot_time, kcenon::thread::metrics::EnhancedSnapshot::tasks_executed, kcenon::thread::metrics::MetricsBase::tasks_executed(), kcenon::thread::metrics::EnhancedSnapshot::tasks_failed, kcenon::thread::metrics::MetricsBase::tasks_failed(), kcenon::thread::metrics::EnhancedSnapshot::tasks_submitted, kcenon::thread::metrics::MetricsBase::tasks_submitted(), kcenon::thread::metrics::EnhancedSnapshot::throughput_1m, throughput_1m_, kcenon::thread::metrics::EnhancedSnapshot::throughput_1s, throughput_1s_, kcenon::thread::metrics::EnhancedSnapshot::total_busy_time_ns, kcenon::thread::metrics::MetricsBase::total_busy_time_ns(), kcenon::thread::metrics::EnhancedSnapshot::total_idle_time_ns, kcenon::thread::metrics::MetricsBase::total_idle_time_ns(), wait_time_, kcenon::thread::metrics::EnhancedSnapshot::wait_time_p50_us, kcenon::thread::metrics::EnhancedSnapshot::wait_time_p90_us, kcenon::thread::metrics::EnhancedSnapshot::wait_time_p99_us, kcenon::thread::metrics::EnhancedSnapshot::worker_utilization, and workers_mutex_.

Referenced by to_json(), and to_prometheus().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ throughput_1m()

const SlidingWindowCounter & kcenon::thread::metrics::EnhancedThreadPoolMetrics::throughput_1m ( ) const
nodiscard

Get the 1-minute throughput counter (read-only).

Returns
Reference to the 1-minute throughput counter.

Definition at line 187 of file enhanced_metrics.cpp.

187 {
188 return throughput_1m_;
189}

References throughput_1m_.

◆ throughput_1s()

const SlidingWindowCounter & kcenon::thread::metrics::EnhancedThreadPoolMetrics::throughput_1s ( ) const
nodiscard

Get the 1-second throughput counter (read-only).

Returns
Reference to the 1-second throughput counter.

Definition at line 183 of file enhanced_metrics.cpp.

183 {
184 return throughput_1s_;
185}

References throughput_1s_.

◆ to_json()

std::string kcenon::thread::metrics::EnhancedThreadPoolMetrics::to_json ( ) const
nodiscard

Export metrics as JSON string.

Returns
JSON representation of the current metrics.

Definition at line 233 of file enhanced_metrics.cpp.

233 {
234 // Delegate to JsonBackend for consistent output format
235 auto backend = BackendRegistry::instance().get("json");
236 if (backend) {
237 return backend->export_enhanced(snapshot());
238 }
239
240 // Fallback if registry is not initialized (shouldn't happen)
241 JsonBackend fallback;
242 return fallback.export_enhanced(snapshot());
243}
static BackendRegistry & instance()
Get the singleton instance.
std::shared_ptr< MetricsBackend > get(const std::string &name) const
Get a backend by name.
EnhancedSnapshot snapshot() const
Get a comprehensive snapshot of all metrics.
@ fallback
Execute fallback job if available.

References kcenon::thread::metrics::JsonBackend::export_enhanced(), kcenon::thread::fallback, kcenon::thread::metrics::BackendRegistry::get(), kcenon::thread::metrics::BackendRegistry::instance(), and snapshot().

Here is the call graph for this function:

◆ to_prometheus()

std::string kcenon::thread::metrics::EnhancedThreadPoolMetrics::to_prometheus ( const std::string & prefix = "thread_pool") const
nodiscard

Export metrics in Prometheus/OpenMetrics format.

Parameters
prefixMetric name prefix (e.g., "thread_pool").
Returns
Prometheus-compatible metrics string.

Definition at line 245 of file enhanced_metrics.cpp.

246 {
247 // Create a PrometheusBackend with the specified prefix
248 PrometheusBackend backend;
249 backend.set_prefix(prefix);
250 return backend.export_enhanced(snapshot());
251}

References kcenon::thread::metrics::PrometheusBackend::export_enhanced(), kcenon::thread::metrics::MetricsBackend::set_prefix(), and snapshot().

Here is the call graph for this function:

◆ update_worker_count()

void kcenon::thread::metrics::EnhancedThreadPoolMetrics::update_worker_count ( std::size_t count)

Update worker count.

Parameters
countNew number of workers to track.

Call this when the thread pool scales up or down.

Definition at line 222 of file enhanced_metrics.cpp.

222 {
223 std::lock_guard<std::mutex> lock(workers_mutex_);
224 if (count > per_worker_metrics_.size()) {
225 auto old_size = per_worker_metrics_.size();
226 per_worker_metrics_.resize(count);
227 for (std::size_t i = old_size; i < count; ++i) {
228 per_worker_metrics_[i].worker_id = i;
229 }
230 }
231}

References per_worker_metrics_, and workers_mutex_.

◆ wait_time()

const LatencyHistogram & kcenon::thread::metrics::EnhancedThreadPoolMetrics::wait_time ( ) const
nodiscard

Get the wait time histogram (read-only).

Returns
Reference to the wait time histogram.

Definition at line 174 of file enhanced_metrics.cpp.

174 {
175 return wait_time_;
176}

References wait_time_.

◆ worker_metrics()

std::vector< WorkerMetrics > kcenon::thread::metrics::EnhancedThreadPoolMetrics::worker_metrics ( ) const
nodiscard

Get per-worker metrics.

Returns
Vector of WorkerMetrics for each tracked worker.

Definition at line 178 of file enhanced_metrics.cpp.

178 {
179 std::lock_guard<std::mutex> lock(workers_mutex_);
180 return per_worker_metrics_;
181}

References per_worker_metrics_, and workers_mutex_.

Member Data Documentation

◆ active_workers_

std::atomic<std::size_t> kcenon::thread::metrics::EnhancedThreadPoolMetrics::active_workers_ {0}
private

Definition at line 426 of file enhanced_metrics.h.

426{0};

Referenced by set_active_workers(), and snapshot().

◆ current_queue_depth_

std::atomic<std::size_t> kcenon::thread::metrics::EnhancedThreadPoolMetrics::current_queue_depth_ {0}
private

Definition at line 420 of file enhanced_metrics.h.

420{0};

Referenced by record_queue_depth(), reset(), and snapshot().

◆ enqueue_latency_

LatencyHistogram kcenon::thread::metrics::EnhancedThreadPoolMetrics::enqueue_latency_
private

Definition at line 408 of file enhanced_metrics.h.

Referenced by enqueue_latency(), record_enqueue(), reset(), and snapshot().

◆ execution_latency_

LatencyHistogram kcenon::thread::metrics::EnhancedThreadPoolMetrics::execution_latency_
private

Definition at line 409 of file enhanced_metrics.h.

Referenced by execution_latency(), record_execution(), reset(), and snapshot().

◆ peak_queue_depth_

std::atomic<std::size_t> kcenon::thread::metrics::EnhancedThreadPoolMetrics::peak_queue_depth_ {0}
private

Definition at line 421 of file enhanced_metrics.h.

421{0};

Referenced by record_queue_depth(), reset(), and snapshot().

◆ per_worker_metrics_

std::vector<WorkerMetrics> kcenon::thread::metrics::EnhancedThreadPoolMetrics::per_worker_metrics_
private

◆ queue_depth_samples_

std::atomic<std::uint64_t> kcenon::thread::metrics::EnhancedThreadPoolMetrics::queue_depth_samples_ {0}
private

Definition at line 423 of file enhanced_metrics.h.

423{0};

Referenced by record_queue_depth(), reset(), and snapshot().

◆ queue_depth_sum_

std::atomic<std::uint64_t> kcenon::thread::metrics::EnhancedThreadPoolMetrics::queue_depth_sum_ {0}
private

Definition at line 422 of file enhanced_metrics.h.

422{0};

Referenced by record_queue_depth(), reset(), and snapshot().

◆ throughput_1m_

SlidingWindowCounter kcenon::thread::metrics::EnhancedThreadPoolMetrics::throughput_1m_
private

Definition at line 414 of file enhanced_metrics.h.

Referenced by record_execution(), reset(), snapshot(), and throughput_1m().

◆ throughput_1s_

SlidingWindowCounter kcenon::thread::metrics::EnhancedThreadPoolMetrics::throughput_1s_
private

Definition at line 413 of file enhanced_metrics.h.

Referenced by record_execution(), reset(), snapshot(), and throughput_1s().

◆ wait_time_

LatencyHistogram kcenon::thread::metrics::EnhancedThreadPoolMetrics::wait_time_
private

Definition at line 410 of file enhanced_metrics.h.

Referenced by record_wait_time(), reset(), snapshot(), and wait_time().

◆ workers_mutex_

std::mutex kcenon::thread::metrics::EnhancedThreadPoolMetrics::workers_mutex_
mutableprivate

The documentation for this class was generated from the following files: