1use core::time::Duration;
4
5use crate::Percent;
6
7#[derive(Clone, Copy, PartialEq, Debug)]
32#[non_exhaustive]
33pub struct Pressure {
34 pub level: PressureLevel,
36 pub some: Stall,
38 pub full: Option<Stall>,
41 pub total: Option<Duration>,
43}
44
45impl Pressure {
46 pub const WARNING_SOME_AVG10: f32 = 10.0;
48 pub const CRITICAL_SOME_AVG10: f32 = 40.0;
50 pub const CRITICAL_FULL_AVG10: f32 = 10.0;
52
53 #[cfg_attr(not(any(target_os = "linux", target_os = "android")), allow(dead_code))] pub(crate) fn from_stalls(
56 some: Stall,
57 full: Option<Stall>,
58 total: Option<Duration>,
59 ) -> Pressure {
60 let full_critical = full.is_some_and(|f| f.avg10.value() > Self::CRITICAL_FULL_AVG10);
61 let level = if full_critical || some.avg10.value() >= Self::CRITICAL_SOME_AVG10 {
62 PressureLevel::Critical
63 } else if some.avg10.value() >= Self::WARNING_SOME_AVG10 {
64 PressureLevel::Warning
65 } else {
66 PressureLevel::Normal
67 };
68 Pressure {
69 level,
70 some,
71 full,
72 total,
73 }
74 }
75
76 #[allow(dead_code)] pub(crate) fn from_level(level: PressureLevel) -> Pressure {
79 Pressure {
80 level,
81 some: Stall::ZERO,
82 full: None,
83 total: None,
84 }
85 }
86}
87
88#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug, Default)]
91#[non_exhaustive]
92pub struct Stall {
93 pub avg10: Percent,
95 pub avg60: Percent,
97 pub avg300: Percent,
99}
100
101impl Stall {
102 pub(crate) const ZERO: Stall = Stall {
103 avg10: Percent::ZERO,
104 avg60: Percent::ZERO,
105 avg300: Percent::ZERO,
106 };
107
108 #[cfg_attr(not(any(target_os = "linux", target_os = "android")), allow(dead_code))] pub(crate) fn new(avg10: Percent, avg60: Percent, avg300: Percent) -> Stall {
110 Stall {
111 avg10,
112 avg60,
113 avg300,
114 }
115 }
116}
117
118#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
120#[non_exhaustive]
121pub enum PressureLevel {
122 Normal,
124 Warning,
126 Critical,
128}
129
130#[cfg(test)]
131mod tests {
132 use super::*;
133
134 fn stall(avg10: f32) -> Stall {
135 Stall::new(Percent::from_value(avg10), Percent::ZERO, Percent::ZERO)
136 }
137
138 #[test]
139 fn test_pressure_level_thresholds() {
140 let level = |some: f32, full: Option<f32>| {
141 Pressure::from_stalls(stall(some), full.map(stall), None).level
142 };
143 assert_eq!(level(0.0, None), PressureLevel::Normal);
144 assert_eq!(level(9.99, Some(10.0)), PressureLevel::Normal);
145 assert_eq!(level(10.0, None), PressureLevel::Warning);
146 assert_eq!(level(39.9, Some(5.0)), PressureLevel::Warning);
147 assert_eq!(level(40.0, None), PressureLevel::Critical);
148 assert_eq!(level(1.0, Some(10.01)), PressureLevel::Critical);
149 assert!(
150 PressureLevel::Normal < PressureLevel::Warning
151 && PressureLevel::Warning < PressureLevel::Critical
152 );
153 }
154
155 #[test]
156 fn test_pressure_from_level_has_no_shares() {
157 let p = Pressure::from_level(PressureLevel::Critical);
158 assert_eq!(p.level, PressureLevel::Critical);
159 assert_eq!(p.some, Stall::ZERO);
160 assert_eq!((p.full, p.total), (None, None));
161 }
162}