Thread System 0.3.1
High-performance C++20 thread pool with work stealing and DAG scheduling
Loading...
Searching...
No Matches
autoscaling_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
12#pragma once
13
14#include "scaling_metrics.h"
15
16#include <chrono>
17#include <cstddef>
18#include <functional>
19#include <thread>
20
21namespace kcenon::thread
22{
52 {
56 enum class mode
57 {
58 disabled,
59 manual,
61 };
62
69 {
71 double queue_depth_threshold = 100.0;
72
75
77 double latency_threshold_ms = 50.0;
78
80 std::size_t pending_jobs_threshold = 1000;
81 };
82
89 {
92
94 double queue_depth_threshold = 10.0;
95
97 std::chrono::seconds idle_duration{60};
98 };
99
100 // ============================================
101 // Worker Bounds
102 // ============================================
103
105 std::size_t min_workers = 1;
106
108 std::size_t max_workers = std::thread::hardware_concurrency();
109
110 // ============================================
111 // Trigger Configurations
112 // ============================================
113
116
119
120 // ============================================
121 // Scaling Behavior
122 // ============================================
123
125 std::size_t scale_up_increment = 1;
126
128 std::size_t scale_down_increment = 1;
129
131 double scale_up_factor = 1.5;
132
135
136 // ============================================
137 // Cooldown Periods
138 // ============================================
139
141 std::chrono::seconds scale_up_cooldown{30};
142
144 std::chrono::seconds scale_down_cooldown{60};
145
146 // ============================================
147 // Sampling Configuration
148 // ============================================
149
151 std::chrono::milliseconds sample_interval{1000};
152
154 std::size_t samples_for_decision = 5;
155
156 // ============================================
157 // Mode
158 // ============================================
159
162
163 // ============================================
164 // Callbacks
165 // ============================================
166
169 std::function<void(scaling_direction, scaling_reason, std::size_t, std::size_t)>
171
176 [[nodiscard]] auto is_valid() const -> bool
177 {
178 if (min_workers == 0)
179 {
180 return false;
181 }
183 {
184 return false;
185 }
187 {
188 return false;
189 }
191 {
192 return false;
193 }
195 {
196 return false;
197 }
199 {
200 return false;
201 }
202 if (samples_for_decision == 0)
203 {
204 return false;
205 }
206 return true;
207 }
208 };
209
210} // namespace kcenon::thread
Core threading foundation of the thread system library.
Definition thread_impl.h:17
scaling_direction
Scaling direction for autoscaling decisions.
scaling_reason
Reason for scaling decision.
Scaling direction and metrics for autoscaling decisions.
double queue_depth_threshold
Jobs per worker threshold (scale down when below)
double utilization_threshold
Worker utilization threshold (0.0 - 1.0, scale down when below)
std::chrono::seconds idle_duration
Duration worker must be idle before removal.
double latency_threshold_ms
P95 latency threshold in milliseconds (scale up when exceeded)
double utilization_threshold
Worker utilization threshold (0.0 - 1.0, scale up when exceeded)
double queue_depth_threshold
Jobs per worker threshold (scale up when exceeded)
std::size_t pending_jobs_threshold
Absolute pending jobs threshold (scale up when exceeded)
Configuration for autoscaling behavior.
auto is_valid() const -> bool
Validates the policy configuration.
std::chrono::seconds scale_down_cooldown
Minimum time between scale-down events.
scale_down_config scale_down
Scale-down trigger configuration.
scale_up_config scale_up
Scale-up trigger configuration.
@ automatic
Fully automatic scaling.
@ manual
Only scale on explicit trigger.
std::size_t scale_down_increment
Number of workers to remove per scale-down event.
std::chrono::seconds scale_up_cooldown
Minimum time between scale-up events.
double scale_up_factor
Multiplicative factor for scaling (used if use_multiplicative_scaling is true)
std::chrono::milliseconds sample_interval
Interval between metric samples.
std::function< void(scaling_direction, scaling_reason, std::size_t, std::size_t)> scaling_callback
Callback invoked on scaling events Parameters: direction, reason, from_count, to_count.
std::size_t samples_for_decision
Number of samples to aggregate before making a decision.
std::size_t scale_up_increment
Number of workers to add per scale-up event.
std::size_t min_workers
Minimum number of workers (never scale below this)
bool use_multiplicative_scaling
Whether to use multiplicative scaling instead of additive.
std::size_t max_workers
Maximum number of workers (never scale above this)