Skip to main content

Resources

A resource is something a target uses while it runs and that the machine has a limited amount of: CPUs, memory, a docker daemon, a GPU. A target declares what it needs; heph starts targets so that the total in use never exceeds what the machine (or your config) provides.

BUILD
target(
name = "build",
driver = "bash",
run = "make",
env = {"MAKEFLAGS": "-j${resource:cpu}"},
resources = {"cpu": 4, "memory": "8G"},
)

heph tells, it does not police. Nothing kills, throttles or pins a process that uses more than it reserved, and no resource value affects the cache key.

Declaring a cost​

resources maps a pool name to an amount. A pool is one kind of resource with a capacity.

BUILD
resources = {"cpu": 0.5, "memory": "2G", "docker": 1}

An amount is a number or a string, read in its pool's unit:

UnitWritten asExamples
cpudecimal ≥ 04, 0.5, "0.25"
byteswhole number, or digits followed by K, M, G or T (binary: 4G is 4·2³⁰)"8G", "4096M", 1024
countwhole number1, "2"

Pool names match [a-z][a-z0-9_]{0,63}, and a target names at most 16 pools. Naming a pool that isn't configured is an error, and heph validate reports it.

A target that declares no cpu costs 1 CPU. HTTP downloads, OCI and Go module downloads cost 0.1 CPU (except a gzipped oci.layer, which costs 1); fs targets and relocating group targets cost nothing. Undeclared memory and custom pools cost 0.

note

A target that costs nothing on any bounded pool starts immediately and is never queued, so nothing limits how many run together. Don't declare cpu: 0 on a target that spawns heavy processes. An exec or bash target always takes one sandbox slot, so even at cpu: 0 it queues.

Configuring pools​

cpu, memory and sandbox always exist. Declare any other pool in .hephconfig:

.hephconfig
resources:
cpu: 8 # CPUs, or a percentage of the default
memory: 24G # a size, or a percentage
sandbox: 32 # a count, or a percentage
docker: 2 # a count
scratch_disk: {unit: bytes, value: 64G} # full form, another unit
tokens: {unit: count} # no value: unbounded
gpu: 0 # exists; each run sets its capacity
  • A bare amount uses the pool's default unit: cpu for cpu, bytes for memory, count for sandbox and any other pool.
  • A custom pool without a value is unbounded: nothing waits, but usage is still counted and shown.
  • A capacity of 0 means the run must set it. Targets using the pool validate everywhere, and fail at start-up if the pool isn't set.
  • Percentages (50%, 75%, 200%) are of the default and only work for cpu, memory and sandbox.

Defaults. cpu is the number of cores. memory is the memory heph may use: the container limit on Linux when one is set, otherwise physical memory. Set memory explicitly if you want a laptop and a CI container to behave the same. sandbox is 4 per CPU of the run's cpu capacity (rounded up, at least 1), so -j 2 makes it 8.

resources: merges per pool across profiles.

Per-run overrides​

OverrideEffect
--resource <name>=<n>Sets one pool for this run. Repeatable; declares the pool if the config doesn't.
HEPH_RESOURCE_<NAME>=<n>Same, from the environment (HEPH_RESOURCE_GPU=2).
-j N / --jobs NSame as --resource cpu=N.

Precedence: flag, then environment, then config, then default.

warning

-j N is the number of CPUs the run may reserve at once, not a number of targets. The default is the core count (it used to be twice that in target slots), so a workspace whose targets declare nothing runs fewer at once than before. Use -j 200% to restore the old concurrency.

The sandbox pool​

sandbox bounds how many build sandboxes exist on disk at once, running or waiting to be removed. A build of many short targets can finish sandboxes faster than the disk deletes them; this pool keeps that backlog from filling the disk.

  • Every exec or bash run (any driver that runs in a build sandbox) takes sandbox: 1 on top of what it declares, admitted together with its CPUs.
  • Its CPUs and other pools come back when the run ends; its sandbox slot only once the sandbox directory has been removed.
  • Cache hits, fs, group, template and other drivers that don't use a build sandbox, and heph run entrypoints take no slot. A failed target's sandbox, kept for inspection, gives its slot back at once.
  • It is always bounded and always a count. Size it with --resource sandbox=<n>, HEPH_RESOURCE_SANDBOX=<n> or resources: {sandbox: <n>}. Like any pool, .hephconfig may set it to 0 ("the run sets it"), but a run may not.
  • A run waiting for a sandbox slot doesn't hold back a target that takes none: a template target, for example, still starts in the free CPUs.
  • It belongs to heph: a target can't declare it in resources or read ${resource:sandbox}. If you had your own pool named sandbox, rename it.

If builds stall on sandbox, sandboxes are being removed slower than runs create them. The wait says so:

⧗ //a:b waits for sandbox 1 (0 free, 2 ahead, 41s) — cleanup is behind: 3 removals pending

and the TUI header shows sandbox: 4 waiting (3 removals pending). Raise the pool only if the disk can hold that many more sandboxes.

Reading the allotment​

heph passes nothing to a target's environment. Ask for the amount by name with ${resource:<pool>}, and map it onto your tool:

BUILD
target(
name = "crate",
driver = "bash",
run = "cargo build --release",
env = {"CARGO_BUILD_JOBS": "${resource:cpu}"},
resources = {"cpu": 8},
)

It is replaced at run time with the amount granted, as a whole number: cpu is rounded up (a cpu: 0.5 target is told 1), bytes and count are integers. It works anywhere a deferred value does, such as env and run.

A pool the target doesn't reserve, or a name that isn't a pool, is an error when the target is loaded.

Entrypoints reserve nothing, so ${resource:cpu} there is the whole cpu capacity.

Sizing from the host​

heph.core.resource("cpu") returns the host's CPU count and heph.core.resource("memory") its memory in bytes. Use them for the amount:

BUILD
target(name = "link", ..., resources = {"cpu": heph.core.resource("cpu") / 2})

This is the machine's value, not the current run's pool. Use it only for a resources amount; anywhere else it puts a per-machine value in the target definition.

Go tests​

The go provider's test state accepts resources for generated test targets. See Test environment.

How targets are admitted​

  • Whatever fits runs. Any waiting target whose amounts fit what is free starts. When a target finishes, its slot goes to the oldest waiting target that reserves the same amounts.
  • The oldest waiter is overtaken only so far. Targets that fit can start ahead of an older target that doesn't, but only up to 16 times what the older target needs of a pool, and never more than 4 times the pool's capacity. Past that, nothing more starts in the capacity it needs: the pool drains and it starts. No target waits forever.
  • All or nothing. A target never holds CPUs while waiting for memory.
  • Clamped. A request above a pool's capacity is reduced to it: cpu: 16 on a 4-CPU run waits for an empty pool, runs alone, and is told 4.

Start order follows arrival only among targets that reserve the same amounts. Across different amounts, a younger target that fits can start before an older one that doesn't.

note

A target that asks for a whole pool, such as heph.core.resource("cpu"), starts only once the pool is empty, after other targets worth about 4 times the pool have started ahead of it. Declare the width the target actually uses, not the whole pool as a way to run alone: a target asking for half the pool starts as soon as half is free.

The TUI header shows each pool in use as held/capacity, plus how many targets wait on it (gpu 1/1, 2 waiting). A clamped target is marked, for example [cpu 16→4]. A target waiting more than 5 seconds is listed with what it waits for:

⧗ //a:b waits for cpu 4 (2 free, 6 ahead, 41s)

ahead is how many targets were queued before it, not a promise that they start first. A target that would fit but is held back for the oldest waiter names what it yields to:

⧗ //a:b waits for cpu 1 (3 free, yielding to a cpu 8 run, 41s)

The contract​

Resources never enter the cache key: changing resources, -j, or a pool's capacity keeps your cache hits. So:

A target's outputs must not depend on its allotment.

Use ${resource:cpu} to size parallelism, never to choose content. A target that can't avoid it should set cache = False.

A custom pool serialises access to an external thing, such as docker: 1 for one target at a time using the daemon. The thing itself is still an undeclared input: holding the pool doesn't make a target hermetic.