Skip to content

Add container machine stats command#1920

Open
nico81 wants to merge 1 commit into
apple:mainfrom
nico81:add-container-machine-stats
Open

Add container machine stats command#1920
nico81 wants to merge 1 commit into
apple:mainfrom
nico81:add-container-machine-stats

Conversation

@nico81

@nico81 nico81 commented Jul 8, 2026

Copy link
Copy Markdown

Type of Change

  • Bug fix
  • 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

  • Tested locally
  • Added/updated tests
  • 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.

A running container machine is backed by a container, so its resource
usage is already available through the existing container stats path.
Expose it with a new `container machine stats [<id>]` subcommand.

- Extract the sampling, CPU%/byte formatting, table rendering, and
  streaming loop out of the private internals of `container stats` into
  a shared StatsRendering helper, parametrized by the id-column header,
  and refactor ContainerStats onto it with no behavior change.
- Add MachineStats, mirroring `machine inspect` for target resolution
  and reusing StatsRendering. It resolves the machine's backing
  container id, supports table/json/yaml/toml and live streaming with
  --no-stream, and labels output with the machine id.
- Add unit tests for the formatting/CPU helpers (previously untested)
  and an integration test for the new command.
- Document the command in command-reference.md and container-machine.md.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Request]: container machine stats to show machine resource usage

1 participant