1
0
mirror of https://github.com/containers/youki synced 2024-12-04 19:18:29 +01:00

Convert memory swap values

This commit is contained in:
Furisto 2021-09-07 21:50:53 +02:00
parent c247c90425
commit cbc8d1af3d

@ -97,8 +97,22 @@ impl Memory {
bail!("invalid swap value: {}", swap);
}
Some(swap) => {
Memory::set(path.join(CGROUP_MEMORY_SWAP), swap)?;
Memory::set(path.join(CGROUP_MEMORY_MAX), limit)?;
// -1 means max
if swap == -1 || limit == -1 {
Memory::set(path.join(CGROUP_MEMORY_SWAP), swap)?;
Memory::set(path.join(CGROUP_MEMORY_MAX), limit)?;
} else {
if swap < limit {
bail!("swap memory ({}) should be bigger than memory limit ({})", swap, limit);
}
// In cgroup v1 swap is memory+swap, but in cgroup v2 swap is
// a separate value, so the swap value in the runtime spec needs
// to be converted from the cgroup v1 value to the cgroup v2 value
// by subtracting limit from swap
Memory::set(path.join(CGROUP_MEMORY_SWAP), swap - limit)?;
Memory::set(path.join(CGROUP_MEMORY_MAX), limit)?;
}
}
None => {
if limit == -1 {
@ -158,7 +172,7 @@ mod tests {
assert_eq!(limit_content, limit.to_string());
let swap_content = read_to_string(tmp.join(CGROUP_MEMORY_SWAP)).expect("read swap limit");
assert_eq!(swap_content, swap.to_string());
assert_eq!(swap_content, (swap - limit).to_string());
let reservation_content =
read_to_string(tmp.join(CGROUP_MEMORY_LOW)).expect("read memory reservation");
@ -306,7 +320,7 @@ mod tests {
let swap_content = read_to_string(tmp.join(CGROUP_MEMORY_SWAP)).expect("read swap limit to string");
let swap_check = match linux_memory.swap {
Some(swap) if swap == -1 => swap_content == "max",
Some(swap) => swap_content == swap.to_string(),
Some(swap) => swap_content == (swap - linux_memory.limit.unwrap()).to_string(),
None => {
match linux_memory.limit {
Some(limit) if limit == -1 => swap_content == "max",