-
Notifications
You must be signed in to change notification settings - Fork 10
Optimize Firecracker snapshot resume #261
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
+216
−22
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
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
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 |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| //go:build linux | ||
|
|
||
| package firecracker | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "os" | ||
| "path/filepath" | ||
| "time" | ||
|
|
||
| "golang.org/x/sys/unix" | ||
| ) | ||
|
|
||
| func waitForSocket(path string, timeout time.Duration) error { | ||
| if err := tryDialUnixSocket(path); err == nil { | ||
| return nil | ||
| } | ||
|
|
||
| parent := filepath.Dir(path) | ||
| fd, err := unix.InotifyInit1(unix.IN_CLOEXEC | unix.IN_NONBLOCK) | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we attempt to not wait via polling, then we fall back to polling |
||
| if err != nil { | ||
| return waitForSocketByPolling(path, timeout) | ||
| } | ||
| defer unix.Close(fd) | ||
|
|
||
| wd, err := unix.InotifyAddWatch(fd, parent, unix.IN_CREATE|unix.IN_MOVED_TO|unix.IN_ATTRIB) | ||
| if err != nil { | ||
| return waitForSocketByPolling(path, timeout) | ||
| } | ||
| defer unix.InotifyRmWatch(fd, uint32(wd)) | ||
|
|
||
| deadline := time.Now().Add(timeout) | ||
| buf := make([]byte, 4096) | ||
| for { | ||
| if err := tryDialUnixSocket(path); err == nil { | ||
| return nil | ||
| } | ||
| remaining := time.Until(deadline) | ||
| if remaining <= 0 { | ||
| return fmt.Errorf("timeout waiting for socket") | ||
| } | ||
|
|
||
| pollTimeout := remaining | ||
| if socketPathExists(path) { | ||
| pollTimeout = minDuration(pollTimeout, socketReadyRetryEvery) | ||
| } | ||
| events := []unix.PollFd{{Fd: int32(fd), Events: unix.POLLIN}} | ||
| n, err := unix.Poll(events, durationMillisCeil(pollTimeout)) | ||
| if err != nil { | ||
| if err == unix.EINTR { | ||
| continue | ||
| } | ||
| return waitForSocketByPolling(path, remaining) | ||
| } | ||
| if n > 0 { | ||
| for { | ||
| n, err := unix.Read(fd, buf) | ||
| if err != nil { | ||
| if err == unix.EAGAIN || err == unix.EWOULDBLOCK { | ||
| break | ||
| } | ||
| return waitForSocketByPolling(path, time.Until(deadline)) | ||
| } | ||
| if n == 0 { | ||
| break | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func socketPathExists(path string) bool { | ||
| _, err := os.Lstat(path) | ||
| return err == nil | ||
| } | ||
|
|
||
| func durationMillisCeil(d time.Duration) int { | ||
| if d <= 0 { | ||
| return 0 | ||
| } | ||
| ms := d / time.Millisecond | ||
| if d%time.Millisecond != 0 { | ||
| ms++ | ||
| } | ||
| if int64(ms) > int64(^uint(0)>>1) { | ||
| return int(^uint(0) >> 1) | ||
| } | ||
| return int(ms) | ||
| } | ||
|
|
||
| func minDuration(a, b time.Duration) time.Duration { | ||
| if a < b { | ||
| return a | ||
| } | ||
| return b | ||
| } | ||
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,9 @@ | ||
| //go:build !linux | ||
|
|
||
| package firecracker | ||
|
|
||
| import "time" | ||
|
|
||
| func waitForSocket(path string, timeout time.Duration) error { | ||
| return waitForSocketByPolling(path, timeout) | ||
| } |
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
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.
Guest runs before alias cleanup
Medium Severity
With resume-on-load enabled, snapshot load can start the guest while still inside
withSnapshotSourceDirAlias, before the temporary source-data symlink is removed. Restore then skips the separateResumecall whenRestoredResumedis set, so alias restores no longer guarantee the guest stays paused until after that teardown finishes.Additional Locations (1)
lib/instances/restore.go#L284-L287Reviewed by Cursor Bugbot for commit b7c7c05. Configure here.