@mark1russell7/harness 0.16.5 → 0.16.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +13 -0
- package/build/{cell-Ckoqr7Xc.d.ts → cell-FMlgjqVq.d.ts} +81 -3
- package/build/{image-Bs5hV85M.js → image-YlflAajQ.js} +6 -2
- package/build/image-YlflAajQ.js.map +1 -0
- package/build/index.d.ts +63 -4
- package/build/index.js +261 -18
- package/build/index.js.map +1 -1
- package/build/schema.d.ts +1 -1
- package/build/schema.js +1 -1
- package/image/Dockerfile +1 -1
- package/image/native/src/bin/memory.rs +460 -0
- package/package.json +5 -3
- package/build/image-Bs5hV85M.js.map +0 -1
package/build/schema.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { M as ImageSource,
|
|
1
|
+
import { M as ImageSource, Rt as Subject, i as Client, o as ClientImage } from "./cell-FMlgjqVq.js";
|
|
2
2
|
import { z } from "zod";
|
|
3
3
|
/**
|
|
4
4
|
* This schema accepts the source of an image (`ImageSource`). It is a pinned image with its digest,
|
package/build/schema.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { B as CLIENT_CAPABILITIES, O as subjectProblems, a as SHIPPED_NAMES, h as imageProblems, t as CLIENT_NAMES } from "./image-
|
|
1
|
+
import { B as CLIENT_CAPABILITIES, O as subjectProblems, a as SHIPPED_NAMES, h as imageProblems, t as CLIENT_NAMES } from "./image-YlflAajQ.js";
|
|
2
2
|
import { z } from "zod";
|
|
3
3
|
// src/schema.ts
|
|
4
4
|
/** This function adds each problem that a check finds as an issue of a schema. */
|
package/image/Dockerfile
CHANGED
|
@@ -26,6 +26,6 @@ FROM alpine:3.23@sha256:865b95f46d98cf867a156fe4a135ad3fe50d2056aa3f25ed31662dff
|
|
|
26
26
|
# tc for netem, ethtool for the offloads that decide whether netem acts on segments
|
|
27
27
|
RUN apk add --no-cache iproute2 iproute2-tc iptables ethtool libelf zlib zstd-libs libgcc
|
|
28
28
|
COPY --from=build /src/prober.bpf.o /src/prober/native/target/release/prober /opt/harness/
|
|
29
|
-
COPY --from=build /src/harness/image/native/target/release/client /src/harness/image/native/target/release/inject /src/harness/image/native/target/release/queue /src/harness/image/native/target/release/ports /src/harness/image/native/target/release/ctfull /src/harness/image/native/target/release/load /opt/harness/
|
|
29
|
+
COPY --from=build /src/harness/image/native/target/release/client /src/harness/image/native/target/release/inject /src/harness/image/native/target/release/queue /src/harness/image/native/target/release/ports /src/harness/image/native/target/release/ctfull /src/harness/image/native/target/release/load /src/harness/image/native/target/release/memory /opt/harness/
|
|
30
30
|
WORKDIR /opt/harness
|
|
31
31
|
CMD ["sleep", "infinity"]
|
|
@@ -0,0 +1,460 @@
|
|
|
1
|
+
//! memory grow --until-pages N [--step N] [--wait-s N] HOST PORT [--mode M] [--max-s N]
|
|
2
|
+
//! memory oom --task NAME:PAGES:ADJ [--task NAME:PAGES:ADJ]... [--wait-s N] HOST PORT [--mode M] [--max-s N]
|
|
3
|
+
//! memory task NAME PAGES ADJ
|
|
4
|
+
//!
|
|
5
|
+
//! This program is the client of the memory cells. Each line is JSON with an `ev` and a `t` on
|
|
6
|
+
//! CLOCK_MONOTONIC. The program does not connect. The address and the port of the target are in its
|
|
7
|
+
//! argv because the harness gives them to each client.
|
|
8
|
+
//!
|
|
9
|
+
//! The harness writes the files of the cgroup of the container, and then it makes the file
|
|
10
|
+
//! `/tmp/cgroup`. The program waits for that file, and it writes `ready` with the limits that it
|
|
11
|
+
//! reads. The container has a cgroup namespace of its own, thus its cgroup is the root of
|
|
12
|
+
//! `/sys/fs/cgroup`.
|
|
13
|
+
//!
|
|
14
|
+
//! `grow` keeps itself on one CPU, because the stock of a charge is for each CPU. It maps anonymous
|
|
15
|
+
//! memory with no huge pages, thus each fault charges one page. It touches that memory in steps of
|
|
16
|
+
//! `--step` pages until `memory.current` reaches `--until-pages`. Before it maps that memory, it locks
|
|
17
|
+
//! the pages that it has, thus a reclaim frees none of them. The `ready` line says `locked` true or
|
|
18
|
+
//! false. A `step` line gives each step: its time, the usage after it and the count of the event
|
|
19
|
+
//! `high`. The last lines are `pressure`, `events` and `done`.
|
|
20
|
+
//!
|
|
21
|
+
//! `oom` starts each task in order, and it waits until the task holds its pages or ends. Each task is
|
|
22
|
+
//! the program again, as `memory task`. A `task` line then gives each task: `held`, `killed` by
|
|
23
|
+
//! SIGKILL, or `ended`. The last lines are `events` and `done`.
|
|
24
|
+
//!
|
|
25
|
+
//! `task` sets its own `oom_score_adj`, and it touches its pages. It then writes `held` with its
|
|
26
|
+
//! resident pages, and it sleeps until its parent stops it.
|
|
27
|
+
|
|
28
|
+
use harness_native::{emit, emit_at};
|
|
29
|
+
use prober::monotonic_ns;
|
|
30
|
+
use serde_json::{json, Map, Value};
|
|
31
|
+
use std::error::Error;
|
|
32
|
+
use std::fs::File;
|
|
33
|
+
use std::io::{BufRead, BufReader};
|
|
34
|
+
use std::os::unix::fs::FileExt;
|
|
35
|
+
use std::os::unix::process::ExitStatusExt;
|
|
36
|
+
use std::path::Path;
|
|
37
|
+
use std::process::{Child, Command, Stdio};
|
|
38
|
+
use std::time::Duration;
|
|
39
|
+
|
|
40
|
+
/// the cgroup of the container: its own namespace shows it at the root of the mount
|
|
41
|
+
const CGROUP: &str = "/sys/fs/cgroup";
|
|
42
|
+
/// the harness makes this file after it writes the files of the cgroup
|
|
43
|
+
const SET: &str = "/tmp/cgroup";
|
|
44
|
+
/// the wait after the last task, so that the OOM killer ends its victim, in milliseconds
|
|
45
|
+
const SETTLE_MS: u64 = 1_000;
|
|
46
|
+
const SECOND_NS: u64 = 1_000_000_000;
|
|
47
|
+
const USAGE: &str = "memory grow --until-pages N [flags] HOST PORT | memory oom --task NAME:PAGES:ADJ... HOST PORT | memory task NAME PAGES ADJ";
|
|
48
|
+
|
|
49
|
+
#[derive(Debug, PartialEq)]
|
|
50
|
+
struct Grow {
|
|
51
|
+
until: u64,
|
|
52
|
+
step: u64,
|
|
53
|
+
wait_s: u64,
|
|
54
|
+
max_s: u64,
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
#[derive(Debug, PartialEq)]
|
|
58
|
+
struct Task {
|
|
59
|
+
name: String,
|
|
60
|
+
pages: u64,
|
|
61
|
+
adj: i32,
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
#[derive(Debug, PartialEq)]
|
|
65
|
+
struct Oom {
|
|
66
|
+
tasks: Vec<Task>,
|
|
67
|
+
wait_s: u64,
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/// Reads the flags of `grow`. The harness gives `--mode` and `--max-s` to each client, and the place of the target.
|
|
71
|
+
fn grow_of(args: &[String]) -> Result<Grow, Box<dyn Error>> {
|
|
72
|
+
let mut plan = Grow { until: 0, step: 64, wait_s: 30, max_s: 600 };
|
|
73
|
+
let mut rest = args.iter();
|
|
74
|
+
while let Some(arg) = rest.next() {
|
|
75
|
+
let mut value = || rest.next().ok_or_else(|| format!("{arg} needs a value"));
|
|
76
|
+
match arg.as_str() {
|
|
77
|
+
"--until-pages" => plan.until = value()?.parse()?,
|
|
78
|
+
"--step" => plan.step = value()?.parse()?,
|
|
79
|
+
"--wait-s" => plan.wait_s = value()?.parse()?,
|
|
80
|
+
// the harness writes the window of the cell in seconds, which can have a fraction
|
|
81
|
+
"--max-s" => plan.max_s = value()?.parse::<f64>()?.ceil() as u64,
|
|
82
|
+
"--mode" => {
|
|
83
|
+
value()?;
|
|
84
|
+
}
|
|
85
|
+
other if other.starts_with("--") => return Err(format!("no flag {other}").into()),
|
|
86
|
+
_ => {}
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
if plan.until == 0 {
|
|
90
|
+
return Err("grow needs --until-pages".into());
|
|
91
|
+
}
|
|
92
|
+
if plan.step == 0 {
|
|
93
|
+
return Err("a step of 0 pages touches nothing".into());
|
|
94
|
+
}
|
|
95
|
+
Ok(plan)
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/// Reads one task of `oom`: its name, its pages and its `oom_score_adj`.
|
|
99
|
+
fn task_of(text: &str) -> Result<Task, Box<dyn Error>> {
|
|
100
|
+
let mut parts = text.split(':');
|
|
101
|
+
match (parts.next(), parts.next(), parts.next(), parts.next()) {
|
|
102
|
+
(Some(name), Some(pages), Some(adj), None) if !name.is_empty() => Ok(Task { name: name.into(), pages: pages.parse()?, adj: adj.parse()? }),
|
|
103
|
+
_ => Err(format!("a task is NAME:PAGES:ADJ, not {text}").into()),
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/// Reads the flags of `oom`.
|
|
108
|
+
fn oom_of(args: &[String]) -> Result<Oom, Box<dyn Error>> {
|
|
109
|
+
let mut plan = Oom { tasks: Vec::new(), wait_s: 30 };
|
|
110
|
+
let mut rest = args.iter();
|
|
111
|
+
while let Some(arg) = rest.next() {
|
|
112
|
+
let mut value = || rest.next().ok_or_else(|| format!("{arg} needs a value"));
|
|
113
|
+
match arg.as_str() {
|
|
114
|
+
"--task" => plan.tasks.push(task_of(value()?)?),
|
|
115
|
+
"--wait-s" => plan.wait_s = value()?.parse()?,
|
|
116
|
+
"--mode" | "--max-s" => {
|
|
117
|
+
value()?;
|
|
118
|
+
}
|
|
119
|
+
other if other.starts_with("--") => return Err(format!("no flag {other}").into()),
|
|
120
|
+
_ => {}
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
if plan.tasks.is_empty() {
|
|
124
|
+
return Err("oom needs a --task".into());
|
|
125
|
+
}
|
|
126
|
+
Ok(plan)
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
fn page_size() -> u64 {
|
|
130
|
+
// SAFETY: sysconf reads a constant of the system
|
|
131
|
+
unsafe { libc::sysconf(libc::_SC_PAGESIZE) as u64 }
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
fn number(text: &str) -> Result<u64, Box<dyn Error>> {
|
|
135
|
+
Ok(text.trim().parse()?)
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/// The counts of a flat keyed file of a cgroup, as `memory.events` gives them: one `key value` on each line.
|
|
139
|
+
fn keyed(text: &str) -> Map<String, Value> {
|
|
140
|
+
text.lines()
|
|
141
|
+
.filter_map(|line| {
|
|
142
|
+
let (name, value) = line.split_once(' ')?;
|
|
143
|
+
Some((name.to_string(), json!(value.trim().parse::<u64>().ok()?)))
|
|
144
|
+
})
|
|
145
|
+
.collect()
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/// One count of a flat keyed file of a cgroup.
|
|
149
|
+
fn count_of(text: &str, key: &str) -> Option<u64> {
|
|
150
|
+
text.lines().find_map(|line| {
|
|
151
|
+
let (name, value) = line.split_once(' ')?;
|
|
152
|
+
if name == key {
|
|
153
|
+
value.trim().parse().ok()
|
|
154
|
+
} else {
|
|
155
|
+
None
|
|
156
|
+
}
|
|
157
|
+
})
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/// The total stall of one line of `memory.pressure` (`some` or `full`), in microseconds.
|
|
161
|
+
fn stall_of(text: &str, kind: &str) -> Option<u64> {
|
|
162
|
+
text.lines()
|
|
163
|
+
.find(|line| line.split_whitespace().next() == Some(kind))?
|
|
164
|
+
.split_whitespace()
|
|
165
|
+
.find_map(|field| field.strip_prefix("total=")?.parse().ok())
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
fn read(name: &str) -> Result<String, Box<dyn Error>> {
|
|
169
|
+
let path = format!("{CGROUP}/{name}");
|
|
170
|
+
Ok(std::fs::read_to_string(&path).map_err(|error| format!("{path}: {error}"))?.trim().to_string())
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/// The limits of the cgroup as the kernel prints them, its usage in pages and its events.
|
|
174
|
+
fn limits(page: u64) -> Result<Value, Box<dyn Error>> {
|
|
175
|
+
Ok(json!({
|
|
176
|
+
"high": read("memory.high")?,
|
|
177
|
+
"max": read("memory.max")?,
|
|
178
|
+
"swap": read("memory.swap.max")?,
|
|
179
|
+
"current": number(&read("memory.current")?)? / page,
|
|
180
|
+
"events": keyed(&read("memory.events")?),
|
|
181
|
+
"page": page,
|
|
182
|
+
}))
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
/// One file of the cgroup, open for the whole run. Each read is a pread at 0, which opens no file:
|
|
186
|
+
/// the `struct file` of an open is charged to the cgroup too.
|
|
187
|
+
struct Knob {
|
|
188
|
+
file: File,
|
|
189
|
+
buffer: Vec<u8>,
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
impl Knob {
|
|
193
|
+
fn open(name: &str) -> Result<Knob, Box<dyn Error>> {
|
|
194
|
+
let path = format!("{CGROUP}/{name}");
|
|
195
|
+
let file = File::open(&path).map_err(|error| format!("{path}: {error}"))?;
|
|
196
|
+
Ok(Knob { file, buffer: vec![0; 4096] })
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
fn text(&mut self) -> Result<String, Box<dyn Error>> {
|
|
200
|
+
let read = self.file.read_at(&mut self.buffer, 0)?;
|
|
201
|
+
Ok(String::from_utf8_lossy(&self.buffer[..read]).trim().to_string())
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
/// Waits for the file of the harness. It gives false when `wait_s` passes first.
|
|
206
|
+
fn wait_set(wait_s: u64) -> bool {
|
|
207
|
+
let end = monotonic_ns() + wait_s * SECOND_NS;
|
|
208
|
+
while !Path::new(SET).exists() {
|
|
209
|
+
if monotonic_ns() > end {
|
|
210
|
+
return false;
|
|
211
|
+
}
|
|
212
|
+
std::thread::sleep(Duration::from_millis(1));
|
|
213
|
+
}
|
|
214
|
+
true
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
/// Keeps the task on the last CPU that it can use, which other work uses less than CPU 0. It gives
|
|
218
|
+
/// that CPU.
|
|
219
|
+
fn pin() -> Result<usize, Box<dyn Error>> {
|
|
220
|
+
let size = std::mem::size_of::<libc::cpu_set_t>();
|
|
221
|
+
// SAFETY: each set is on this stack, passed with its exact size
|
|
222
|
+
unsafe {
|
|
223
|
+
let mut allowed: libc::cpu_set_t = std::mem::zeroed();
|
|
224
|
+
if libc::sched_getaffinity(0, size, &mut allowed) != 0 {
|
|
225
|
+
return Err(std::io::Error::last_os_error().into());
|
|
226
|
+
}
|
|
227
|
+
let cpu = (0..libc::CPU_SETSIZE as usize).rev().find(|&cpu| libc::CPU_ISSET(cpu, &allowed)).ok_or("the task can use no CPU")?;
|
|
228
|
+
let mut one: libc::cpu_set_t = std::mem::zeroed();
|
|
229
|
+
libc::CPU_SET(cpu, &mut one);
|
|
230
|
+
if libc::sched_setaffinity(0, size, &one) != 0 {
|
|
231
|
+
return Err(std::io::Error::last_os_error().into());
|
|
232
|
+
}
|
|
233
|
+
Ok(cpu)
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
/// Locks each page that the program maps at this point: its code, its libraries and its stack. A
|
|
238
|
+
/// reclaim above `memory.high` then frees no page of the program, and each step charges one batch. It
|
|
239
|
+
/// gives false when the kernel refuses the lock, for example above `RLIMIT_MEMLOCK`. It comes before the
|
|
240
|
+
/// map of the region, because `MCL_CURRENT` faults in each page that is mapped.
|
|
241
|
+
fn lock() -> bool {
|
|
242
|
+
// SAFETY: the call changes no memory of the program
|
|
243
|
+
unsafe { libc::mlockall(libc::MCL_CURRENT) == 0 }
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
/// Anonymous memory with no huge pages: the fault of each page charges one page to the cgroup.
|
|
247
|
+
struct Region {
|
|
248
|
+
base: *mut u8,
|
|
249
|
+
pages: u64,
|
|
250
|
+
page: u64,
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
impl Region {
|
|
254
|
+
fn map(pages: u64, page: u64) -> Result<Region, Box<dyn Error>> {
|
|
255
|
+
let bytes = (pages * page) as usize;
|
|
256
|
+
// SAFETY: a new private anonymous mapping, which nothing else holds
|
|
257
|
+
let base = unsafe {
|
|
258
|
+
libc::mmap(
|
|
259
|
+
std::ptr::null_mut(),
|
|
260
|
+
bytes,
|
|
261
|
+
libc::PROT_READ | libc::PROT_WRITE,
|
|
262
|
+
libc::MAP_PRIVATE | libc::MAP_ANONYMOUS | libc::MAP_NORESERVE,
|
|
263
|
+
-1,
|
|
264
|
+
0,
|
|
265
|
+
)
|
|
266
|
+
};
|
|
267
|
+
if base == libc::MAP_FAILED {
|
|
268
|
+
return Err(std::io::Error::last_os_error().into());
|
|
269
|
+
}
|
|
270
|
+
// SAFETY: the range is the mapping above
|
|
271
|
+
if unsafe { libc::madvise(base, bytes, libc::MADV_NOHUGEPAGE) } != 0 {
|
|
272
|
+
return Err(std::io::Error::last_os_error().into());
|
|
273
|
+
}
|
|
274
|
+
Ok(Region { base: base.cast(), pages, page })
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
/// Writes one byte in each page from `from` to `to`, in the mapping.
|
|
278
|
+
fn touch(&self, from: u64, to: u64) {
|
|
279
|
+
for index in from..to.min(self.pages) {
|
|
280
|
+
// SAFETY: the page is inside the mapping
|
|
281
|
+
unsafe { std::ptr::write_volatile(self.base.add((index * self.page) as usize), 1) }
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
fn grow(plan: &Grow) -> Result<(), Box<dyn Error>> {
|
|
287
|
+
let page = page_size();
|
|
288
|
+
let cpu = pin()?;
|
|
289
|
+
if !wait_set(plan.wait_s) {
|
|
290
|
+
emit("deadline", json!({ "wait": "cgroup" }));
|
|
291
|
+
return Ok(());
|
|
292
|
+
}
|
|
293
|
+
let mut current = Knob::open("memory.current")?;
|
|
294
|
+
let mut events = Knob::open("memory.events")?;
|
|
295
|
+
let locked = lock();
|
|
296
|
+
let region = Region::map(plan.until, page)?;
|
|
297
|
+
let mut ready = limits(page)?;
|
|
298
|
+
ready["cpu"] = json!(cpu);
|
|
299
|
+
ready["locked"] = json!(locked);
|
|
300
|
+
emit("ready", ready);
|
|
301
|
+
let end = monotonic_ns() + plan.max_s * SECOND_NS;
|
|
302
|
+
let mut usage = number(¤t.text()?)? / page;
|
|
303
|
+
let mut touched = 0;
|
|
304
|
+
let mut index = 0u64;
|
|
305
|
+
while usage < plan.until {
|
|
306
|
+
if touched >= region.pages {
|
|
307
|
+
return Err(format!("{touched} pages are touched, and the usage is {usage} pages, below {}", plan.until).into());
|
|
308
|
+
}
|
|
309
|
+
if monotonic_ns() > end {
|
|
310
|
+
emit("deadline", json!({ "wait": "growth", "current": usage }));
|
|
311
|
+
return Ok(());
|
|
312
|
+
}
|
|
313
|
+
let start = monotonic_ns();
|
|
314
|
+
region.touch(touched, touched + plan.step);
|
|
315
|
+
let stop = monotonic_ns();
|
|
316
|
+
touched += plan.step;
|
|
317
|
+
usage = number(¤t.text()?)? / page;
|
|
318
|
+
let high = count_of(&events.text()?, "high").unwrap_or(0);
|
|
319
|
+
emit_at("step", stop, json!({ "n": index, "ns": stop - start, "current": usage, "high": high }));
|
|
320
|
+
index += 1;
|
|
321
|
+
}
|
|
322
|
+
let pressure = std::fs::read_to_string(format!("{CGROUP}/memory.pressure")).ok();
|
|
323
|
+
let stall = |kind: &str| pressure.as_deref().and_then(|text| stall_of(text, kind));
|
|
324
|
+
emit("pressure", json!({ "some": stall("some"), "full": stall("full") }));
|
|
325
|
+
emit("events", Value::Object(keyed(&events.text()?)));
|
|
326
|
+
emit("done", json!({ "touched": touched }));
|
|
327
|
+
Ok(())
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
fn oom(plan: &Oom) -> Result<(), Box<dyn Error>> {
|
|
331
|
+
let page = page_size();
|
|
332
|
+
if !wait_set(plan.wait_s) {
|
|
333
|
+
emit("deadline", json!({ "wait": "cgroup" }));
|
|
334
|
+
return Ok(());
|
|
335
|
+
}
|
|
336
|
+
emit("ready", limits(page)?);
|
|
337
|
+
let me = std::env::current_exe()?;
|
|
338
|
+
let mut children: Vec<(&Task, Child)> = Vec::new();
|
|
339
|
+
for task in &plan.tasks {
|
|
340
|
+
let mut child = Command::new(&me)
|
|
341
|
+
.args(["task", &task.name, &task.pages.to_string(), &task.adj.to_string()])
|
|
342
|
+
.stdout(Stdio::piped())
|
|
343
|
+
.spawn()?;
|
|
344
|
+
let out = child.stdout.take().ok_or("the task has no output")?;
|
|
345
|
+
// each line of the task goes on as a line of this program, until the task holds its pages or ends
|
|
346
|
+
for line in BufReader::new(out).lines() {
|
|
347
|
+
let line = line?;
|
|
348
|
+
println!("{line}");
|
|
349
|
+
if line.contains("\"ev\":\"held\"") {
|
|
350
|
+
break;
|
|
351
|
+
}
|
|
352
|
+
}
|
|
353
|
+
children.push((task, child));
|
|
354
|
+
}
|
|
355
|
+
std::thread::sleep(Duration::from_millis(SETTLE_MS));
|
|
356
|
+
for (task, child) in &mut children {
|
|
357
|
+
let pid = child.id();
|
|
358
|
+
let fields = match child.try_wait()? {
|
|
359
|
+
None => json!({ "name": task.name, "pid": pid, "outcome": "held", "signal": null }),
|
|
360
|
+
Some(status) => {
|
|
361
|
+
let outcome = if status.signal() == Some(libc::SIGKILL) { "killed" } else { "ended" };
|
|
362
|
+
json!({ "name": task.name, "pid": pid, "outcome": outcome, "signal": status.signal(), "code": status.code() })
|
|
363
|
+
}
|
|
364
|
+
};
|
|
365
|
+
emit("task", fields);
|
|
366
|
+
}
|
|
367
|
+
emit("events", Value::Object(keyed(&read("memory.events")?)));
|
|
368
|
+
for (_, child) in &mut children {
|
|
369
|
+
if child.try_wait()?.is_none() {
|
|
370
|
+
child.kill()?;
|
|
371
|
+
child.wait()?;
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
emit("done", json!({}));
|
|
375
|
+
Ok(())
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
fn task(args: &[String]) -> Result<(), Box<dyn Error>> {
|
|
379
|
+
let [name, pages, adj] = args else {
|
|
380
|
+
return Err("memory task NAME PAGES ADJ".into());
|
|
381
|
+
};
|
|
382
|
+
let pages: u64 = pages.parse()?;
|
|
383
|
+
let adj: i32 = adj.parse()?;
|
|
384
|
+
// a value below the value of the parent needs CAP_SYS_RESOURCE
|
|
385
|
+
std::fs::write("/proc/self/oom_score_adj", adj.to_string()).map_err(|error| format!("oom_score_adj {adj}: {error}"))?;
|
|
386
|
+
let region = Region::map(pages, page_size())?;
|
|
387
|
+
region.touch(0, pages);
|
|
388
|
+
let statm = std::fs::read_to_string("/proc/self/statm")?;
|
|
389
|
+
let rss: u64 = statm.split_whitespace().nth(1).ok_or("statm has no resident count")?.parse()?;
|
|
390
|
+
emit("held", json!({ "name": name, "pid": std::process::id(), "pages": pages, "rss": rss, "adj": adj }));
|
|
391
|
+
loop {
|
|
392
|
+
std::thread::sleep(Duration::from_secs(1));
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
fn main() {
|
|
397
|
+
let args: Vec<String> = std::env::args().skip(1).collect();
|
|
398
|
+
let outcome = match args.first().map(String::as_str) {
|
|
399
|
+
Some("grow") => grow_of(&args[1..]).and_then(|plan| grow(&plan)),
|
|
400
|
+
Some("oom") => oom_of(&args[1..]).and_then(|plan| oom(&plan)),
|
|
401
|
+
Some("task") => task(&args[1..]),
|
|
402
|
+
_ => Err(USAGE.into()),
|
|
403
|
+
};
|
|
404
|
+
// an error is a line too: the harness ends the run at it, and the judge makes the run void
|
|
405
|
+
if let Err(error) = outcome {
|
|
406
|
+
emit("error", json!({ "what": error.to_string() }));
|
|
407
|
+
std::process::exit(1);
|
|
408
|
+
}
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
#[cfg(test)]
|
|
412
|
+
mod tests {
|
|
413
|
+
use super::*;
|
|
414
|
+
|
|
415
|
+
fn argv(each: &[&str]) -> Vec<String> {
|
|
416
|
+
each.iter().map(|arg| arg.to_string()).collect()
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
#[test]
|
|
420
|
+
fn a_growth_reads_its_flags_and_skips_the_flags_of_the_harness() {
|
|
421
|
+
let plan = grow_of(&argv(&["--until-pages", "30400", "10.0.0.2", "6392", "--mode", "idle", "--max-s", "70.5"])).expect("a plan");
|
|
422
|
+
assert_eq!(plan, Grow { until: 30_400, step: 64, wait_s: 30, max_s: 71 });
|
|
423
|
+
assert!(grow_of(&argv(&["10.0.0.2", "6392"])).is_err());
|
|
424
|
+
assert!(grow_of(&argv(&["--until-pages", "10", "--step", "0"])).is_err());
|
|
425
|
+
assert!(grow_of(&argv(&["--until-pages", "10", "--no-such", "1"])).is_err());
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
#[test]
|
|
429
|
+
fn an_oom_reads_each_task_with_a_negative_adjustment() {
|
|
430
|
+
let plan = oom_of(&argv(&["--task", "a:46080:-1000", "--task", "b:7680:0", "10.0.0.2", "6392", "--mode", "idle", "--max-s", "30"])).expect("a plan");
|
|
431
|
+
assert_eq!(plan.tasks, vec![Task { name: "a".into(), pages: 46_080, adj: -1000 }, Task { name: "b".into(), pages: 7_680, adj: 0 }]);
|
|
432
|
+
assert!(oom_of(&argv(&["10.0.0.2", "6392"])).is_err());
|
|
433
|
+
assert!(task_of("a:1").is_err());
|
|
434
|
+
assert!(task_of(":1:0").is_err());
|
|
435
|
+
assert!(task_of("a:1:0:9").is_err());
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
#[test]
|
|
439
|
+
fn the_files_of_a_cgroup_give_their_counts() {
|
|
440
|
+
let events = "low 0\nhigh 52\nmax 0\noom 1\noom_kill 1\noom_group_kill 0\n";
|
|
441
|
+
assert_eq!(count_of(events, "high"), Some(52));
|
|
442
|
+
assert_eq!(count_of(events, "oom_kill"), Some(1));
|
|
443
|
+
assert_eq!(count_of(events, "nothing"), None);
|
|
444
|
+
assert_eq!(keyed(events).get("oom"), Some(&json!(1)));
|
|
445
|
+
assert_eq!(keyed(events).len(), 6);
|
|
446
|
+
let pressure = "some avg10=0.00 avg60=0.00 avg300=0.00 total=1234\nfull avg10=0.00 avg60=0.00 avg300=0.00 total=567\n";
|
|
447
|
+
assert_eq!(stall_of(pressure, "some"), Some(1234));
|
|
448
|
+
assert_eq!(stall_of(pressure, "full"), Some(567));
|
|
449
|
+
assert_eq!(stall_of("", "some"), None);
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
#[test]
|
|
453
|
+
fn a_region_takes_a_touch_of_each_of_its_pages() {
|
|
454
|
+
let page = page_size();
|
|
455
|
+
let region = Region::map(8, page).expect("a mapping");
|
|
456
|
+
region.touch(0, 100);
|
|
457
|
+
// SAFETY: the page is inside the mapping
|
|
458
|
+
assert_eq!(unsafe { std::ptr::read_volatile(region.base.add((7 * page) as usize)) }, 1);
|
|
459
|
+
}
|
|
460
|
+
}
|
package/package.json
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
{
|
|
2
2
|
"$schema": "https://json.schemastore.org/package",
|
|
3
3
|
"name": "@mark1russell7/harness",
|
|
4
|
-
"version": "0.16.
|
|
5
|
-
"description": "Network labs in Docker, declared as data. A cell builds a topology of networks, routers and proxies around a stock target. It injects a fault at layer 3 or 4 from a NET_ADMIN sidecar, measures the records of the kernel and judges a prediction. The faults are black holes, resets, ICMP unreachable, one-way and MTU drops, tc netem, the toxics of Toxiproxy and frozen processes. Its disk tier cuts the power of a program on LazyFS: the disk loses each write after the last fsync. Its replicated groups are Redis with Sentinels, MongoDB replica sets, PostgreSQL standbys, Kafka, RabbitMQ and BGP fabrics with BFD. A lab can also operate in a QEMU guest. Its first use is connection timers: keep-alive, TCP retransmission, heartbeats and reconnects.",
|
|
4
|
+
"version": "0.16.7",
|
|
5
|
+
"description": "Network labs in Docker, declared as data. A cell builds a topology of networks, routers and proxies around a stock target. It injects a fault at layer 3 or 4 from a NET_ADMIN sidecar, measures the records of the kernel and judges a prediction. The faults are black holes, resets, ICMP unreachable, one-way and MTU drops, tc netem, the toxics of Toxiproxy and frozen processes. Its disk tier cuts the power of a program on LazyFS: the disk loses each write after the last fsync. Its memory tier sets the limits of the cgroup of a client, and judges its throttle above memory.high and the choice of the OOM killer. Its replicated groups are Redis with Sentinels, MongoDB replica sets, PostgreSQL standbys, Kafka, RabbitMQ and BGP fabrics with BFD. A lab can also operate in a QEMU guest. Its first use is connection timers: keep-alive, TCP retransmission, heartbeats and reconnects.",
|
|
6
6
|
"keywords": [
|
|
7
7
|
"fault-injection",
|
|
8
8
|
"chaos-engineering",
|
|
@@ -20,7 +20,9 @@
|
|
|
20
20
|
"bgp",
|
|
21
21
|
"qemu",
|
|
22
22
|
"lazyfs",
|
|
23
|
-
"power-loss"
|
|
23
|
+
"power-loss",
|
|
24
|
+
"cgroup",
|
|
25
|
+
"oom-killer"
|
|
24
26
|
],
|
|
25
27
|
"homepage": "https://mark1russell7.github.io/systems/",
|
|
26
28
|
"repository": {
|