atmos/kernel/
mem_quota.rs1use core::sync::atomic::{AtomicBool, AtomicI64, AtomicU32, AtomicU8, AtomicUsize, Ordering};
37
38pub const GROUPS: usize = 32;
40pub const CORES: usize = 4;
42pub const RESERVE: usize = 32 * 1024 * 1024;
44pub const LOW_FREE: usize = 2 * RESERVE;
47pub const MIN_VICTIM: i64 = 16 * 1024 * 1024;
49
50pub const fn app_limit(total: usize) -> usize {
55 total.saturating_sub(RESERVE) / 8 * 7
56}
57
58#[derive(Clone, Copy, Debug, PartialEq, Eq)]
60pub enum Verdict {
61 Keep,
63 OverLimit,
65 LargestUnderPressure,
67}
68
69pub struct Ledger {
71 cur: [AtomicU8; CORES],
73 bytes: [AtomicI64; GROUPS],
75 owner: [AtomicUsize; GROUPS],
77 low: AtomicBool,
79 killing: AtomicU8,
81 next: AtomicU8,
83 threads: [AtomicU32; GROUPS],
88}
89
90impl Default for Ledger {
91 fn default() -> Self {
92 Self::new()
93 }
94}
95
96impl Ledger {
97 pub const fn new() -> Self {
98 Self {
99 cur: [const { AtomicU8::new(0) }; CORES],
100 bytes: [const { AtomicI64::new(0) }; GROUPS],
101 owner: [const { AtomicUsize::new(0) }; GROUPS],
102 low: AtomicBool::new(false),
103 killing: AtomicU8::new(0),
104 next: AtomicU8::new(1),
105 threads: [const { AtomicU32::new(0) }; GROUPS],
106 }
107 }
108
109 #[inline]
114 pub fn is_closed(&self, g: u8) -> bool {
115 if g == 0 {
116 return false;
117 }
118 match self.owner.get(g as usize) {
119 Some(o) => o.load(Ordering::Acquire) == 0,
120 None => false,
121 }
122 }
123
124 pub fn thread_enter(&self, g: u8) {
126 if g == 0 {
127 return;
128 }
129 if let Some(t) = self.threads.get(g as usize) {
130 t.fetch_add(1, Ordering::AcqRel);
131 }
132 }
133
134 pub fn thread_leave(&self, g: u8) -> u32 {
136 if g == 0 {
137 return 0;
138 }
139 match self.threads.get(g as usize) {
140 Some(t) => {
141 let mut cur = t.load(Ordering::Acquire);
143 loop {
144 let next = cur.saturating_sub(1);
145 match t.compare_exchange_weak(cur, next, Ordering::AcqRel, Ordering::Acquire) {
146 Ok(_) => return next,
147 Err(v) => cur = v,
148 }
149 }
150 }
151 None => 0,
152 }
153 }
154
155 pub fn move_thread(&self, from: u8, to: u8) {
157 if from == to {
158 return;
159 }
160 self.thread_leave(from);
161 self.thread_enter(to);
162 }
163
164 pub fn threads_of(&self, g: u8) -> u32 {
166 match self.threads.get(g as usize) {
167 Some(t) if g != 0 => t.load(Ordering::Acquire),
168 _ => 0,
169 }
170 }
171
172 #[inline]
174 pub fn core_group(&self, core: usize) -> u8 {
175 match self.cur.get(core) {
176 Some(c) => c.load(Ordering::Relaxed),
177 None => 0,
178 }
179 }
180
181 #[inline]
183 pub fn set_core_group(&self, core: usize, group: u8) {
184 if let Some(c) = self.cur.get(core) {
185 c.store(group, Ordering::Relaxed);
186 }
187 }
188
189 #[inline]
192 pub fn on_alloc(&self, core: usize, size: usize, free_after: usize) {
193 let g = self.core_group(core) as usize;
194 if g != 0 {
195 if let Some(b) = self.bytes.get(g) {
196 b.store(b.load(Ordering::Relaxed).wrapping_add(size as i64), Ordering::Relaxed);
197 }
198 }
199 self.update_low(free_after);
200 }
201
202 #[inline]
204 pub fn on_dealloc(&self, core: usize, size: usize, free_after: usize) {
205 let g = self.core_group(core) as usize;
206 if g != 0 {
207 if let Some(b) = self.bytes.get(g) {
208 b.store(b.load(Ordering::Relaxed).wrapping_sub(size as i64), Ordering::Relaxed);
209 }
210 }
211 self.update_low(free_after);
212 }
213
214 #[inline]
216 fn update_low(&self, free_after: usize) {
217 let low = free_after < LOW_FREE;
218 if self.low.load(Ordering::Relaxed) != low {
219 self.low.store(low, Ordering::Relaxed);
220 }
221 }
222
223 #[inline]
225 pub fn is_low(&self) -> bool {
226 self.low.load(Ordering::Relaxed)
227 }
228
229 pub fn bytes_of(&self, g: u8) -> i64 {
231 match self.bytes.get(g as usize) {
232 Some(b) => b.load(Ordering::Relaxed).max(0),
233 None => 0,
234 }
235 }
236
237 pub fn owner_of(&self, g: u8) -> Option<usize> {
239 let v = self.owner.get(g as usize)?.load(Ordering::Acquire);
240 (v != 0).then(|| v - 1)
241 }
242
243 pub fn killing(&self) -> u8 {
245 self.killing.load(Ordering::Acquire)
246 }
247
248 pub fn open_group(&self, pid: usize) -> u8 {
250 let start = self.next.load(Ordering::Relaxed) as usize;
251 for i in 0..GROUPS - 1 {
252 let g = 1 + (start.wrapping_sub(1).wrapping_add(i)) % (GROUPS - 1);
254 if self.threads[g].load(Ordering::Acquire) != 0 {
256 continue;
257 }
258 let slot = &self.owner[g];
259 if slot.compare_exchange(0, pid + 1, Ordering::AcqRel, Ordering::Relaxed).is_ok() {
260 self.bytes[g].store(0, Ordering::Relaxed);
261 let nx = 1 + g % (GROUPS - 1);
262 self.next.store(nx as u8, Ordering::Relaxed);
263 return g as u8;
264 }
265 }
266 0
267 }
268
269 pub fn close_group_of(&self, pid: usize) -> Option<u8> {
271 for g in 1..GROUPS {
272 if self.owner[g].load(Ordering::Acquire) == pid + 1 {
273 self.close_group(g as u8);
274 return Some(g as u8);
275 }
276 }
277 None
278 }
279
280 pub fn close_group(&self, g: u8) {
282 if let Some(o) = self.owner.get(g as usize) {
283 o.store(0, Ordering::Release);
284 }
285 let _ = self.killing.compare_exchange(g, 0, Ordering::AcqRel, Ordering::Relaxed);
286 }
287
288 pub fn verdict(&self, g: u8, total: usize) -> Verdict {
292 if g == 0 || g as usize >= GROUPS {
293 return Verdict::Keep;
294 }
295 let own = self.bytes_of(g);
296 let low = self.is_low();
297 let over = own > app_limit(total) as i64;
298 if !low && !over {
299 return Verdict::Keep;
300 }
301 if self.owner_of(g).is_none() {
302 return Verdict::Keep;
303 }
304 let k = self.killing();
305 if k != 0 && k != g {
306 return Verdict::Keep;
308 }
309 if over {
310 return Verdict::OverLimit;
311 }
312 if own < MIN_VICTIM {
313 return Verdict::Keep;
314 }
315 for h in 1..GROUPS as u8 {
317 if h == g || self.owner_of(h).is_none() {
318 continue;
319 }
320 let other = self.bytes_of(h);
321 if other > own || (other == own && h < g) {
322 return Verdict::Keep;
323 }
324 }
325 Verdict::LargestUnderPressure
326 }
327
328 pub fn begin_kill(&self, g: u8) -> bool {
331 match self.killing.compare_exchange(0, g, Ordering::AcqRel, Ordering::Relaxed) {
332 Ok(_) => true,
333 Err(cur) => cur == g,
334 }
335 }
336}