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

[WIP] Add support for cpuacct in cgroup v1. #92

Merged
merged 2 commits into from
Jun 17, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
3 changes: 3 additions & 0 deletions src/cgroups/v1/controller_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::fmt::Display;

pub enum ControllerType {
Cpu,
CpuAcct,
CpuSet,
Devices,
HugeTlb,
Expand All @@ -16,6 +17,7 @@ impl Display for ControllerType {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let print = match *self {
Self::Cpu => "cpu",
Self::CpuAcct => "cpuacct",
Self::CpuSet => "cpuset",
Self::Devices => "devices",
Self::HugeTlb => "hugetlb",
Expand All @@ -32,6 +34,7 @@ impl Display for ControllerType {

pub const CONTROLLERS: &[ControllerType] = &[
ControllerType::Cpu,
ControllerType::CpuAcct,
ControllerType::CpuSet,
ControllerType::Devices,
ControllerType::HugeTlb,
Expand Down
57 changes: 57 additions & 0 deletions src/cgroups/v1/cpuacct.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
use std::{fs, path::Path};

use anyhow::Result;
use nix::unistd::Pid;
use oci_spec::LinuxResources;

use crate::cgroups::common::{self, CGROUP_PROCS};

use super::Controller;

const CGROUP_CPUACCT_TASKS: &str = "tasks";

pub struct CpuAcct {}

impl Controller for CpuAcct {
fn apply(_linux_resources: &LinuxResources, cgroup_path: &Path, pid: Pid) -> Result<()> {
log::debug!("Apply cpuacct cgroup config");
fs::create_dir_all(cgroup_path)?;

Self::apply(cgroup_path, pid)?;

common::write_cgroup_file(cgroup_path.join(CGROUP_PROCS), pid)?;
Ok(())
}
}

impl CpuAcct {
fn apply(root_path: &Path, pid: Pid) -> Result<()> {
common::write_cgroup_file_str(root_path.join(CGROUP_CPUACCT_TASKS), &pid.to_string())?;
Copy link
Collaborator

@Furisto Furisto Jun 16, 2021

Choose a reason for hiding this comment

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

Why is this required? My understanding is that writing the pid to the cgroup.procs file already moves all of its threads.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thank you for your comment.
I tried it in my environment but this process was unnecessary.
I will commit the fix later.


Ok(())
}
}

#[cfg(test)]
mod tests {
use std::fs;

use super::*;
use crate::cgroups::test::setup;
use nix::unistd::Pid;

#[test]
fn test_set_cpuacct() {
// arrange
let (tmp, cpuacct) = setup("test_set_cpuacct", CGROUP_CPUACCT_TASKS);
let pid = Pid::from_raw(1000);

// act
CpuAcct::apply(&tmp, pid).expect("apply cpuacct");

// assert
let content = fs::read_to_string(&cpuacct)
.unwrap_or_else(|_| panic!("read {} file content", CGROUP_CPUACCT_TASKS));
assert_eq!(content, "1000");
}
}
5 changes: 3 additions & 2 deletions src/cgroups/v1/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ use nix::unistd::Pid;
use procfs::process::Process;

use super::{
blkio::Blkio, controller_type::CONTROLLERS, cpu::Cpu, cpuset::CpuSet, devices::Devices,
hugetlb::Hugetlb, memory::Memory, network_classifier::NetworkClassifier,
blkio::Blkio, controller_type::CONTROLLERS, cpu::Cpu, cpuacct::CpuAcct, cpuset::CpuSet,
devices::Devices, hugetlb::Hugetlb, memory::Memory, network_classifier::NetworkClassifier,
network_priority::NetworkPriority, pids::Pids, util, Controller,
};

Expand Down Expand Up @@ -61,6 +61,7 @@ impl CgroupManager for Manager {
for subsys in &self.subsystems {
match subsys.0.as_str() {
"cpu" => Cpu::apply(linux_resources, &subsys.1, pid)?,
"cpuacct" => CpuAcct::apply(linux_resources, &subsys.1, pid)?,
"cpuset" => CpuSet::apply(linux_resources, &subsys.1, pid)?,
"devices" => Devices::apply(linux_resources, &subsys.1, pid)?,
"hugetlb" => Hugetlb::apply(linux_resources, &subsys.1, pid)?,
Expand Down
1 change: 1 addition & 0 deletions src/cgroups/v1/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ mod blkio;
mod controller;
mod controller_type;
mod cpu;
mod cpuacct;
mod cpuset;
mod devices;
mod hugetlb;
Expand Down
6 changes: 5 additions & 1 deletion src/cgroups/v1/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,13 @@ pub fn get_subsystem_mount_points(subsystem: &str) -> Result<PathBuf> {
return m.mount_point.ends_with("cpu,cpuacct")
|| m.mount_point.ends_with("cpu");
}
if subsystem == "cpuacct" {
return m.mount_point.ends_with("cpu,cpuacct")
|| m.mount_point.ends_with("cpuacct");
}
}
m.mount_point.ends_with(&subsystem)
})
.map(|m| m.mount_point)
.ok_or_else(|| anyhow!("could not find mountpoint for {}", subsystem))
}
}