diff --git a/vminitd/Sources/Cgroup/Cgroup2Manager.swift b/vminitd/Sources/Cgroup/Cgroup2Manager.swift index 65ba6869..da6b72f6 100644 --- a/vminitd/Sources/Cgroup/Cgroup2Manager.swift +++ b/vminitd/Sources/Cgroup/Cgroup2Manager.swift @@ -252,6 +252,21 @@ package struct Cgroup2Manager: Sendable { ) } + package func setMemoryLow(bytes: UInt64) throws { + self.logger?.debug( + "setting memory.low", + metadata: [ + "path": "\(self.path.path)", + "bytes": "\(bytes)", + ] + ) + + try Self.writeValue( + path: self.path, + value: String(bytes), + fileName: "memory.low") + } + package func getMemoryEvents() throws -> MemoryEvents { let content = try readFileContent(fileName: "memory.events") let values = parseKeyValuePairs(content) diff --git a/vminitd/Sources/vminitd/AgentCommand.swift b/vminitd/Sources/vminitd/AgentCommand.swift index b361dde2..6b61ede9 100644 --- a/vminitd/Sources/vminitd/AgentCommand.swift +++ b/vminitd/Sources/vminitd/AgentCommand.swift @@ -123,19 +123,23 @@ struct AgentCommand: AsyncParsableCommand { try cgManager.toggleAllAvailableControllers(enable: true) // Set memory.high threshold to 75 MiB - let threshold: UInt64 = 75 * 1024 * 1024 - try cgManager.setMemoryHigh(bytes: threshold) + let high: UInt64 = 75 * 1024 * 1024 + // Set memory.low to 50 MiB to avoid reclaiming vminitd's memory + let low: UInt64 = 50 * 1024 * 1024 + + try cgManager.setMemoryHigh(bytes: high) + try cgManager.setMemoryLow(bytes: low) try cgManager.addProcess(pid: getpid()) let memoryMonitor = try MemoryMonitor( cgroupManager: cgManager, - threshold: threshold, + threshold: high, logger: log ) { [log] (currentUsage, highMark) in log.warning( "vminitd memory threshold exceeded", metadata: [ - "threshold_bytes": "\(threshold)", + "threshold_bytes": "\(high)", "current_bytes": "\(currentUsage)", "high_events_total": "\(highMark)", ])