19#include <unordered_map>
187 template<
typename Func>
188 auto execute(Func&& func) ->
decltype(func()) {
189 using result_type =
decltype(func());
190 using value_type =
typename result_type::value_type;
194 "Rate limit exceeded for '" +
get_name() +
"'");
217 using clock = std::chrono::steady_clock;
224 ,
tokens_(static_cast<double>(capacity))
228 std::lock_guard<std::mutex> lock(
mutex_);
231 if (
tokens_ >=
static_cast<double>(count)) {
232 tokens_ -=
static_cast<double>(count);
244 auto now = clock::now();
245 auto elapsed = std::chrono::duration<double>(now -
last_refill_).count();
270 using clock = std::chrono::steady_clock;
280 std::lock_guard<std::mutex> lock(
mutex_);
283 if (
water_ +
static_cast<double>(count) <=
static_cast<double>(
capacity_)) {
284 water_ +=
static_cast<double>(count);
296 auto now = clock::now();
297 auto elapsed = std::chrono::duration<double>(now -
last_leak_).count();
332 std::lock_guard<std::mutex> lock(
mutex_);
337 "Memory quota exceeded for '" +
name_ +
"'");
350 return common::ok(
true);
358 std::lock_guard<std::mutex> lock(
mutex_);
423 template<
typename Func>
424 auto execute(Func&& func) ->
decltype(func()) {
429 return std::forward<Func>(func)();
470 std::lock_guard<std::mutex> lock(
mutex_);
474 "Rate limiter '" + name +
"' already exists");
479 return common::ok(
true);
488 std::lock_guard<std::mutex> lock(
mutex_);
500 std::lock_guard<std::mutex> lock(
mutex_);
504 "Memory quota '" + name +
"' already exists");
507 memory_quotas_[name] = std::make_unique<memory_quota_manager>(name, quota);
508 return common::ok(
true);
517 std::lock_guard<std::mutex> lock(
mutex_);
529 std::lock_guard<std::mutex> lock(
mutex_);
533 "CPU throttler '" + name +
"' already exists");
537 return common::ok(
true);
546 std::lock_guard<std::mutex> lock(
mutex_);
556 std::lock_guard<std::mutex> lock(
mutex_);
559 if (manager->is_over_critical_threshold()) {
560 return common::ok(
false);
564 return common::ok(
true);
572 std::lock_guard<std::mutex> lock(
mutex_);
573 std::unordered_map<std::string, resource_metrics> all_metrics;
580 all_metrics[
"memory_" + name] = manager->get_metrics();
584 all_metrics[
"cpu_" + name] = throttler->get_metrics();
593 std::unordered_map<std::string, std::unique_ptr<memory_quota_manager>>
memory_quotas_;
606 const std::string& name,
double rate,
size_t capacity,
608 return std::make_unique<token_bucket_limiter>(name, rate, capacity, strategy);
615 const std::string& name,
double rate,
size_t capacity) {
616 return std::make_unique<leaky_bucket_limiter>(name, rate, capacity);
623 const std::string& name,
size_t max_bytes,
625 return std::make_unique<memory_quota_manager>(name, max_bytes, strategy);
632 return std::make_unique<resource_manager>(name);
Throttles operations based on CPU usage.
resource_metrics metrics_
cpu_throttle_config config_
const std::string & get_name() const
Get the name of this throttler.
cpu_throttler(const std::string &name, const cpu_throttle_config &config)
resource_metrics get_metrics() const
Get current metrics.
auto execute(Func &&func) -> decltype(func())
Execute a function with CPU throttling.
Leaky bucket rate limiter implementation.
bool try_acquire(size_t count=1) override
Try to acquire tokens.
std::chrono::steady_clock clock
clock::time_point last_leak_
leaky_bucket_limiter(const std::string &name, double rate, size_t capacity)
const std::string & get_name() const override
Get the name of this rate limiter.
Manages memory quota with tracking and throttling.
resource_metrics get_metrics() const
Get current metrics.
memory_quota_manager(const std::string &name, const resource_quota "a)
size_t current_usage() const
Get current memory usage.
memory_quota_manager(const std::string &name, size_t max_bytes, throttling_strategy strategy=throttling_strategy::reject)
common::Result< bool > allocate(size_t bytes)
Allocate memory from the quota.
resource_metrics metrics_
bool is_over_warning_threshold() const
Check if usage is over warning threshold.
bool is_over_critical_threshold() const
Check if usage is over critical threshold.
const std::string & get_name() const
Get the name of this manager.
void deallocate(size_t bytes)
Deallocate memory back to the quota.
Base interface for rate limiters.
auto execute(Func &&func) -> decltype(func())
Execute a function with rate limiting.
virtual const std::string & get_name() const =0
Get the name of this rate limiter.
virtual ~rate_limiter()=default
virtual bool try_acquire(size_t count=1)=0
Try to acquire tokens.
Coordinates multiple resource management components.
common::Result< bool > add_memory_quota(const std::string &name, const resource_quota "a)
Add a memory quota manager.
cpu_throttler * get_cpu_throttler(const std::string &name)
Get a CPU throttler by name.
std::unordered_map< std::string, std::unique_ptr< memory_quota_manager > > memory_quotas_
rate_limiter * get_rate_limiter(const std::string &name)
Get a rate limiter by name.
common::Result< bool > is_healthy() const
Check if all resources are healthy.
resource_manager(const std::string &name)
memory_quota_manager * get_memory_quota(const std::string &name)
Get a memory quota manager by name.
std::unordered_map< std::string, std::unique_ptr< rate_limiter > > rate_limiters_
common::Result< bool > add_cpu_throttler(const std::string &name, const cpu_throttle_config &config)
Add a CPU throttler.
common::Result< bool > add_rate_limiter(const std::string &name, const rate_limit_config &config)
Add a rate limiter.
std::unordered_map< std::string, std::unique_ptr< cpu_throttler > > cpu_throttlers_
std::unordered_map< std::string, resource_metrics > get_all_metrics() const
Get metrics for all managed resources.
Token bucket rate limiter implementation.
const std::string & get_name() const override
Get the name of this rate limiter.
std::chrono::steady_clock clock
token_bucket_limiter(const std::string &name, double rate, size_t capacity, throttling_strategy=throttling_strategy::reject)
clock::time_point last_refill_
bool try_acquire(size_t count=1) override
Try to acquire tokens.
throttling_strategy
Strategy for handling resource exhaustion.
@ reject
Reject requests immediately when limit exceeded.
@ delay
Delay requests until resources are available.
std::unique_ptr< memory_quota_manager > create_memory_quota_manager(const std::string &name, size_t max_bytes, throttling_strategy strategy=throttling_strategy::reject)
Create a memory quota manager.
std::unique_ptr< token_bucket_limiter > create_token_bucket_limiter(const std::string &name, double rate, size_t capacity, throttling_strategy strategy=throttling_strategy::reject)
Create a token bucket rate limiter.
std::unique_ptr< resource_manager > create_resource_manager(const std::string &name)
Create a resource manager.
resource_type
Type of resource being managed.
@ memory
Memory/DRAM power domain (RAPL)
@ cpu
CPU power domain (RAPL)
std::unique_ptr< leaky_bucket_limiter > create_leaky_bucket_limiter(const std::string &name, double rate, size_t capacity)
Create a leaky bucket rate limiter.
Result pattern type definitions for monitoring system.
Configuration for CPU throttling.
bool validate() const
Validate configuration.
double max_cpu_usage
Maximum CPU usage (0.0 to 1.0)
double warning_threshold
Warning threshold (0.0 to 1.0)
std::chrono::milliseconds check_interval
throttling_strategy strategy
Configuration for rate limiting.
throttling_strategy strategy
size_t burst_capacity
Maximum burst capacity.
double rate_per_second
Rate of token refill per second.
bool validate() const
Validate configuration.
Metrics for resource usage tracking.
resource_metrics(const resource_metrics &other)
std::atomic< size_t > rejected_operations
std::atomic< size_t > total_allocations
resource_metrics & operator=(const resource_metrics &other)
std::atomic< size_t > current_usage
std::atomic< size_t > delayed_operations
std::atomic< size_t > peak_usage
resource_metrics()=default
Configuration for resource quotas.
size_t critical_threshold
Critical level threshold.
throttling_strategy strategy
size_t max_value
Maximum allowed resource usage.
size_t warning_threshold
Warning level threshold.
bool validate() const
Validate configuration.
resource_quota(resource_type t, size_t max, throttling_strategy s=throttling_strategy::reject)