-
Notifications
You must be signed in to change notification settings - Fork 268
Add custom OOM killer for Linux containers #653
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
base: main
Are you sure you want to change the base?
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 |
|---|---|---|
|
|
@@ -33,6 +33,9 @@ struct RunCommand: ParsableCommand { | |
| var bundlePath: String | ||
|
|
||
| mutating func run() throws { | ||
| let syncPipe = FileDescriptor(rawValue: 3) | ||
| let ackPipe = FileDescriptor(rawValue: 4) | ||
|
|
||
| do { | ||
| let spec: ContainerizationOCI.Spec | ||
| do { | ||
|
|
@@ -41,7 +44,7 @@ struct RunCommand: ParsableCommand { | |
| } catch { | ||
| throw App.Failure(message: "failed to load OCI bundle at \(bundlePath): \(error)") | ||
| } | ||
| try execInNamespace(spec: spec) | ||
| try execInNamespace(spec: spec, syncPipe: syncPipe, ackPipe: ackPipe) | ||
| } catch { | ||
| App.writeError(error) | ||
| throw error | ||
|
|
@@ -85,8 +88,8 @@ struct RunCommand: ParsableCommand { | |
|
|
||
| private func childSetup( | ||
| spec: ContainerizationOCI.Spec, | ||
| ackPipe: FileDescriptor, | ||
| syncPipe: FileDescriptor | ||
| syncPipe: FileDescriptor, | ||
| ackPipe: FileDescriptor | ||
| ) throws { | ||
| guard let process = spec.process else { | ||
| throw App.Failure(message: "no process configuration found in runtime spec") | ||
|
|
@@ -243,12 +246,44 @@ struct RunCommand: ParsableCommand { | |
| return unshareFlags | ||
| } | ||
|
|
||
| private func execInNamespace(spec: ContainerizationOCI.Spec) throws { | ||
| let syncPipe = FileDescriptor(rawValue: 3) | ||
| let ackPipe = FileDescriptor(rawValue: 4) | ||
| private func startMemoryMonitor(spec: ContainerizationOCI.Spec, syncPipe: FileDescriptor, ackPipe: FileDescriptor) throws { | ||
| let oomLimit = 1_000_000 | ||
|
|
||
| let unshareFlags = try setupNamespaces(namespaces: spec.linux?.namespaces) | ||
| let errorPipe = FileDescriptor(rawValue: 5) | ||
|
|
||
| let processID = fork() | ||
| guard processID != -1 else { | ||
| try? syncPipe.close() | ||
| try? ackPipe.close() | ||
| throw App.Errno(stage: "fork") | ||
| } | ||
|
|
||
| guard processID == 0 else { | ||
| return | ||
| } | ||
|
|
||
| try syncPipe.close() | ||
| try ackPipe.close() | ||
| try errorPipe.close() | ||
|
|
||
| if let linux = spec.linux, !linux.cgroupsPath.isEmpty { | ||
| let cgroupManager = try Cgroup2Manager.load(group: URL(filePath: linux.cgroupsPath)) | ||
|
|
||
| while true { | ||
| usleep(1_000_000) | ||
| let events = try cgroupManager.getMemoryEvents() | ||
|
|
||
| if events.max > oomLimit { | ||
|
Contributor
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.
|
||
| try cgroupManager.kill() | ||
|
Contributor
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. Should we use |
||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private func execInNamespace(spec: ContainerizationOCI.Spec, syncPipe: FileDescriptor, ackPipe: FileDescriptor) throws { | ||
| try startMemoryMonitor(spec: spec, syncPipe: syncPipe, ackPipe: ackPipe) | ||
|
|
||
| let unshareFlags = try setupNamespaces(namespaces: spec.linux?.namespaces) | ||
| guard unshare(unshareFlags) == 0 else { | ||
| throw App.Errno(stage: "unshare(\(unshareFlags))") | ||
| } | ||
|
|
@@ -261,7 +296,7 @@ struct RunCommand: ParsableCommand { | |
| } | ||
|
|
||
| if processID == 0 { // child | ||
| try childSetup(spec: spec, ackPipe: ackPipe, syncPipe: syncPipe) | ||
| try childSetup(spec: spec, syncPipe: syncPipe, ackPipe: ackPipe) | ||
| } else { // parent process | ||
| // Setup cgroup before child enters cgroup namespace | ||
| if let linux = spec.linux { | ||
|
|
||
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.
Can we move
MemoryMonitorto theCgrouplibrary and use it instead of pulling memory events with a fixed interval? CC @dcantah