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