pub struct ResourceMonitor { /* private fields */ }Expand description
Resource monitor for tracking and limiting resource usage.
Implementations§
Source§impl ResourceMonitor
impl ResourceMonitor
Sourcepub fn new(limits: ResourceLimits) -> Self
pub fn new(limits: ResourceLimits) -> Self
Create a new resource monitor with the given limits.
Sourcepub fn can_allocate(&self, resource_type: ResourceType, amount: u64) -> bool
pub fn can_allocate(&self, resource_type: ResourceType, amount: u64) -> bool
Check if a resource allocation can be made.
Sourcepub fn record_allocation(&mut self, resource_type: ResourceType, amount: u64)
pub fn record_allocation(&mut self, resource_type: ResourceType, amount: u64)
Record a resource allocation.
Sourcepub fn record_deallocation(&mut self, resource_type: ResourceType, amount: u64)
pub fn record_deallocation(&mut self, resource_type: ResourceType, amount: u64)
Record a resource deallocation.
Sourcepub fn update_usage(&mut self, resource_type: ResourceType, current: u64)
pub fn update_usage(&mut self, resource_type: ResourceType, current: u64)
Update current usage (for absolute measurements like CPU).
Sourcepub fn is_throttled(&self, resource_type: ResourceType) -> bool
pub fn is_throttled(&self, resource_type: ResourceType) -> bool
Check if a resource type is currently throttled.
Sourcepub fn get_stats(&self, resource_type: ResourceType) -> Option<ResourceStats>
pub fn get_stats(&self, resource_type: ResourceType) -> Option<ResourceStats>
Get usage statistics for a resource type.
Sourcepub fn get_all_stats(&self) -> HashMap<ResourceType, ResourceStats>
pub fn get_all_stats(&self) -> HashMap<ResourceType, ResourceStats>
Get all resource statistics.
Sourcepub fn get_allocation_rate(
&self,
resource_type: ResourceType,
window: Duration,
) -> u64
pub fn get_allocation_rate( &self, resource_type: ResourceType, window: Duration, ) -> u64
Calculate recent allocation rate (bytes/sec or percent/sec).
Sourcepub fn cleanup_old_records(&mut self, older_than: Duration)
pub fn cleanup_old_records(&mut self, older_than: Duration)
Clean old allocation records (older than specified duration).
Sourcepub fn reset_stats(&mut self)
pub fn reset_stats(&mut self)
Reset all statistics.
Sourcepub fn health_score(&self) -> f64
pub fn health_score(&self) -> f64
Get overall system health score (0.0 to 1.0).
Sourcepub fn is_over_limit(&self) -> bool
pub fn is_over_limit(&self) -> bool
Check if any resource is over limit.
Sourcepub fn calculate_degradation_level(&self) -> DegradationLevel
pub fn calculate_degradation_level(&self) -> DegradationLevel
Calculate the appropriate degradation level based on current resource usage.
This method analyzes resource utilization and determines what level of degradation is appropriate to maintain system stability.
Sourcepub fn update_degradation_level(&mut self)
pub fn update_degradation_level(&mut self)
Update the degradation level based on current resource usage.
This should be called periodically to adjust system behavior based on resource pressure.
Sourcepub fn degradation_level(&self) -> DegradationLevel
pub fn degradation_level(&self) -> DegradationLevel
Get the current degradation level.
Sourcepub fn should_accept_requests(&self) -> bool
pub fn should_accept_requests(&self) -> bool
Check if the system should accept new requests based on degradation level.
Sourcepub fn should_run_background_tasks(&self) -> bool
pub fn should_run_background_tasks(&self) -> bool
Check if background tasks should run based on degradation level.
Sourcepub fn recommended_cache_size(&self, base_size: usize) -> usize
pub fn recommended_cache_size(&self, base_size: usize) -> usize
Get recommended cache size based on degradation level.
Sourcepub fn recommended_concurrency(&self, base_concurrency: usize) -> usize
pub fn recommended_concurrency(&self, base_concurrency: usize) -> usize
Get recommended concurrency limit based on degradation level.
Sourcepub fn sample_cpu_usage(&mut self) -> f32
pub fn sample_cpu_usage(&mut self) -> f32
Sample actual system CPU usage and update statistics.
This method refreshes CPU usage from the operating system and updates the internal statistics. Returns the current CPU usage percentage (0-100). Also updates degradation level based on new resource usage.
Sourcepub fn sample_memory_usage(&mut self) -> u64
pub fn sample_memory_usage(&mut self) -> u64
Sample actual system memory usage and update statistics.
This method refreshes memory usage from the operating system and updates the internal statistics. Returns the current memory usage in bytes. Also updates degradation level based on new resource usage.
Sourcepub fn sample_all_system_resources(&mut self) -> (f32, u64)
pub fn sample_all_system_resources(&mut self) -> (f32, u64)
Sample all system resources (CPU and memory) at once.
This is more efficient than calling individual sample methods separately. Returns a tuple of (cpu_usage_percent, memory_used_bytes). Also updates degradation level based on new resource usage.
Sourcepub fn total_system_memory(&self) -> u64
pub fn total_system_memory(&self) -> u64
Get total system memory in bytes.
Sourcepub fn predict_usage(
&self,
resource_type: ResourceType,
window: Duration,
forecast_duration: Duration,
) -> Option<u64>
pub fn predict_usage( &self, resource_type: ResourceType, window: Duration, forecast_duration: Duration, ) -> Option<u64>
Predict future resource usage based on recent trends.
Uses simple linear regression on recent allocation history to predict usage at a future time.
§Arguments
resource_type- The resource type to predictwindow- Duration of historical data to analyzeforecast_duration- How far into the future to predict
§Returns
Returns predicted usage value, or None if insufficient data.
Sourcepub fn should_proactive_throttle(
&self,
resource_type: ResourceType,
prediction_window: Duration,
forecast_duration: Duration,
) -> bool
pub fn should_proactive_throttle( &self, resource_type: ResourceType, prediction_window: Duration, forecast_duration: Duration, ) -> bool
Check if proactive throttling should be enabled based on predictions.
Analyzes predicted resource usage and determines if throttling should be enabled preemptively to avoid hitting limits.
§Arguments
resource_type- The resource type to checkprediction_window- Duration of historical data for predictionforecast_duration- How far ahead to predict
§Returns
Returns true if proactive throttling is recommended, false otherwise.
Sourcepub fn get_throttle_intensity(
&self,
resource_type: ResourceType,
prediction_window: Duration,
forecast_duration: Duration,
) -> f64
pub fn get_throttle_intensity( &self, resource_type: ResourceType, prediction_window: Duration, forecast_duration: Duration, ) -> f64
Get recommended throttle intensity based on predictions.
Returns a value between 0.0 (no throttling) and 1.0 (maximum throttling) based on predicted resource pressure.
§Arguments
resource_type- The resource type to analyzeprediction_window- Duration of historical data for predictionforecast_duration- How far ahead to predict
§Returns
Returns throttle intensity (0.0 to 1.0), where higher values mean more aggressive throttling.
Source§impl ResourceMonitor
impl ResourceMonitor
Sourcepub fn start_monitoring(&self, config: MonitoringConfig) -> MonitoringHandle
pub fn start_monitoring(&self, config: MonitoringConfig) -> MonitoringHandle
Start background monitoring of system resources.
This spawns a background task that periodically samples CPU and memory usage and updates the resource monitor’s statistics. The returned handle can be used to stop the monitoring gracefully.
§Arguments
config- Configuration for the monitoring task
§Example
use chie_core::resource_mgmt::{ResourceMonitor, ResourceLimits, MonitoringConfig};
use std::time::Duration;
let limits = ResourceLimits::default();
let monitor = ResourceMonitor::new(limits);
// Start background monitoring
let config = MonitoringConfig {
sample_interval: Duration::from_secs(10),
..Default::default()
};
let handle = monitor.start_monitoring(config);
// Monitor runs in background...
// Stop when done
handle.stop().await.unwrap();Auto Trait Implementations§
impl Freeze for ResourceMonitor
impl RefUnwindSafe for ResourceMonitor
impl Send for ResourceMonitor
impl Sync for ResourceMonitor
impl Unpin for ResourceMonitor
impl UnwindSafe for ResourceMonitor
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> FmtForward for T
impl<T> FmtForward for T
Source§fn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
fn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
self to use its Binary implementation when Debug-formatted.Source§fn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
fn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
self to use its Display implementation when
Debug-formatted.Source§fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
self to use its LowerExp implementation when
Debug-formatted.Source§fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
self to use its LowerHex implementation when
Debug-formatted.Source§fn fmt_octal(self) -> FmtOctal<Self>where
Self: Octal,
fn fmt_octal(self) -> FmtOctal<Self>where
Self: Octal,
self to use its Octal implementation when Debug-formatted.Source§fn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
fn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
self to use its Pointer implementation when
Debug-formatted.Source§fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
self to use its UpperExp implementation when
Debug-formatted.Source§fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
self to use its UpperHex implementation when
Debug-formatted.Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> Pipe for Twhere
T: ?Sized,
impl<T> Pipe for Twhere
T: ?Sized,
Source§fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
Source§fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
self and passes that borrow into the pipe function. Read moreSource§fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
self and passes that borrow into the pipe function. Read moreSource§fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
Source§fn pipe_borrow_mut<'a, B, R>(
&'a mut self,
func: impl FnOnce(&'a mut B) -> R,
) -> R
fn pipe_borrow_mut<'a, B, R>( &'a mut self, func: impl FnOnce(&'a mut B) -> R, ) -> R
Source§fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
self, then passes self.as_ref() into the pipe function.Source§fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
self, then passes self.as_mut() into the pipe
function.Source§fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
self, then passes self.deref() into the pipe function.Source§impl<T> PolicyExt for Twhere
T: ?Sized,
impl<T> PolicyExt for Twhere
T: ?Sized,
Source§impl<T> Tap for T
impl<T> Tap for T
Source§fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
Borrow<B> of a value. Read moreSource§fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
BorrowMut<B> of a value. Read moreSource§fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
AsRef<R> view of a value. Read moreSource§fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
AsMut<R> view of a value. Read moreSource§fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
Deref::Target of a value. Read moreSource§fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
Deref::Target of a value. Read moreSource§fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
.tap() only in debug builds, and is erased in release builds.Source§fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
.tap_mut() only in debug builds, and is erased in release
builds.Source§fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
.tap_borrow() only in debug builds, and is erased in release
builds.Source§fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
.tap_borrow_mut() only in debug builds, and is erased in release
builds.Source§fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
.tap_ref() only in debug builds, and is erased in release
builds.Source§fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
.tap_ref_mut() only in debug builds, and is erased in release
builds.Source§fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
.tap_deref() only in debug builds, and is erased in release
builds.