container icon indicating copy to clipboard operation
container copied to clipboard

Add container machine stats command

Open nico81 opened this issue 4 months ago • 0 comments

Type of Change

  • [ ] Bug fix
  • [x] New feature
  • [ ] Breaking change
  • [ ] Documentation update

Motivation and Context

container stats shows live CPU / memory / network / block-I/O / process stats for containers, but there is no equivalent for container machines — container machine only covers lifecycle (create, run, ls, inspect, set, set-default, logs, stop, rm). When running a machine as a persistent Linux environment there is no first-class way to see its resource usage; today you would have to discover the machine's internal backing-container id and run container stats <that-id>.

This adds container machine stats [<id>] [--no-stream] [--format table|json|yaml|toml], the machine counterpart to container stats. Resolves #1919.

A running machine is already backed by a container (MachineSnapshot.containerId), so the data comes straight from the existing path — ContainerClient().stats(id:), the same call container machine run already uses on the backing container — with no new collection logic and no new API route. The shared sampling / CPU% / byte formatting / table rendering / streaming is extracted out of the private internals of container stats into a StatsRendering helper reused by both commands (no behavior change to container stats); output rows are labeled by machine id.

Testing

  • [x] Tested locally
  • [x] Added/updated tests
  • [x] Added/updated docs

swift build clean and swift test --filter StatsRendering green — new unit tests cover the CPU% / byte-format helpers (previously untested), plus an integration test for the new command. Manually verified against a running machine: container machine stats <name> (table / json / yaml) matches container stats <backing-id>, and stopped / nonexistent machines produce clear errors. Docs updated: command-reference.md and container-machine.md.

nico81 avatar Jul 08 '26 10:07 nico81