Skip to content

GPUs

This page describes how the daemon finds the GPUs of the host, gives them to sandboxes, checks them and takes them back. It is for a contributor who changes internal/gpu, the gpus mode of the helper, or the GPU code of the manager and the Docker driver. After you read this page, you know the rules that the code keeps, where each rule is, and where the tests are.

Not proven on real hardware. None of this code ran on a machine with an NVIDIA GPU. It ran only against stand-ins: shell scripts in the place of nvidia-smi, GPUs in memory, the test engine and a stand-in for the Engine API. Issue #1 in ROADMAP.md is the proof on real hardware. See Not proven.

In this page, a GPU is one physical NVIDIA GPU of the host. The daemon knows each GPU by its UUID, for example GPU-5b6c1d2e-....

File Contents
internal/gpu/gpu.go The types Device, Usage, Prober, NvidiaSMI, Request, ErrUnavailable, Status and Allocator. Discovery and the GPU allocator.
internal/guest/gpus.go The gpus mode of the helper: runGPUs, visibleGPUs, deviceFiles and the answer words.
internal/sandbox/manager.go Create and boot ask for GPUs. checkGPUs checks them. discard, forget and retryOrphans release them. New holds them again after a restart.
internal/sandbox/suspend.go suspend releases the GPUs. wakeLocked asks for them again.
internal/sandbox/snapshot.go Fork gives a GPU request to each copy.
internal/engine/engine.go The type GPURequest and the field CreateSpec.GPU.
internal/engine/docker.go Docker.Create writes HostConfig.DeviceRequests.
internal/api/server.go gpuField reads the gpu field of a request. Server.gpus serves GET /v1/gpus.
cmd/boxfork/main.go serve reads the flags --gpu-driver, --gpu-busy-mb, --gpu-idle-suspend, --gpu-check and makes the GPU allocator.
cmd/boxfork/cli.go The command boxfork gpus.
internal/tools/tools.go The tool gpu_list for models.
scripts/install.sh detect_gpus, install_nvidia_toolkit and check_gpu_driver.

The reason for whole GPUs is in decision 6 of decisions.md. The security view is in Boundary 5 of security.md. The flags are in configuration.md. The gpu field and the GPU route are in http-api.md. The general create path is in manager.md and in architecture.md. This page does not repeat them.

The GPU allocator gives each GPU to a maximum of one sandbox. A sandbox gets whole GPUs only. No mode shares a GPU between two sandboxes. A request can ask for more than one GPU: "gpu": 2 gives two whole GPUs to one sandbox. The GPU allocator gives all GPUs of a request or none.

Decision 6 speaks of one GPU for one sandbox. The rule that the code keeps is one owner for each GPU.

The package comment of gpu.go gives the reason. Two users of one GPU have no isolation of memory or of faults between them. The cost: a host runs a maximum of one GPU sandbox for each GPU.

When the daemon starts, serve calls gpu.NewAllocator with gpu.NvidiaSMI{} and the value of --gpu-busy-mb. NewAllocator calls Prober.Devices one time. The list of GPUs does not change while the daemon runs. A GPU that you add needs a restart of the daemon.

NvidiaSMI.Devices runs nvidia-smi --query-gpu=index,uuid,name,memory.total --format=csv,noheader,nounits.

  • If nvidia-smi is not on the PATH of the daemon, Devices gives an empty list and no error.
  • If nvidia-smi fails and gives no usable row, Devices gives an error. serve then stops with “reading this host’s GPUs”.
  • If nvidia-smi fails but gives some usable rows, Devices uses those rows. A GPU that the driver lost has no row.
  • parseRows keeps only a row that has the number of fields of the query.
  • number reads a value such as [N/A] or [Not Supported] as 0.
  • Devices sorts the GPUs by index.

If the list is empty, serve sets the allocator to nil. In the manager, Config.GPUs nil means a host with no GPUs.

NvidiaSMI.Usage runs nvidia-smi --query-gpu=uuid,memory.used,utilization.gpu. The GPU allocator calls it for each allocation and for each status request. A GPU that is not in its output is a GPU that does not answer. Each call of nvidia-smi has a time limit of 20 seconds (SMITimeout), or less if the context of the caller ends first. After the limit, the call fails with “nvidia-smi did not answer in 20s; check the NVIDIA driver”. The daemon waits a maximum of one second more for the output of the processes that nvidia-smi started (WaitDelay).

A client asks for GPUs with the field gpu. The forms are in http-api.md. gpuField.UnmarshalJSON makes a gpu.Request with Count, Model and MinMemoryMB. The CLI flag --gpu <n> and the tool argument gpu give only a count.

Manager.Create applies these rules before it starts anything:

  • A count of 0 or less becomes no request.
  • A request on a host with no allocator fails with ErrUnavailable “this host has no GPUs”.
  • A client value for NVIDIA_VISIBLE_DEVICES in env fails with InvalidError.
  • A sandbox with GPUs and no memory value gets 8192 MiB.
  • A sandbox with GPUs gets the idle time Config.GPUIdleSuspend, if the request gives none and the host can save files.
  • A request with GPUs never gets a waiting sandbox: take in pool.go refuses it.

Allocator.matches decides if a GPU matches a request. Model must be a part of the name of the GPU. Upper case and lower case are equal. The memory of the GPU must be MinMemoryMB or more.

Manager.boot asks the GPU allocator for the GPUs before it makes the container. After the allocation, each failure in boot goes through the local function fail, which calls discard.

flowchart TD
A["Manager.Create with a gpu.Request"] -->|"no allocator"| U["ErrUnavailable: 409 gpu_unavailable"]
A --> B["Manager.boot"]
B --> C["Allocator.Allocate"]
C -->|"not enough free GPUs"| U
C -->|"nvidia-smi fails"| I["error: 500 internal"]
C --> D["CreateSpec.GPU, NVIDIA_VISIBLE_DEVICES, label boxfork.gpus"]
D --> E["image, network, Create, CopyIn, Start, runtime check, probe, setDNS, giveVolumes"]
E -->|"a step fails"| F["fail: discard removes the container, then releases the GPUs"]
E --> G{"--gpu-check off?"}
G -->|yes| N["gpu_verified false"]
G -->|no| H["checkGPUs: guest gpus"]
H -->|"OK with exactly the UUIDs"| T["Allocator.Trust, gpu_verified true"]
H -->|"correct COUNT or UNKNOWN, mode auto"| N
H -->|"any other answer"| X["Allocator.Distrust"]
X --> F
T --> R["Create records the sandbox, then the client gets it"]
N --> R

Allocator.Allocate does these steps:

  1. It refuses a count below 1, and a host with no GPUs.
  2. It calls Prober.Usage before it takes its lock, because the call starts a program.
  3. If Usage fails, Allocate gives the error “reading GPU usage”. This error is not an ErrUnavailable.
  4. Under its lock, it examines each GPU in the order of the index.
  5. It skips a GPU that does not match the request.
  6. It skips a GPU that has an owner.
  7. It skips a GPU that is not in the output of Usage.
  8. It skips a free GPU whose used memory is busyAboveMB or more, when busyAboveMB is more than 0.
  9. It puts each GPU with the suspect mark after all other GPUs.
  10. If fewer GPUs match than the count, it gives an ErrUnavailable that names the number on the host.
  11. If fewer GPUs are free than the count, it gives an ErrUnavailable with the causes.
  12. Else it records the sandbox ID as the owner of each GPU and gives the GPUs.

Step 10 counts each GPU that matches, also a held one. Thus its error means that this host can never give the request.

boot makes an engine.GPURequest with the driver Config.GPUDriver, which comes from --gpu-driver.

Driver DeviceIDs NVIDIA_VISIBLE_DEVICES in the container Capabilities
nvidia, the default The UUIDs of the GPUs The UUIDs, separated by commas [["gpu"]]
cdi nvidia.com/gpu=<index> for each GPU void none

Docker.Create in docker.go writes one item in HostConfig.DeviceRequests. The item has Driver, DeviceIDs, and Capabilities when the driver is not cdi. boot also writes the UUIDs, separated by commas, in the label boxfork.gpus of the container (LabelGPUs). After a restart, this label tells the daemon which GPUs a container holds when no record knows the container.

Docker.Info fills Info.CDIDevices with the CDI devices that the engine reports. No code of the daemon reads this field. Thus the daemon does not make sure at its start that the CDI names exist.

An engine can start a container without the GPUs of the request and give no error. Thus boot asks the sandbox which GPUs it sees before any client gets the sandbox. checkGPUs runs after probe, setDNS and giveVolumes, unless Config.GPUCheck is off.

checkGPUs runs the helper as /.boxfork/guest gpus through Manager.ask. It waits a maximum of 45 seconds. Manager.askAs reads a maximum of 16 KiB of the answer. The helper writes one line. The first word of the line is one of the constants in gpus.go.

visibleGPUs in the helper makes the line:

  • If nvidia-smi is on the PATH of the sandbox, the helper runs nvidia-smi --query-gpu=uuid --format=csv,noheader. It waits a maximum of gpuQuery, 20 seconds.
  • If nvidia-smi succeeds, the line is OK and the UUIDs, separated by commas. OK alone means that the sandbox sees no GPU.
  • If nvidia-smi fails, the line is ERR and the reason. oneLine keeps a maximum of 300 bytes of the reason.
  • If nvidia-smi is not there, deviceFiles examines /dev.
  • With no /dev/nvidiactl, the line is OK. The comment on deviceFiles gives the reason: each program that uses an NVIDIA GPU opens that file.
  • With /dev/nvidiactl, the line is COUNT and the number of files whose name matches ^nvidia[0-9]+$.
  • If the helper cannot read /dev, the line is UNKNOWN and the reason.

checkGPUs compares the line with the GPUs that the sandbox got:

Answer auto require
OK and exactly the UUIDs of the sandbox, in any order Verified Verified
OK and other UUIDs, more UUIDs, or none Removed Removed
ERR and a reason Removed Removed
COUNT and the number of GPUs of the sandbox Accepted, not verified Removed
COUNT and a different number Removed Removed
UNKNOWN and a reason Accepted, not verified, with a warning in the log Removed
No answer, a time-out, or a line that checkGPUs does not understand Removed Removed

With off, the daemon asks nothing. It accepts the sandbox without verification.

For a removed sandbox, boot calls Allocator.Distrust for its GPUs and then fail. The create fails, and the error text says what the sandbox saw. The HTTP answer is 500 internal. For a verified sandbox, boot calls Allocator.Trust, which clears the suspect mark. A sandbox that the daemon accepts without verification changes no mark.

Sandbox.GPUVerified is the field gpu_verified of a sandbox. The JSON omits it when it is false.

  • It is true only when the helper answered OK with exactly the UUIDs of the sandbox.
  • It is false with --gpu-check off, and for a sandbox that auto accepted without verification.
  • suspend sets it to false. A wake sets it from a new check.
  • The state file keeps it. A restart does not ask the sandbox again.

The answer comes from code in the sandbox, and that code can replace nvidia-smi or the helper. Thus gpu_verified finds a defective GPU setup. It does not find an image that somebody made to give a false answer. See the list of risks that the daemon does not stop in security.md.

A sandbox without a GPU must not get any GPU of the host. The code has these mechanisms:

  • boot sets NVIDIA_VISIBLE_DEVICES to void for each container before it adds GPUs. The value of the container replaces the value of the image.
  • An image can set this variable to all. On a host where the NVIDIA runtime is the default, all gives each GPU to the container. The comment on gpuEnv says so.
  • The image of a snapshot can contain the variable with the UUIDs of its source sandbox. void replaces that value too.
  • Create refuses NVIDIA_VISIBLE_DEVICES in the env of a request. The client gets 400 invalid_request.
  • With no request, CreateSpec.GPU is nil. Then Docker.Create sends no DeviceRequests.
  • boot sets the label boxfork.gpus on each container, with an empty value when there are no GPUs. The image of a snapshot has the labels of its source sandbox. Thus a copy does not get the GPU label of its source.
  • The trial containers of checkSnapshots in snapshot.go also get void and an empty label.
  • A pool never starts a sandbox with GPUs.
  • A fork with gpu set to 0 makes copies with no GPU.

A sandbox with GPUs must not see other GPUs. DeviceIDs, the variable and the label contain only its own GPUs. checkGPUs removes a sandbox that sees one more UUID. With the cdi driver, a sandbox with GPUs also gets void. The engine attaches the GPUs by their CDI names.

The GPU allocator reads the state of the GPUs only when it needs it: at each allocation and at each status request. No loop of the daemon examines the GPUs.

  • No answer. A GPU that is not in the output of Usage does not go to a sandbox. Status sets missing only when Usage succeeded.
  • Busy. A free GPU with busyAboveMB MiB of used memory, or more, does not go to a sandbox. The allocator takes that memory as the work of a program outside boxfork. A value of 0 stops this rule. Status sets busy only for a GPU with no owner.
  • Suspect. Distrust sets the mark after a failed check. Allocate gives such a GPU only after all other GPUs. Trust clears the mark after a verified check.
  • A held GPU that does not answer. Nothing changes for its sandbox. The sandbox keeps the GPU and runs. boxfork gpus shows the sandbox ID with (not answering).
  • A restart. Allocator.Restore gives the UUIDs of a record that the driver does not list now. New keeps the sandbox and writes a warning.

The suspect mark is only in memory. A restart of the daemon clears all marks.

The owner and the suspect mark are two separate fields of Allocator: owner and suspect. The diagram shows the usual paths.

stateDiagram-v2
state "Free" as Free
state "Free, with the suspect mark" as Doubted
state "Held by a sandbox" as Held
state "Held for a container that the engine did not remove" as Orphan
[*] --> Free: NewAllocator lists the GPU
[*] --> Held: Restore after a restart
[*] --> Orphan: Restore for a leftover container
Free --> Held: Allocate
Doubted --> Held: Allocate, when no other GPU is free
Held --> Held: verified check, Trust clears the mark
Held --> Doubted: failed check, Distrust, then discard releases
Held --> Free: Release after the container is gone
Held --> Orphan: the engine cannot remove the container
Orphan --> Free: retryOrphans removes the container, then Release
note right of Free
Allocate also skips a free GPU
that nvidia-smi does not list now,
and a free GPU that another program uses.
end note
State owner suspect Allocate gives it Fields in GET /v1/gpus
Free none false Yes none of the marks
Free, with the mark none true Only after all other GPUs suspect
Held by a sandbox The sandbox ID true or false No sandbox_id
Held for a container that the engine did not remove The sandbox ID, or leftover-<container ID> true or false No sandbox_id
Does not answer any any No missing
Busy none any No busy

The GPU allocator gets a GPU back only after the engine removed the container that held it. A GPU that a second sandbox gets while the first container runs is a GPU that two containers share. Allocator.Release takes an owner and releases each GPU of that owner.

Event Path in the code When the GPUs go back If the engine cannot remove the container
Kill Kill, remove, forget After Engine.Remove succeeds The sandbox stays. The client gets the error. The GPUs stay held.
Timeout Sweep, remove with onlyExpired, forget After Engine.Remove succeeds The log has an error. The next sweep, one second later, tries again.
The container ended by itself Reconcile, reap, forget After Engine.Remove succeeds reap tries again at the next reconcile, 20 seconds later.
The engine says that the container is not there engineErr, forget At once Not applicable
A create or a wake fails after the allocation fail, discard At once when no container exists. Else after Engine.Remove succeeds, by ID or by name. The container goes to orphans, with the sandbox ID as owner.
Suspend suspend After Engine.Remove succeeds, or after Inspect gives ErrNotFound The container goes to orphans, with the sandbox ID as owner.
An orphan retryOrphans, from Reconcile After Engine.Remove succeeds, if the owner has no other orphan and no container It tries again at the next reconcile.

forget does not release the GPUs when an orphan of the same owner exists (hasOrphan). retryOrphans does not release them when the owner is a sandbox that has a container again. The details of the kill and of the orphans are in manager.md.

The details are in suspend.md and in snapshots-and-forks.md. The GPU parts are these:

  • suspend releases the GPUs only after the container is gone. It sets GPUs to nil and GPUVerified to false.
  • The record keeps the request of the create in record.Want. The state file has it as gpu_request.
  • wakeLocked calls boot with Want. The wake gets GPUs that match the same request, which can be different GPUs.
  • The wake runs checkGPUs again. A failed check leaves the sandbox suspended and sets the suspect mark.
  • If no GPU is free, the wake fails with ErrUnavailable: “was suspended while it sat idle and cannot carry on until it has a GPU again”.
  • wakeLocked gives an error while an orphan of the sandbox exists. That container can still hold the GPUs.
  • record.wantsGPUs gives the GPU count of a sandbox, also of a suspended one.
  • Fork gives each copy a request with the count of wantsGPUs, if the fork request has no gpu field. Model and MinMemoryMB do not go to the copy.
  • Each copy needs free GPUs of its own. If a copy cannot get them, the fork fails and removes all copies.

New in manager.go holds the GPUs again from the records and from the labels:

What New finds What New does
A suspended record, with no container It clears GPUs and GPUVerified. It holds no GPU.
A record with GPUs on a host with no allocator It drops the record. The loop for leftover containers then removes its container.
A record with a GPU that a different owner holds Restore gives an error. New drops the record and removes its container as a leftover.
A record with a GPU that the driver does not list Restore holds it and gives it as gone. New keeps the sandbox and writes a warning.
A leftover container that the engine cannot remove Restore holds the GPUs of its label under the owner leftover-<container ID>. The container goes to orphans.

Restore examines all UUIDs before it records one. It records all of them, also the UUIDs that the driver does not list.

The boxfork gpus command and the HTTP route

Section titled “The boxfork gpus command and the HTTP route”

GET /v1/gpus calls Server.gpus in server.go. With an allocator, the body is {"gpus": [...]} from Allocator.Status. With no allocator, the array is empty. Each item is a gpu.Status: the fields of Device, sandbox_id, busy, missing, suspect, memory_used_mb and utilization_pct. The fields are in http-api.md.

If Usage fails, Status gives each GPU with no load and no missing mark. The route gives no error.

boxfork gpus in cli.go calls client.GPUs and prints a table. --json prints the list. The column USED BY shows the first true condition of this list:

  1. The GPU does not answer and has an owner.
  2. The GPU does not answer.
  3. The GPU has an owner.
  4. The GPU is busy.
  5. The GPU has the suspect mark.

The texts are in cli.md. The tool gpu_list in tools.go uses the same order with longer texts. boxfork info and GET /v1/info give gpus: the number from Allocator.Count, which does not change while the daemon runs.

What the installer does for the NVIDIA Container Toolkit

Section titled “What the installer does for the NVIDIA Container Toolkit”

scripts/install.sh prepares Docker for GPUs. It does not install the NVIDIA driver.

  1. detect_gpus runs nvidia-smi --query-gpu=driver_version --format=csv,noheader.
  2. If that works, the installer keeps the number of lines and the first driver version.
  3. If nvidia-smi is there and fails, the installer writes a warning and continues with no GPU setup.
  4. If nvidia-smi is not there and lspci shows an NVIDIA device, the installer writes a warning.
  5. With no GPU, install_nvidia_toolkit does nothing.
  6. If nvidia-ctk or nvidia-container-runtime is on the machine, the installer installs no package.
  7. With --no-nvidia-toolkit, the installer installs no package and writes a warning.
  8. With apt-get, it adds the key and the package list of NVIDIA, then installs nvidia-container-toolkit.
  9. With dnf or yum, it adds the repository file of NVIDIA, then installs nvidia-container-toolkit.
  10. If the machine has none of these package managers, it writes a warning and stops this part.
  11. If nvidia-ctk is there, it runs nvidia-ctk runtime configure --runtime=docker and reloads Docker.
  12. When it sets up gVisor on a GPU machine, it adds --nvproxy=true to runsc install.
  13. check_gpu_driver compares the driver version with runsc nvproxy list-supported-drivers. It warns if the version is not in the list.

Note: In the case of step 3, the daemon does not start, because NvidiaSMI.Devices gives an error. See Failure cases.

The variable BOXFORK_INSTALL_NVIDIA_TOOLKIT=no has the same effect as --no-nvidia-toolkit. The installer does not make a CDI specification. Thus --gpu-driver cdi needs a step by hand that this repository does not document. The installer gives no --gpu-* flag to serve. Such a flag goes in BOXFORK_EXTRA_ARGS of the environment file.

At a boot, scripts/cloud-startup.sh runs the installer again in one case. The machine has a GPU that answers now, and it had none at the first install.

  • It knows only NVIDIA GPUs. NvidiaSMI is the only Prober that serve uses.
  • It does not clear the memory of a GPU between two sandboxes.
  • It does not watch a GPU that a sandbox holds.
  • It does not save the suspect mark.
  • It does not read the list of GPUs again while it runs.
Function File What it does
NvidiaSMI.Devices gpu.go It lists the GPUs. No nvidia-smi on the PATH gives an empty list.
NvidiaSMI.Usage gpu.go It gives the used memory and the use in percent for each UUID that the driver lists.
parseRows, number gpu.go They read the CSV output of nvidia-smi.
NewAllocator gpu.go It reads the GPUs one time and makes the GPU allocator.
Allocator.Count gpu.go The number of GPUs at the start.
Allocator.Allocate gpu.go It gives the GPUs of one request to one owner, or none.
Allocator.Release gpu.go It releases each GPU of one owner.
Allocator.Distrust, Allocator.Trust gpu.go They set and clear the suspect mark.
Allocator.Restore gpu.go It records an owner again after a restart. It gives the UUIDs that the driver does not list.
Allocator.Status gpu.go It gives each GPU with its owner, its load and its marks.
runGPUs, visibleGPUs gpus.go The gpus mode of the helper.
deviceFiles gpus.go It counts the device files when nvidia-smi is not in the sandbox.
Manager.Create manager.go The GPU rules of the request.
Manager.boot manager.go It allocates the GPUs, writes the spec, starts the container and calls checkGPUs.
Manager.checkGPUs manager.go It asks the helper and compares the answer with the GPUs of the sandbox.
Manager.discard manager.go It removes a container and then releases the GPUs of its owner. If the removal fails, it keeps them for an orphan.
Manager.forget manager.go It drops a record and releases its GPUs, unless an orphan of the owner exists.
Manager.retryOrphans manager.go It removes orphans and then releases their GPUs.
New manager.go It holds the GPUs of records and of leftover containers again.
record.wantsGPUs manager.go The GPU count of a sandbox, also of a suspended one.
Manager.suspend, Manager.wakeLocked suspend.go They release the GPUs, and ask for them again.
Manager.Fork snapshot.go It gives each copy a GPU request.
Docker.Create docker.go It writes DeviceRequests.
gpuField.UnmarshalJSON server.go It reads the forms of the gpu field.
Server.gpus server.go It serves GET /v1/gpus.
Kit.gpus tools.go The tool gpu_list.
Name Value Where Meaning
gpuEnv NVIDIA_VISIBLE_DEVICES manager.go The variable that the daemon sets on each container.
literal in boot void manager.go The value of gpuEnv with no GPU, and with the cdi driver.
LabelGPUs boxfork.gpus manager.go The label with the UUIDs of the GPUs of a container.
GPUCheckAuto, GPUCheckRequire, GPUCheckOff auto, require, off manager.go The modes of --gpu-check. The default is auto.
Config.GPUDriver nvidia or cdi manager.go The default is nvidia. serve refuses other values.
literal in boot nvidia.com/gpu=<index> manager.go The device name with the cdi driver.
literal in Docker.Create [["gpu"]] docker.go Capabilities, when the driver is not cdi.
GPUsOK, GPUsUnknown, GPUsErr, GPUsCount OK, UNKNOWN, ERR, COUNT gpus.go The first word of the answer of the gpus mode.
gpuQuery 20 seconds gpus.go The wait of the helper for nvidia-smi in the sandbox.
gpuFile ^nvidia[0-9]+$ gpus.go The names of the GPU device files.
limit in oneLine 300 bytes gpus.go The maximum length of a reason in the answer.
literal in checkGPUs 45 seconds manager.go The wait of the daemon for the answer of guest gpus.
limit in askAs 16 KiB manager.go The maximum part of the answer that the daemon reads.
%.80q in checkGPUs 80 characters manager.go The maximum part of a bad answer in the error text.
literal in Create 8192 MiB manager.go The memory of a sandbox with GPUs that gives no value.
--gpu-busy-mb, busyAboveMB 512 MiB. 0 or less stops the check. main.go, gpu.go Used memory at which a free GPU is busy.
--gpu-idle-suspend, Config.GPUIdleSuspend 0, which means never. Else 10 seconds or more (MinSuspendAfter). main.go The default idle time of a sandbox with GPUs.
limit in discard 30 seconds manager.go The time for the removal of a container after a failed create.
limit in wakeLocked 5 minutes suspend.go The time for a wake, with the allocation and the check.
interval of Reconcile 20 seconds Watch, manager.go The interval of retryOrphans and reap.
interval of Sweep 1 second Watch, manager.go The interval of the timeouts.
owner of a leftover leftover-<container ID> New, manager.go The owner of the GPUs of a leftover container.
time limit of the nvidia-smi of the host none gpu.go Only the context of the caller stops it.
Error Text Source HTTP answer
*gpu.ErrUnavailable “this host has no GPUs” Allocate, Create, boot, Fork 409 gpu_unavailable
*gpu.ErrUnavailable “requested N GPU(s) but this host has M GPUs”, or “M GPUs matching the request” Allocate 409 gpu_unavailable
*gpu.ErrUnavailable “requested N GPU(s) but only K free: H held by other sandboxes”, then “B in use by programs outside boxfork” and “S no longer answering” when they apply Allocate 409 gpu_unavailable
*gpu.ErrUnavailable “sandbox ID was suspended while it sat idle and cannot carry on until it has a GPU again; its files are kept (reason)” wakeLocked 409 gpu_unavailable
*gpu.ErrUnavailable The reason, then “each copy of a GPU sandbox needs a GPU of its own, or fork with gpu set to 0 for copies without one” Fork, when the copies get the count of the original 409 gpu_unavailable
*sandbox.InvalidError “NVIDIA_VISIBLE_DEVICES is set by boxfork; ask for GPUs with the gpu option” Create 400 invalid_request
*sandbox.InvalidError “invalid JSON body: gpu must be a count, true, or an object like …” gpuField.UnmarshalJSON, decode 400 invalid_request
error “reading GPU usage: …” Allocate 500 internal
error “the sandbox’s container was given … but …, so it was removed”, in six forms checkGPUs 500 internal
error “gpu count must be at least 1” Allocate. Create never sends such a count. A client cannot cause it.
error “reading this host’s GPUs: nvidia-smi: …” serve The daemon does not start.
error “unknown –gpu-driver”, “unknown –gpu-check”, “–gpu-idle-suspend must be 0 or at least 10s” serve The daemon does not start.

A change must keep these rules.

  • One GPU has a maximum of one owner. Only Allocate and Restore set an owner.
  • A request gets all its GPUs or none. Allocate records owners only after all checks.
  • The GPU allocator gets a GPU back only after the engine removed the container, or said that the container is not there.
  • Allocate and Status call the Prober outside the lock of the allocator.
  • The manager never calls the GPU allocator while it holds m.mu.
  • Each container that boot makes has NVIDIA_VISIBLE_DEVICES: void, or the UUIDs of its own GPUs with the nvidia driver.
  • A client cannot set NVIDIA_VISIBLE_DEVICES.
  • Each container that boot makes has the label boxfork.gpus: empty, or the UUIDs of its own GPUs.
  • After the allocation, each failure in boot goes through fail. Do not return directly.
  • No client gets a sandbox with GPUs before checkGPUs accepts it, unless the mode is off.
  • gpu_verified is true only when the sandbox named exactly its own UUIDs.
  • A failed check calls Distrust before discard. A verified check calls Trust.
  • The daemon reads a maximum of 16 KiB of the answer of the helper, and waits a maximum of 45 seconds.
  • A restart keeps a sandbox that runs, also when the driver does not list its GPU.
Failure What happens Where
nvidia-smi is not on the PATH of the daemon The host has no GPUs. A GPU request gets 409. At a restart, New keeps each GPU sandbox whose container runs, with its files. It writes the warning “this host lists no GPUs now”. The sandbox keeps its GPU list, but no allocator counts those GPUs. NvidiaSMI.Devices, New
nvidia-smi fails at the start and gives no usable row serve stops with “reading this host’s GPUs”. serve
nvidia-smi of the host does not stop After 20 seconds the call fails. At the start, serve stops with the error. A create with a GPU and a wake get the error. NvidiaSMI.query
nvidia-smi fails at an allocation The create fails with “reading GPU usage”. HTTP 500 internal. Allocate
nvidia-smi fails at a status request GET /v1/gpus gives each GPU with no load and no missing mark. Status
A free GPU does not answer Allocate does not give it. The status shows missing. Allocate
A held GPU does not answer The sandbox keeps it and runs. The status shows missing with the owner. Status
Not enough free GPUs 409 gpu_unavailable with the causes. Allocate
The engine attaches no GPU or other GPUs checkGPUs fails. boot distrusts the GPUs and removes the container. HTTP 500 internal. boot
The GPU driver fails in the sandbox The answer is ERR. The same result as the line above. checkGPUs
The image has no nvidia-smi The helper counts the device files. A correct count gives a sandbox that is not verified. With require, the create fails. deviceFiles, checkGPUs
The engine cannot remove the container after a failed check The container goes to orphans. The GPUs stay held until retryOrphans removes it. discard
A kill cannot remove the container The sandbox stays, the client gets the error, and the GPUs stay held. remove
A wake finds no free GPU 409 gpu_unavailable. The sandbox stays suspended with its files. wakeLocked
A fork cannot get GPUs for each copy 409 gpu_unavailable. The fork removes all copies. Fork
A GPU of a record is not listed at a restart The sandbox stays, with a warning. New
A GPU of a record has a different owner at a restart New drops the record and removes the container. New
--gpu-idle-suspend on a host that cannot save files serve writes a warning. No sandbox is suspended. serve
  1. Read the invariants on this page.
  2. Change Allocator.Allocate or Allocator.matches in gpu.go.
  3. Keep the call to Prober.Usage before the lock.
  4. Record the owners only after the last check.
  5. If you add a field to Request, add it to gpuField and to http-api.md.
  6. Decide if Fork must copy the new field. Today it copies only the count.
  7. If you add a field to Status, show it in boxfork gpus and in gpu_list.
  8. Add a test in gpu_test.go with fakeProber.
  9. Run go test -race ./internal/gpu/ ./internal/sandbox/.
  1. Write a type that has the methods of Prober.
  2. Give an empty list and no error when the tool of the vendor is not installed.
  3. Give each GPU a UUID that does not change.
  4. Leave a GPU that does not answer out of the result of Usage.
  5. Make serve in main.go select the prober.
  6. Change boot, which knows only the drivers nvidia and cdi and the variable NVIDIA_VISIBLE_DEVICES.
  7. Change visibleGPUs and deviceFiles in gpus.go for the new vendor.
  8. Change the installer, which knows only the NVIDIA Container Toolkit.
  9. Run the proof on a machine with that hardware before you write that it works.
  1. Change visibleGPUs or deviceFiles in gpus.go.
  2. Start each answer line with a constant word, as GPUsOK does.
  3. Keep the answer on one line. Make each reason short with oneLine.
  4. Change checkGPUs in manager.go to read the new answer.
  5. Remove the sandbox for each answer that checkGPUs does not understand.
  6. Change the answer of the test engine in Fake.ask in fake.go.
  7. Add a case to gpus_test.go and to gpucheck_test.go.
  8. Run go test -race ./internal/guest/ ./internal/sandbox/.
  1. Change boot in manager.go, where the driver selects the device names.
  2. Keep NVIDIA_VISIBLE_DEVICES and the label boxfork.gpus correct for the new driver.
  3. Change Docker.Create in docker.go if the request body changes.
  4. Change the test of the request body in docker_test.go.
  5. Add the new value to the check of --gpu-driver in serve.
  6. Change Fake.ask, which answers UNKNOWN for the cdi driver.
  7. Run go test -race ./internal/engine/... ./internal/sandbox/.
  8. Start a GPU sandbox on a real engine with a real GPU.
  1. Change detect_gpus, install_nvidia_toolkit or check_gpu_driver in install.sh.
  2. Change the stand-ins and the tests in the section “GPUs” of install_test.sh.
  3. Run make test-install.
File What it tests
internal/gpu/gpu_test.go NvidiaSMI with a shell script in the place of nvidia-smi. The GPU allocator with fakeProber.
internal/guest/gpus_test.go The gpus mode with a script in the place of nvidia-smi, and with a directory in the place of /dev.
internal/sandbox/gpucheck_test.go checkGPUs: each answer, each mode, and the limit on the answer.
internal/sandbox/manager_test.go Exclusive GPUs, CDI names, the variable, the label, restarts, orphans.
internal/sandbox/faults_test.go Engine failures with GPUs, the suspect mark, the device count, a restart when the driver does not list a GPU.
internal/sandbox/suspend_test.go The release at a suspension and the new allocation at a wake.
internal/engine/docker_test.go The body of DeviceRequests against a stand-in for the Engine API.
internal/api/server_test.go The forms of the gpu field, GET /v1/gpus, and a fork of a GPU sandbox.
internal/tools/tools_test.go The tool gpu_list and GPUs in the tools for create and fork.
cmd/boxfork/cli_test.go boxfork create --gpu 1, boxfork ls and boxfork gpus.
scripts/install_test.sh The installer against stand-ins for nvidia-smi, nvidia-ctk, runsc, apt-get, dnf and yum.

The manager tests, the API tests and the CLI tests use GPUs in memory with the names GPU-a, GPU-0 and so on. The helper in these tests does not run nvidia-smi: the test engine answers guest gpus itself, from the field GPUsSeen or from the spec. The stand-ins for the API tests and the SDK tests are in apitest.go.

The most important tests:

Test What it proves with stand-ins
TestConcurrentAllocationsNeverShareADevice Many allocations at the same time never give one GPU twice.
TestAllocateIsAllOrNothing A request that cannot get all its GPUs gets none.
TestAllocateFiltersByModelAndMemory Model and MinMemoryMB.
TestAllocateSkipsDevicesUsedOutsideBoxfork The busy rule.
TestAllocateSkipsDevicesThatStoppedAnswering The missing rule.
TestADoubtedDeviceIsHandedOutLast The order of the suspect mark.
TestRestoreRebuildsOwnershipAndRejectsConflicts Restore.
TestNvidiaSMIWithALostDevice A failed nvidia-smi that still gives the rows of the other GPUs.
TestNvidiaSMIMissingBinaryMeansNoGPUs, TestNvidiaSMIFailureIsAnError Discovery with no nvidia-smi, and with a failed one.
TestGPUsCommand, TestGPUsFromDeviceFiles The answers of the helper.
TestGPUsAreExclusive Two sandboxes never hold one GPU. A kill that does not remove the container keeps the GPU held.
TestCDIDeviceNames The device names of the cdi driver.
TestSandboxesOnlySeeTheGPUsTheyWereGiven void for a sandbox with no GPU, the UUIDs for a sandbox with GPUs, and the refusal of the variable from a client.
TestAGPUSandboxIsAskedWhatItSees Each wrong answer removes the sandbox and releases the GPU. gpu_verified stays across a restart.
TestTheAnswerAboutGPUsIsReadWithABound An answer with no end does not block the daemon.
TestASandboxThatCannotLookAtItsGPUs The modes auto, require and off.
TestASandboxWithoutNvidiaSMIIsCountedByItsDeviceFiles The COUNT answer.
TestAGPUThatFailsItsSandboxIsNotTheNextOneHandedOut Distrust and Trust through the manager.
TestAGPUStaysHeldWhileItsContainerCannotBeRemoved Orphans after a failed create, and a leftover container at the start.
TestAGPUOutlivesItsSandboxWhileAContainerIsStillOnIt A kill and a timeout do not release a GPU that an orphan holds.
TestARestartWhileASandboxsGPUIsGone New keeps a sandbox whose GPU the driver does not list.
TestRestartRecovery The GPUs of a sandbox that survived a restart stay held. The GPU of a dead sandbox is free.
TestSuspendingFreesTheGPU, TestASandboxComesBackWithTheKindOfGPUItHad The release at a suspension, and Want at a wake.
TestCreateSendsLimitsHardeningAndGPUs, TestCreateWithCDIAndWithoutOptionalSettings The body of DeviceRequests for nvidia and for cdi.
TestGPURequests The forms of the gpu field and GET /v1/gpus.

testing.md has a break-it check for the refusal of NVIDIA_VISIBLE_DEVICES.

None of the GPU code ran on a machine with an NVIDIA GPU. It ran only against stand-ins. Until issue #1 is done, each statement about GPUs on this page comes from the code and from tests with stand-ins.

  • Discovery. The tests use a shell script with a fixed output. Nobody compared it with the output of a real nvidia-smi.
  • The nvidia driver. No engine with a GPU got a DeviceRequests item from the daemon. The tests examine only the request body.
  • The cdi driver. No engine with CDI got a request from the daemon. The test engine answers UNKNOWN for cdi, thus no test makes a verified CDI sandbox.
  • The index in the CDI names. Nobody made sure that the index of nvidia-smi is the index in the CDI specification.
  • void. Nobody made sure that NVIDIA_VISIBLE_DEVICES=void gives no GPU on a host where the NVIDIA runtime is the default.
  • The check at creation. The gpus mode never ran in a container with a GPU. The COUNT answer never counted real device files.
  • gVisor. The installer gives --nvproxy=true to runsc. No GPU sandbox ran under gVisor.
  • Podman. No GPU sandbox ran on Podman.
  • The release. No test shows that a real GPU is free for a second container immediately after the engine removed the first container.
  • Health. No test used a real GPU that does not answer. The rules for missing and busy ran only with GPUs in memory.
  • The default 512 of --gpu-busy-mb. The tree does not give a reason for this value. No test with a real GPU examined it.
  • The installer. It ran only against stand-ins for nvidia-smi, nvidia-ctk, runsc and the package managers. It never ran on a GPU machine.

To repeat: none of this ran on real NVIDIA hardware. It ran only against stand-ins.