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-....
The parts
Section titled “The parts”| 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.
How it works
Section titled “How it works”One GPU, one owner
Section titled “One GPU, one owner”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.
Discovery
Section titled “Discovery”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-smiis not on thePATHof the daemon,Devicesgives an empty list and no error. - If
nvidia-smifails and gives no usable row,Devicesgives an error.servethen stops with “reading this host’s GPUs”. - If
nvidia-smifails but gives some usable rows,Devicesuses those rows. A GPU that the driver lost has no row. parseRowskeeps only a row that has the number of fields of the query.numberreads a value such as[N/A]or[Not Supported]as 0.Devicessorts 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).
The request
Section titled “The request”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_DEVICESinenvfails withInvalidError. - 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:
takein 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.
The allocation flow
Section titled “The allocation flow”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 --> RAllocator.Allocate does these steps:
- It refuses a count below 1, and a host with no GPUs.
- It calls
Prober.Usagebefore it takes its lock, because the call starts a program. - If
Usagefails,Allocategives the error “reading GPU usage”. This error is not anErrUnavailable. - Under its lock, it examines each GPU in the order of the index.
- It skips a GPU that does not match the request.
- It skips a GPU that has an owner.
- It skips a GPU that is not in the output of
Usage. - It skips a free GPU whose used memory is
busyAboveMBor more, whenbusyAboveMBis more than 0. - It puts each GPU with the
suspectmark after all other GPUs. - If fewer GPUs match than the count, it gives an
ErrUnavailablethat names the number on the host. - If fewer GPUs are free than the count, it gives an
ErrUnavailablewith the causes. - 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.
How the engine attaches a GPU
Section titled “How the engine attaches a GPU”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.
The check at creation
Section titled “The check at creation”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-smiis on thePATHof the sandbox, the helper runsnvidia-smi --query-gpu=uuid --format=csv,noheader. It waits a maximum ofgpuQuery, 20 seconds. - If
nvidia-smisucceeds, the line isOKand the UUIDs, separated by commas.OKalone means that the sandbox sees no GPU. - If
nvidia-smifails, the line isERRand the reason.oneLinekeeps a maximum of 300 bytes of the reason. - If
nvidia-smiis not there,deviceFilesexamines/dev. - With no
/dev/nvidiactl, the line isOK. The comment ondeviceFilesgives the reason: each program that uses an NVIDIA GPU opens that file. - With
/dev/nvidiactl, the line isCOUNTand the number of files whose name matches^nvidia[0-9]+$. - If the helper cannot read
/dev, the line isUNKNOWNand 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.
The gpu_verified state
Section titled “The gpu_verified state”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
OKwith exactly the UUIDs of the sandbox. - It is false with
--gpu-check off, and for a sandbox thatautoaccepted without verification. suspendsets 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.
What a sandbox without a GPU must not see
Section titled “What a sandbox without a GPU must not see”A sandbox without a GPU must not get any GPU of the host. The code has these mechanisms:
bootsetsNVIDIA_VISIBLE_DEVICEStovoidfor 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,allgives each GPU to the container. The comment ongpuEnvsays so. - The image of a snapshot can contain the variable with the UUIDs of its source sandbox.
voidreplaces that value too. CreaterefusesNVIDIA_VISIBLE_DEVICESin theenvof a request. The client gets 400invalid_request.- With no request,
CreateSpec.GPUis nil. ThenDocker.Createsends noDeviceRequests. bootsets the labelboxfork.gpuson 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
checkSnapshotsin snapshot.go also getvoidand an empty label. - A pool never starts a sandbox with GPUs.
- A fork with
gpuset 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.
GPU health
Section titled “GPU health”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
Usagedoes not go to a sandbox.Statussetsmissingonly whenUsagesucceeded. - Busy. A free GPU with
busyAboveMBMiB 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.Statussetsbusyonly for a GPU with no owner. - Suspect.
Distrustsets the mark after a failed check.Allocategives such a GPU only after all other GPUs.Trustclears 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 gpusshows the sandbox ID with(not answering). - A restart.
Allocator.Restoregives the UUIDs of a record that the driver does not list now.Newkeeps the sandbox and writes a warning.
The suspect mark is only in memory. A restart of the daemon clears all marks.
States of a GPU
Section titled “States of a GPU”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 |
Release
Section titled “Release”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.
Suspend, wake and fork
Section titled “Suspend, wake and fork”The details are in suspend.md and in snapshots-and-forks.md. The GPU parts are these:
suspendreleases the GPUs only after the container is gone. It setsGPUsto nil andGPUVerifiedto false.- The record keeps the request of the create in
record.Want. The state file has it asgpu_request. wakeLockedcallsbootwithWant. The wake gets GPUs that match the same request, which can be different GPUs.- The wake runs
checkGPUsagain. A failed check leaves the sandbox suspended and sets thesuspectmark. - 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”. wakeLockedgives an error while an orphan of the sandbox exists. That container can still hold the GPUs.record.wantsGPUsgives the GPU count of a sandbox, also of a suspended one.Forkgives each copy a request with the count ofwantsGPUs, if the fork request has nogpufield.ModelandMinMemoryMBdo 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.
Restart
Section titled “Restart”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:
- The GPU does not answer and has an owner.
- The GPU does not answer.
- The GPU has an owner.
- The GPU is busy.
- The GPU has the
suspectmark.
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.
detect_gpusrunsnvidia-smi --query-gpu=driver_version --format=csv,noheader.- If that works, the installer keeps the number of lines and the first driver version.
- If
nvidia-smiis there and fails, the installer writes a warning and continues with no GPU setup. - If
nvidia-smiis not there andlspcishows an NVIDIA device, the installer writes a warning. - With no GPU,
install_nvidia_toolkitdoes nothing. - If
nvidia-ctkornvidia-container-runtimeis on the machine, the installer installs no package. - With
--no-nvidia-toolkit, the installer installs no package and writes a warning. - With
apt-get, it adds the key and the package list of NVIDIA, then installsnvidia-container-toolkit. - With
dnforyum, it adds the repository file of NVIDIA, then installsnvidia-container-toolkit. - If the machine has none of these package managers, it writes a warning and stops this part.
- If
nvidia-ctkis there, it runsnvidia-ctk runtime configure --runtime=dockerand reloads Docker. - When it sets up gVisor on a GPU machine, it adds
--nvproxy=truetorunsc install. check_gpu_drivercompares the driver version withrunsc 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.
What the code does not do
Section titled “What the code does not do”- It knows only NVIDIA GPUs.
NvidiaSMIis the onlyProberthatserveuses. - 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
suspectmark. - It does not read the list of GPUs again while it runs.
Reference
Section titled “Reference”Functions
Section titled “Functions”| 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. |
Constants and limits
Section titled “Constants and limits”| 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. |
Errors
Section titled “Errors”| 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. |
Invariants
Section titled “Invariants”A change must keep these rules.
- One GPU has a maximum of one owner. Only
AllocateandRestoreset an owner. - A request gets all its GPUs or none.
Allocaterecords 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.
AllocateandStatuscall theProberoutside the lock of the allocator.- The manager never calls the GPU allocator while it holds
m.mu. - Each container that
bootmakes hasNVIDIA_VISIBLE_DEVICES:void, or the UUIDs of its own GPUs with thenvidiadriver. - A client cannot set
NVIDIA_VISIBLE_DEVICES. - Each container that
bootmakes has the labelboxfork.gpus: empty, or the UUIDs of its own GPUs. - After the allocation, each failure in
bootgoes throughfail. Do not return directly. - No client gets a sandbox with GPUs before
checkGPUsaccepts it, unless the mode isoff. gpu_verifiedis true only when the sandbox named exactly its own UUIDs.- A failed check calls
Distrustbeforediscard. A verified check callsTrust. - 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 cases
Section titled “Failure cases”| 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 |
How to change it
Section titled “How to change it”Change the rules of the GPU allocator
Section titled “Change the rules of the GPU allocator”- Read the invariants on this page.
- Change
Allocator.AllocateorAllocator.matchesin gpu.go. - Keep the call to
Prober.Usagebefore the lock. - Record the owners only after the last check.
- If you add a field to
Request, add it togpuFieldand to http-api.md. - Decide if
Forkmust copy the new field. Today it copies only the count. - If you add a field to
Status, show it inboxfork gpusand ingpu_list. - Add a test in gpu_test.go with
fakeProber. - Run
go test -race ./internal/gpu/ ./internal/sandbox/.
Add a GPU vendor or a different prober
Section titled “Add a GPU vendor or a different prober”- Write a type that has the methods of
Prober. - Give an empty list and no error when the tool of the vendor is not installed.
- Give each GPU a UUID that does not change.
- Leave a GPU that does not answer out of the result of
Usage. - Make
servein main.go select the prober. - Change
boot, which knows only the driversnvidiaandcdiand the variableNVIDIA_VISIBLE_DEVICES. - Change
visibleGPUsanddeviceFilesin gpus.go for the new vendor. - Change the installer, which knows only the NVIDIA Container Toolkit.
- Run the proof on a machine with that hardware before you write that it works.
Change the check at creation
Section titled “Change the check at creation”- Change
visibleGPUsordeviceFilesin gpus.go. - Start each answer line with a constant word, as
GPUsOKdoes. - Keep the answer on one line. Make each reason short with
oneLine. - Change
checkGPUsin manager.go to read the new answer. - Remove the sandbox for each answer that
checkGPUsdoes not understand. - Change the answer of the test engine in
Fake.askin fake.go. - Add a case to gpus_test.go and to gpucheck_test.go.
- Run
go test -race ./internal/guest/ ./internal/sandbox/.
Change how the engine attaches a GPU
Section titled “Change how the engine attaches a GPU”- Change
bootin manager.go, where the driver selects the device names. - Keep
NVIDIA_VISIBLE_DEVICESand the labelboxfork.gpuscorrect for the new driver. - Change
Docker.Createin docker.go if the request body changes. - Change the test of the request body in docker_test.go.
- Add the new value to the check of
--gpu-driverinserve. - Change
Fake.ask, which answersUNKNOWNfor thecdidriver. - Run
go test -race ./internal/engine/... ./internal/sandbox/. - Start a GPU sandbox on a real engine with a real GPU.
Change the installer
Section titled “Change the installer”- Change
detect_gpus,install_nvidia_toolkitorcheck_gpu_driverin install.sh. - Change the stand-ins and the tests in the section “GPUs” of install_test.sh.
- Run
make test-install.
Where the tests are
Section titled “Where the tests are”| 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.
Not proven
Section titled “Not proven”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
nvidiadriver. No engine with a GPU got aDeviceRequestsitem from the daemon. The tests examine only the request body. - The
cdidriver. No engine with CDI got a request from the daemon. The test engine answersUNKNOWNforcdi, thus no test makes a verified CDI sandbox. - The index in the CDI names. Nobody made sure that the index of
nvidia-smiis the index in the CDI specification. void. Nobody made sure thatNVIDIA_VISIBLE_DEVICES=voidgives no GPU on a host where the NVIDIA runtime is the default.- The check at creation. The
gpusmode never ran in a container with a GPU. TheCOUNTanswer never counted real device files. - gVisor. The installer gives
--nvproxy=truetorunsc. 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
missingandbusyran 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,runscand 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.