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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
//! Warns the agent about the memory its jobs hold, and kills a runaway job
//! it left running as the last resort.
//!
//! The guard protects the home environment: the machine the embedder runs
//! on and, when the agent works in a container that has a memory limit, the
//! container. The two are judged apart, each by its own pressure level (the
//! host's, or the container's `memory.pressure`) against its own memory (the
//! machine's physical memory, or the container's limit). Jobs of a remote
//! execution environment are in neither.
//!
//! Within a domain, a job is killed in only two cases, each after a final
//! warning went unanswered:
//!
//! 1. the agent's jobs together hold more than the domain's memory: its
//! largest job goes;
//! 2. every agent's jobs together hold more than the domain's memory while
//! pressure is critical: the agent that contributes the most loses its
//! largest job, and once that agent's total drops, the next contributor
//! is first in line, while both conditions hold.
//!
//! Each agent runs its own guard and kills only its own jobs; the footprint
//! registry of [`JobMemory`] only totals and ranks.
use std::time::Duration;
use aither_sandbox::{JobMemory, MemorySize, MemorySnapshot};
use crate::clock::Instant;
use crate::host_load::{HostLoadEvent, MemoryPressure, psi_memory_pressure};
use crate::pressure::{LevelTracker, Shift};
#[cfg(all(test, unix))]
mod tests;
/// The final warning an agent has not yet acted on.
#[derive(Debug)]
struct Warned {
task_id: String,
since: Instant,
window: Duration,
}
/// What the guard remembers of one domain between readings.
#[derive(Debug)]
struct Watch {
pressure: LevelTracker<MemoryPressure>,
warned: Option<Warned>,
}
impl Watch {
const fn new() -> Self {
Self {
pressure: LevelTracker::new(MemoryPressure::Normal),
warned: None,
}
}
/// Takes the domain's pressure (`None` when it could not be read, which
/// leaves the last level in force) and its memory at `now`, and returns
/// the alerts due, killing the job a final warning named when its window
/// passed unanswered.
async fn observe(
&mut self,
memory: &JobMemory,
now: Instant,
pressure: Option<MemoryPressure>,
snapshot: &MemorySnapshot,
) -> Vec<HostLoadEvent> {
let domain = snapshot.domain;
let pressure = pressure.unwrap_or_else(|| self.pressure.level());
let mut events = Vec::new();
match self.pressure.observe(pressure) {
Some(Shift::Rose(MemoryPressure::Warning)) => {
events.push(HostLoadEvent::MemoryYellow {
domain,
jobs: snapshot.jobs.clone(),
});
}
Some(Shift::Rose(MemoryPressure::Critical)) => {
events.push(HostLoadEvent::MemoryRed {
domain,
jobs: snapshot.jobs.clone(),
});
}
Some(Shift::Fell(level)) => {
events.push(HostLoadEvent::MemoryDroppedBack {
domain,
level,
jobs: snapshot.jobs.clone(),
});
}
Some(Shift::Rose(MemoryPressure::Normal)) | None => {}
}
let capacity = snapshot.capacity_bytes;
let alone = snapshot.exceeded_by(snapshot.own_bytes);
let together = snapshot.exceeded_by(snapshot.total_bytes)
&& pressure == MemoryPressure::Critical
&& snapshot.largest_contributor;
if !(alone || together) {
self.warned = None;
return events;
}
match self.warned.take() {
Some(warned) => {
// A job that is gone was acted on; the next tick warns about
// the next one if the conditions still hold.
if let Some(job) = snapshot
.jobs
.iter()
.find(|job| job.task_id == warned.task_id)
{
if now.duration_since(warned.since) >= warned.window {
if memory.kill_runaway(job, snapshot).await {
events.push(HostLoadEvent::JobKilledForMemory {
domain,
task_id: job.task_id.clone(),
command: job.command.clone(),
footprint: MemorySize(job.footprint_bytes),
capacity: MemorySize(capacity),
});
}
} else {
self.warned = Some(warned);
}
}
}
None => {
if let Some(job) = snapshot.jobs.first().filter(|job| job.footprint_bytes > 0) {
let window = memory.response_window();
self.warned = Some(Warned {
task_id: job.task_id.clone(),
since: now,
window,
});
events.push(HostLoadEvent::MemoryFinalWarning {
domain,
job: job.clone(),
window,
capacity: MemorySize(capacity),
});
}
}
}
events
}
}
/// The memory guard of one agent.
#[derive(Debug)]
pub struct RunawayGuard {
memory: JobMemory,
machine: Watch,
container: Watch,
}
impl RunawayGuard {
/// A guard over the jobs `memory` sees.
#[must_use]
pub const fn new(memory: JobMemory) -> Self {
Self {
memory,
machine: Watch::new(),
container: Watch::new(),
}
}
/// Takes the host's memory pressure at `now` (`None` when it could not
/// be read, which leaves the last level in force) and returns the alerts
/// due, for the machine and for the container when the agent works in one
/// that has a memory limit; kills the job a final warning named when its
/// window passed unanswered.
pub async fn observe(
&mut self,
now: Instant,
pressure: Option<MemoryPressure>,
) -> Vec<HostLoadEvent> {
let mut events = self.observe_machine(now, pressure).await;
events.extend(self.observe_container(now).await);
events
}
async fn observe_machine(
&mut self,
now: Instant,
pressure: Option<MemoryPressure>,
) -> Vec<HostLoadEvent> {
match self.memory.snapshot().await {
Ok(snapshot) => {
self.machine
.observe(&self.memory, now, pressure, &snapshot)
.await
}
Err(error) => {
tracing::warn!(%error, "the memory of the agent's jobs could not be read");
Vec::new()
}
}
}
async fn observe_container(&mut self, now: Instant) -> Vec<HostLoadEvent> {
match self.memory.container_snapshot().await {
Ok(Some(container)) => {
// A kernel without PSI states no pressure, which is none.
let pressure = if container.pressure.is_empty() {
Some(MemoryPressure::Normal)
} else {
let level = psi_memory_pressure(&container.pressure);
if level.is_none() {
tracing::warn!(
pressure = container.pressure,
"the container's memory pressure is not understood"
);
}
level
};
self.container
.observe(&self.memory, now, pressure, &container.snapshot)
.await
}
// No container, or one with no memory limit: nothing to judge.
Ok(None) => Vec::new(),
Err(error) => {
tracing::warn!(%error, "the memory of the container could not be read");
Vec::new()
}
}
}
}