Skip to main content

feldera_types/
memory_pressure.rs

1use std::fmt::Display;
2
3use serde::{Deserialize, Serialize};
4use utoipa::ToSchema;
5
6pub const MODERATE_MEMORY_PRESSURE_THRESHOLD: f64 = 0.85;
7pub const HIGH_MEMORY_PRESSURE_THRESHOLD: f64 = 0.90;
8pub const CRITICAL_MEMORY_PRESSURE_THRESHOLD: f64 = 0.95;
9
10/// Memory pressure level.
11///
12/// The current memory pressure level is computed as a function of the current process
13/// resident set size (RSS) and the user-configured memory limit (`max_rss`).
14///
15/// As the memory pressure level increases, the system will apply increasing backpressure to
16/// push state cached in memory to storage.
17///
18/// - `Low`: less than 85% of the user-configured memory limit has been allocated.
19/// - `Moderate`: between 85% and 90% of the user-configured memory limit has been allocated.
20/// - `High`: between 90% and 95% of the user-configured memory limit has been allocated.
21/// - `Critical`: more than 95% of the user-configured memory limit has been allocated.
22#[derive(
23    Debug, Default, Clone, Copy, Eq, PartialEq, PartialOrd, Ord, Serialize, Deserialize, ToSchema,
24)]
25#[repr(u8)]
26#[serde(rename_all = "snake_case")]
27pub enum MemoryPressure {
28    /// Less than 85% of the user-configured memory limit has been allocated.
29    #[default]
30    Low = 0,
31    /// Between 85% and 90% of the user-configured memory limit has been allocated.
32    Moderate = 1,
33    /// Between 90% and 95% of the user-configured memory limit has been allocated.
34    High = 2,
35    /// More than 95% of the user-configured memory limit has been allocated.
36    Critical = 3,
37}
38
39impl TryFrom<u8> for MemoryPressure {
40    type Error = ();
41
42    fn try_from(value: u8) -> Result<Self, Self::Error> {
43        match value {
44            0 => Ok(MemoryPressure::Low),
45            1 => Ok(MemoryPressure::Moderate),
46            2 => Ok(MemoryPressure::High),
47            3 => Ok(MemoryPressure::Critical),
48            _ => Err(()),
49        }
50    }
51}
52
53impl MemoryPressure {
54    pub fn as_str(&self) -> &'static str {
55        match self {
56            MemoryPressure::Low => "low",
57            MemoryPressure::Moderate => "moderate",
58            MemoryPressure::High => "high",
59            MemoryPressure::Critical => "critical",
60        }
61    }
62}
63
64impl Display for MemoryPressure {
65    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
66        f.write_str(self.as_str())
67    }
68}