-
Notifications
You must be signed in to change notification settings - Fork 11
feat(agent): hook CUDA VMM prepare and restore around native checkpoint #110
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
Open
Open
Changes from all commits
Commits
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,156 @@ | ||
| // SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| package cuda | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "os" | ||
| "os/exec" | ||
| "path/filepath" | ||
| "strconv" | ||
| "strings" | ||
|
|
||
| snapshotv1alpha1 "github.com/ai-dynamo/snapshot/api/v1alpha1" | ||
| "github.com/go-logr/logr" | ||
| ) | ||
|
|
||
| const ( | ||
| cuinterposerCoordinator = "/usr/local/bin/cuinterposer-coordinator" | ||
| cuinterposerSocketPrefix = "cuinterposer-" | ||
| cuinterposerStateFile = "cuinterposer.state" | ||
| ) | ||
|
Comment on lines
+19
to
+23
|
||
|
|
||
| func snapshotControlDir() string { | ||
| return strings.TrimPrefix(snapshotv1alpha1.SnapshotControlMountPath, string(os.PathSeparator)) | ||
| } | ||
|
|
||
| func cuinterposerEndpointPath(procRoot string, observedPID, namespacePID int) string { | ||
| return filepath.Join( | ||
| procRoot, | ||
| strconv.Itoa(observedPID), | ||
| "root", | ||
| snapshotControlDir(), | ||
| fmt.Sprintf("%s%d.sock", cuinterposerSocketPrefix, namespacePID), | ||
| ) | ||
| } | ||
|
|
||
| // DetectCUDAInterposition reports whether the live CUDA processes are running | ||
| // the interposer. The signal is the shim's Unix sockets under the container's | ||
| // snapshot-control mount, not /proc/<pid>/environ: Python setproctitle | ||
| // (vLLM/SGLang) can clobber procfs environment while the sockets remain. | ||
| // No sockets skips prepare. A partial or invalid set fails closed. | ||
| func DetectCUDAInterposition(procRoot string, observedPIDs, namespacePIDs []int) (bool, error) { | ||
| if len(observedPIDs) != len(namespacePIDs) { | ||
| return false, fmt.Errorf( | ||
| "cuinterposer PID mapping count mismatch: observed=%d namespace=%d", | ||
| len(observedPIDs), | ||
| len(namespacePIDs), | ||
| ) | ||
| } | ||
| if len(observedPIDs) == 0 { | ||
| return false, nil | ||
| } | ||
| validEndpoints := 0 | ||
| seenEndpoints := 0 | ||
| for index, observedPID := range observedPIDs { | ||
| endpoint := cuinterposerEndpointPath(procRoot, observedPID, namespacePIDs[index]) | ||
| info, err := os.Lstat(endpoint) | ||
| if os.IsNotExist(err) { | ||
| continue | ||
| } | ||
| if err != nil { | ||
| return false, fmt.Errorf("stat cuinterposer endpoint %q: %w", endpoint, err) | ||
| } | ||
| seenEndpoints++ | ||
| if info.Mode()&os.ModeSocket != 0 { | ||
| validEndpoints++ | ||
| } | ||
| } | ||
| if seenEndpoints == 0 { | ||
| return false, nil | ||
| } | ||
| if validEndpoints != len(observedPIDs) { | ||
| return false, fmt.Errorf( | ||
| "cuinterposer endpoint missing or invalid for %d of %d CUDA processes", | ||
| len(observedPIDs)-validEndpoints, | ||
| len(observedPIDs), | ||
| ) | ||
| } | ||
| return true, nil | ||
| } | ||
|
|
||
| func HasCUDAInterpositionState(checkpointDir string) (bool, error) { | ||
| _, err := os.Stat(filepath.Join(checkpointDir, cuinterposerStateFile)) | ||
| if os.IsNotExist(err) { | ||
| return false, nil | ||
| } | ||
| if err != nil { | ||
| return false, err | ||
| } | ||
| return true, nil | ||
| } | ||
|
|
||
| func PrepareCUDAInterposition( | ||
| ctx context.Context, | ||
| checkpointDir string, | ||
| procRoot string, | ||
| observedPIDs []int, | ||
| namespacePIDs []int, | ||
| log logr.Logger, | ||
| ) error { | ||
| args, err := cuinterposerArgs("prepare", checkpointDir, procRoot, observedPIDs, namespacePIDs) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| output, err := exec.CommandContext(ctx, cuinterposerCoordinator, args...).CombinedOutput() | ||
| if err != nil { | ||
| return fmt.Errorf("%s failed: %w (output: %s)", cuinterposerCoordinator, err, strings.TrimSpace(string(output))) | ||
| } | ||
| log.Info("Prepared cuinterposer state") | ||
| return nil | ||
| } | ||
|
|
||
| func RestoreCUDAInterposition( | ||
| ctx context.Context, | ||
| checkpointDir string, | ||
| observedPIDs []int, | ||
| namespacePIDs []int, | ||
| ) error { | ||
| args, err := cuinterposerArgs("restore", checkpointDir, "", observedPIDs, namespacePIDs) | ||
|
|
||
| if err != nil { | ||
| return err | ||
| } | ||
| output, err := exec.CommandContext(ctx, cuinterposerCoordinator, args...).CombinedOutput() | ||
| if err != nil { | ||
| return fmt.Errorf("%s failed: %w (output: %s)", cuinterposerCoordinator, err, strings.TrimSpace(string(output))) | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| func cuinterposerArgs(operation, checkpointDir, procRoot string, observedPIDs, namespacePIDs []int) ([]string, error) { | ||
| if len(observedPIDs) != len(namespacePIDs) { | ||
| return nil, fmt.Errorf( | ||
| "cuinterposer PID mapping count mismatch: observed=%d namespace=%d", | ||
| len(observedPIDs), | ||
| len(namespacePIDs), | ||
| ) | ||
| } | ||
| args := []string{ | ||
| "--" + operation, | ||
| "--proc-root", | ||
| procRoot, | ||
| "--checkpoint-dir", | ||
| checkpointDir, | ||
| } | ||
| for index, observedPID := range observedPIDs { | ||
| args = append( | ||
| args, | ||
| "--process", | ||
| strconv.Itoa(observedPID), | ||
| strconv.Itoa(namespacePIDs[index]), | ||
| ) | ||
| } | ||
| return args, nil | ||
| } | ||
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,155 @@ | ||
| // SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| package cuda | ||
|
|
||
| import ( | ||
| "net" | ||
| "os" | ||
| "path/filepath" | ||
| "strconv" | ||
| "testing" | ||
| ) | ||
|
|
||
| func TestDetectCUDAInterpositionSkipsEmptyPIDList(t *testing.T) { | ||
| interposed, err := DetectCUDAInterposition(t.TempDir(), nil, nil) | ||
| if err != nil { | ||
| t.Fatalf("DetectCUDAInterposition() error = %v", err) | ||
| } | ||
| if interposed { | ||
| t.Fatal("DetectCUDAInterposition() = true, want skip") | ||
| } | ||
| } | ||
|
|
||
| func TestDetectCUDAInterpositionPIDCountMismatch(t *testing.T) { | ||
| _, err := DetectCUDAInterposition(t.TempDir(), []int{101}, nil) | ||
| if err == nil { | ||
| t.Fatal("expected PID mapping count mismatch") | ||
| } | ||
| } | ||
|
|
||
| func TestDetectCUDAInterpositionSkipsWithoutSockets(t *testing.T) { | ||
| procRoot := shortTempDir(t) | ||
| mustControlDir(t, procRoot, 101, 1) | ||
| interposed, err := DetectCUDAInterposition(procRoot, []int{101, 102}, []int{1, 2}) | ||
| if err != nil { | ||
| t.Fatalf("DetectCUDAInterposition() error = %v", err) | ||
| } | ||
| if interposed { | ||
| t.Fatal("DetectCUDAInterposition() = true, want skip") | ||
| } | ||
| } | ||
|
|
||
| func TestDetectCUDAInterpositionIgnoresProcfsEnviron(t *testing.T) { | ||
| procRoot := shortTempDir(t) | ||
| mustControlDir(t, procRoot, 101, 1) | ||
| mustControlDir(t, procRoot, 102, 2) | ||
| mustEnviron(t, procRoot, 101, "IRRELEVANT=1\x00") | ||
| mustEnviron(t, procRoot, 102, "IRRELEVANT=1\x00") | ||
|
|
||
| interposed, err := DetectCUDAInterposition(procRoot, []int{101, 102}, []int{1, 2}) | ||
| if err != nil { | ||
| t.Fatalf("DetectCUDAInterposition() error = %v", err) | ||
| } | ||
| if interposed { | ||
| t.Fatal("DetectCUDAInterposition() keyed off procfs environ") | ||
| } | ||
| } | ||
|
|
||
| func TestDetectCUDAInterpositionRequiresEveryCUDAProcessSocket(t *testing.T) { | ||
| procRoot := shortTempDir(t) | ||
| listenUnix(t, cuinterposerEndpointPath(procRoot, 101, 1)) | ||
|
|
||
| _, err := DetectCUDAInterposition(procRoot, []int{101, 102}, []int{1, 2}) | ||
| if err == nil { | ||
| t.Fatal("expected missing endpoint to fail closed") | ||
| } | ||
|
|
||
| listenUnix(t, cuinterposerEndpointPath(procRoot, 102, 2)) | ||
| interposed, err := DetectCUDAInterposition(procRoot, []int{101, 102}, []int{1, 2}) | ||
| if err != nil { | ||
| t.Fatalf("DetectCUDAInterposition() error = %v", err) | ||
| } | ||
| if !interposed { | ||
| t.Fatal("DetectCUDAInterposition() = false, want true") | ||
| } | ||
| } | ||
|
|
||
| func TestDetectCUDAInterpositionRejectsNonSocketEndpoint(t *testing.T) { | ||
| procRoot := shortTempDir(t) | ||
| listenUnix(t, cuinterposerEndpointPath(procRoot, 101, 1)) | ||
| path := cuinterposerEndpointPath(procRoot, 102, 2) | ||
| if err := os.MkdirAll(filepath.Dir(path), 0700); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| if err := os.WriteFile(path, []byte("not a socket"), 0600); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
|
|
||
| _, err := DetectCUDAInterposition(procRoot, []int{101, 102}, []int{1, 2}) | ||
| if err == nil { | ||
| t.Fatal("expected non-socket endpoint to fail closed") | ||
| } | ||
| } | ||
|
|
||
| func TestHasCUDAInterpositionState(t *testing.T) { | ||
| checkpointDir := t.TempDir() | ||
| present, err := HasCUDAInterpositionState(checkpointDir) | ||
| if err != nil { | ||
| t.Fatalf("HasCUDAInterpositionState() error = %v", err) | ||
| } | ||
| if present { | ||
| t.Fatal("HasCUDAInterpositionState() = true for missing sidecar") | ||
| } | ||
| if err := os.WriteFile(filepath.Join(checkpointDir, cuinterposerStateFile), []byte("state"), 0600); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| present, err = HasCUDAInterpositionState(checkpointDir) | ||
| if err != nil { | ||
| t.Fatalf("HasCUDAInterpositionState() error = %v", err) | ||
| } | ||
| if !present { | ||
| t.Fatal("HasCUDAInterpositionState() = false, want true") | ||
| } | ||
| } | ||
|
|
||
| func shortTempDir(t *testing.T) string { | ||
| t.Helper() | ||
| dir, err := os.MkdirTemp("", "cui") | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| t.Cleanup(func() { _ = os.RemoveAll(dir) }) | ||
| return dir | ||
| } | ||
|
|
||
| func mustControlDir(t *testing.T, procRoot string, observedPID, namespacePID int) { | ||
| t.Helper() | ||
| if err := os.MkdirAll(filepath.Dir(cuinterposerEndpointPath(procRoot, observedPID, namespacePID)), 0700); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| } | ||
|
|
||
| func mustEnviron(t *testing.T, procRoot string, observedPID int, content string) { | ||
| t.Helper() | ||
| path := filepath.Join(procRoot, strconv.Itoa(observedPID), "environ") | ||
| if err := os.MkdirAll(filepath.Dir(path), 0700); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| if err := os.WriteFile(path, []byte(content), 0600); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| } | ||
|
|
||
| func listenUnix(t *testing.T, path string) { | ||
| t.Helper() | ||
| if err := os.MkdirAll(filepath.Dir(path), 0700); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| _ = os.Remove(path) | ||
| listener, err := net.Listen("unix", path) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| t.Cleanup(func() { _ = listener.Close() }) | ||
| } |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎯 Functional Correctness | 🟠 Major | ⚡ Quick win
Use the required CUDA VMM interface.
These constants probe
cuinterposer-*sockets, persistcuinterposer.state, and executecuinterposer-coordinator. The required interface usescuda-vmm-<nspid>.sock,cuda-vmm.state, and/usr/local/bin/snapshot-cuda-vmm. Valid CUDA VMM workloads will skip preparation, and valid CUDA VMM artifacts will not restore state.Replace these identifiers and update the command arguments for the
snapshot-cuda-vmmcontract. Update the related tests to assert that contract.🤖 Prompt for AI Agents