Monitoring System 0.1.0
System resource monitoring with pluggable collectors and alerting
Loading...
Searching...
No Matches
kcenon::monitoring::memory_quota_manager Class Reference

Manages memory quota with tracking and throttling. More...

#include <resource_manager.h>

Collaboration diagram for kcenon::monitoring::memory_quota_manager:
Collaboration graph

Public Member Functions

 memory_quota_manager (const std::string &name, const resource_quota &quota)
 
 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.
 
void deallocate (size_t bytes)
 Deallocate memory back to the quota.
 
size_t current_usage () const
 Get current memory usage.
 
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.
 
resource_metrics get_metrics () const
 Get current metrics.
 
const std::string & get_name () const
 Get the name of this manager.
 

Private Attributes

std::string name_
 
resource_quota quota_
 
resource_metrics metrics_
 
std::mutex mutex_
 

Detailed Description

Manages memory quota with tracking and throttling.

Definition at line 317 of file resource_manager.h.

Constructor & Destructor Documentation

◆ memory_quota_manager() [1/2]

kcenon::monitoring::memory_quota_manager::memory_quota_manager ( const std::string & name,
const resource_quota & quota )
inline

Definition at line 319 of file resource_manager.h.

◆ memory_quota_manager() [2/2]

kcenon::monitoring::memory_quota_manager::memory_quota_manager ( const std::string & name,
size_t max_bytes,
throttling_strategy strategy = throttling_strategy::reject )
inline

Definition at line 322 of file resource_manager.h.

324 : name_(name), quota_(resource_type::memory, max_bytes, strategy) {}

Member Function Documentation

◆ allocate()

common::Result< bool > kcenon::monitoring::memory_quota_manager::allocate ( size_t bytes)
inline

Allocate memory from the quota.

Parameters
bytesNumber of bytes to allocate
Returns
Success or error if quota exceeded

Definition at line 331 of file resource_manager.h.

331 {
332 std::lock_guard<std::mutex> lock(mutex_);
333
334 if (metrics_.current_usage.load() + bytes > quota_.max_value) {
336 return common::make_error<bool>(static_cast<int>(monitoring_error_code::resource_exhausted),
337 "Memory quota exceeded for '" + name_ + "'");
338 }
339
340 metrics_.current_usage += bytes;
342
343 // Update peak usage
344 size_t current = metrics_.current_usage.load();
345 size_t peak = metrics_.peak_usage.load();
346 while (current > peak && !metrics_.peak_usage.compare_exchange_weak(peak, current)) {
347 // Retry until successful
348 }
349
350 return common::ok(true);
351 }
std::atomic< size_t > rejected_operations
size_t max_value
Maximum allowed resource usage.

References kcenon::monitoring::resource_metrics::current_usage, kcenon::monitoring::resource_quota::max_value, metrics_, mutex_, name_, kcenon::monitoring::resource_metrics::peak_usage, quota_, kcenon::monitoring::resource_metrics::rejected_operations, kcenon::monitoring::resource_exhausted, and kcenon::monitoring::resource_metrics::total_allocations.

◆ current_usage()

size_t kcenon::monitoring::memory_quota_manager::current_usage ( ) const
inline

Get current memory usage.

Definition at line 366 of file resource_manager.h.

366 {
367 return metrics_.current_usage.load();
368 }

References kcenon::monitoring::resource_metrics::current_usage, and metrics_.

◆ deallocate()

void kcenon::monitoring::memory_quota_manager::deallocate ( size_t bytes)
inline

Deallocate memory back to the quota.

Parameters
bytesNumber of bytes to deallocate

Definition at line 357 of file resource_manager.h.

357 {
358 std::lock_guard<std::mutex> lock(mutex_);
359 size_t current = metrics_.current_usage.load();
360 metrics_.current_usage.store(current >= bytes ? current - bytes : 0);
361 }

References kcenon::monitoring::resource_metrics::current_usage, metrics_, and mutex_.

◆ get_metrics()

resource_metrics kcenon::monitoring::memory_quota_manager::get_metrics ( ) const
inline

Get current metrics.

Definition at line 387 of file resource_manager.h.

387 {
388 return metrics_;
389 }

References metrics_.

◆ get_name()

const std::string & kcenon::monitoring::memory_quota_manager::get_name ( ) const
inline

Get the name of this manager.

Definition at line 394 of file resource_manager.h.

394 {
395 return name_;
396 }

References name_.

◆ is_over_critical_threshold()

bool kcenon::monitoring::memory_quota_manager::is_over_critical_threshold ( ) const
inline

Check if usage is over critical threshold.

Definition at line 380 of file resource_manager.h.

380 {
382 }
size_t critical_threshold
Critical level threshold.

References kcenon::monitoring::resource_quota::critical_threshold, kcenon::monitoring::resource_metrics::current_usage, metrics_, and quota_.

◆ is_over_warning_threshold()

bool kcenon::monitoring::memory_quota_manager::is_over_warning_threshold ( ) const
inline

Check if usage is over warning threshold.

Definition at line 373 of file resource_manager.h.

373 {
375 }
size_t warning_threshold
Warning level threshold.

References kcenon::monitoring::resource_metrics::current_usage, metrics_, quota_, and kcenon::monitoring::resource_quota::warning_threshold.

Member Data Documentation

◆ metrics_

resource_metrics kcenon::monitoring::memory_quota_manager::metrics_
mutableprivate

◆ mutex_

std::mutex kcenon::monitoring::memory_quota_manager::mutex_
mutableprivate

Definition at line 402 of file resource_manager.h.

Referenced by allocate(), and deallocate().

◆ name_

std::string kcenon::monitoring::memory_quota_manager::name_
private

Definition at line 399 of file resource_manager.h.

Referenced by allocate(), and get_name().

◆ quota_

resource_quota kcenon::monitoring::memory_quota_manager::quota_
private

The documentation for this class was generated from the following file: