Repository navigation
feat: add host PID broker client and CUDA context accounting #251
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
iemAnshuman
wants to merge
55
commits into
Project-HAMi:main
from
iemAnshuman:feat-hostpid-broker-client
Closed
Changes from 12 commits
Commits
Show all changes
55 commits
Select commit
Hold shift + click to select a range
94c0b01
add primary context accounting state
iemAnshuman 1dc5556
measure primary context memory lazily
iemAnshuman a86c547
add bounded host pid broker client
iemAnshuman b7196ed
use the host pid broker during init
iemAnshuman 90bdcac
enforce the host pid broker deadline
iemAnshuman 35acc72
suppress fallback probe accounting warning
iemAnshuman 49c754c
harden the host PID broker client
iemAnshuman 758c2ea
harden forked process accounting
iemAnshuman 95ecfc7
complete broker accounting lifecycle
iemAnshuman 507d140
map visible CUDA devices by PCI identity
iemAnshuman c0dc37c
keep device accounting indexes explicit
iemAnshuman 973a989
reject unaccounted primary-context retains
iemAnshuman e52afdf
build: remove duplicate CUDA hook source
iemAnshuman 317553c
test: keep context accounting assertions active
iemAnshuman eacaafb
fix: initialize missing utilization samples
iemAnshuman 3037196
fix: synchronize host PID monitor reset
iemAnshuman 596e354
fix: preserve merged process memory usage
iemAnshuman f08b8bc
fix: bound NVML process allocation
iemAnshuman 078a2d2
fix: bound CUDA device mapping lookups
iemAnshuman 6d780e2
fix: set broker socket flags atomically
iemAnshuman ada4328
test: bound broker socket paths
iemAnshuman 2a5a311
fix: isolate context measurement by device
iemAnshuman 64356b9
docs: distinguish broker and fallback locking
iemAnshuman 80771e3
docs: require cache mapping checks
iemAnshuman 0e328a7
docs: link post init lock migration
iemAnshuman f65b6df
fix: use fixed width memory measurements
iemAnshuman 3bfaf67
fix: use fixed width broker timing types
iemAnshuman 9a5f602
fix: name broker socket path capacity
iemAnshuman d2d49d2
style: space CUDA mapping function brace
iemAnshuman 08d1d90
test: use fixed width broker timing
iemAnshuman 5a19ebf
test: bound broker directory template copy
iemAnshuman f60d70b
test: name overlong broker path size
iemAnshuman f5b2093
docs: hyphenate post init lock
iemAnshuman 5f1c805
docs: list broker fallback conditions
iemAnshuman aba801d
docs: update host PID client status
iemAnshuman 8d59bf9
docs: clarify broker mount contract
iemAnshuman 8218aef
fix: serialize host PID fallback across caches
iemAnshuman e3ff7d6
test: size fallback descriptor paths safely
iemAnshuman b0df70a
fix: use trusted host PID fallback mount
iemAnshuman bc74844
fix: bound postinit lock acquisition
iemAnshuman 17e5b8d
fix: harden host PID fallback locking
iemAnshuman 4c8a114
test: cover host PID fallback hardening
iemAnshuman c302258
fix: share the host PID fallback deadline
iemAnshuman 607d7e8
docs: describe hardened fallback locking
iemAnshuman ea310b1
docs: state the broker transaction timeout
iemAnshuman 2a87ae1
docs: align fallback selection with the gate
iemAnshuman 276e98c
test: extend the fallback lock timeout
iemAnshuman af203b2
test: widen the fallback deadline margin
iemAnshuman f62a6e9
test: guard fallback pipe cleanup
iemAnshuman 363d46f
test: release an unexpected cache lock
iemAnshuman 08c7542
style: fix the fallback lock header guard
iemAnshuman 0598f11
style: order the fallback test includes
iemAnshuman 46bccc2
test: use the cache path extent
iemAnshuman 4421b09
style: use fixed width fallback types
iemAnshuman 6d67df3
Merge remote-tracking branch 'origin/main' into pr251-conflict-resolu…
iemAnshuman File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| # Host PID broker client | ||
|
|
||
| Status: local draft. The feature is disabled by default. | ||
|
|
||
| ## Purpose | ||
|
|
||
| The client lets HAMi-core learn its host PID without creating a temporary CUDA primary context during `postInit()`. It uses the version 1 Unix socket protocol documented by HAMi and preserves the existing NVML discovery path as its fallback. A successful broker lookup still initializes NVML before utilization and context accounting use it. | ||
|
|
||
| ## Enablement | ||
|
|
||
| The client is enabled only when `LIBVGPU_HOSTPID_BROKER` is the exact string `1`. Every other value leaves the current behavior unchanged. | ||
|
|
||
| The expected container socket is fixed at `/tmp/vgpulock/hostpid/broker.sock`. A caller cannot redirect the production client to another path. | ||
|
|
||
| ## Trust checks | ||
|
|
||
| Before using a reply, the client verifies all of the following: | ||
|
|
||
| 1. The socket path is the fixed production path. | ||
|
|
||
| 2. The immediate directory is a real directory owned by root and is not writable by group or other users. | ||
|
|
||
| 3. The socket is a real Unix socket owned by root. | ||
|
|
||
| 4. The directory is visible through a read only mount. | ||
|
|
||
| 5. The connected server peer has UID 0 according to `SO_PEERCRED`. | ||
|
|
||
| 6. The reply has the expected magic, version, success status, and a positive PID that fits in `pid_t`. | ||
|
|
||
| One monotonic deadline covers connect, request write, and response read. A trickle response cannot renew the deadline. | ||
|
|
||
| ## Fallback | ||
|
|
||
| If the gate is disabled or any trust, connection, deadline, or protocol check fails, `postInit()` acquires the existing process death safe lock and runs the current NVML discovery path. The broker failure does not supply a PID and does not bypass the fallback. | ||
|
|
||
| ## CUDA context accounting | ||
|
|
||
| The old discovery path obtains both the host PID and the primary context size from a temporary context. The broker path skips that retain and release probe, then measures memory when the application first retains the real primary context. | ||
|
|
||
| After NVML starts, the broker path maps each visible CUDA device to its physical NVML index by PCI identity. This mapping does not parse `CUDA_VISIBLE_DEVICES`, so an index list and a GPU UUID use the same driver supplied identity. Devices outside the CUDA visibility set remain unmapped. | ||
|
|
||
| Per device limits, monitored memory, and utilization remain indexed by the visible CUDA ordinal because they describe the workload's device view. Allocation and primary context usage are indexed by physical NVML device so processes with different visibility orders still aggregate against the same GPU. Reverse lookup scans only visible CUDA devices, and an invalid or missing mapping fails closed before shared memory access. | ||
|
|
||
| Accounting state is kept per visible CUDA device. A nested retain adds one context charge, and the final successful release removes it. A failed add can be retried by a later retain. A failed removal stays recorded so a later retain and final release cycle can retry without adding the same charge twice. Fork handlers clear inherited accounting state and the cached parent slot before the child repeats initialization. | ||
|
|
||
| Hardware validation must prove that the context appears in NVML within the bounded measurement window and that the final shared region charge matches the observed primary context memory. The matrix must include a nonzero physical GPU selected by UUID and must verify the CUDA ordinal, NVML index, and primary context accounting index. | ||
|
|
||
| ## Compatibility | ||
|
|
||
| 1. New HAMi-core with no server uses the existing fallback. | ||
|
|
||
| 2. Old HAMi-core ignores the new environment value and mount. | ||
|
|
||
| 3. A rejected or unknown protocol version uses the existing fallback. | ||
|
|
||
| 4. Existing workloads do not receive the broker mount until they are recreated. | ||
|
|
||
| The separate post init lock migration from PR 248 has a different mixed binary constraint and is not changed by this client. | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| # Post init lock upgrade contract | ||
|
|
||
| Status: required for upgrades across the semaphore to record lock boundary. | ||
|
|
||
| ## Why a rolling upgrade is unsafe | ||
|
|
||
| Older HAMi-core processes serialize host PID discovery with | ||
| `sem_postinit`, a process shared unnamed semaphore inside the shared cache. | ||
| Newer processes keep that field for layout compatibility but serialize with a | ||
| POSIX record lock at a fixed offset in the cache file. | ||
|
|
||
| The two primitives do not observe each other. An old process can hold the | ||
| semaphore while a new process holds the record lock, so both can execute host | ||
| PID discovery at the same time. The cache version fields do not prevent this. | ||
| Existing binaries only report a version mismatch and continue. | ||
|
coderabbitai[bot] marked this conversation as resolved.
Resolved
|
||
|
|
||
| The local mixed protocol probe reproduces the overlap directly. It is under | ||
| `evidence/issue-1662/lock-benchmark/mixed_protocol_probe.c` in the companion | ||
| evidence tree. | ||
|
|
||
| ## Required upgrade | ||
|
|
||
| 1. Cordon the node and stop every GPU workload that can map the affected | ||
| shared cache. | ||
|
|
||
| 2. Confirm that no old HAMi-core process still has the cache open. Check every | ||
| effective `CUDA_DEVICE_MEMORY_SHARED_CACHE` path, including generated | ||
| per-container `usage.cache` files and the `/tmp/cudevshr.cache` fallback. | ||
|
|
||
| 3. Remove each cache only after all processes that mapped it have exited. | ||
|
coderabbitai[bot] marked this conversation as resolved.
Resolved
|
||
|
|
||
| 4. Install one HAMi-core revision across the node. | ||
|
|
||
| 5. Restart the device plugin and recreate workloads. | ||
|
|
||
| 6. Validate one low-density workload before uncordoning the node. Confirm host | ||
| PID detection, memory accounting, and record lock acquisition. | ||
|
|
||
| Do not delete or replace a cache while a process still maps it. Unlinking the | ||
| path does not move an existing mapping to the new file and can split accounting | ||
| between two cache objects. | ||
|
|
||
| ## Required rollback | ||
|
|
||
| Rollback across this boundary uses the same drain procedure. Stopping only the | ||
| device plugin is insufficient because workload processes contain HAMi-core and | ||
| retain their cache mappings. | ||
|
|
||
| ## Deployment guarantee | ||
|
|
||
| The current implementation does not support a mixed-version grace period and | ||
| does not fail closed when an old process joins. Cluster rollout policy must | ||
| prevent mixed HAMi-core revisions on a shared cache. A future rolling protocol | ||
| would need an enforced version handshake that both old and new binaries obey. | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,3 @@ | ||
| add_library(cuda_mod OBJECT context.c device.c hook.c event.c hook.c memory.c stream.c graph.c) | ||
| add_library(cuda_mod OBJECT context.c context_accounting.c device.c hook.c event.c hook.c memory.c stream.c graph.c) | ||
| target_compile_options(cuda_mod PUBLIC ${LIBRARY_COMPILE_FLAGS}) | ||
| target_link_libraries(cuda_mod PUBLIC nvidia-ml -lcuda) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.