Thread System 0.3.1
High-performance C++20 thread pool with work stealing and DAG scheduling
Loading...
Searching...
No Matches
autoscaler.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
14#include "autoscaling_policy.h"
15#include "scaling_metrics.h"
16
18
19#include <atomic>
20#include <chrono>
21#include <condition_variable>
22#include <deque>
23#include <memory>
24#include <mutex>
25#include <thread>
26
27namespace kcenon::thread
28{
29 // Forward declaration
30 class thread_pool;
31
95 {
96 public:
102 explicit autoscaler(thread_pool& pool, autoscaling_policy policy = {});
103
107 ~autoscaler();
108
109 // Non-copyable, non-movable
110 autoscaler(const autoscaler&) = delete;
111 autoscaler& operator=(const autoscaler&) = delete;
114
115 // ============================================
116 // Control
117 // ============================================
118
125 auto start() -> void;
126
132 auto stop() -> void;
133
138 [[nodiscard]] auto is_active() const -> bool;
139
140 // ============================================
141 // Manual Triggers
142 // ============================================
143
151 [[nodiscard]] auto evaluate_now() -> scaling_decision;
152
160 auto scale_to(std::size_t target_workers) -> common::VoidResult;
161
166 auto scale_up() -> common::VoidResult;
167
172 auto scale_down() -> common::VoidResult;
173
174 // ============================================
175 // Configuration
176 // ============================================
177
182 auto set_policy(autoscaling_policy policy) -> void;
183
188 [[nodiscard]] auto get_policy() const -> const autoscaling_policy&;
189
190 // ============================================
191 // Metrics & Statistics
192 // ============================================
193
198 [[nodiscard]] auto get_current_metrics() const -> scaling_metrics_sample;
199
205 [[nodiscard]] auto get_metrics_history(std::size_t count = 60) const
206 -> std::vector<scaling_metrics_sample>;
207
212 [[nodiscard]] auto get_stats() const -> autoscaling_stats;
213
217 auto reset_stats() -> void;
218
219 private:
223 auto monitor_loop() -> void;
224
229 [[nodiscard]] auto collect_metrics() const -> scaling_metrics_sample;
230
236 [[nodiscard]] auto make_decision(const std::vector<scaling_metrics_sample>& samples) const
238
243 auto execute_scaling(const scaling_decision& decision) -> void;
244
249 [[nodiscard]] auto can_scale_up() const -> bool;
250
255 [[nodiscard]] auto can_scale_down() const -> bool;
256
262 auto add_workers(std::size_t count) -> common::VoidResult;
263
269 auto remove_workers(std::size_t count) -> common::VoidResult;
270
273
274 std::atomic<bool> running_{false};
275 std::unique_ptr<std::thread> monitor_thread_;
276
277 mutable std::mutex mutex_;
278 std::condition_variable cv_;
279
280 std::deque<scaling_metrics_sample> metrics_history_;
281 mutable std::mutex history_mutex_;
282
283 std::chrono::steady_clock::time_point last_scale_up_time_;
284 std::chrono::steady_clock::time_point last_scale_down_time_;
285
287 mutable std::mutex stats_mutex_;
288
289 // Cached values from previous sample for delta calculations
290 std::uint64_t last_jobs_completed_{0};
291 std::uint64_t last_jobs_submitted_{0};
292 std::chrono::steady_clock::time_point last_sample_time_;
293 };
294
295} // namespace kcenon::thread
Configuration for autoscaling behavior and thresholds.
Manages automatic scaling of thread pool workers based on load metrics.
Definition autoscaler.h:95
auto collect_metrics() const -> scaling_metrics_sample
Collects current metrics from the pool.
auto can_scale_down() const -> bool
Checks if scale-down cooldown has elapsed.
auto scale_to(std::size_t target_workers) -> common::VoidResult
Manually scales to a specific worker count.
std::uint64_t last_jobs_submitted_
Definition autoscaler.h:291
std::atomic< bool > running_
Definition autoscaler.h:274
std::chrono::steady_clock::time_point last_sample_time_
Definition autoscaler.h:292
std::condition_variable cv_
Definition autoscaler.h:278
auto is_active() const -> bool
Checks if the autoscaler is currently active.
auto make_decision(const std::vector< scaling_metrics_sample > &samples) const -> scaling_decision
Makes a scaling decision based on recent samples.
auto get_stats() const -> autoscaling_stats
Gets autoscaling statistics.
std::unique_ptr< std::thread > monitor_thread_
Definition autoscaler.h:275
auto scale_up() -> common::VoidResult
Manually scales up by the configured increment.
autoscaling_policy policy_
Definition autoscaler.h:272
autoscaler(const autoscaler &)=delete
auto get_metrics_history(std::size_t count=60) const -> std::vector< scaling_metrics_sample >
Gets historical metrics samples.
auto execute_scaling(const scaling_decision &decision) -> void
Executes a scaling decision.
auto evaluate_now() -> scaling_decision
Manually triggers a scaling evaluation.
std::chrono::steady_clock::time_point last_scale_down_time_
Definition autoscaler.h:284
autoscaling_stats stats_
Definition autoscaler.h:286
auto start() -> void
Starts the autoscaling monitor thread.
std::deque< scaling_metrics_sample > metrics_history_
Definition autoscaler.h:280
auto stop() -> void
Stops the autoscaling monitor thread.
autoscaler & operator=(const autoscaler &)=delete
~autoscaler()
Destructor. Stops the monitor thread if running.
auto scale_down() -> common::VoidResult
Manually scales down by the configured increment.
std::uint64_t last_jobs_completed_
Definition autoscaler.h:290
auto reset_stats() -> void
Resets autoscaling statistics.
auto get_current_metrics() const -> scaling_metrics_sample
Collects current metrics from the thread pool.
autoscaler(autoscaler &&)=delete
auto can_scale_up() const -> bool
Checks if scale-up cooldown has elapsed.
auto get_policy() const -> const autoscaling_policy &
Gets the current autoscaling policy.
auto add_workers(std::size_t count) -> common::VoidResult
Adds workers to the pool.
autoscaler(thread_pool &pool, autoscaling_policy policy={})
Constructs an autoscaler for the given thread pool.
auto remove_workers(std::size_t count) -> common::VoidResult
Removes workers from the pool.
auto set_policy(autoscaling_policy policy) -> void
Updates the autoscaling policy.
std::chrono::steady_clock::time_point last_scale_up_time_
Definition autoscaler.h:283
autoscaler & operator=(autoscaler &&)=delete
auto monitor_loop() -> void
Main monitoring loop running in the background thread.
A thread pool for concurrent execution of jobs using multiple worker threads.
Error codes and utilities for the thread system.
Core threading foundation of the thread system library.
Definition thread_impl.h:17
STL namespace.
Scaling direction and metrics for autoscaling decisions.
Configuration for autoscaling behavior.
Statistics for autoscaling operations.
Scaling decision result.
Metrics sample for autoscaling decisions.