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
//! memeater - memory stress test
//!
//! Allocates and touches memory to stress test the system.
extern crate alloc;
use alloc::vec::Vec;
use crate::io;
use crate::sys;
use super::get_arg;
/// memeater - memory stress test
///
/// # Synopsis
/// ```text
/// memeater [size_in_MB]
/// ```
///
/// # Description
/// Allocates and touches memory to stress test the system.
/// Default allocation is 100MB.
///
/// # Exit Status
/// - 0: Success (or interrupted)
pub fn memeater(argc: i32, argv: *const *const u8) -> i32 {
let size_mb = if argc > 1 {
match unsafe { get_arg(argv, 1) }.and_then(|s| sys::parse_u64(s)) {
Some(n) => n as usize,
None => 100,
}
} else {
100 // Default 100MB
};
io::write_str(1, b"memeater: allocating ");
let mut buf = [0u8; 16];
let size_str = sys::format_u64(size_mb as u64, &mut buf);
io::write_all(1, size_str);
io::write_str(1, b" MB...\n");
let total_bytes = size_mb * 1024 * 1024;
let mut memory: Vec<u8> = Vec::with_capacity(total_bytes);
// Fill memory to ensure it's actually allocated
for i in 0..total_bytes {
memory.push((i & 0xFF) as u8);
}
io::write_str(1, b"memeater: allocated. Press Ctrl+C to exit.\n");
// Keep the memory allocated until interrupted
loop {
// Touch memory periodically to prevent it from being swapped out
for i in (0..memory.len()).step_by(4096) {
let _ = memory[i];
}
unsafe {
libc::sleep(1);
}
}
}
#[cfg(test)]
mod tests {
// memeater is designed to run indefinitely, so no automated tests
}