Skip to content

Commit de58d1d

Browse files
authored
Set memory.low for vminitd to avoid kernel reclaiming its memory (#682)
Sets `memory.low` for `vminitd` cgroup to make kernel avoid reclaiming its memory under high memory pressure. Even if the container process runs in memory cgroup, consuming lots of page cache can lead to reclaiming `vminitd`'s page cache memory , which hangs the `vminitd`. Setting `memory.low` makes kernel avoid reclaiming `vminitd`'s page so that we can guarantee the proper `vminitd` operation under high memory pressure.
1 parent 833b07f commit de58d1d

2 files changed

Lines changed: 23 additions & 4 deletions

File tree

vminitd/Sources/Cgroup/Cgroup2Manager.swift

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,21 @@ package struct Cgroup2Manager: Sendable {
252252
)
253253
}
254254

255+
package func setMemoryLow(bytes: UInt64) throws {
256+
self.logger?.debug(
257+
"setting memory.low",
258+
metadata: [
259+
"path": "\(self.path.path)",
260+
"bytes": "\(bytes)",
261+
]
262+
)
263+
264+
try Self.writeValue(
265+
path: self.path,
266+
value: String(bytes),
267+
fileName: "memory.low")
268+
}
269+
255270
package func getMemoryEvents() throws -> MemoryEvents {
256271
let content = try readFileContent(fileName: "memory.events")
257272
let values = parseKeyValuePairs(content)

vminitd/Sources/vminitd/AgentCommand.swift

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -123,19 +123,23 @@ struct AgentCommand: AsyncParsableCommand {
123123
try cgManager.toggleAllAvailableControllers(enable: true)
124124

125125
// Set memory.high threshold to 75 MiB
126-
let threshold: UInt64 = 75 * 1024 * 1024
127-
try cgManager.setMemoryHigh(bytes: threshold)
126+
let high: UInt64 = 75 * 1024 * 1024
127+
// Set memory.low to 50 MiB to avoid reclaiming vminitd's memory
128+
let low: UInt64 = 50 * 1024 * 1024
129+
130+
try cgManager.setMemoryHigh(bytes: high)
131+
try cgManager.setMemoryLow(bytes: low)
128132
try cgManager.addProcess(pid: getpid())
129133

130134
let memoryMonitor = try MemoryMonitor(
131135
cgroupManager: cgManager,
132-
threshold: threshold,
136+
threshold: high,
133137
logger: log
134138
) { [log] (currentUsage, highMark) in
135139
log.warning(
136140
"vminitd memory threshold exceeded",
137141
metadata: [
138-
"threshold_bytes": "\(threshold)",
142+
"threshold_bytes": "\(high)",
139143
"current_bytes": "\(currentUsage)",
140144
"high_events_total": "\(highMark)",
141145
])

0 commit comments

Comments
 (0)