Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
# Per-call resource metrics and performance budgets

See the [Phase 11 foundations plan](../../.opencode/plans/phase-11-foundations.md) and the [Phase 9 plan](../../.opencode/plans/phase-9-portable-platform.md).

## Intent

Fuel is the platform's main performance ceiling for guests: every guest call
runs on a fresh `SandboxLimits::fuel` (10M by default) and traps, poisoning the
world, when it runs out. Nobody could see how close a guest was to that line
without hand instrumentation. Territory's styled view uses about 7.1M to 7.8M
of the 10M, and that was found by hand. This slice makes the cost

1. **measurable** from the host (`WorldInstance::metrics`),
2. **visible to authors** offline (`id world measure`), and
3. **guarded** against regressions in CI (`checked_in_examples_stay_within_fuel_budgets`).

## What is measured

`WorldInstance` records the fuel each guest call spent, in a fixed array keyed
by `GuestCall { Init, Update, View, Layout, Records, Wants, Snapshot, Restore }`.
Each `CallStats` holds `calls`, `fuel_total`, `fuel_max`, and `fuel_last`.

- Every call already resets fuel with `set_fuel(limits.fuel)` first, so fuel
spent is `limits.fuel - get_fuel()` afterwards. It is read once the whole
call is done, so it includes the input allocation, the guest `free`, and the
`plaza_out_len` read around the main export.
- A trapped call is recorded too. The diagnostic getters (`plaza_error_ptr`,
`plaza_error_len`) refill the store to 10_000, so `guest_error_suffix`
captures the trap's fuel first. A call that ran out of fuel leaves the store
at zero and so records the full limit.
- **Init** covers the module start function plus `plaza_init`; both run on the
one budget set at instantiation. It is recorded when the instance is built.
- **Layout** is the `plaza_layout` call. A guest with no layout export makes
`layout_at` fall back to `view`, which is then counted as **View**.
- Calls refused before reaching the guest (a poisoned world, an oversized
message, an absent optional export) are not recorded.
- The trait-level `snapshot` verifies the snapshot on a probe instance. The
live instance records its own `snapshot` and `records` calls; the probe's
calls are not added to it.
- Metrics live in memory on the instance. They are never journaled, never on
the wire, and never part of a snapshot, so they cannot affect replay or
determinism. Recording is a few integer updates: no lock, no allocation.

## `id world measure`

```
id world measure --module FILE.wasm [--seed N] [--input HEX]... [--width W --height H]
```

Offline and gated on the `sandbox` feature. It compiles the module with the
default limits and instantiates it. It then runs `view`, `layout_at(W, H)` if
both `--width` and `--height` are given, `records`, `wants`, and `snapshot`.
Then each `--input` runs as participant 1 through `update`, with a `view` after
each. It prints one JSON object: `module_bytes`, `fuel_limit`, `calls` (per
kind: `calls`, `fuel_total`, `fuel_max`), `worst_call`, and
`worst_call_percent_of_limit`. Kinds that never ran are omitted. It exits
nonzero only for a compile failure, a malformed `--input`, or a trap.

The reads run before the inputs, so they show the cost at the initial state.
Use `--input` to see how update cost and later views grow with state.

## How budgets are set

`FUEL_BUDGETS` in `src/sandbox.rs` has one row per (example, call kind). The
test drives each example through a fixed script (`drive_*` in the test
module), then runs `view`, `layout_at(40, 20)`, `records`, `wants`, and
`snapshot` at the final state, and restores the snapshot on a fresh instance.
The observed value is the worst single call of that kind. A budget is the
measured baseline plus 25 percent, rounded up to the next 100_000. The test also
requires every example module to be at most 1 MiB, and fails on a call that
has no budget row, so a new export cannot slip in unguarded.

On failure the message names the example and call, the actual fuel, and the
budget, then says: _re-baseline deliberately: update the table and say why in
the commit_.

### Baseline (fuel)

| Example | init | update | view | layout | records | wants | snapshot | restore |
| ----------- | ----: | -----: | --------: | --------: | ------: | ------: | -------: | ------: |
| roc-counter | 1,215 | 3,759 | 1,327 | 1,337 | 1,866 | 733 | 874 | 3,238 |
| tic-tac-toe | 1,242 | 7,839 | 5,329 | 6,265 | 7,347 | 1,664 | 769 | 14,204 |
| roc-screen | 1,215 | 11,491 | 257,078 | 1,192,583 | 2,795 | 733 | 870 | 3,243 |
| roc-grid | 1,242 | 5,035 | 981,969 | 981,300 | 1,664 | 737 | 1,143 | 5,688 |
| roc-chat | 1,271 | 89,763 | 5,997 | 6,949 | 23,438 | 42,766 | 4,767 | 7,884 |
| lounge | 1,271 | 69,255 | 5,550 | 6,502 | 71,278 | 194,873 | 70,261 | 33,299 |
| territory | 1,255 | 64,520 | 7,798,929 | 7,798,260 | 56,536 | 2,444 | 7,794 | 21,745 |

Fuel counts Wasm instructions, so these numbers do not depend on the host or
on a debug or release build. They are exact for these module bytes and scripts.

### Headroom against the 10M default

Each example's worst call as a percent of the limit:

| Example | Worst call | Fuel | % of 10M |
| ------------- | ---------- | ------------: | -------: |
| roc-counter | update | 3,759 | 0.04 |
| tic-tac-toe | restore | 14,204 | 0.14 |
| roc-screen | layout | 1,192,583 | 11.93 |
| roc-grid | view | 981,969 | 9.82 |
| roc-chat | update | 89,763 | 0.90 |
| lounge | wants | 194,873 | 1.95 |
| **territory** | **view** | **7,798,929** | **78.0** |

**Above 50 percent: territory only.** An empty territory board costs 6.54M for
a view (65 percent); the script's two seats and a few claimed cells take it to
7.80M. A hand run with one seat and one cell gave 7.1M (71 percent).

**Territory's view exceeds the default limit at six or more seats**, which its
own `max_seats = 8` allows. Measured in a scratch test with every seat joined
and active, view fuel was:

| Seats | 1 | 2 | 4 | 6 | 8 |
| --------- | ----: | ----: | ----: | ----------: | ----------: |
| View fuel | 7.33M | 8.10M | 9.74M | out of fuel | out of fuel |

Each seat adds about 0.8M, and claimed cells add little (16 cells per seat
moved the 2-seat view by 0.03M). A view that runs out of fuel traps and poisons
the world, so a full territory world is broken under the default profile today.
This slice records the problem; it does not fix it (non-goals below). The
`FUEL_BUDGETS` row guards the two-seat baseline, so it will not catch this by
itself.

## How to re-baseline

Re-baseline when a guest or the platform legitimately changes cost:

1. Rebuild the example module as usual.
2. Run `cargo test --lib -- --ignored print_fuel_baseline --nocapture` from
`pkgs/id`. It prints one `FUEL_BUDGETS` row per call kind, with the new
budget and the measured baseline in a comment.
3. Paste the rows over `FUEL_BUDGETS` and update the baseline table above.
4. Say why in the commit message. A rise without a reason is the regression the
test exists to catch.

## Limits

- Budgets are 25 percent over a scripted state, not a worst case. Cost grows
with state, as territory shows, so a pass does not mean a world cannot run
out of fuel.
- Rounding up to 100_000 makes budgets for tiny calls loose: any call under
80,000 fuel has a 100,000 budget, and 100,000 is 1 percent of the limit. They
guard against order-of-magnitude regressions only.
- A budget near the limit guards little. A budget cannot exceed what the guest
can spend, which is the limit, and territory's 9.8M budget sits close to it.
- Metrics are per instance and in memory. A restarted or replaced world starts
from zero, and nothing aggregates across worlds.
- Only guest fuel is measured. Host-side time (JSON parsing, compression, the
network) and guest memory are not.

## Non-goals

No wire change and no `stats` frame change; no host-side metrics endpoint; no
change to the default limits; no optimisation of the guests.

## Follow-ups

- **Host-side metrics endpoint.** Expose `WorldInstance::metrics` through the
world host (and `id world stats`) so a live world's headroom is visible, next
to the execution profile it already prints.
- **Fix territory's fuel use.** The view costs about 6.5M on an empty board
and 0.8M per seat. Options: a cheaper render path, or a higher world
profile for it. Either needs its own decision.
- **Look at the shared screen code.** The two grid-like views cost heavily for
what they draw: roc-grid's view is 0.98M and roc-screen's layout is 1.19M.
Hypothesis, unmeasured: the cost is mostly in the shared `Screen` rendering
rather than in each app. Measure with `id world measure` before changing it.
- **Crowded-state budgets.** Add a second territory script with all eight
seats once the view fits, so the budget covers the worst case.
- **Warn on high usage.** Have `id world install` or `compile` run the
measurement and warn when a call uses more than half the profile's fuel.
- **Per-world budgets.** Compare against a world's own execution profile
rather than the 10M default.

## References

- [Phase 11 foundations plan](../../.opencode/plans/phase-11-foundations.md)
- [Phase 9 plan](../../.opencode/plans/phase-9-portable-platform.md)
- `src/sandbox.rs`: `GuestCall`, `CallStats`, `CallMetrics`, `FUEL_BUDGETS`, `print_fuel_baseline`
- `src/commands/world.rs`: `measure_module`; `src/cli.rs`: `WorldCommand::Measure`
54 changes: 54 additions & 0 deletions pkgs/id/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1651,6 +1651,32 @@ pub enum WorldCommand {
#[arg(long, default_value = "world")]
name: String,
},
/// Measure a compiled `.wasm` world program's fuel use, offline.
///
/// Compiles the module with the default limits, runs `view`, then
/// `layout` (with `--width`/`--height`), `records`, `wants`, and
/// `snapshot`, then each `--input` as participant 1 followed by a `view`.
/// Prints one JSON object with, per call kind, the calls made, total and
/// worst fuel, and the worst call as a percent of the fuel limit. Exits
/// nonzero only if the module fails to compile or a call traps.
#[cfg(feature = "sandbox")]
Measure {
/// Wasm module built for the `id` Roc world ABI.
#[arg(long)]
module: PathBuf,
/// Deterministic seed supplied to the guest initializer.
#[arg(long, default_value_t = 1)]
seed: u64,
/// Event to apply as participant 1, hex-encoded (repeatable, in order).
#[arg(long = "input", value_name = "HEX")]
inputs: Vec<String>,
/// Viewport width for the `layout` call (needs `--height`).
#[arg(long, requires = "height")]
width: Option<u16>,
/// Viewport height for the `layout` call (needs `--width`).
#[arg(long, requires = "width")]
height: Option<u16>,
},
/// Tail a world's recent events (bounded by retention and a deadline).
Logs {
/// The host's node ID (64 hex characters).
Expand Down Expand Up @@ -3846,6 +3872,34 @@ mod tests {
},
other => panic!("unexpected: {other:?}"),
}
#[cfg(feature = "sandbox")]
{
let cli = Cli::parse_from([
"id", "world", "measure", "--module", "a.wasm", "--input", "ff", "--input", "00",
"--width", "40", "--height", "20",
]);
match cli.command {
Some(Command::World(cmd)) => match *cmd {
WorldCommand::Measure {
inputs,
width,
height,
seed,
..
} => {
assert_eq!(inputs, ["ff", "00"]);
assert_eq!((width, height, seed), (Some(40), Some(20), 1));
}
other => panic!("unexpected: {other:?}"),
},
other => panic!("unexpected: {other:?}"),
}
// A width alone has no viewport to lay out.
assert!(
Cli::try_parse_from(["id", "world", "measure", "--module", "a", "--width", "9"])
.is_err()
);
}
let node = "a".repeat(64);
let cli = Cli::parse_from(["id", "world", "stats", &node, "--capability", "c"]);
assert!(matches!(
Expand Down
106 changes: 106 additions & 0 deletions pkgs/id/src/commands/world.rs
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,20 @@ pub async fn cmd_world(command: WorldCommand) -> Result<()> {
Ok(())
}
WorldCommand::New { dir, name } => scaffold_world(&dir, &name),
#[cfg(feature = "sandbox")]
WorldCommand::Measure {
module,
seed,
inputs,
width,
height,
} => {
let wasm = std::fs::read(&module)
.with_context(|| format!("read Wasm module {}", module.display()))?;
let report = measure_module(&wasm, seed, &inputs, width.zip(height))?;
println!("{}", serde_json::to_string_pretty(&report)?);
Ok(())
}
WorldCommand::Logs {
node,
capability,
Expand Down Expand Up @@ -542,6 +556,72 @@ pub async fn cmd_world(command: WorldCommand) -> Result<()> {
}
}

/// Drive a module through the calls `id world measure` reports on and return
/// the JSON report.
///
/// # Errors
///
/// A module that fails to compile, a malformed `--input`, or a call that traps.
#[cfg(feature = "sandbox")]
fn measure_module(
wasm: &[u8],
seed: u64,
inputs: &[String],
viewport: Option<(u16, u16)>,
) -> Result<serde_json::Value> {
use crate::sandbox::{GuestCall, Sandbox, SandboxLimits};

let events = inputs
.iter()
.map(|hex| {
crate::world_session::decode_hex(hex)
.with_context(|| format!("`{hex}` is not an even-length hex string"))
})
.collect::<Result<Vec<_>>>()?;
let limits = SandboxLimits::default();
let sandbox = Sandbox::compile(wasm, limits)?;
let mut world = sandbox.instantiate(seed)?;
world.view()?;
if let Some((width, height)) = viewport {
world.layout_at(width, height)?;
}
world.records()?;
world.wants()?;
world.snapshot()?;
for event in &events {
world.update(1, event)?;
world.view()?;
}

let mut calls = serde_json::Map::new();
let mut worst = (GuestCall::Init, 0);
for (call, stats) in world.metrics().iter() {
if stats.calls == 0 {
continue;
}
if stats.fuel_max > worst.1 {
worst = (call, stats.fuel_max);
}
calls.insert(
call.name().to_owned(),
serde_json::json!({
"calls": stats.calls,
"fuel_total": stats.fuel_total,
"fuel_max": stats.fuel_max,
}),
);
}
#[allow(clippy::cast_precision_loss)]
let worst_percent = worst.1 as f64 * 100.0 / limits.fuel as f64;
Ok(serde_json::json!({
"module_bytes": wasm.len(),
"fuel_limit": limits.fuel,
"calls": calls,
"worst_call": worst.0.name(),
"worst_call_percent_of_limit": (worst_percent * 100.0).round() / 100.0,
}))
}

/// Scaffold a Roc world app in `dir`.
///
/// The directory must be absent or empty. Writes `main.roc`, `build.sh`
Expand Down Expand Up @@ -840,4 +920,30 @@ mod tests {
let bad = dir.path().join("bad");
assert!(scaffold_world(&bad, "../escape").is_err());
}

#[cfg(feature = "sandbox")]
#[test]
fn measure_reports_fuel_per_call_kind_for_a_checked_in_module() {
let wasm = include_bytes!("../../examples/roc-counter/counter.wasm");
let report =
super::measure_module(wasm, 7, &["696e63".to_owned()], Some((40, 20))).unwrap();
assert_eq!(report["module_bytes"], wasm.len());
assert_eq!(report["fuel_limit"], 10_000_000);
let calls = &report["calls"];
// The initial view plus one after the input.
assert_eq!(calls["view"]["calls"], 2);
for kind in ["init", "update", "layout", "records", "wants", "snapshot"] {
assert_eq!(calls[kind]["calls"], 1, "{kind}: {report}");
assert!(calls[kind]["fuel_max"].as_u64().unwrap() > 0, "{kind}");
}
assert!(calls.get("restore").is_none(), "{report}");
assert!(report["worst_call_percent_of_limit"].as_f64().unwrap() < 1.0);

// Without a viewport there is no layout call; bad hex is refused
// before the module runs.
let report = super::measure_module(wasm, 7, &[], None).unwrap();
assert!(report["calls"].get("layout").is_none());
assert!(super::measure_module(wasm, 7, &["zz".to_owned()], None).is_err());
assert!(super::measure_module(b"not wasm", 7, &[], None).is_err());
}
}
Loading
Loading