Thread System 0.3.1
High-performance C++20 thread pool with work stealing and DAG scheduling
Loading...
Searching...
No Matches
autoscaling_pool_policy.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
13#pragma once
14
15#include "pool_policy.h"
18
19#include <atomic>
20#include <memory>
21#include <string>
22
23namespace kcenon::thread
24{
60 {
61 public:
67 explicit autoscaling_pool_policy(thread_pool& pool, const autoscaling_policy& config = {});
68
75 explicit autoscaling_pool_policy(std::shared_ptr<autoscaler> scaler);
76
80 ~autoscaling_pool_policy() override;
81
82 // Non-copyable
85
86 // Non-movable (std::atomic is not movable)
89
90 // ============================================
91 // pool_policy Interface
92 // ============================================
93
101 auto on_enqueue(job& j) -> common::VoidResult override;
102
109 void on_job_start(job& j) override;
110
119 void on_job_complete(job& j, bool success, const std::exception* error = nullptr) override;
120
125 [[nodiscard]] auto get_name() const -> std::string override;
126
131 [[nodiscard]] auto is_enabled() const -> bool override;
132
140 void set_enabled(bool enabled) override;
141
142 // ============================================
143 // Autoscaling Specific Methods
144 // ============================================
145
152 void start();
153
157 void stop();
158
163 [[nodiscard]] auto is_active() const -> bool;
164
171 [[nodiscard]] auto get_autoscaler() const -> std::shared_ptr<autoscaler>;
172
177 [[nodiscard]] auto get_stats() const -> autoscaling_stats;
178
183 void set_policy(const autoscaling_policy& config);
184
189 [[nodiscard]] auto get_policy() const -> const autoscaling_policy&;
190
195 [[nodiscard]] auto evaluate_now() -> scaling_decision;
196
202 auto scale_to(std::size_t target_workers) -> common::VoidResult;
203
204 private:
206 std::atomic<bool> enabled_{true};
207 };
208
209} // namespace kcenon::thread
Automatic scaling of thread pool workers based on load metrics.
Configuration for autoscaling behavior and thresholds.
Manages automatic scaling of thread pool workers based on load metrics.
Definition autoscaler.h:95
Pool policy that implements automatic scaling for dynamic worker management.
autoscaling_pool_policy & operator=(const autoscaling_pool_policy &)=delete
auto get_stats() const -> autoscaling_stats
Gets current autoscaling statistics.
auto get_autoscaler() const -> std::shared_ptr< autoscaler >
Gets the underlying autoscaler.
autoscaling_pool_policy(thread_pool &pool, const autoscaling_policy &config={})
Constructs an autoscaling pool policy with the given configuration.
autoscaling_pool_policy(autoscaling_pool_policy &&)=delete
auto scale_to(std::size_t target_workers) -> common::VoidResult
Manually scales to a specific worker count.
void set_policy(const autoscaling_policy &config)
Updates the autoscaling policy configuration.
void start()
Starts the autoscaler monitor thread.
auto get_name() const -> std::string override
Gets the policy name.
~autoscaling_pool_policy() override
Destructor. Stops the autoscaler if running.
void set_enabled(bool enabled) override
Enables or disables the policy.
autoscaling_pool_policy(const autoscaling_pool_policy &)=delete
auto evaluate_now() -> scaling_decision
Manually triggers a scaling evaluation.
auto is_enabled() const -> bool override
Checks if the policy is enabled.
auto is_active() const -> bool
Checks if the autoscaler is currently active.
void on_job_start(job &j) override
Called when job starts executing.
autoscaling_pool_policy & operator=(autoscaling_pool_policy &&)=delete
void on_job_complete(job &j, bool success, const std::exception *error=nullptr) override
Called when a job completes.
auto on_enqueue(job &j) -> common::VoidResult override
Called before a job is enqueued.
void stop()
Stops the autoscaler monitor thread.
auto get_policy() const -> const autoscaling_policy &
Gets the current autoscaling policy configuration.
Represents an error in the thread system.
Represents a unit of work (task) to be executed, typically by a job queue.
Definition job.h:136
Base interface for thread pool policies.
Definition pool_policy.h:81
A thread pool for concurrent execution of jobs using multiple worker threads.
Core threading foundation of the thread system library.
Definition thread_impl.h:17
STL namespace.
Base interface for extensible thread pool behavior policies.
Configuration for autoscaling behavior.
Statistics for autoscaling operations.
Scaling decision result.