diff --git a/Cargo.lock b/Cargo.lock index 884f323..b129230 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -854,7 +854,7 @@ checksum = "3ad82d2a33cdc9674dc7465672f271e096168fcdbe0f799d9e6db8c5892679dc" [[package]] name = "wsbx" -version = "0.1.2" +version = "0.1.3" dependencies = [ "anyhow", "clap", diff --git a/Cargo.toml b/Cargo.toml index 05ddc38..217d3a6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "wsbx" -version = "0.1.2" +version = "0.1.3" edition = "2024" [dependencies] diff --git a/README.md b/README.md index c5b618e..a35325f 100644 --- a/README.md +++ b/README.md @@ -16,7 +16,8 @@ wsbx run -p 8000 alpine # host's localhost:8000 reachable as localh wsbx exec NAME # shell in a running sandbox (alias: sh), as the image's user wsbx exec -u root NAME -- apk add git # command, exit code propagated wsbx forward NAME 3000 8080:80 # sandbox's localhost:3000 on the Mac's localhost:3000 (until Ctrl-C; alias fwd) -wsbx ps # sandboxes: running/stopped, image, disk use, workspace (alias: ls) +wsbx ps # sandboxes: status, CPU, memory, net/block I/O, PIDs, disk, image (alias: ls) +wsbx ps -f # same, refreshed every second like `docker stats` wsbx rm NAME... # delete stopped sandboxes; --disk deletes only the disk (fresh on next start) wsbx pull IMAGE wsbx gc # delete cached images no sandbox uses diff --git a/src/exec.rs b/src/exec.rs index a509b78..6c271f8 100644 --- a/src/exec.rs +++ b/src/exec.rs @@ -76,7 +76,7 @@ pub fn exec(h: &Path, name: &str, user: Option, argv: Vec) -> Re } }); } - let code = output(s); + let code = output(s, &mut io::stdout().lock(), &mut io::stderr().lock()); if let Some(s) = saved { stty(&[s.trim()]); } @@ -90,10 +90,28 @@ fn send(w: &Mutex, kind: u8, p: &[u8]) -> io::Result<()> { w.lock().unwrap().write_all(&frame(kind, p)) } -/// Copy guest output frames to stdout/stderr until the exit status arrives (None: connection closed first). -fn output(s: UnixStream) -> io::Result> { +/// Run a command as root in a running sandbox (no tty, empty stdin) and return its stdout, at most 64 KiB. +pub fn capture(sock: &Path, argv: &[&str]) -> Result { + let mut s = UnixStream::connect(sock)?; + s.set_read_timeout(Some(Duration::from_secs(2)))?; + // uid 0, not "root": works even if the image has no /etc/passwd + s.write_all(format!("{}\n", json!({"argv": argv, "user": "0", "tty": false})).as_bytes())?; + s.write_all(&frame(b'e', &[]))?; + const CAP: usize = 64 << 10; + let mut buf = vec![0u8; CAP]; + let mut w = &mut buf[..]; // a slice writer errors once full: the guest can't make us buffer more + match output(s, &mut w, &mut io::sink())? { + Some(0) => { + let n = CAP - w.len(); + Ok(String::from_utf8_lossy(&buf[..n]).into_owned()) + } + c => bail!("guest command failed ({c:?})"), + } +} + +/// Copy guest output frames to out/err until the exit status arrives (None: connection closed first). +fn output(s: UnixStream, out: &mut impl Write, err: &mut impl Write) -> io::Result> { let mut r = io::BufReader::new(s); - let (mut out, mut err) = (io::stdout().lock(), io::stderr().lock()); loop { let mut h = [0u8; 5]; if r.read_exact(&mut h).is_err() { @@ -102,11 +120,11 @@ fn output(s: UnixStream) -> io::Result> { let mut p = (&mut r).take(u32::from_be_bytes([h[1], h[2], h[3], h[4]]).into()); match h[0] { b'o' => { - io::copy(&mut p, &mut out)?; + io::copy(&mut p, out)?; out.flush()?; } b'r' => { - io::copy(&mut p, &mut err)?; + io::copy(&mut p, err)?; } b'x' => { let mut c = [0u8; 4]; diff --git a/src/main.rs b/src/main.rs index e0c0dc3..eeca42f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -81,9 +81,13 @@ enum Cmd { #[arg(required = true)] ports: Vec, }, - /// List sandboxes and whether they're running + /// List sandboxes with live stats (CPU, memory, network, block I/O, processes) #[command(visible_alias = "ps")] - Ls, + Ls { + /// Keep refreshing every second, like `docker stats` (Ctrl-C to stop) + #[arg(short, long)] + follow: bool, + }, /// Remove stopped sandboxes Rm { #[arg(required = true)] @@ -168,7 +172,7 @@ fn main() -> Result<()> { Cmd::Pi { a, pi_auth } => agents::pi(&h, a, pi_auth), Cmd::Exec { name, user, cmd } => exec::exec(&h, &name, user, cmd), Cmd::Forward { name, ports } => forward::forward(&h, &name, ports), - Cmd::Ls => sandbox::ls(&h), + Cmd::Ls { follow } => sandbox::ls(&h, follow), Cmd::Gc => sandbox::gc(&h), Cmd::Rm { names, disk } => sandbox::rm(&h, names, disk), Cmd::Version => Ok(println!("wsbx {}", env!("CARGO_PKG_VERSION"))), diff --git a/src/sandbox.rs b/src/sandbox.rs index 1395e76..f311911 100644 --- a/src/sandbox.rs +++ b/src/sandbox.rs @@ -1,11 +1,15 @@ //! Sandbox bookkeeping: listing, removal, image gc. +use crate::exec; use crate::oci; use crate::util::sanitize; use anyhow::{Context, Result, anyhow, bail}; use serde_json::Value; +use std::collections::HashMap; use std::fs; +use std::io::Write; use std::os::unix::fs::MetadataExt; -use std::path::Path; +use std::path::{Path, PathBuf}; +use std::time::Duration; /// The lock `run` holds for the VM's lifetime; Err if the sandbox is running. pub fn lock_stopped(dir: &Path) -> Result { @@ -14,26 +18,156 @@ pub fn lock_stopped(dir: &Path) -> Result { Ok(f) } -pub fn ls(h: &Path) -> Result<()> { - println!("{:<28} {:<8} {:<40} {:>8} WORKSPACE", "NAME", "STATUS", "IMAGE", "DISK"); - let Ok(rd) = fs::read_dir(h.join("sandboxes")) else { return Ok(()) }; - let mut rows: Vec<_> = rd.flatten().collect(); - rows.sort_by_key(|e| e.file_name()); - for e in rows { +/// Guest counters, read from /proc through the exec channel. +#[derive(Default, Debug, PartialEq)] +struct Stats { + busy: u64, + total: u64, + ncpu: u64, + mem_used: u64, + mem_total: u64, + rx: u64, + tx: u64, + rd: u64, + wr: u64, + pids: u64, +} + +const STATS_SH: &str = "cd /proc; B=/.wsbx/busybox; $B grep ^cpu stat; $B grep -E '^Mem(Total|Available):' meminfo; \ + $B tail -n +3 net/dev; $B grep ' vda ' diskstats; set -- [0-9]*; echo pids $#"; + +fn parse(out: &str) -> Stats { + let (mut s, mut avail) = (Stats::default(), 0); + for l in out.lines() { + let f: Vec<&str> = l.split_whitespace().collect(); + let num = |i: usize| f.get(i).and_then(|v| v.parse::().ok()).unwrap_or(0); + match f.first().copied() { + Some("cpu") => { + let v: Vec = (1..=8).map(num).collect(); // user nice system idle iowait irq softirq steal + s.total = v.iter().sum(); + s.busy = s.total.saturating_sub(v[3] + v[4]); + } + Some(c) if c.starts_with("cpu") => s.ncpu += 1, + Some("MemTotal:") => s.mem_total = num(1) * 1024, + Some("MemAvailable:") => avail = num(1) * 1024, + Some("pids") => s.pids = num(1), + _ if f.get(2) == Some(&"vda") => (s.rd, s.wr) = (num(5) * 512, num(9) * 512), + _ => { + // net/dev: " eth0:123 ..." (no space once rx bytes outgrow the column) + if let Some((ifc, rest)) = l.split_once(':') + && ifc.trim() != "lo" + { + let n: Vec = rest.split_whitespace().filter_map(|v| v.parse().ok()).collect(); + s.rx += n.first().copied().unwrap_or(0); + s.tx += n.get(8).copied().unwrap_or(0); + } + } + } + } + s.mem_used = s.mem_total.saturating_sub(avail); + s +} + +fn human(b: u64) -> String { + let (mut v, mut u) = (b as f64, 0); + while v >= 1024.0 && u < 4 { + v /= 1024.0; + u += 1; + } + if u == 0 { format!("{b}B") } else { format!("{v:.1}{}", ["", "KiB", "MiB", "GiB", "TiB"][u]) } +} + +struct Row { + name: String, + dir: PathBuf, + meta: Value, + running: bool, +} + +fn rows(h: &Path) -> Result> { + let Ok(rd) = fs::read_dir(h.join("sandboxes")) else { return Ok(vec![]) }; + let mut rows = vec![]; + for e in rd.flatten() { let Ok(m) = fs::read(e.path().join("meta.json")) else { continue }; - let m: Value = serde_json::from_slice(&m)?; - let used = fs::metadata(e.path().join("disk.img")).map(|m| m.blocks() * 512).unwrap_or(0); - let status = if lock_stopped(&e.path()).is_err() { "running" } else { "stopped" }; - println!( - "{:<28} {:<8} {:<40} {:>6}MB {}", - e.file_name().to_string_lossy(), - status, - m["image"].as_str().unwrap_or("?"), - used / 1_000_000, - m["workspace"].as_str().unwrap_or("-") - ); + let running = lock_stopped(&e.path()).is_err(); + rows.push(Row { name: e.file_name().to_string_lossy().into(), dir: e.path(), meta: serde_json::from_slice(&m)?, running }); + } + rows.sort_by(|a, b| a.name.cmp(&b.name)); + Ok(rows) +} + +/// Stats of all running sandboxes, sampled in parallel; sandboxes that don't answer are left out. +fn sample(rows: &[Row]) -> HashMap { + std::thread::scope(|sc| { + let hs: Vec<_> = rows + .iter() + .filter(|r| r.running) + .map(|r| (r.name.clone(), sc.spawn(|| exec::capture(&r.dir.join("exec.sock"), &["/.wsbx/busybox", "sh", "-c", STATS_SH])))) + .collect(); + hs.into_iter().filter_map(|(n, j)| Some((n, parse(&j.join().ok()?.ok()?)))).collect() + }) +} + +fn table(rows: &[Row], prev: &HashMap, cur: &HashMap) -> Vec<[String; 11]> { + let head = ["NAME", "STATUS", "CPU %", "MEM USAGE / LIMIT", "MEM %", "NET I/O", "BLOCK I/O", "PIDS", "DISK", "IMAGE", "WORKSPACE"]; + let mut t = vec![head.map(String::from)]; + for r in rows { + let pct = |a: u64, b: u64| format!("{:.1}%", a as f64 * 100.0 / b.max(1) as f64); + let st = cur.get(&r.name).map(|c| { + // like docker: 100% = one vCPU busy + let cpu = prev.get(&r.name).filter(|p| c.total > p.total).map(|p| pct(c.busy.saturating_sub(p.busy) * c.ncpu.max(1), c.total - p.total)); + [ + cpu.unwrap_or("-".into()), + format!("{} / {}", human(c.mem_used), human(c.mem_total)), + pct(c.mem_used, c.mem_total), + format!("{} / {}", human(c.rx), human(c.tx)), + format!("{} / {}", human(c.rd), human(c.wr)), + c.pids.to_string(), + ] + }); + let [cpu, mem, memp, net, blk, pids] = st.unwrap_or_else(|| std::array::from_fn(|_| "-".into())); + let disk = fs::metadata(r.dir.join("disk.img")).map(|m| m.blocks() * 512).unwrap_or(0); + let s = |k: &str| r.meta[k].as_str().unwrap_or("-").to_string(); + let status = if r.running { "running" } else { "stopped" }; + t.push([r.name.clone(), status.into(), cpu, mem, memp, net, blk, pids, human(disk), s("image"), s("workspace")]); + } + t +} + +/// `wsbx ps`; with `follow`, redraws every second like `docker stats` until Ctrl-C. +pub fn ls(h: &Path, follow: bool) -> Result<()> { + let mut prev = HashMap::new(); + let mut first = true; + loop { + let rows = rows(h)?; + let cur = sample(&rows); + if first && !cur.is_empty() { + // CPU % needs two samples + (prev, first) = (cur, false); + std::thread::sleep(Duration::from_millis(500)); + continue; + } + let t = table(&rows, &prev, &cur); + let w: Vec = (0..11).map(|i| t.iter().map(|r| r[i].chars().count()).max().unwrap_or(0)).collect(); + let mut out = String::new(); + for r in &t { + let line: String = r + .iter() + .enumerate() + .map(|(i, c)| if (2..9).contains(&i) { format!("{c:>w$} ", w = w[i]) } else { format!("{c:, disk: bool) -> Result<()> { @@ -60,6 +194,21 @@ pub fn rm(h: &Path, names: Vec, disk: bool) -> Result<()> { Ok(()) } +#[cfg(test)] +mod tests { + #[test] + fn parse_proc() { + let out = "cpu 100 0 50 800 50 0 0 0 0 0\ncpu0 1 2 3\ncpu1 1 2 3\nMemTotal: 2000 kB\nMemAvailable: 500 kB\n\ + \x20 lo: 999 0 0 0 0 0 0 0 999 0\n eth0:123456789 1 0 0 0 0 0 0 42 1 0 0 0 0 0 0\n\ + \x20254 0 vda 10 0 4 0 7 0 8 0 0 0 0\npids 17\n"; + let s = super::parse(out); + assert_eq!((s.total, s.busy, s.ncpu), (1000, 150, 2)); + assert_eq!((s.mem_total, s.mem_used), (2000 * 1024, 1500 * 1024)); + assert_eq!((s.rx, s.tx, s.rd, s.wr, s.pids), (123456789, 42, 2048, 4096, 17)); + assert_eq!(super::human(1536), "1.5KiB"); + } +} + /// Delete cached images no sandbox was created from. pub fn gc(h: &Path) -> Result<()> { let used: Vec = fs::read_dir(h.join("sandboxes"))