Thread System 0.3.1
High-performance C++20 thread pool with work stealing and DAG scheduling
Loading...
Searching...
No Matches
autoscaling_pool_policy.cpp
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
7
8namespace kcenon::thread
9{
10
12 : autoscaler_(std::make_shared<autoscaler>(pool, config))
13{
14}
15
16autoscaling_pool_policy::autoscaling_pool_policy(std::shared_ptr<autoscaler> scaler)
17 : autoscaler_(std::move(scaler))
18{
19}
20
22{
23 if (autoscaler_ && autoscaler_->is_active()) {
24 autoscaler_->stop();
25 }
26}
27
28auto autoscaling_pool_policy::on_enqueue(job& j) -> common::VoidResult
29{
30 (void)j; // Autoscaling does not reject jobs
31 return common::ok();
32}
33
35{
36 (void)j; // Metrics are collected by the autoscaler's monitor thread
37}
38
39void autoscaling_pool_policy::on_job_complete(job& j, bool success, const std::exception* error)
40{
41 (void)j;
42 (void)success;
43 (void)error;
44 // Metrics are collected by the autoscaler's monitor thread
45}
46
48{
49 return "autoscaling_pool_policy";
50}
51
53{
54 return enabled_.load(std::memory_order_acquire);
55}
56
58{
59 bool was_enabled = enabled_.exchange(enabled, std::memory_order_acq_rel);
60
61 if (autoscaler_) {
62 if (enabled && !was_enabled) {
63 // Transitioning to enabled - start the autoscaler
64 if (!autoscaler_->is_active()) {
65 autoscaler_->start();
66 }
67 } else if (!enabled && was_enabled) {
68 // Transitioning to disabled - stop the autoscaler
69 if (autoscaler_->is_active()) {
70 autoscaler_->stop();
71 }
72 }
73 }
74}
75
77{
78 if (autoscaler_ && enabled_.load(std::memory_order_acquire)) {
79 autoscaler_->start();
80 }
81}
82
84{
85 if (autoscaler_) {
86 autoscaler_->stop();
87 }
88}
89
91{
92 return autoscaler_ && autoscaler_->is_active();
93}
94
96{
97 return autoscaler_;
98}
99
101{
102 if (autoscaler_) {
103 return autoscaler_->get_stats();
104 }
105 return autoscaling_stats{};
106}
107
109{
110 if (autoscaler_) {
111 autoscaler_->set_policy(config);
112 }
113}
114
116{
117 static autoscaling_policy default_policy;
118 if (autoscaler_) {
119 return autoscaler_->get_policy();
120 }
121 return default_policy;
122}
123
125{
126 if (autoscaler_) {
127 return autoscaler_->evaluate_now();
128 }
129 return scaling_decision{};
130}
131
132auto autoscaling_pool_policy::scale_to(std::size_t target_workers) -> common::VoidResult
133{
134 if (autoscaler_) {
135 return autoscaler_->scale_to(target_workers);
136 }
137 return common::ok();
138}
139
140} // namespace kcenon::thread
Pool policy implementing automatic worker scaling based on load.
Manages automatic scaling of thread pool workers based on load metrics.
Definition autoscaler.h:95
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.
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.
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.
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
A thread pool for concurrent execution of jobs using multiple worker threads.
Base job class for schedulable work units in the thread system.
Core threading foundation of the thread system library.
Definition thread_impl.h:17
STL namespace.
Configuration for autoscaling behavior.
Statistics for autoscaling operations.
Scaling decision result.