Thread System 0.3.1
High-performance C++20 thread pool with work stealing and DAG scheduling
Loading...
Searching...
No Matches
metrics_base.h
Go to the documentation of this file.
1// BSD 3-Clause License
2// Copyright (c) 2024, 🍀☀🌕🌥 🌊
3// See the LICENSE file in the project root for full license information.
4
12#pragma once
13
19 // end of metrics
20
21#include <atomic>
22#include <cstdint>
23
25
36 std::uint64_t tasks_submitted{0};
37
41 std::uint64_t tasks_executed{0};
42
46 std::uint64_t tasks_failed{0};
47
51 std::uint64_t total_busy_time_ns{0};
52
56 std::uint64_t total_idle_time_ns{0};
57};
58
85public:
89 virtual ~MetricsBase() = default;
90
91 // =========================================================================
92 // Recording Methods
93 // =========================================================================
94
99 void record_submission(std::size_t count = 1) {
100 tasks_submitted_.fetch_add(count, std::memory_order_relaxed);
101 }
102
108 void record_execution(std::uint64_t duration_ns, bool success) {
109 if (success) {
110 tasks_executed_.fetch_add(1, std::memory_order_relaxed);
111 } else {
112 tasks_failed_.fetch_add(1, std::memory_order_relaxed);
113 }
114 total_busy_time_ns_.fetch_add(duration_ns, std::memory_order_relaxed);
115 }
116
121 void record_idle_time(std::uint64_t duration_ns) {
122 total_idle_time_ns_.fetch_add(duration_ns, std::memory_order_relaxed);
123 }
124
131 virtual void reset() {
132 tasks_submitted_.store(0, std::memory_order_relaxed);
133 tasks_executed_.store(0, std::memory_order_relaxed);
134 tasks_failed_.store(0, std::memory_order_relaxed);
135 total_busy_time_ns_.store(0, std::memory_order_relaxed);
136 total_idle_time_ns_.store(0, std::memory_order_relaxed);
137 }
138
139 // =========================================================================
140 // Query Methods
141 // =========================================================================
142
147 [[nodiscard]] std::uint64_t tasks_submitted() const {
148 return tasks_submitted_.load(std::memory_order_relaxed);
149 }
150
155 [[nodiscard]] std::uint64_t tasks_executed() const {
156 return tasks_executed_.load(std::memory_order_relaxed);
157 }
158
163 [[nodiscard]] std::uint64_t tasks_failed() const {
164 return tasks_failed_.load(std::memory_order_relaxed);
165 }
166
171 [[nodiscard]] std::uint64_t total_busy_time_ns() const {
172 return total_busy_time_ns_.load(std::memory_order_relaxed);
173 }
174
179 [[nodiscard]] std::uint64_t total_idle_time_ns() const {
180 return total_idle_time_ns_.load(std::memory_order_relaxed);
181 }
182
187 [[nodiscard]] BaseSnapshot base_snapshot() const {
188 return BaseSnapshot{
189 tasks_submitted_.load(std::memory_order_relaxed),
190 tasks_executed_.load(std::memory_order_relaxed),
191 tasks_failed_.load(std::memory_order_relaxed),
192 total_busy_time_ns_.load(std::memory_order_relaxed),
193 total_idle_time_ns_.load(std::memory_order_relaxed),
194 };
195 }
196
203 [[nodiscard]] double utilization() const {
204 const auto busy = total_busy_time_ns_.load(std::memory_order_relaxed);
205 const auto idle = total_idle_time_ns_.load(std::memory_order_relaxed);
206 const auto total = busy + idle;
207 return (total > 0) ? static_cast<double>(busy) / static_cast<double>(total) : 0.0;
208 }
209
216 [[nodiscard]] double success_rate() const {
217 const auto executed = tasks_executed_.load(std::memory_order_relaxed);
218 const auto failed = tasks_failed_.load(std::memory_order_relaxed);
219 const auto total = executed + failed;
220 return (total > 0) ? static_cast<double>(executed) / static_cast<double>(total) : 1.0;
221 }
222
223protected:
227 MetricsBase() = default;
228
232 MetricsBase(const MetricsBase&) = delete;
233
238
243
248
249 // =========================================================================
250 // Protected Members (accessible to derived classes)
251 // =========================================================================
252
256 std::atomic<std::uint64_t> tasks_submitted_{0};
257
261 std::atomic<std::uint64_t> tasks_executed_{0};
262
266 std::atomic<std::uint64_t> tasks_failed_{0};
267
271 std::atomic<std::uint64_t> total_busy_time_ns_{0};
272
276 std::atomic<std::uint64_t> total_idle_time_ns_{0};
277};
278
279} // namespace kcenon::thread::metrics
Abstract base class for thread pool metrics.
void record_idle_time(std::uint64_t duration_ns)
Record idle time.
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.
MetricsBase & operator=(MetricsBase &&)=delete
Move assignment operator (deleted for thread safety).
std::uint64_t tasks_executed() const
Get the total number of tasks successfully executed.
virtual ~MetricsBase()=default
Virtual destructor for proper cleanup in derived classes.
MetricsBase & operator=(const MetricsBase &)=delete
Copy assignment operator (deleted for thread safety).
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.
MetricsBase(MetricsBase &&)=delete
Move constructor (deleted for thread safety).
MetricsBase(const MetricsBase &)=delete
Copy constructor (deleted for thread safety).
std::uint64_t tasks_failed() const
Get the total number of failed tasks.
double success_rate() const
Calculate task success rate.
BaseSnapshot base_snapshot() const
Get a base snapshot of common metrics.
std::atomic< std::uint64_t > total_busy_time_ns_
Accumulated busy time in nanoseconds.
MetricsBase()=default
Default constructor.
std::atomic< std::uint64_t > tasks_submitted_
Counter for submitted tasks.
double utilization() const
Calculate worker utilization ratio.
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.
void record_submission(std::size_t count=1)
Record task submission(s).
virtual void reset()
Reset all metrics to their initial state.
void record_execution(std::uint64_t duration_ns, bool success)
Record task execution completion.
@ idle
Worker is idle, waiting for work.
@ failed
Execution failed.
Base snapshot structure containing common metric values.
std::uint64_t tasks_executed
Total tasks successfully executed.
std::uint64_t tasks_failed
Total tasks that failed during execution.
std::uint64_t total_idle_time_ns
Total idle time across all workers in nanoseconds.
std::uint64_t total_busy_time_ns
Total busy time across all workers in nanoseconds.
std::uint64_t tasks_submitted
Total tasks submitted to the pool.