Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 43 additions & 8 deletions vminitd/Sources/vmexec/RunCommand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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()
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we move MemoryMonitor to the Cgroup library and use it instead of pulling memory events with a fixed interval? CC @dcantah


if events.max > oomLimit {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

events.max represents the number of events. 1_000_000 seems to be a too large threshold. Would it be appropriate to use a threshold of zero?

try cgroupManager.kill()
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we use App.writeError(error) and exit(code) when try fails?

}
}
}
}

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))")
}
Expand All @@ -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 {
Expand Down
Loading