Skip to content
New issue

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

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

Already on GitHub? # to your account

fix a memory subsystem #36

Merged
merged 4 commits into from
May 26, 2021
Merged
Show file tree
Hide file tree
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
14 changes: 6 additions & 8 deletions src/cgroups/blkio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ mod tests {
fn setup(testname: &str, throttle_type: &str) -> (PathBuf, PathBuf) {
let tmp = create_temp_dir(testname).expect("create temp directory for test");
let throttle_file = set_fixture(&tmp, throttle_type, "")
.expect(&format!("set fixture for {}", throttle_type));
.unwrap_or_else(|_| panic!("set fixture for {}", throttle_type));

(tmp, throttle_file)
}
Expand Down Expand Up @@ -182,7 +182,7 @@ mod tests {

Blkio::apply(&test_root, &blkio).expect("apply blkio");
let content = fs::read_to_string(throttle)
.expect(&format!("read {} content", CGROUP_BLKIO_THROTTLE_READ_BPS));
.unwrap_or_else(|_| panic!("read {} content", CGROUP_BLKIO_THROTTLE_READ_BPS));

assert_eq!("8:0 102400", content);
}
Expand All @@ -202,7 +202,7 @@ mod tests {

Blkio::apply(&test_root, &blkio).expect("apply blkio");
let content = fs::read_to_string(throttle)
.expect(&format!("read {} content", CGROUP_BLKIO_THROTTLE_WRITE_BPS));
.unwrap_or_else(|_| panic!("read {} content", CGROUP_BLKIO_THROTTLE_WRITE_BPS));

assert_eq!("8:0 102400", content);
}
Expand All @@ -222,7 +222,7 @@ mod tests {

Blkio::apply(&test_root, &blkio).expect("apply blkio");
let content = fs::read_to_string(throttle)
.expect(&format!("read {} content", CGROUP_BLKIO_THROTTLE_READ_IOPS));
.unwrap_or_else(|_| panic!("read {} content", CGROUP_BLKIO_THROTTLE_READ_IOPS));

assert_eq!("8:0 102400", content);
}
Expand All @@ -243,10 +243,8 @@ mod tests {
.build();

Blkio::apply(&test_root, &blkio).expect("apply blkio");
let content = fs::read_to_string(throttle).expect(&format!(
"read {} content",
CGROUP_BLKIO_THROTTLE_WRITE_IOPS
));
let content = fs::read_to_string(throttle)
.unwrap_or_else(|_| panic!("read {} content", CGROUP_BLKIO_THROTTLE_WRITE_IOPS));

assert_eq!("8:0 102400", content);
}
Expand Down
32 changes: 22 additions & 10 deletions src/cgroups/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,9 @@ impl Memory {
}

fn set_memory(val: i64, cgroup_root: &Path) -> Result<()> {
if val == 0 {
return Ok(());
}
let path = cgroup_root.join(CGROUP_MEMORY_LIMIT);

match Self::set(val, &path) {
Expand All @@ -158,16 +161,16 @@ impl Memory {
Errno::EBUSY => {
let usage = Self::get_memory_usage(cgroup_root)?;
let max_usage = Self::get_memory_max_usage(cgroup_root)?;
Err(anyhow!(
bail!(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good catch, I haven't use anyhow, but this looks much cleaner.

"unable to set memory limit to {} (current usage: {}, peak usage: {})",
val,
usage,
max_usage,
))
)
}
_ => Err(anyhow!(e)),
_ => bail!(e),
},
None => Err(anyhow!(e)),
None => bail!(e),
}
}
}
Expand Down Expand Up @@ -225,12 +228,8 @@ impl Memory {
}
}
None => match resource.swap {
Some(swap) => {
Self::set_memory_and_swap(0, swap, false, cgroup_root)?;
}
None => {
Self::set_memory_and_swap(0, 0, false, cgroup_root)?;
}
Some(swap) => Self::set_memory_and_swap(0, swap, false, cgroup_root)?,
None => Self::set_memory_and_swap(0, 0, false, cgroup_root)?,
},
}
Ok(())
Expand Down Expand Up @@ -271,6 +270,19 @@ mod tests {
assert_eq!(limit.to_string(), content)
}

#[test]
fn pass_set_memory_if_limit_is_zero() {
let sample_val = "1024";
let limit = 0;
let tmp = create_temp_dir("pass_set_memory_if_limit_is_zero")
.expect("create temp directory for test");
set_fixture(&tmp, CGROUP_MEMORY_LIMIT, sample_val).expect("Set fixure for memory limit");
Memory::set_memory(limit, &tmp).expect("Set memory limit");
let content =
std::fs::read_to_string(tmp.join(CGROUP_MEMORY_LIMIT)).expect("Read to string");
assert_eq!(content, sample_val)
}

#[test]
fn test_set_swap() {
let limit = 512;
Expand Down
6 changes: 1 addition & 5 deletions src/cgroups/network_priority.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,7 @@ mod tests {
priority: 2,
},
];
let priorities_string = priorities
.clone()
.iter()
.map(|p| p.to_string())
.collect::<String>();
let priorities_string = priorities.iter().map(|p| p.to_string()).collect::<String>();
let network = LinuxNetwork {
class_id: None,
priorities,
Expand Down