Skip to content
Open
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
84 changes: 57 additions & 27 deletions src/fs/cgroup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,29 @@ impl Cgroup {
/// Create this control group.
pub fn create(&self) -> Result<()> {
if self.hier.v2() {
create_v2_cgroup(self.hier.root(), &self.path, &self.specified_controllers)
create_v2_cgroup(
self.hier.root(),
&self.path,
&self.specified_controllers,
true,
)
} else {
for subsystem in &self.subsystems {
subsystem.to_controller().create();
}
Ok(())
}
}

/// Create this control group, don't enable subtree
pub fn create_disabled(&self) -> Result<()> {
if self.hier.v2() {
create_v2_cgroup(
self.hier.root(),
&self.path,
&self.specified_controllers,
false,
)
} else {
for subsystem in &self.subsystems {
subsystem.to_controller().create();
Expand Down Expand Up @@ -127,7 +149,7 @@ impl Cgroup {
path: P,
relative_paths: HashMap<String, String>,
) -> Result<Cgroup> {
let cg = Cgroup::load_with_relative_paths(hier, path, relative_paths);
let cg = Cgroup::load_with_relative_paths(hier, path, &relative_paths);
cg.create()?;
Ok(cg)
}
Expand Down Expand Up @@ -185,38 +207,43 @@ impl Cgroup {
///
/// Returns a handle to the control group (that possibly does not exist until `create()` has
/// been called on the cgroup.
///
/// Note that this method is only meaningful for cgroup v1, call it is equivalent to call `load` in the v2 mode
pub fn load_with_relative_paths<P: AsRef<Path>>(
hier: Box<dyn Hierarchy>,
path: P,
relative_paths: HashMap<String, String>,
relative_paths: &HashMap<String, String>,
) -> Cgroup {
// relative_paths only valid for cgroup v1
if hier.v2() {
return Self::load(hier, path);
}

let path = path.as_ref();
let mut subsystems = hier.subsystems();
if path.as_os_str() != "" {
subsystems = subsystems
.into_iter()
.map(|x| {
let cn = x.controller_name();
if relative_paths.contains_key(&cn) {
let rp = relative_paths.get(&cn).unwrap();
let valid_path = rp.trim_start_matches('/').to_string();
let mut p = PathBuf::from(valid_path);
subsystems = subsystems
.into_iter()
.map(|x| {
let cn = if hier.v2() {
"".to_string()
} else {
x.controller_name()
};
if relative_paths.contains_key(&cn) {
let rp = relative_paths.get(&cn).unwrap();
let valid_path = rp.trim_start_matches('/').to_string();
let mut p = PathBuf::from(valid_path);
if path.as_os_str() != "" {
p.push(path);
x.enter(p.as_ref())
} else {
x.enter(path)
}
})
.collect::<Vec<_>>();
}
x.enter(p.as_ref())
} else if path.as_os_str() != "" {
x.enter(path)
} else {
x
}
})
.collect::<Vec<_>>();

let rp = relative_paths.get("").unwrap();
let valid_path = rp.trim_start_matches('/').to_string();
let mut pb = PathBuf::from(valid_path);
pb.push(path);

let path = if hier.v2() { pb.as_path() } else { path };
Cgroup {
subsystems,
hier,
Expand Down Expand Up @@ -530,6 +557,7 @@ fn create_v2_cgroup(
root: PathBuf,
path: &str,
specified_controllers: &Option<Vec<String>>,
enabled: bool,
) -> Result<()> {
// controler list ["memory", "cpu"]
let controllers = if let Some(s_controllers) = specified_controllers.clone() {
Expand All @@ -545,7 +573,9 @@ fn create_v2_cgroup(
let mut fp = root;

// enable for root
enable_controllers(&controllers, &fp);
if enabled {
enable_controllers(&controllers, &fp);
}

// path: "a/b/c"
let elements = path.split('/').collect::<Vec<&str>>();
Expand All @@ -560,7 +590,7 @@ fn create_v2_cgroup(
}
}

if i < last_index {
if enabled && i < last_index {
// enable controllers for substree
enable_controllers(&controllers, &fp);
}
Expand Down