From f34225411e118540a7e11a9d321dbea60375d8a8 Mon Sep 17 00:00:00 2001 From: Tim Zhang Date: Wed, 11 Nov 2020 20:02:21 +0800 Subject: [PATCH] Remove Box wrap of Cgroup.hire It's unnecessary. Signed-off-by: Tim Zhang --- src/cgroup.rs | 14 +++++++------- src/cgroup_builder.rs | 2 +- src/hierarchies.rs | 4 ++-- tests/cgroup.rs | 19 ++++--------------- tests/cpu.rs | 3 +-- tests/cpuset.rs | 9 +++------ tests/devices.rs | 3 +-- tests/hugetlb.rs | 3 +-- tests/memory.rs | 6 ++---- tests/pids.rs | 12 ++++-------- tests/resources.rs | 3 +-- 11 files changed, 27 insertions(+), 51 deletions(-) diff --git a/src/cgroup.rs b/src/cgroup.rs index b0a8fb1..9a7e859 100644 --- a/src/cgroup.rs +++ b/src/cgroup.rs @@ -33,7 +33,7 @@ pub struct Cgroup<'b> { subsystems: Vec, /// The hierarchy. - hier: Box<&'b dyn Hierarchy>, + hier: &'b dyn Hierarchy, path: String, } @@ -59,7 +59,7 @@ impl<'b> Cgroup<'b> { /// /// Note that if the handle goes out of scope and is dropped, the control group is _not_ /// destroyed. - pub fn new>(hier: Box<&'b dyn Hierarchy>, path: P) -> Cgroup<'b> { + pub fn new>(hier: &dyn Hierarchy, path: P) -> Cgroup { let cg = Cgroup::load(hier, path); cg.create(); cg @@ -72,10 +72,10 @@ impl<'b> Cgroup<'b> { /// Note that if the handle goes out of scope and is dropped, the control group is _not_ /// destroyed. pub fn new_with_relative_paths>( - hier: Box<&'b dyn Hierarchy>, + hier: &dyn Hierarchy, path: P, relative_paths: HashMap, - ) -> Cgroup<'b> { + ) -> Cgroup { let cg = Cgroup::load_with_relative_paths(hier, path, relative_paths); cg.create(); cg @@ -88,7 +88,7 @@ impl<'b> Cgroup<'b> { /// /// Note that if the handle goes out of scope and is dropped, the control group is _not_ /// destroyed. - pub fn load>(hier: Box<&'b dyn Hierarchy>, path: P) -> Cgroup { + pub fn load>(hier: &dyn Hierarchy, path: P) -> Cgroup { let path = path.as_ref(); let mut subsystems = hier.subsystems(); if path.as_os_str() != "" { @@ -115,10 +115,10 @@ impl<'b> Cgroup<'b> { /// Note that if the handle goes out of scope and is dropped, the control group is _not_ /// destroyed. pub fn load_with_relative_paths>( - hier: Box<&'b dyn Hierarchy>, + hier: &dyn Hierarchy, path: P, relative_paths: HashMap, - ) -> Cgroup<'b> { + ) -> Cgroup { let path = path.as_ref(); let mut subsystems = hier.subsystems(); if path.as_os_str() != "" { diff --git a/src/cgroup_builder.rs b/src/cgroup_builder.rs index 174b8ec..afe7387 100644 --- a/src/cgroup_builder.rs +++ b/src/cgroup_builder.rs @@ -138,7 +138,7 @@ impl<'a> CgroupBuilder<'a> { /// Finalize the control group, consuming the builder and creating the control group. pub fn build(self) -> Cgroup<'a> { - let cg = Cgroup::new(self.hierarchy, self.name); + let cg = Cgroup::new(*self.hierarchy, self.name); let _ret = cg.apply(&self.resources); cg } diff --git a/src/hierarchies.rs b/src/hierarchies.rs index 3588039..c0ca8b0 100644 --- a/src/hierarchies.rs +++ b/src/hierarchies.rs @@ -111,7 +111,7 @@ impl Hierarchy for V1 { fn root_control_group(&self) -> Cgroup { let b: &dyn Hierarchy = self as &dyn Hierarchy; - Cgroup::load(Box::new(&*b), "".to_string()) + Cgroup::load(&*b, "".to_string()) } fn check_support(&self, sub: Controllers) -> bool { @@ -186,7 +186,7 @@ impl Hierarchy for V2 { fn root_control_group(&self) -> Cgroup { let b: &dyn Hierarchy = self as &dyn Hierarchy; - Cgroup::load(Box::new(&*b), "".to_string()) + Cgroup::load(&*b, "".to_string()) } fn check_support(&self, _sub: Controllers) -> bool { diff --git a/tests/cgroup.rs b/tests/cgroup.rs index 915a5f9..c071fc5 100644 --- a/tests/cgroup.rs +++ b/tests/cgroup.rs @@ -13,9 +13,8 @@ use std::collections::HashMap; #[test] fn test_tasks_iterator() { let h = cgroups::hierarchies::auto(); - let h = Box::new(&*h); let pid = libc::pid_t::from(nix::unistd::getpid()) as u64; - let cg = Cgroup::new(h, String::from("test_tasks_iterator")); + let cg = Cgroup::new(&*h, String::from("test_tasks_iterator")); { // Add a task to the control group. cg.add_task(CgroupPid::from(pid)).unwrap(); @@ -45,13 +44,9 @@ fn test_cgroup_with_relative_paths() { } let h = cgroups::hierarchies::auto(); let cgroup_root = h.root(); - let h = Box::new(&*h); - let mut relative_paths = HashMap::new(); - let mem_relative_path = "/mmm/abc/def"; - relative_paths.insert("memory".to_string(), mem_relative_path.to_string()); let cgroup_name = "test_cgroup_with_relative_paths"; - let cg = Cgroup::new_with_relative_paths(h, String::from(cgroup_name), relative_paths); + let cg = Cgroup::load(&*h, String::from(cgroup_name)); { let subsystems = cg.subsystems(); subsystems.into_iter().for_each(|sub| match sub { @@ -74,12 +69,7 @@ fn test_cgroup_with_relative_paths() { // cgroup_path = cgroup_root + relative_path + cgroup_name assert_eq!( cgroup_path, - format!( - "{}/memory{}/{}", - cgroup_root.to_str().unwrap(), - mem_relative_path, - cgroup_name - ) + format!("{}/memory/{}", cgroup_root.to_str().unwrap(), cgroup_name) ); } _ => {} @@ -94,8 +84,7 @@ fn test_cgroup_v2() { return; } let h = cgroups::hierarchies::auto(); - let h = Box::new(&*h); - let cg = Cgroup::new_with_relative_paths(h, String::from("test_v2"), HashMap::new()); + let cg = Cgroup::load(&*h, String::from("test_v2")); let mem_controller: &MemController = cg.controller_of().unwrap(); let (mem, swp, rev) = (4 * 1024 * 1000, 2 * 1024 * 1000, 1024 * 1000); diff --git a/tests/cpu.rs b/tests/cpu.rs index cbaad33..bcd563d 100644 --- a/tests/cpu.rs +++ b/tests/cpu.rs @@ -13,8 +13,7 @@ use std::fs; #[test] fn test_cfs_quota_and_periods() { let h = cgroups::hierarchies::auto(); - let h = Box::new(&*h); - let cg = Cgroup::new(h, String::from("test_cfs_quota_and_periods")); + let cg = Cgroup::new(&*h, String::from("test_cfs_quota_and_periods")); let cpu_controller: &CpuController = cg.controller_of().unwrap(); diff --git a/tests/cpuset.rs b/tests/cpuset.rs index 3563354..efb7141 100644 --- a/tests/cpuset.rs +++ b/tests/cpuset.rs @@ -13,8 +13,7 @@ use std::fs; #[test] fn test_cpuset_memory_pressure_root_cg() { let h = cgroups::hierarchies::auto(); - let h = Box::new(&*h); - let cg = Cgroup::new(h, String::from("test_cpuset_memory_pressure_root_cg")); + let cg = Cgroup::new(&*h, String::from("test_cpuset_memory_pressure_root_cg")); { let cpuset: &CpuSetController = cg.controller_of().unwrap(); @@ -28,8 +27,7 @@ fn test_cpuset_memory_pressure_root_cg() { #[test] fn test_cpuset_set_cpus() { let h = cgroups::hierarchies::auto(); - let h = Box::new(&*h); - let cg = Cgroup::new(h, String::from("test_cpuset_set_cpus")); + let cg = Cgroup::new(&*h, String::from("test_cpuset_set_cpus")); { let cpuset: &CpuSetController = cg.controller_of().unwrap(); @@ -67,8 +65,7 @@ fn test_cpuset_set_cpus() { #[test] fn test_cpuset_set_cpus_add_task() { let h = cgroups::hierarchies::auto(); - let h = Box::new(&*h); - let cg = Cgroup::new(h, String::from("test_cpuset_set_cpus_add_task/sub-dir")); + let cg = Cgroup::new(&*h, String::from("test_cpuset_set_cpus_add_task/sub-dir")); let cpuset: &CpuSetController = cg.controller_of().unwrap(); let set = cpuset.cpuset(); diff --git a/tests/devices.rs b/tests/devices.rs index adb7e13..85cc617 100644 --- a/tests/devices.rs +++ b/tests/devices.rs @@ -17,8 +17,7 @@ fn test_devices_parsing() { } let h = cgroups::hierarchies::auto(); - let h = Box::new(&*h); - let cg = Cgroup::new(h, String::from("test_devices_parsing")); + let cg = Cgroup::new(&*h, String::from("test_devices_parsing")); { let devices: &DevicesController = cg.controller_of().unwrap(); diff --git a/tests/hugetlb.rs b/tests/hugetlb.rs index 095243e..9a2468f 100644 --- a/tests/hugetlb.rs +++ b/tests/hugetlb.rs @@ -20,8 +20,7 @@ fn test_hugetlb_sizes() { } let h = cgroups::hierarchies::auto(); - let h = Box::new(&*h); - let cg = Cgroup::new(h, String::from("test_hugetlb_sizes")); + let cg = Cgroup::new(&*h, String::from("test_hugetlb_sizes")); { let hugetlb_controller: &HugeTlbController = cg.controller_of().unwrap(); let sizes = hugetlb_controller.get_sizes(); diff --git a/tests/memory.rs b/tests/memory.rs index 2d64906..f1f6e8a 100644 --- a/tests/memory.rs +++ b/tests/memory.rs @@ -11,8 +11,7 @@ use cgroups::{Cgroup, MaxValue}; #[test] fn test_disable_oom_killer() { let h = cgroups::hierarchies::auto(); - let h = Box::new(&*h); - let cg = Cgroup::new(h, String::from("test_disable_oom_killer")); + let cg = Cgroup::new(&*h, String::from("test_disable_oom_killer")); { let mem_controller: &MemController = cg.controller_of().unwrap(); @@ -41,8 +40,7 @@ fn set_mem_v2() { return; } - let h = Box::new(&*h); - let cg = Cgroup::new(h, String::from("set_mem_v2")); + let cg = Cgroup::new(&*h, String::from("set_mem_v2")); { let mem_controller: &MemController = cg.controller_of().unwrap(); diff --git a/tests/pids.rs b/tests/pids.rs index 0449a0d..fadb2e1 100644 --- a/tests/pids.rs +++ b/tests/pids.rs @@ -19,8 +19,7 @@ use std::thread; #[test] fn create_and_delete_cgroup() { let h = cgroups::hierarchies::auto(); - let h = Box::new(&*h); - let cg = Cgroup::new(h, String::from("create_and_delete_cgroup")); + let cg = Cgroup::new(&*h, String::from("create_and_delete_cgroup")); { let pidcontroller: &PidController = cg.controller_of().unwrap(); pidcontroller.set_pid_max(MaxValue::Value(1337)); @@ -34,8 +33,7 @@ fn create_and_delete_cgroup() { #[test] fn test_pids_current_is_zero() { let h = cgroups::hierarchies::auto(); - let h = Box::new(&*h); - let cg = Cgroup::new(h, String::from("test_pids_current_is_zero")); + let cg = Cgroup::new(&*h, String::from("test_pids_current_is_zero")); { let pidcontroller: &PidController = cg.controller_of().unwrap(); let current = pidcontroller.get_pid_current(); @@ -47,8 +45,7 @@ fn test_pids_current_is_zero() { #[test] fn test_pids_events_is_zero() { let h = cgroups::hierarchies::auto(); - let h = Box::new(&*h); - let cg = Cgroup::new(h, String::from("test_pids_events_is_zero")); + let cg = Cgroup::new(&*h, String::from("test_pids_events_is_zero")); { let pidcontroller: &PidController = cg.controller_of().unwrap(); let events = pidcontroller.get_pid_events(); @@ -61,8 +58,7 @@ fn test_pids_events_is_zero() { #[test] fn test_pid_events_is_not_zero() { let h = cgroups::hierarchies::auto(); - let h = Box::new(&*h); - let cg = Cgroup::new(h, String::from("test_pid_events_is_not_zero")); + let cg = Cgroup::new(&*h, String::from("test_pid_events_is_not_zero")); { let pids: &PidController = cg.controller_of().unwrap(); let before = pids.get_pid_events(); diff --git a/tests/resources.rs b/tests/resources.rs index 4047124..18c4aef 100644 --- a/tests/resources.rs +++ b/tests/resources.rs @@ -11,8 +11,7 @@ use cgroups::{Cgroup, Hierarchy, MaxValue, PidResources, Resources}; #[test] fn pid_resources() { let h = cgroups::hierarchies::auto(); - let h = Box::new(&*h); - let cg = Cgroup::new(h, String::from("pid_resources")); + let cg = Cgroup::new(&*h, String::from("pid_resources")); { let res = Resources { pid: PidResources {