-
Notifications
You must be signed in to change notification settings - Fork 292
feat(uffd,fc): balloon free-page-hinting + envd reclaim on pause #2550
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -133,6 +133,8 @@ type Process struct { | |
| Exit *utils.ErrorOnce | ||
|
|
||
| client *apiClient | ||
|
|
||
| balloonInstalled bool | ||
| } | ||
|
|
||
| func NewProcess( | ||
|
|
@@ -440,13 +442,13 @@ func (p *Process) Create( | |
| telemetry.ReportEvent(ctx, "set fc entropy config") | ||
|
|
||
| if freePageReporting { | ||
| err = p.client.enableFreePageReporting(ctx) | ||
| if err != nil { | ||
| if err := p.client.installBalloon(ctx, freePageReporting); err != nil { | ||
| fcStopErr := p.Stop(ctx) | ||
|
|
||
| return errors.Join(fmt.Errorf("error enabling free page reporting: %w", err), fcStopErr) | ||
| return errors.Join(fmt.Errorf("error installing balloon device: %w", err), fcStopErr) | ||
| } | ||
| telemetry.ReportEvent(ctx, "enabled free page reporting") | ||
| p.balloonInstalled = true | ||
| telemetry.ReportEvent(ctx, "installed balloon device") | ||
| } | ||
|
|
||
| err = p.client.startVM(ctx) | ||
|
|
@@ -710,6 +712,44 @@ func (p *Process) Pause(ctx context.Context) error { | |
| return p.client.pauseVM(ctx) | ||
| } | ||
|
|
||
| // DrainBalloon triggers a free-page-hinting run and blocks until the guest | ||
| // acknowledges or ctx fires. No-op when the balloon wasn't installed. | ||
| func (p *Process) DrainBalloon(ctx context.Context) error { | ||
| if !p.balloonInstalled { | ||
| return nil | ||
|
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.
|
||
| } | ||
|
|
||
| ctx, span := tracer.Start(ctx, "drain-balloon") | ||
| defer span.End() | ||
|
|
||
| if err := p.client.startBalloonHinting(ctx, true /* ackOnStop */); err != nil { | ||
| return fmt.Errorf("start balloon hinting: %w", err) | ||
| } | ||
|
|
||
| backoff := 5 * time.Millisecond | ||
| for { | ||
| select { | ||
| case <-ctx.Done(): | ||
| return ctx.Err() | ||
| case <-time.After(backoff): | ||
| } | ||
|
|
||
| host, guest, err := p.client.describeBalloonHinting(ctx) | ||
| if err != nil { | ||
| return fmt.Errorf("balloon hinting status: %w", err) | ||
| } | ||
| // host_cmd is monotonic and we just called start, so host > 0 | ||
| // after FC accepts it. Require it to guard against transient | ||
| // nil/zero responses returning a false-positive completion. | ||
| if host > 0 && guest >= host { | ||
| return nil | ||
| } | ||
| if backoff < 50*time.Millisecond { | ||
| backoff *= 2 | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // CreateSnapshot VM needs to be paused before creating a snapshot. | ||
| func (p *Process) CreateSnapshot(ctx context.Context, snapfilePath string) error { | ||
| ctx, childSpan := tracer.Start(ctx, "create-snapshot-fc") | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| package sandbox | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "net/http" | ||
| "strconv" | ||
| "time" | ||
|
|
||
| "connectrpc.com/connect" | ||
| "go.uber.org/zap" | ||
|
|
||
| "github.com/e2b-dev/infra/packages/shared/pkg/consts" | ||
| "github.com/e2b-dev/infra/packages/shared/pkg/grpc" | ||
| "github.com/e2b-dev/infra/packages/shared/pkg/grpc/envd/process" | ||
| "github.com/e2b-dev/infra/packages/shared/pkg/grpc/envd/process/processconnect" | ||
| "github.com/e2b-dev/infra/packages/shared/pkg/logger" | ||
| ) | ||
|
|
||
| // Steps separated by ';' so each runs even if a previous one fails. On | ||
| // timeout envd kills bash; the in-flight syscall finishes and remaining | ||
| // steps are skipped. | ||
| const reclaimScript = `sync; echo 3 > /proc/sys/vm/drop_caches 2>/dev/null; echo 1 > /proc/sys/vm/compact_memory 2>/dev/null; fstrim -av 2>/dev/null` | ||
|
|
||
| // bestEffortReclaim asks envd to reclaim guest memory + disk before pause. | ||
| // All failures are swallowed. | ||
| func (s *Sandbox) bestEffortReclaim(ctx context.Context, timeout time.Duration) { | ||
| ctx, span := tracer.Start(ctx, "envd-reclaim") | ||
| defer span.End() | ||
|
|
||
| rcCtx, cancel := context.WithTimeout(ctx, timeout) | ||
| defer cancel() | ||
|
|
||
| addr := fmt.Sprintf("http://%s:%d", s.Slot.HostIPString(), consts.DefaultEnvdServerPort) | ||
| pc := processconnect.NewProcessClient(&http.Client{Transport: sandboxHttpClient.Transport}, addr) | ||
|
|
||
| req := connect.NewRequest(&process.StartRequest{ | ||
| Process: &process.ProcessConfig{Cmd: "/bin/bash", Args: []string{"-c", reclaimScript}}, | ||
| }) | ||
| req.Header().Set("Connect-Timeout-Ms", strconv.FormatInt(int64(timeout/time.Millisecond), 10)) | ||
| grpc.SetUserHeader(req.Header(), "root") | ||
|
|
||
| stream, err := pc.Start(rcCtx, req) | ||
| if err != nil { | ||
| logger.L().Warn(ctx, "envd reclaim failed", logger.WithSandboxID(s.Runtime.SandboxID), zap.Error(err)) | ||
|
|
||
| return | ||
| } | ||
| defer stream.Close() | ||
|
|
||
| for stream.Receive() { | ||
| } | ||
| } |


Uh oh!
There was an error while loading. Please reload this page.