armybox 0.3.0

A memory-safe #[no_std] BusyBox/Toybox clone in Rust - 299 Unix utilities in ~500KB
Documentation
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
//! vmstat - report virtual memory statistics
//!
//! Report information about processes, memory, paging, etc.

use alloc::vec::Vec;
use crate::io;
use crate::sys;

/// vmstat - report virtual memory statistics
///
/// # Synopsis
/// ```text
/// vmstat [delay [count]]
/// vmstat [-n] [delay [count]]
/// ```
///
/// # Description
/// Report information about processes, memory, paging, block IO,
/// traps, and CPU activity.
///
/// # Options
/// - `-n`: Display header only once
///
/// # Exit Status
/// - 0: Success
pub fn vmstat(argc: i32, argv: *const *const u8) -> i32 {
    let mut delay: u32 = 0;
    let mut count: Option<u32> = None;
    let mut show_header_once = false;

    // Parse arguments
    let mut i = 1;
    while i < argc as usize {
        let arg = match unsafe { super::get_arg(argv, i as i32) } {
            Some(a) => a,
            None => break,
        };

        if arg == b"-n" {
            show_header_once = true;
        } else if delay == 0 {
            delay = sys::parse_u64(arg).unwrap_or(0) as u32;
        } else if count.is_none() {
            count = Some(sys::parse_u64(arg).unwrap_or(1) as u32);
        }
        i += 1;
    }

    // Print header
    print_header();

    let mut iteration = 0u32;
    loop {
        // Read memory stats
        let mem = read_meminfo();
        let cpu = read_stat();
        let proc_stats = read_proc_stats();

        print_stats(&mem, &cpu, &proc_stats);

        iteration += 1;

        // Check if we should continue
        if delay == 0 {
            break;
        }

        if let Some(max) = count {
            if iteration >= max {
                break;
            }
        }

        // Sleep
        unsafe { libc::sleep(delay) };

        // Print header again if not -n
        if !show_header_once && iteration % 25 == 0 {
            print_header();
        }
    }

    0
}

fn print_header() {
    io::write_str(1, b"procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----\n");
    io::write_str(1, b" r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st\n");
}

/// Memory statistics from /proc/meminfo
struct MemInfo {
    free: u64,
    buffers: u64,
    cached: u64,
    swap_total: u64,
    swap_free: u64,
    swap_in: u64,
    swap_out: u64,
}

/// CPU statistics from /proc/stat
struct CpuStats {
    user: u64,
    nice: u64,
    system: u64,
    idle: u64,
    iowait: u64,
    irq: u64,
    softirq: u64,
    steal: u64,
    interrupts: u64,
    context_switches: u64,
}

/// Process statistics
struct ProcStats {
    running: u32,
    blocked: u32,
}

fn read_meminfo() -> MemInfo {
    let mut mem = MemInfo {
        free: 0,
        buffers: 0,
        cached: 0,
        swap_total: 0,
        swap_free: 0,
        swap_in: 0,
        swap_out: 0,
    };

    let fd = io::open(b"/proc/meminfo", libc::O_RDONLY, 0);
    if fd < 0 {
        return mem;
    }

    let mut buf = [0u8; 4096];
    let n = io::read(fd, &mut buf);
    io::close(fd);

    if n <= 0 {
        return mem;
    }

    let content = &buf[..n as usize];
    for line in content.split(|&c| c == b'\n') {
        if line.starts_with(b"MemFree:") {
            mem.free = parse_meminfo_value(line);
        } else if line.starts_with(b"Buffers:") {
            mem.buffers = parse_meminfo_value(line);
        } else if line.starts_with(b"Cached:") {
            mem.cached = parse_meminfo_value(line);
        } else if line.starts_with(b"SwapTotal:") {
            mem.swap_total = parse_meminfo_value(line);
        } else if line.starts_with(b"SwapFree:") {
            mem.swap_free = parse_meminfo_value(line);
        }
    }

    // Read swap activity from /proc/vmstat
    let fd = io::open(b"/proc/vmstat", libc::O_RDONLY, 0);
    if fd >= 0 {
        let n = io::read(fd, &mut buf);
        io::close(fd);

        if n > 0 {
            let content = &buf[..n as usize];
            for line in content.split(|&c| c == b'\n') {
                if line.starts_with(b"pswpin ") {
                    mem.swap_in = parse_stat_value(line);
                } else if line.starts_with(b"pswpout ") {
                    mem.swap_out = parse_stat_value(line);
                }
            }
        }
    }

    mem
}

fn read_stat() -> CpuStats {
    let mut cpu = CpuStats {
        user: 0,
        nice: 0,
        system: 0,
        idle: 0,
        iowait: 0,
        irq: 0,
        softirq: 0,
        steal: 0,
        interrupts: 0,
        context_switches: 0,
    };

    let fd = io::open(b"/proc/stat", libc::O_RDONLY, 0);
    if fd < 0 {
        return cpu;
    }

    let mut buf = [0u8; 4096];
    let n = io::read(fd, &mut buf);
    io::close(fd);

    if n <= 0 {
        return cpu;
    }

    let content = &buf[..n as usize];
    for line in content.split(|&c| c == b'\n') {
        if line.starts_with(b"cpu ") {
            // cpu user nice system idle iowait irq softirq steal guest guest_nice
            let parts: Vec<&[u8]> = line.split(|&c| c == b' ')
                .filter(|p| !p.is_empty())
                .collect();
            if parts.len() >= 9 {
                cpu.user = sys::parse_u64(parts[1]).unwrap_or(0);
                cpu.nice = sys::parse_u64(parts[2]).unwrap_or(0);
                cpu.system = sys::parse_u64(parts[3]).unwrap_or(0);
                cpu.idle = sys::parse_u64(parts[4]).unwrap_or(0);
                cpu.iowait = sys::parse_u64(parts[5]).unwrap_or(0);
                cpu.irq = sys::parse_u64(parts[6]).unwrap_or(0);
                cpu.softirq = sys::parse_u64(parts[7]).unwrap_or(0);
                if parts.len() >= 9 {
                    cpu.steal = sys::parse_u64(parts[8]).unwrap_or(0);
                }
            }
        } else if line.starts_with(b"intr ") {
            // First number after "intr " is total interrupts
            let parts: Vec<&[u8]> = line.split(|&c| c == b' ')
                .filter(|p| !p.is_empty())
                .collect();
            if parts.len() >= 2 {
                cpu.interrupts = sys::parse_u64(parts[1]).unwrap_or(0);
            }
        } else if line.starts_with(b"ctxt ") {
            cpu.context_switches = parse_stat_value(line);
        }
    }

    cpu
}

fn read_proc_stats() -> ProcStats {
    let mut stats = ProcStats {
        running: 0,
        blocked: 0,
    };

    let fd = io::open(b"/proc/stat", libc::O_RDONLY, 0);
    if fd < 0 {
        return stats;
    }

    let mut buf = [0u8; 4096];
    let n = io::read(fd, &mut buf);
    io::close(fd);

    if n <= 0 {
        return stats;
    }

    let content = &buf[..n as usize];
    for line in content.split(|&c| c == b'\n') {
        if line.starts_with(b"procs_running ") {
            stats.running = parse_stat_value(line) as u32;
        } else if line.starts_with(b"procs_blocked ") {
            stats.blocked = parse_stat_value(line) as u32;
        }
    }

    stats
}

/// Parse value from meminfo line like "MemFree:      1234 kB"
fn parse_meminfo_value(line: &[u8]) -> u64 {
    let parts: Vec<&[u8]> = line.split(|&c| c == b' ')
        .filter(|p| !p.is_empty())
        .collect();
    if parts.len() >= 2 {
        sys::parse_u64(parts[1]).unwrap_or(0)
    } else {
        0
    }
}

/// Parse value from stat line like "ctxt 12345"
fn parse_stat_value(line: &[u8]) -> u64 {
    let parts: Vec<&[u8]> = line.split(|&c| c == b' ')
        .filter(|p| !p.is_empty())
        .collect();
    if parts.len() >= 2 {
        sys::parse_u64(parts[1]).unwrap_or(0)
    } else {
        0
    }
}

fn print_stats(mem: &MemInfo, cpu: &CpuStats, proc_stats: &ProcStats) {
    let mut buf = [0u8; 16];

    // Calculate CPU percentages
    let total_cpu = cpu.user + cpu.nice + cpu.system + cpu.idle +
                    cpu.iowait + cpu.irq + cpu.softirq + cpu.steal;

    let (us, sy, id, wa, st) = if total_cpu > 0 {
        let us = ((cpu.user + cpu.nice) * 100 / total_cpu) as u32;
        let sy = ((cpu.system + cpu.irq + cpu.softirq) * 100 / total_cpu) as u32;
        let wa = (cpu.iowait * 100 / total_cpu) as u32;
        let st = (cpu.steal * 100 / total_cpu) as u32;
        let id = 100u32.saturating_sub(us + sy + wa + st);
        (us, sy, id, wa, st)
    } else {
        (0, 0, 100, 0, 0)
    };

    // Calculate swap used
    let swap_used = mem.swap_total.saturating_sub(mem.swap_free);

    // Print: r b swpd free buff cache si so bi bo in cs us sy id wa st

    // r (running processes)
    print_field(proc_stats.running as u64, 2, &mut buf);

    // b (blocked processes)
    print_field(proc_stats.blocked as u64, 2, &mut buf);

    // swpd (swap used, kB)
    print_field(swap_used, 7, &mut buf);

    // free (free memory, kB)
    print_field(mem.free, 7, &mut buf);

    // buff (buffer memory, kB)
    print_field(mem.buffers, 6, &mut buf);

    // cache (cached memory, kB)
    print_field(mem.cached, 6, &mut buf);

    // si (swap in, kB/s - simplified as total)
    print_field(mem.swap_in % 10000, 5, &mut buf);

    // so (swap out, kB/s - simplified as total)
    print_field(mem.swap_out % 10000, 5, &mut buf);

    // bi (blocks in - placeholder)
    print_field(0, 6, &mut buf);

    // bo (blocks out - placeholder)
    print_field(0, 6, &mut buf);

    // in (interrupts per second - simplified)
    print_field((cpu.interrupts / 100) % 10000, 5, &mut buf);

    // cs (context switches per second - simplified)
    print_field((cpu.context_switches / 100) % 10000, 5, &mut buf);

    // us (user CPU %)
    print_field(us as u64, 3, &mut buf);

    // sy (system CPU %)
    print_field(sy as u64, 3, &mut buf);

    // id (idle CPU %)
    print_field(id as u64, 3, &mut buf);

    // wa (IO wait CPU %)
    print_field(wa as u64, 3, &mut buf);

    // st (steal CPU %)
    print_field(st as u64, 3, &mut buf);

    io::write_str(1, b"\n");
}

fn print_field(value: u64, width: usize, buf: &mut [u8; 16]) {
    let s = sys::format_u64(value, buf);
    for _ in 0..(width.saturating_sub(s.len())) {
        io::write_str(1, b" ");
    }
    io::write_all(1, s);
}

#[cfg(test)]
mod tests {
    extern crate std;
    use std::process::Command;
    use std::path::PathBuf;

    fn get_armybox_path() -> PathBuf {
        if let Ok(path) = std::env::var("ARMYBOX_PATH") {
            return PathBuf::from(path);
        }
        let manifest_dir = std::env::var("CARGO_MANIFEST_DIR")
            .map(PathBuf::from)
            .unwrap_or_else(|_| std::env::current_dir().unwrap());
        let release = manifest_dir.join("target/release/armybox");
        if release.exists() { return release; }
        manifest_dir.join("target/debug/armybox")
    }

    #[test]
    fn test_vmstat_runs() {
        let armybox = get_armybox_path();
        if !armybox.exists() { return; }

        let output = Command::new(&armybox)
            .args(["vmstat"])
            .output()
            .unwrap();

        assert_eq!(output.status.code(), Some(0));
        let stdout = std::string::String::from_utf8_lossy(&output.stdout);
        assert!(stdout.contains("procs"));
        assert!(stdout.contains("memory"));
    }

    #[test]
    fn test_vmstat_with_header_option() {
        let armybox = get_armybox_path();
        if !armybox.exists() { return; }

        let output = Command::new(&armybox)
            .args(["vmstat", "-n"])
            .output()
            .unwrap();

        assert_eq!(output.status.code(), Some(0));
    }
}