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) {
28 std::chrono::nanoseconds
latency,
52 while (depth > current_peak) {
55 std::memory_order_relaxed,
56 std::memory_order_relaxed)) {
67 std::size_t worker_id,
69 std::uint64_t duration_ns) {
81 worker.is_busy = busy;
83 worker.busy_time_ns += duration_ns;
85 worker.idle_time_ns += duration_ns;
87 if (!busy && duration_ns > 0) {
89 worker.tasks_executed++;
132 static_cast<double>(samples);
141 if (total_time > 0) {
144 static_cast<double>(total_time);
152 auto worker_total = worker.busy_time_ns + worker.idle_time_ns;
153 if (worker_total > 0) {
155 static_cast<double>(worker.busy_time_ns) /
156 static_cast<double>(worker_total));
214 worker.tasks_executed = 0;
215 worker.busy_time_ns = 0;
216 worker.idle_time_ns = 0;
217 worker.is_busy =
false;
227 for (std::size_t i = old_size; i < count; ++i) {
237 return backend->export_enhanced(
snapshot());
246 const std::string& prefix)
const {
static BackendRegistry & instance()
Get the singleton instance.
std::shared_ptr< MetricsBackend > get(const std::string &name) const
Get a backend by name.
std::atomic< std::size_t > active_workers_
void record_submission()
Record a task submission.
std::atomic< std::size_t > current_queue_depth_
const SlidingWindowCounter & throughput_1s() const
Get the 1-second throughput counter (read-only).
const LatencyHistogram & execution_latency() const
Get the execution latency histogram (read-only).
void set_active_workers(std::size_t count)
Set the number of active workers.
void record_execution(std::chrono::nanoseconds latency, bool success)
Record task execution completion.
EnhancedSnapshot snapshot() const
Get a comprehensive snapshot of all metrics.
const SlidingWindowCounter & throughput_1m() const
Get the 1-minute throughput counter (read-only).
LatencyHistogram wait_time_
std::string to_prometheus(const std::string &prefix="thread_pool") const
Export metrics in Prometheus/OpenMetrics format.
SlidingWindowCounter throughput_1s_
std::vector< WorkerMetrics > worker_metrics() const
Get per-worker metrics.
LatencyHistogram execution_latency_
void record_worker_state(std::size_t worker_id, bool busy, std::uint64_t duration_ns=0)
Update worker state.
std::atomic< std::uint64_t > queue_depth_sum_
void record_wait_time(std::chrono::nanoseconds wait)
Record wait time (time spent in queue).
std::string to_json() const
Export metrics as JSON string.
std::atomic< std::size_t > peak_queue_depth_
std::vector< WorkerMetrics > per_worker_metrics_
void reset() override
Reset all metrics to initial state.
std::atomic< std::uint64_t > queue_depth_samples_
const LatencyHistogram & wait_time() const
Get the wait time histogram (read-only).
const LatencyHistogram & enqueue_latency() const
Get the enqueue latency histogram (read-only).
static double ns_to_us(double ns)
void record_enqueue(std::chrono::nanoseconds latency)
Record enqueue operation latency.
LatencyHistogram enqueue_latency_
EnhancedThreadPoolMetrics(std::size_t worker_count=0)
Constructs enhanced metrics with the specified worker count.
void update_worker_count(std::size_t count)
Update worker count.
SlidingWindowCounter throughput_1m_
void record_queue_depth(std::size_t depth)
Record current queue depth.
std::mutex workers_mutex_
std::string export_enhanced(const EnhancedSnapshot &snapshot) const override
Export enhanced metrics snapshot.
HDR-style histogram for latency distribution with logarithmic buckets.
void record(std::chrono::nanoseconds value)
Record a latency value.
double p50() const
Get the 50th percentile (median).
double p90() const
Get the 90th percentile.
double p99() const
Get the 99th percentile.
void reset()
Reset all buckets and counters to zero.
virtual void set_prefix(const std::string &prefix)
Set metric name prefix.
std::uint64_t total_busy_time_ns() const
Get the total busy time in nanoseconds.
std::atomic< std::uint64_t > tasks_failed_
Counter for failed tasks.
std::uint64_t tasks_executed() const
Get the total number of tasks successfully executed.
std::atomic< std::uint64_t > total_idle_time_ns_
Accumulated idle time in nanoseconds.
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::atomic< std::uint64_t > total_busy_time_ns_
Accumulated busy time in nanoseconds.
std::atomic< std::uint64_t > tasks_submitted_
Counter for submitted tasks.
std::uint64_t total_idle_time_ns() const
Get the total idle time in nanoseconds.
std::atomic< std::uint64_t > tasks_executed_
Counter for successfully executed tasks.
virtual void reset()
Reset all metrics to their initial state.
Prometheus/OpenMetrics format backend.
std::string export_enhanced(const EnhancedSnapshot &snapshot) const override
Export enhanced metrics snapshot.
Sliding window counter for throughput measurement.
void reset()
Reset the counter.
void increment(std::size_t count=1)
Increment the counter.
double rate_per_second() const
Get the current rate per second.
Enhanced metrics snapshot with latency percentiles and throughput.
Abstract interface for metrics export backends.
@ latency
Latency threshold exceeded.
@ fallback
Execute fallback job if available.
Enhanced snapshot with latency percentiles and throughput.
double throughput_1s
Tasks completed per second (1-second window).
std::size_t peak_queue_depth
Peak queue depth since last reset.
double worker_utilization
Overall worker utilization (0.0 - 1.0).
std::size_t active_workers
Number of active workers.
double enqueue_latency_p50_us
Median (P50) enqueue latency in microseconds.
std::uint64_t tasks_failed
Total tasks that failed during execution.
std::uint64_t total_busy_time_ns
Total busy time across all workers in nanoseconds.
std::chrono::steady_clock::time_point snapshot_time
Timestamp when this snapshot was taken.
double enqueue_latency_p90_us
90th percentile enqueue latency in microseconds.
double wait_time_p90_us
90th percentile wait time in microseconds.
double execution_latency_p99_us
99th percentile execution latency in microseconds.
std::uint64_t tasks_executed
Total tasks successfully executed.
std::uint64_t tasks_submitted
Total tasks submitted to the pool.
std::uint64_t total_idle_time_ns
Total idle time across all workers in nanoseconds.
double avg_queue_depth
Average queue depth over the sampling period.
double execution_latency_p50_us
Median execution latency in microseconds.
double wait_time_p50_us
Median wait time (queue time) in microseconds.
double throughput_1m
Tasks completed per second (1-minute window average).
double execution_latency_p90_us
90th percentile execution latency in microseconds.
std::size_t current_queue_depth
Current queue depth.
double wait_time_p99_us
99th percentile wait time in microseconds.
std::vector< double > per_worker_utilization
Per-worker utilization (0.0 - 1.0 each).
double enqueue_latency_p99_us
99th percentile enqueue latency in microseconds.