Skip to content

Commit 40727e4

Browse files
authored
Vminitd: Write cgroup limits (#322)
Fixes #320 Today we don't actually setup any cgroup limits, as because there's a 1-1 mapping from container<->vm we can just use the VMs resources as the limit (can't use more than 1GB if that's all the guest sees :) ). However, if we ever supported > 1 container in the guest it'd be necessary to actually setup the cg limits. This change just sets a memory limit and cpu toggles to match whatever was specific for the container.
1 parent d18d118 commit 40727e4

File tree

11 files changed

+659
-498
lines changed

11 files changed

+659
-498
lines changed

Sources/Containerization/LinuxContainer.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -467,6 +467,19 @@ public final class LinuxContainer: Container, Sendable {
467467
// Linux toggles.
468468
spec.linux?.sysctl = config.sysctl
469469

470+
// Resource limits.
471+
// CPU: quota/period model where period is 100ms (100,000µs) and quota is cpus * period
472+
// Memory: limit in bytes
473+
spec.linux?.resources = LinuxResources(
474+
memory: LinuxMemory(
475+
limit: Int64(config.memoryInBytes)
476+
),
477+
cpu: LinuxCPU(
478+
quota: Int64(config.cpus * 100_000),
479+
period: 100_000
480+
)
481+
)
482+
470483
return spec
471484
}
472485

Sources/ContainerizationOCI/Spec.swift

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -470,15 +470,15 @@ public struct LinuxCPU: Codable, Sendable {
470470
public var idle: Int64?
471471

472472
public init(
473-
shares: UInt64?,
474-
quota: Int64?,
475-
burst: UInt64?,
476-
period: UInt64?,
477-
realtimeRuntime: Int64?,
478-
realtimePeriod: Int64?,
479-
cpus: String,
480-
mems: String,
481-
idle: Int64?
473+
shares: UInt64? = nil,
474+
quota: Int64? = nil,
475+
burst: UInt64? = nil,
476+
period: UInt64? = nil,
477+
realtimeRuntime: Int64? = nil,
478+
realtimePeriod: Int64? = nil,
479+
cpus: String = "",
480+
mems: String = "",
481+
idle: Int64? = nil
482482
) {
483483
self.shares = shares
484484
self.quota = quota

0 commit comments

Comments
 (0)