diff --git a/deploy/README.md b/deploy/README.md index 480efbe..acbf1e4 100644 --- a/deploy/README.md +++ b/deploy/README.md @@ -299,6 +299,31 @@ The peak may include other jobs, display usage, and driver usage. To estimate a job's own usage, sample it on an otherwise idle GPU. If sampling fails the peak is empty. Unpinned fallback jobs record `vram_peak_scope=unavailable`. +**Reading the budget inside the job (#185).** A gpu job container gets its +share as environment variables, set on the container and never written to the +JIT credential file: + +| Variable | Pinned job | Unpinned fallback job | cpu job | +|---|---|---|---| +| `RUNNER_VRAM_GB` | GiB charged on its GPU | `unknown` | not set | +| `RUNNER_VRAM_TOTAL_GB` | GiB of that card, rounded down | not set | not set | +| `NVIDIA_VISIBLE_DEVICES` | the pinned GPU index | image default | image default | + +A step can turn the budget into a fraction of the card it sees, for example +with PyTorch: + +```python +import os, torch +total = int(os.environ["RUNNER_VRAM_TOTAL_GB"]) +share = int(os.environ["RUNNER_VRAM_GB"]) +torch.cuda.set_per_process_memory_fraction(share / total, device=0) +``` + +This is cooperative, not a limit (see below). Guard for `unknown` or an unset +variable and skip the call there. Steps that run as separate containers through +the host Docker socket do not inherit these variables: pass them on with +`docker run -e RUNNER_VRAM_GB -e RUNNER_VRAM_TOTAL_GB -e NVIDIA_VISIBLE_DEVICES ...`. + ```sh sudo install -m 0644 deploy/runner-types.sample.yaml \ /etc/github-runner-listener/runner-types.yaml diff --git a/doc/changelog/CHANGELOG.md b/doc/changelog/CHANGELOG.md index de0ebc4..ead870d 100644 --- a/doc/changelog/CHANGELOG.md +++ b/doc/changelog/CHANGELOG.md @@ -8,6 +8,14 @@ versioning follows [Semantic Versioning](https://semver.org/spec/v2.0.0.html). ### Added +- **The charged VRAM reaches the gpu job container** (#185): the container + engine argv carries `RUNNER_VRAM_GB` (the charged GiB, or `unknown` for an + unpinned fail-safe job) and `RUNNER_VRAM_TOTAL_GB` (the card GiB) when set. + `RUNNER_VRAM_TOTAL_GB` is delivered only with a known (numeric) budget. + Neither is written to the JIT env-file; a cpu job gets neither. The + listener provisioner exports `RUNNER_VRAM_TOTAL_GB` (the pinned card's + total, floored to whole GiB; empty otherwise). `deploy/README.md` shows how + a step reads them. - **GPU jobs declare their VRAM and share a GPU** (ADR-0007, #180): a gpu type with `concurrency: mode: vram` admits a job that adds `vram-gb-` or `vram-gb-all` to `runs-on` on the first GPU where the committed VRAM plus its diff --git a/lib/runner-container.sh b/lib/runner-container.sh index 535f4c1..0f0ca02 100644 --- a/lib/runner-container.sh +++ b/lib/runner-container.sh @@ -72,6 +72,10 @@ # non-empty value adds `--env NVIDIA_VISIBLE_DEVICES=` to override it. # EMPTY (every cpu job, and the GPU fail-safe) adds nothing. : "${RUNNER_GPU:=}" +# #185: charged VRAM (GiB or "unknown") and the card total (GiB), passed to the +# job container as RUNNER_VRAM_GB / RUNNER_VRAM_TOTAL_GB; empty adds no flag. +: "${RUNNER_VRAM_GB:=}" +: "${RUNNER_VRAM_TOTAL_GB:=}" # The runner's home INSIDE the image: where the bundled actions/runner (run.sh, # config.sh, bin/) lives and where the job runs from. Both the self-built images @@ -245,6 +249,17 @@ runner_container_run() { if [[ -n "${RUNNER_GPU}" ]]; then gpu_args=(--env "NVIDIA_VISIBLE_DEVICES=${RUNNER_GPU}") fi + # Charged VRAM budget and card total (#185): engine flags on the argv, never + # the env-file. Independent of RUNNER_GPU so "unknown" (unpinned) is delivered. + if [[ -n "${RUNNER_VRAM_GB}" ]]; then + gpu_args+=(--env "RUNNER_VRAM_GB=${RUNNER_VRAM_GB}") + fi + # The card total only goes with a known budget: "unknown" or no budget means + # there is nothing to take a fraction of, so no total is delivered. + if [[ -n "${RUNNER_VRAM_GB}" && "${RUNNER_VRAM_GB}" != unknown && + -n "${RUNNER_VRAM_TOTAL_GB}" ]]; then + gpu_args+=(--env "RUNNER_VRAM_TOTAL_GB=${RUNNER_VRAM_TOTAL_GB}") + fi # Deliver the single-use JIT credential to the container OFF the host argv # (#136, restores the ADR-0003 invariant). Splicing `--jitconfig "${encoded}"` # directly onto the host ` run ...` argv put the credential in the HOST diff --git a/listener/listener.go b/listener/listener.go index c754ca7..c6fd923 100644 --- a/listener/listener.go +++ b/listener/listener.go @@ -109,6 +109,9 @@ type ProvisionRequest struct { // VRAMGB is the VRAM, in GiB, charged for the job on that GPU; 0 when // not pinned. Recorded in the job's ledger record. VRAMGB int64 + // VRAMTotalMiB is the total memory, in MiB, of the card the job is + // pinned to (#185); 0 when not pinned. + VRAMTotalMiB int64 } // Provisioner runs one ephemeral job in a throwaway container. The production @@ -525,10 +528,10 @@ func (l *Listener) reconcile(ctx context.Context, stats *scaleset.RunnerScaleSet return nil } - gpu, minor, vramGB := -1, -1, int64(0) + gpu, minor, vramGB, vramTotalMiB := -1, -1, int64(0), int64(0) if l.gpuProbe != nil { var ok bool - gpu, minor, vramGB, ok = l.admitGPU(ctx, labels) + gpu, minor, vramGB, vramTotalMiB, ok = l.admitGPU(ctx, labels) if !ok || ctx.Err() != nil { <-l.sem return nil @@ -566,6 +569,7 @@ func (l *Listener) reconcile(ctx context.Context, stats *scaleset.RunnerScaleSet GPU: gpu, GPUFallback: l.gpuProbe != nil && gpu < 0, VRAMGB: vramGB, + VRAMTotalMiB: vramTotalMiB, }, } if l.cfg.DockerSocket != "" { @@ -924,7 +928,10 @@ func (l *Listener) committedSize() jobSize { // admitGPU is the per-job shared-GPU gate (#180, ADR-0007): it blocks until a // GPU can take the job's declared VRAM (pickGPU), re-probing after the settle // window while none can, and returns the GPU index, device minor and GiB to -// charge there. It returns false only when the context is cancelled. +// charge there, plus that card's total MiB (#185; 0 when not pinned). The total +// is taken from the selected reading under the same lock, never looked up later +// in a snapshot the capacity reporter may have replaced. It returns false only +// when the context is cancelled. // // It fails safe when the probe errors: whole-GPU, one job per GPU, over the // last good GPU list (only a GPU with nothing committed takes a job, charged @@ -932,7 +939,7 @@ func (l *Listener) committedSize() jobSize { // flight, and the unpinned job holds the host until it exits. // Run serializes dispatch, so only releases can change commitments between // this selection and chargeGPU. -func (l *Listener) admitGPU(ctx context.Context, labels []string) (gpu, minor int, gb int64, ok bool) { +func (l *Listener) admitGPU(ctx context.Context, labels []string) (gpu, minor int, gb, totalMiB int64, ok bool) { for { want := int64(jobVRAM(labels, int(wholeGPU))) gpus, err := l.gpuProbe.Probe(ctx) @@ -947,18 +954,19 @@ func (l *Listener) admitGPU(ctx context.Context, labels []string) (gpu, minor in case len(gpus) > 0: selected, cost, fits := pickGPU(gpus, l.committedVRAM, want, l.reserve) gpu, minor, gb, ok = selected.Index, selected.Minor, cost, fits + totalMiB = selected.TotalMiB case l.inFlight.Load() == 0: gpu, minor, ok = -1, -1, true } } l.vramMu.Unlock() if ok { - return gpu, minor, gb, true + return gpu, minor, gb, totalMiB, true } select { case <-time.After(l.settle): case <-ctx.Done(): - return 0, 0, 0, false + return 0, 0, 0, 0, false } } } diff --git a/listener/provision-job.sh b/listener/provision-job.sh index 28c3c07..06dba16 100755 --- a/listener/provision-job.sh +++ b/listener/provision-job.sh @@ -28,6 +28,14 @@ # empty = not pinned # RUNNER_VRAM_GB VRAM GiB charged, or "unknown" for an unpinned # vram-mode job +# RUNNER_VRAM_TOTAL_GB whole GiB of the pinned card (#185); empty when +# not pinned. Both reach the job container as env of +# the same names, by case: +# pinned job: RUNNER_VRAM_GB= and +# RUNNER_VRAM_TOTAL_GB= +# unpinned fallback: RUNNER_VRAM_GB=unknown, no +# RUNNER_VRAM_TOTAL_GB +# ordinary job: neither # # Each invocation runs EXACTLY ONE ephemeral job in a throwaway container that # is torn down on exit (--rm), then returns the in-container job's exit status diff --git a/listener/provisioner.go b/listener/provisioner.go index 2d6fb2c..73b64fb 100644 --- a/listener/provisioner.go +++ b/listener/provisioner.go @@ -47,6 +47,7 @@ type ContainerProvisioner struct { // RUNNER_JOB_WORKDIR absolute same-path work folder or explicit empty (off) // RUNNER_GPU GPU index a vram job is pinned to, or explicit empty // RUNNER_VRAM_GB VRAM GiB charged for the pinned job, "unknown" for unpinned VRAM mode, or empty +// RUNNER_VRAM_TOTAL_GB whole GiB of the pinned card (floored), or explicit empty (#185) // // The script's stdout/stderr are inherited so the container's job log streams // straight through, and a non-zero exit becomes a non-nil error so the listener @@ -92,11 +93,17 @@ func (c *ContainerProvisioner) Provision(ctx context.Context, req ProvisionReque if req.DockerSocket != "" { workDir = req.JobWorkDir } - // A job pinned to a GPU (#180) passes its index and charged VRAM; an - // unpinned one exports both EMPTY so nothing inherited pins it. - gpu, vram := "", "" + // Three cases, each exporting every value explicitly so nothing inherited + // leaks through: + // - pinned job (#180): GPU index, charged GiB and the card total GiB. + // - unpinned fallback (GPUFallback): GPU index and total empty, budget + // "unknown" (#185). + // - ordinary job: all three empty. + gpu, vram, vramTotal := "", "", "" if req.VRAMGB > 0 && req.GPU >= 0 { gpu, vram = strconv.Itoa(req.GPU), strconv.FormatInt(req.VRAMGB, 10) + // The card total in whole GiB, floored (#185). + vramTotal = strconv.FormatInt(req.VRAMTotalMiB/1024, 10) } else if req.GPUFallback { vram = "unknown" } @@ -104,6 +111,7 @@ func (c *ContainerProvisioner) Provision(ctx context.Context, req ProvisionReque "RUNNER_DEVICES="+strings.Join(req.Devices, " "), "RUNNER_GPU="+gpu, "RUNNER_VRAM_GB="+vram, + "RUNNER_VRAM_TOTAL_GB="+vramTotal, "RUNNER_RUNTIME="+req.Runtime, "RUNNER_BUILD_TOOL="+req.BuildTool, "RUNNER_DOCKER_SOCKET="+req.DockerSocket, diff --git a/listener/provisioner_test.go b/listener/provisioner_test.go index fd0d59b..1ac8eec 100644 --- a/listener/provisioner_test.go +++ b/listener/provisioner_test.go @@ -342,3 +342,39 @@ func TestContainerProvisionerUnpinnedGPUEnv(t *testing.T) { }) } } + +// A pinned job (#185) passes the card's total VRAM in whole GiB, floored, so +// the job can compute its share; an unpinned job exports it EMPTY so an +// inherited value never leaks into the container. +func TestContainerProvisionerPassesVRAMTotalAsEnv(t *testing.T) { + t.Setenv("RUNNER_VRAM_TOTAL_GB", "99") + capFile := filepath.Join(t.TempDir(), "env") + script := writeScript(t, "#!/usr/bin/env bash\n"+ + "printf 'TOTAL=[%s]\\n' \"${RUNNER_VRAM_TOTAL_GB-}\" > '"+capFile+"'\n") + p := &ContainerProvisioner{Script: script} + for _, tc := range []struct { + name string + request ProvisionRequest + want string + }{ + // 32607 MiB / 1024 = 31.84 GiB, floored to 31. + {name: "pinned", request: ProvisionRequest{GPU: 1, VRAMGB: 8, VRAMTotalMiB: 32607}, want: "TOTAL=[31]"}, + {name: "CPU", request: ProvisionRequest{GPU: -1, VRAMTotalMiB: 32607}, want: "TOTAL=[]"}, + {name: "VRAM fallback", request: ProvisionRequest{GPU: -1, GPUFallback: true}, want: "TOTAL=[]"}, + } { + t.Run(tc.name, func(t *testing.T) { + req := tc.request + req.JobID, req.Image, req.EncodedJITConfig = "job", "img", "ENC" + if err := p.Provision(context.Background(), req); err != nil { + t.Fatalf("Provision: %v", err) + } + got, err := os.ReadFile(capFile) + if err != nil { + t.Fatal(err) + } + if strings.TrimSpace(string(got)) != tc.want { + t.Errorf("env = %q, want %q", got, tc.want) + } + }) + } +} diff --git a/listener/reactive_test.go b/listener/reactive_test.go index 0263000..e32b084 100644 --- a/listener/reactive_test.go +++ b/listener/reactive_test.go @@ -417,6 +417,46 @@ func TestVRAMJobsShareOneGPU(t *testing.T) { } } +// The request carries the total VRAM of the card the job was admitted onto +// (#185), so the job can turn its charge into a fraction of that card. GPU 0 +// is busy, so the job lands on GPU 1, a 16380 MiB card, not the 32607 MiB one. +func TestPinnedJobRequestCarriesAdmittedCardTotal(t *testing.T) { + prov, stop := heldGPUListen(t, []GPU{rtx5090(0, 30000), rtx2000(1, 0)}, + assigned(1, "four", "gpu", "vram-gb-4"), + ) + defer stop() + req, ok := prov.next(t, 2*time.Second) + if !ok { + t.Fatal("the vram-gb-4 job never started") + } + if req.GPU != 1 { + t.Fatalf("job pinned to GPU %d, want GPU 1", req.GPU) + } + if req.VRAMTotalMiB != 16380 { + t.Errorf("VRAMTotalMiB = %d, want 16380 (the admitted card, not 32607)", req.VRAMTotalMiB) + } +} + +// admitGPU hands back the admitted card's own total (#185) rather than leaving +// the caller to look it up in a snapshot the reporter may replace; a job with +// no pin (no reading ever) gets 0. +func TestAdmitGPUReturnsAdmittedCardTotal(t *testing.T) { + l := New(&fakeSession{}, &recordingMinter{}, &recordingProvisioner{}, + Config{GPUProbe: stubGPUProbe{gpus: []GPU{rtx5090(0, 30000), rtx2000(1, 0)}}, + Reserve: 20, SettleWindow: time.Millisecond, MaxRunners: 100}) + gpu, _, _, total, ok := l.admitGPU(context.Background(), []string{"vram-gb-4"}) + if !ok || gpu != 1 || total != 16380 { + t.Fatalf("admitGPU = (gpu %d, total %d, %v), want (1, 16380, true)", gpu, total, ok) + } + + idle := New(&fakeSession{}, &recordingMinter{}, &recordingProvisioner{}, + Config{GPUProbe: stubGPUProbe{err: errors.New("no nvidia-smi")}, Reserve: 20, SettleWindow: time.Millisecond, MaxRunners: 100}) + gpu, _, _, total, ok = idle.admitGPU(context.Background(), []string{"vram-gb-1"}) + if !ok || gpu != -1 || total != 0 { + t.Fatalf("unpinned admitGPU = (gpu %d, total %d, %v), want (-1, 0, true)", gpu, total, ok) + } +} + // A job with no vram-gb label holds the whole GPU, so a second one waits for // the first to finish: today's one-job-per-GPU behaviour is the default. func TestUndeclaredGPUJobsRunOnePerGPU(t *testing.T) { @@ -478,7 +518,7 @@ func TestAdmitGPUSettlesThenAdmits(t *testing.T) { p := &countingGPUProbe{overUntil: 2, over: []GPU{rtx5090(0, 30000)}, ok: []GPU{rtx5090(0, 0)}} l := New(&fakeSession{}, &recordingMinter{}, &recordingProvisioner{}, Config{GPUProbe: p, Reserve: 20, SettleWindow: time.Millisecond, MaxRunners: 100}) - gpu, _, gb, ok := l.admitGPU(context.Background(), []string{"vram-gb-4"}) + gpu, _, gb, _, ok := l.admitGPU(context.Background(), []string{"vram-gb-4"}) if !ok || gpu != 0 || gb != 4 { t.Fatalf("admitGPU = (%d, %d, %v), want (0, 4, true) once the GPU frees", gpu, gb, ok) } @@ -496,7 +536,7 @@ func TestAdmitGPUCtxCancelUnblocks(t *testing.T) { time.Sleep(10 * time.Millisecond) cancel() }() - if _, _, _, ok := l.admitGPU(ctx, []string{"vram-gb-1"}); ok { + if _, _, _, _, ok := l.admitGPU(ctx, []string{"vram-gb-1"}); ok { t.Fatal("admitGPU must return false when cancelled while no GPU has room") } } @@ -620,14 +660,14 @@ func TestGPUAdmissionFailSafeUsesLastKnownGPUs(t *testing.T) { Config{GPUProbe: p, Reserve: 20, SettleWindow: time.Millisecond, MaxRunners: 100}) ctx := context.Background() - gpu, _, gb, ok := l.admitGPU(ctx, []string{"vram-gb-4"}) + gpu, _, gb, _, ok := l.admitGPU(ctx, []string{"vram-gb-4"}) if !ok || gpu != 0 || gb != 4 { t.Fatalf("healthy admitGPU = (%d, %d, %v), want (0, 4, true)", gpu, gb, ok) } l.chargeGPU(gpu, gb) p.set(nil, errors.New("nvidia-smi failed")) - gpu, _, gb, ok = l.admitGPU(ctx, []string{"vram-gb-1"}) + gpu, _, gb, _, ok = l.admitGPU(ctx, []string{"vram-gb-1"}) if !ok || gpu != 1 || gb != 25 { t.Fatalf("fail-safe admitGPU = (%d, %d, %v), want (1, 25, true): the empty GPU, whole", gpu, gb, ok) } @@ -635,12 +675,12 @@ func TestGPUAdmissionFailSafeUsesLastKnownGPUs(t *testing.T) { short, cancel := context.WithTimeout(ctx, 50*time.Millisecond) defer cancel() - if gpu, _, gb, ok := l.admitGPU(short, []string{"vram-gb-1"}); ok { + if gpu, _, gb, _, ok := l.admitGPU(short, []string{"vram-gb-1"}); ok { t.Fatalf("fail-safe admitted (%d, %d) although every known GPU holds a job", gpu, gb) } l.releaseGPU(0, 4) - gpu, _, gb, ok = l.admitGPU(ctx, []string{"vram-gb-1"}) + gpu, _, gb, _, ok = l.admitGPU(ctx, []string{"vram-gb-1"}) if !ok || gpu != 0 || gb != 25 { t.Fatalf("fail-safe admitGPU after release = (%d, %d, %v), want (0, 25, true)", gpu, gb, ok) } @@ -651,14 +691,14 @@ func TestGPUAdmissionFailSafeUsesLastKnownGPUs(t *testing.T) { func TestGPUAdmissionFailSafeWithoutAnyReading(t *testing.T) { l := New(&fakeSession{}, &recordingMinter{}, &recordingProvisioner{}, Config{GPUProbe: stubGPUProbe{err: errors.New("no nvidia-smi")}, Reserve: 20, SettleWindow: time.Millisecond, MaxRunners: 100}) - gpu, _, _, ok := l.admitGPU(context.Background(), []string{"vram-gb-1"}) + gpu, _, _, _, ok := l.admitGPU(context.Background(), []string{"vram-gb-1"}) if !ok || gpu != -1 { t.Fatalf("admitGPU on an idle listener = (gpu %d, %v), want (-1, true)", gpu, ok) } l.inFlight.Store(1) short, cancel := context.WithTimeout(context.Background(), 50*time.Millisecond) defer cancel() - if _, _, _, ok := l.admitGPU(short, []string{"vram-gb-1"}); ok { + if _, _, _, _, ok := l.admitGPU(short, []string{"vram-gb-1"}); ok { t.Fatal("admitGPU without any reading admitted a second job beside a running one") } } @@ -769,7 +809,7 @@ func TestGPUFailSafeRecoveryWaitsForUnpinnedJob(t *testing.T) { } ctx, cancel := context.WithTimeout(context.Background(), 50*time.Millisecond) defer cancel() - if _, _, _, ok := l.admitGPU(ctx, []string{"vram-gb-1"}); ok { + if _, _, _, _, ok := l.admitGPU(ctx, []string{"vram-gb-1"}); ok { t.Fatal("probe recovery admitted work beside an unpinned job") } } diff --git a/test/bats/integration/listener_provision.bats b/test/bats/integration/listener_provision.bats index 257da4e..63aa5d6 100644 --- a/test/bats/integration/listener_provision.bats +++ b/test/bats/integration/listener_provision.bats @@ -665,3 +665,39 @@ EOF2 [ "${status}" -eq 0 ] grep -q $'gpu_used_peak_mib=\tvram_peak_scope=unavailable' "${RUNNER_HISTORY_LEDGER}" } + +# The charged VRAM reaches the job container (#185): both vars are engine flags +# placed before the image; an unpinned job gets only "unknown"; a cpu job none. +# Argv is one token per line in ${CAP}; the image is the last positional "img". + +vram_env_before_image() { + awk -v want="$1" ' + !done && $0 == "img" { done = 1 } + !done && prev == "--env" && $0 == want { found = 1 } + { prev = $0 } + END { exit (found && done) ? 0 : 1 } + ' "${CAP}" +} + +@test "provision-job.sh passes RUNNER_VRAM_GB and RUNNER_VRAM_TOTAL_GB to the engine argv (#185)" { + make_nvidia_smi 1024 + RUNNER_GPU=1 RUNNER_VRAM_GB=8 RUNNER_VRAM_TOTAL_GB=31 \ + run "${SCRIPT}" job-vramenv "$(jit_file ENC)" img + [ "${status}" -eq 0 ] + vram_env_before_image 'RUNNER_VRAM_GB=8' + vram_env_before_image 'RUNNER_VRAM_TOTAL_GB=31' +} + +@test "provision-job.sh passes only an unknown budget for an unpinned job and nothing for a cpu job (#185)" { + RUNNER_GPU='' RUNNER_VRAM_GB=unknown RUNNER_VRAM_TOTAL_GB='' \ + run "${SCRIPT}" job-vramunk "$(jit_file ENC)" img + [ "${status}" -eq 0 ] + vram_env_before_image 'RUNNER_VRAM_GB=unknown' + [ "$(grep -c '^RUNNER_VRAM_' "${CAP}")" -eq 1 ] + rm -f "${CAP}" + RUNNER_GPU='' RUNNER_VRAM_GB='' RUNNER_VRAM_TOTAL_GB='' \ + run "${SCRIPT}" job-vramcpu "$(jit_file ENC)" img + [ "${status}" -eq 0 ] + [ -s "${CAP}" ] + ! grep -q '^RUNNER_VRAM_' "${CAP}" || return 1 +} diff --git a/test/bats/integration/runner_container.bats b/test/bats/integration/runner_container.bats index 88dd785..6787415 100644 --- a/test/bats/integration/runner_container.bats +++ b/test/bats/integration/runner_container.bats @@ -483,3 +483,67 @@ teardown() { rm -rf "${FAKE_RH}" "${STUB}"; } [ -n "${image_line}" ] [ "${pin_line}" -lt "${image_line}" ] } + +# --- #185 the charged VRAM reaches the job container --- + +# True when NAME=VALUE is the value of an `--env` flag that sits BEFORE the +# image on the engine argv (a pair after the image is a command arg, not env). +engine_env_before_image() { + awk -v want="$1" -v img='my/image:tag' ' + !done && $0 == img { done = 1 } + !done && prev == "--env" && $0 == want { found = 1 } + { prev = $0 } + END { exit (found && done) ? 0 : 1 } + ' "${CAP}" +} + +# True when no argv token starts with RUNNER_VRAM_ (anywhere on the argv). +no_vram_env_on_argv() { ! grep -q '^RUNNER_VRAM_' "${CAP}"; } + +@test "runner_container_run passes RUNNER_VRAM_GB and RUNNER_VRAM_TOTAL_GB as engine env (#185)" { + make_cli docker + RUNNER_GPU=1 RUNNER_VRAM_GB=8 RUNNER_VRAM_TOTAL_GB=32 RUNNER_RUNTIME=nvidia \ + run runner_container_run "${RUNNER_DIR}" ENC my/image:tag + [ "${status}" -eq 0 ] + engine_env_before_image 'RUNNER_VRAM_GB=8' + engine_env_before_image 'RUNNER_VRAM_TOTAL_GB=32' + # The budget is an engine flag, never part of the credential env-file; the + # env-file capture must exist and hold the JIT key first (not just be empty). + [ -s "${ENVCAP}" ] + [ "$(head -n1 "${ENVCAP}")" = 'ACTIONS_RUNNER_INPUT_JITCONFIG=ENC' ] + ! grep -qF -- 'RUNNER_VRAM_' "${ENVCAP}" || return 1 +} + +@test "runner_container_run gives an unknown budget and no card total (#185)" { + make_cli docker + RUNNER_VRAM_GB=unknown RUNNER_VRAM_TOTAL_GB=32 \ + run runner_container_run "${RUNNER_DIR}" ENC my/image:tag + [ "${status}" -eq 0 ] + engine_env_before_image 'RUNNER_VRAM_GB=unknown' + ! grep -qxF -- 'RUNNER_VRAM_TOTAL_GB=32' "${CAP}" || return 1 + [ "$(grep -c '^RUNNER_VRAM_' "${CAP}")" -eq 1 ] +} + +@test "runner_container_run adds no RUNNER_VRAM_ flag when the budget is unset or empty (#185)" { + make_cli docker + # CAP is append-only, so each invocation starts from an empty capture and + # must itself write a fresh argv (-s): an invocation that never reached the + # engine cannot pass on a previous run's capture. + : >"${CAP}" + run runner_container_run "${RUNNER_DIR}" ENC my/image:tag + [ "${status}" -eq 0 ] + [ -s "${CAP}" ] + no_vram_env_on_argv || return 1 + : >"${CAP}" + RUNNER_VRAM_GB='' RUNNER_VRAM_TOTAL_GB='' \ + run runner_container_run "${RUNNER_DIR}" ENC my/image:tag + [ "${status}" -eq 0 ] + [ -s "${CAP}" ] + no_vram_env_on_argv || return 1 + # A total with no budget is not delivered either. + : >"${CAP}" + RUNNER_VRAM_TOTAL_GB=32 run runner_container_run "${RUNNER_DIR}" ENC my/image:tag + [ "${status}" -eq 0 ] + [ -s "${CAP}" ] + no_vram_env_on_argv || return 1 +}