21#include <unordered_map>
35namespace kcenon {
namespace monitoring {
158 std::shared_ptr<kcenon::monitoring::metrics_collector>
collector_;
168 std::shared_ptr<kcenon::monitoring::metrics_collector> collector,
178 common::Result<kcenon::monitoring::metrics_snapshot>
collect() {
205 if (is_first_adaptation) {
229 auto now = std::chrono::system_clock::now();
234 auto time_since_last_change = std::chrono::duration_cast<std::chrono::milliseconds>(
315 static thread_local std::mt19937 gen(std::random_device{}());
316 std::uniform_real_distribution<> dis(0.0, 1.0);
330 double effective_load = cpu_usage;
335 effective_load = std::max(effective_load, cfg.
high_threshold + 1.0);
344 effective_load *= 0.8;
347 effective_load *= 1.2;
395 if (raw_level == current_level) {
396 return current_level;
400 double effective_load = cpu_usage;
402 effective_load = std::max(effective_load, cfg.
high_threshold + 1.0);
409 effective_load *= 0.8;
412 effective_load *= 1.2;
425 if (raw_level > current_level) {
428 static_cast<load_level>(
static_cast<int>(current_level) + 1), cfg);
429 if (effective_load < next_threshold + margin) {
430 return current_level;
434 if (effective_load > current_threshold - margin) {
435 return current_level;
463 std::unique_ptr<monitor_impl>
impl_;
481 const std::
string& name,
510 const std::string& name
537 const std::string& name,
558 const std::string& name,
559 std::shared_ptr<kcenon::monitoring::metrics_collector> collector,
581 other.monitor_ =
nullptr;
582 other.registered_ =
false;
586 if (
this != &
other) {
593 other.monitor_ =
nullptr;
594 other.registered_ =
false;
Adaptive collector wrapper.
adaptation_stats get_stats() const
Get current adaptation statistics.
common::Result< kcenon::monitoring::metrics_snapshot > collect()
Collect metrics with adaptive sampling.
void adapt(const kcenon::monitoring::system_metrics &sys_metrics)
Adapt collection behavior based on load.
static load_level calculate_load_level_with_config(double cpu_usage, double memory_usage, const adaptive_config &cfg)
Calculate load level from metrics with provided config.
std::shared_ptr< kcenon::monitoring::metrics_collector > collector_
std::chrono::milliseconds get_current_interval() const
Get current collection interval.
adaptive_config get_config() const
Get adaptive configuration.
adaptive_collector(std::shared_ptr< kcenon::monitoring::metrics_collector > collector, const adaptive_config &config={})
static double get_threshold_for_level(load_level level, const adaptive_config &cfg)
Get the threshold value for a given load level.
void set_enabled(bool enabled)
Enable or disable adaptive behavior.
std::atomic< bool > enabled_
void set_config(const adaptive_config &config)
Set adaptive configuration.
bool is_enabled() const
Check if adaptive behavior is enabled.
std::atomic< double > current_sampling_rate_
static load_level calculate_load_level_with_hysteresis(double cpu_usage, double memory_usage, load_level current_level, const adaptive_config &cfg)
Calculate load level with hysteresis support (ARC-005)
bool should_sample() const
Determine if current sample should be collected.
Adaptive monitoring controller.
adaptive_monitor(const adaptive_monitor &)=delete
void set_global_strategy(adaptation_strategy strategy)
Set global adaptation strategy.
common::Result< bool > unregister_collector(const std::string &name)
Unregister a collector.
common::Result< bool > stop()
Stop adaptive monitoring.
adaptive_monitor(adaptive_monitor &&) noexcept
common::Result< bool > start()
Start adaptive monitoring.
std::unique_ptr< monitor_impl > impl_
adaptive_monitor & operator=(const adaptive_monitor &)=delete
std::vector< std::string > get_active_collectors() const
Get recommended collectors based on load.
std::unordered_map< std::string, adaptation_stats > get_all_stats() const
Get all collector statistics.
common::Result< adaptation_stats > get_collector_stats(const std::string &name) const
Get adaptation statistics for a collector.
bool is_running() const
Check if monitoring is active.
common::Result< bool > force_adaptation()
Force adaptation cycle.
common::Result< bool > register_collector(const std::string &name, std::shared_ptr< kcenon::monitoring::metrics_collector > collector, const adaptive_config &config={})
Register a collector for adaptive monitoring.
common::Result< bool > set_collector_priority(const std::string &name, int priority)
Set priority for a collector (higher priority = keep active longer)
Adaptive monitoring scope.
adaptive_monitor * monitor_
adaptive_scope(const std::string &name, std::shared_ptr< kcenon::monitoring::metrics_collector > collector, const adaptive_config &config={})
adaptive_scope(adaptive_scope &&other) noexcept
adaptive_scope(const adaptive_scope &)=delete
bool is_registered() const
std::string collector_name_
adaptive_scope & operator=(adaptive_scope &&other) noexcept
adaptive_scope & operator=(const adaptive_scope &)=delete
Abstract base class for metric collectors.
virtual common::Result< metrics_snapshot > collect()=0
Collect metrics.
Monitoring system specific error codes.
Core monitoring system interface definitions.
load_level
System load levels.
adaptation_strategy
Adaptation strategy for monitoring behavior.
adaptive_monitor & global_adaptive_monitor()
Global adaptive monitor instance.
Result pattern type definitions for monitoring system.
std::uint64_t upscale_count
std::chrono::system_clock::time_point last_level_change
std::uint64_t hysteresis_prevented_changes
std::chrono::milliseconds current_interval
std::uint64_t samples_dropped
std::chrono::system_clock::time_point last_adaptation
std::uint64_t cooldown_prevented_changes
std::uint64_t samples_collected
std::uint64_t total_adaptations
double current_sampling_rate
std::uint64_t downscale_count
double average_memory_usage
load_level current_load_level
Adaptive configuration parameters.
double memory_warning_threshold
std::chrono::milliseconds cooldown_period
double memory_critical_threshold
std::chrono::milliseconds low_interval
std::chrono::milliseconds critical_interval
std::chrono::seconds adaptation_interval
std::chrono::milliseconds moderate_interval
double high_sampling_rate
double get_sampling_rate_for_load(load_level level) const
Get sampling rate for load level.
std::chrono::milliseconds idle_interval
double critical_sampling_rate
std::chrono::milliseconds get_interval_for_load(load_level level) const
Get collection interval for load level.
double moderate_sampling_rate
std::chrono::milliseconds high_interval
double idle_sampling_rate
double moderate_threshold
adaptation_strategy strategy
Extended error information with context.
double memory_usage_percent