Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert memory swap values #285

Merged
merged 2 commits into from
Sep 8, 2021
Merged
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
37 changes: 33 additions & 4 deletions cgroups/src/v2/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -286,6 +300,11 @@ mod tests {
if linux_memory.limit.is_none() {
return result.is_err();
}
if let Some(limit) = linux_memory.limit {
if limit != -1 && swap != -1 && swap < limit {
return result.is_err();
}
}
}

if let Some(reservation) = linux_memory.reservation {
Expand All @@ -306,7 +325,17 @@ 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) => {
if let Some(limit) = linux_memory.limit {
if limit == -1 {
swap_content == swap.to_string()
} else {
swap_content == (swap - linux_memory.limit.unwrap()).to_string()
}
} else {
false
}
}
None => {
match linux_memory.limit {
Some(limit) if limit == -1 => swap_content == "max",
Expand Down