mirror of
https://github.com/kata-containers/cgroups-rs.git
synced 2026-08-05 02:13:23 +00:00
Compare commits
13 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
bcb7c6cd54 | ||
|
|
2554aa65d0 | ||
|
|
34f935be89 | ||
|
|
5485d8dd46 | ||
|
|
ec4cda1dd9 | ||
|
|
4b5a190ecc | ||
|
|
45b626e0c0 | ||
|
|
0e2430fde1 | ||
|
|
5aa7e6c90e | ||
|
|
5bb27a2692 | ||
|
|
0b2a0405e2 | ||
|
|
c4850ef2ef | ||
|
|
aa207edca8 |
1
.clippy.toml
Normal file
1
.clippy.toml
Normal file
@@ -0,0 +1 @@
|
|||||||
|
upper-case-acronyms-aggressive = true
|
||||||
@@ -5,7 +5,7 @@ repository = "https://github.com/kata-containers/cgroups-rs"
|
|||||||
keywords = ["linux", "cgroup", "containers", "isolation"]
|
keywords = ["linux", "cgroup", "containers", "isolation"]
|
||||||
categories = ["os", "api-bindings", "os::unix-apis"]
|
categories = ["os", "api-bindings", "os::unix-apis"]
|
||||||
license = "MIT OR Apache-2.0"
|
license = "MIT OR Apache-2.0"
|
||||||
version = "0.2.4"
|
version = "0.2.6"
|
||||||
authors = ["The Kata Containers community <kata-dev@lists.katacontainers.io>", "Levente Kurusa <lkurusa@acm.org>", "Sam Wilson <tecywiz121@hotmail.com>"]
|
authors = ["The Kata Containers community <kata-dev@lists.katacontainers.io>", "Levente Kurusa <lkurusa@acm.org>", "Sam Wilson <tecywiz121@hotmail.com>"]
|
||||||
edition = "2018"
|
edition = "2018"
|
||||||
homepage = "https://github.com/kata-containers/cgroups-rs"
|
homepage = "https://github.com/kata-containers/cgroups-rs"
|
||||||
|
|||||||
@@ -311,9 +311,8 @@ impl Cgroup {
|
|||||||
|
|
||||||
pub const UNIFIED_MOUNTPOINT: &str = "/sys/fs/cgroup";
|
pub const UNIFIED_MOUNTPOINT: &str = "/sys/fs/cgroup";
|
||||||
|
|
||||||
fn enable_controllers(controllers: &[String], path: &PathBuf) {
|
fn enable_controllers(controllers: &[String], path: &Path) {
|
||||||
let mut f = path.clone();
|
let f = path.join("cgroup.subtree_control");
|
||||||
f.push("cgroup.subtree_control");
|
|
||||||
for c in controllers {
|
for c in controllers {
|
||||||
let body = format!("+{}", c);
|
let body = format!("+{}", c);
|
||||||
let _rest = fs::write(f.as_path(), body.as_bytes());
|
let _rest = fs::write(f.as_path(), body.as_bytes());
|
||||||
|
|||||||
@@ -240,10 +240,10 @@ impl DeviceResourceBuilder {
|
|||||||
access: Vec<crate::devices::DevicePermissions>,
|
access: Vec<crate::devices::DevicePermissions>,
|
||||||
) -> DeviceResourceBuilder {
|
) -> DeviceResourceBuilder {
|
||||||
self.cgroup.resources.devices.devices.push(DeviceResource {
|
self.cgroup.resources.devices.devices.push(DeviceResource {
|
||||||
|
allow,
|
||||||
|
devtype,
|
||||||
major,
|
major,
|
||||||
minor,
|
minor,
|
||||||
devtype,
|
|
||||||
allow,
|
|
||||||
access,
|
access,
|
||||||
});
|
});
|
||||||
self
|
self
|
||||||
|
|||||||
@@ -45,7 +45,7 @@ pub struct Cpu {
|
|||||||
|
|
||||||
/// The current state of the control group and its processes.
|
/// The current state of the control group and its processes.
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
struct CFSQuotaAndPeriod {
|
struct CfsQuotaAndPeriod {
|
||||||
quota: MaxValue,
|
quota: MaxValue,
|
||||||
period: u64,
|
period: u64,
|
||||||
}
|
}
|
||||||
@@ -284,7 +284,7 @@ impl CpuController {
|
|||||||
|
|
||||||
impl CustomizedAttribute for CpuController {}
|
impl CustomizedAttribute for CpuController {}
|
||||||
|
|
||||||
fn parse_cfs_quota_and_period(mut file: File) -> Result<CFSQuotaAndPeriod> {
|
fn parse_cfs_quota_and_period(mut file: File) -> Result<CfsQuotaAndPeriod> {
|
||||||
let mut content = String::new();
|
let mut content = String::new();
|
||||||
file.read_to_string(&mut content)
|
file.read_to_string(&mut content)
|
||||||
.map_err(|e| Error::with_cause(ReadFailed, e))?;
|
.map_err(|e| Error::with_cause(ReadFailed, e))?;
|
||||||
@@ -299,5 +299,5 @@ fn parse_cfs_quota_and_period(mut file: File) -> Result<CFSQuotaAndPeriod> {
|
|||||||
.parse::<u64>()
|
.parse::<u64>()
|
||||||
.map_err(|e| Error::with_cause(ParseError, e))?;
|
.map_err(|e| Error::with_cause(ParseError, e))?;
|
||||||
|
|
||||||
Ok(CFSQuotaAndPeriod { quota, period })
|
Ok(CfsQuotaAndPeriod { quota, period })
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -49,6 +49,7 @@ impl Default for DeviceType {
|
|||||||
|
|
||||||
impl DeviceType {
|
impl DeviceType {
|
||||||
/// Convert a DeviceType into the character that the kernel recognizes.
|
/// Convert a DeviceType into the character that the kernel recognizes.
|
||||||
|
#[allow(clippy::should_implement_trait, clippy::wrong_self_convention)]
|
||||||
pub fn to_char(&self) -> char {
|
pub fn to_char(&self) -> char {
|
||||||
match self {
|
match self {
|
||||||
DeviceType::All => 'a',
|
DeviceType::All => 'a',
|
||||||
@@ -82,6 +83,7 @@ pub enum DevicePermissions {
|
|||||||
|
|
||||||
impl DevicePermissions {
|
impl DevicePermissions {
|
||||||
/// Convert a DevicePermissions into the character that the kernel recognizes.
|
/// Convert a DevicePermissions into the character that the kernel recognizes.
|
||||||
|
#[allow(clippy::should_implement_trait, clippy::wrong_self_convention)]
|
||||||
pub fn to_char(&self) -> char {
|
pub fn to_char(&self) -> char {
|
||||||
match self {
|
match self {
|
||||||
DevicePermissions::Read => 'r',
|
DevicePermissions::Read => 'r',
|
||||||
|
|||||||
@@ -76,6 +76,7 @@ impl fmt::Display for Error {
|
|||||||
|
|
||||||
impl StdError for Error {
|
impl StdError for Error {
|
||||||
fn cause(&self) -> Option<&dyn StdError> {
|
fn cause(&self) -> Option<&dyn StdError> {
|
||||||
|
#[allow(clippy::manual_map)]
|
||||||
match self.cause {
|
match self.cause {
|
||||||
Some(ref x) => Some(&**x),
|
Some(ref x) => Some(&**x),
|
||||||
None => None,
|
None => None,
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ use nix::sys::eventfd;
|
|||||||
use std::fs::{self, File};
|
use std::fs::{self, File};
|
||||||
use std::io::Read;
|
use std::io::Read;
|
||||||
use std::os::unix::io::{AsRawFd, FromRawFd};
|
use std::os::unix::io::{AsRawFd, FromRawFd};
|
||||||
use std::path::{Path, PathBuf};
|
use std::path::Path;
|
||||||
use std::sync::mpsc::{self, Receiver};
|
use std::sync::mpsc::{self, Receiver};
|
||||||
use std::thread;
|
use std::thread;
|
||||||
|
|
||||||
@@ -17,18 +17,18 @@ use crate::error::*;
|
|||||||
|
|
||||||
// notify_on_oom returns channel on which you can expect event about OOM,
|
// notify_on_oom returns channel on which you can expect event about OOM,
|
||||||
// if process died without OOM this channel will be closed.
|
// if process died without OOM this channel will be closed.
|
||||||
pub fn notify_on_oom_v2(key: &str, dir: &PathBuf) -> Result<Receiver<String>> {
|
pub fn notify_on_oom_v2(key: &str, dir: &Path) -> Result<Receiver<String>> {
|
||||||
register_memory_event(key, dir, "memory.oom_control", "")
|
register_memory_event(key, dir, "memory.oom_control", "")
|
||||||
}
|
}
|
||||||
|
|
||||||
// notify_on_oom returns channel on which you can expect event about OOM,
|
// notify_on_oom returns channel on which you can expect event about OOM,
|
||||||
// if process died without OOM this channel will be closed.
|
// if process died without OOM this channel will be closed.
|
||||||
pub fn notify_on_oom_v1(key: &str, dir: &PathBuf) -> Result<Receiver<String>> {
|
pub fn notify_on_oom_v1(key: &str, dir: &Path) -> Result<Receiver<String>> {
|
||||||
register_memory_event(key, dir, "memory.oom_control", "")
|
register_memory_event(key, dir, "memory.oom_control", "")
|
||||||
}
|
}
|
||||||
|
|
||||||
// level is one of "low", "medium", or "critical"
|
// level is one of "low", "medium", or "critical"
|
||||||
pub fn notify_memory_pressure(key: &str, dir: &PathBuf, level: &str) -> Result<Receiver<String>> {
|
pub fn notify_memory_pressure(key: &str, dir: &Path, level: &str) -> Result<Receiver<String>> {
|
||||||
if level != "low" && level != "medium" && level != "critical" {
|
if level != "low" && level != "medium" && level != "critical" {
|
||||||
return Err(Error::from_string(format!(
|
return Err(Error::from_string(format!(
|
||||||
"invalid pressure level {}",
|
"invalid pressure level {}",
|
||||||
@@ -41,7 +41,7 @@ pub fn notify_memory_pressure(key: &str, dir: &PathBuf, level: &str) -> Result<R
|
|||||||
|
|
||||||
fn register_memory_event(
|
fn register_memory_event(
|
||||||
key: &str,
|
key: &str,
|
||||||
cg_dir: &PathBuf,
|
cg_dir: &Path,
|
||||||
event_name: &str,
|
event_name: &str,
|
||||||
arg: &str,
|
arg: &str,
|
||||||
) -> Result<Receiver<String>> {
|
) -> Result<Receiver<String>> {
|
||||||
|
|||||||
@@ -130,7 +130,7 @@ impl FreezerController {
|
|||||||
let mut s = String::new();
|
let mut s = String::new();
|
||||||
let res = file.read_to_string(&mut s);
|
let res = file.read_to_string(&mut s);
|
||||||
match res {
|
match res {
|
||||||
Ok(_) => match s.as_ref() {
|
Ok(_) => match s.trim() {
|
||||||
"FROZEN" => Ok(FreezerState::Frozen),
|
"FROZEN" => Ok(FreezerState::Frozen),
|
||||||
"THAWED" => Ok(FreezerState::Thawed),
|
"THAWED" => Ok(FreezerState::Thawed),
|
||||||
"1" => Ok(FreezerState::Frozen),
|
"1" => Ok(FreezerState::Frozen),
|
||||||
|
|||||||
@@ -35,7 +35,7 @@ use crate::cgroup::Cgroup;
|
|||||||
/// Process mounts information.
|
/// Process mounts information.
|
||||||
///
|
///
|
||||||
/// See `proc(5)` for format details.
|
/// See `proc(5)` for format details.
|
||||||
#[derive(Debug, PartialEq, Eq, Hash)]
|
#[derive(Debug, PartialEq, Eq, Hash, Clone)]
|
||||||
pub struct Mountinfo {
|
pub struct Mountinfo {
|
||||||
/// Mount pathname relative to the process's root.
|
/// Mount pathname relative to the process's root.
|
||||||
pub mount_point: PathBuf,
|
pub mount_point: PathBuf,
|
||||||
@@ -102,12 +102,12 @@ pub fn mountinfo_self() -> Vec<Mountinfo> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// The standard, original cgroup implementation. Often referred to as "cgroupv1".
|
/// The standard, original cgroup implementation. Often referred to as "cgroupv1".
|
||||||
#[derive(Debug)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct V1 {
|
pub struct V1 {
|
||||||
mountinfo: Vec<Mountinfo>,
|
mountinfo: Vec<Mountinfo>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct V2 {
|
pub struct V2 {
|
||||||
root: String,
|
root: String,
|
||||||
}
|
}
|
||||||
|
|||||||
11
src/lib.rs
11
src/lib.rs
@@ -365,11 +365,9 @@ where
|
|||||||
.map(|file| {
|
.map(|file| {
|
||||||
let bf = BufReader::new(file);
|
let bf = BufReader::new(file);
|
||||||
let mut v = Vec::new();
|
let mut v = Vec::new();
|
||||||
for line in bf.lines() {
|
for line in bf.lines().flatten() {
|
||||||
if let Ok(line) = line {
|
let n = line.trim().parse().unwrap_or(0u64);
|
||||||
let n = line.trim().parse().unwrap_or(0u64);
|
v.push(n);
|
||||||
v.push(n);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
v.into_iter().map(CgroupPid::from).collect()
|
v.into_iter().map(CgroupPid::from).collect()
|
||||||
})
|
})
|
||||||
@@ -383,7 +381,7 @@ where
|
|||||||
|
|
||||||
// remove_dir aims to remove cgroup path. It does so recursively,
|
// remove_dir aims to remove cgroup path. It does so recursively,
|
||||||
// by removing any subdirectories (sub-cgroups) first.
|
// by removing any subdirectories (sub-cgroups) first.
|
||||||
fn remove_dir(dir: &PathBuf) -> Result<()> {
|
fn remove_dir(dir: &Path) -> Result<()> {
|
||||||
// try the fast path first.
|
// try the fast path first.
|
||||||
if fs::remove_dir(dir).is_ok() {
|
if fs::remove_dir(dir).is_ok() {
|
||||||
return Ok(());
|
return Ok(());
|
||||||
@@ -740,6 +738,7 @@ impl Default for MaxValue {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl MaxValue {
|
impl MaxValue {
|
||||||
|
#[allow(clippy::should_implement_trait, clippy::wrong_self_convention)]
|
||||||
fn to_i64(&self) -> i64 {
|
fn to_i64(&self) -> i64 {
|
||||||
match self {
|
match self {
|
||||||
MaxValue::Max => -1,
|
MaxValue::Max => -1,
|
||||||
|
|||||||
@@ -36,12 +36,12 @@ fn test_cpuset_set_cpus() {
|
|||||||
assert_eq!(0, set.cpus.len());
|
assert_eq!(0, set.cpus.len());
|
||||||
} else {
|
} else {
|
||||||
// for cgroup v1, cpuset is copied from parent.
|
// for cgroup v1, cpuset is copied from parent.
|
||||||
assert_eq!(true, !set.cpus.is_empty());
|
assert!(!set.cpus.is_empty());
|
||||||
}
|
}
|
||||||
|
|
||||||
// 0
|
// 0
|
||||||
let r = cpuset.set_cpus("0");
|
let r = cpuset.set_cpus("0");
|
||||||
assert_eq!(true, r.is_ok());
|
assert!(r.is_ok());
|
||||||
|
|
||||||
let set = cpuset.cpuset();
|
let set = cpuset.cpuset();
|
||||||
assert_eq!(1, set.cpus.len());
|
assert_eq!(1, set.cpus.len());
|
||||||
@@ -52,7 +52,7 @@ fn test_cpuset_set_cpus() {
|
|||||||
let cpus = cpus.trim();
|
let cpus = cpus.trim();
|
||||||
if !cpus.is_empty() {
|
if !cpus.is_empty() {
|
||||||
let r = cpuset.set_cpus(&cpus);
|
let r = cpuset.set_cpus(&cpus);
|
||||||
assert_eq!(true, r.is_ok());
|
assert!(r.is_ok());
|
||||||
let set = cpuset.cpuset();
|
let set = cpuset.cpuset();
|
||||||
assert_eq!(1, set.cpus.len());
|
assert_eq!(1, set.cpus.len());
|
||||||
assert_eq!(format!("{}-{}", set.cpus[0].0, set.cpus[0].1), cpus);
|
assert_eq!(format!("{}-{}", set.cpus[0].0, set.cpus[0].1), cpus);
|
||||||
@@ -72,14 +72,14 @@ fn test_cpuset_set_cpus_add_task() {
|
|||||||
assert_eq!(0, set.cpus.len());
|
assert_eq!(0, set.cpus.len());
|
||||||
} else {
|
} else {
|
||||||
// for cgroup v1, cpuset is copied from parent.
|
// for cgroup v1, cpuset is copied from parent.
|
||||||
assert_eq!(true, !set.cpus.is_empty());
|
assert!(!set.cpus.is_empty());
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add a task to the control group.
|
// Add a task to the control group.
|
||||||
let pid_i = libc::pid_t::from(nix::unistd::getpid()) as u64;
|
let pid_i = libc::pid_t::from(nix::unistd::getpid()) as u64;
|
||||||
let _ = cg.add_task(CgroupPid::from(pid_i));
|
let _ = cg.add_task(CgroupPid::from(pid_i));
|
||||||
let tasks = cg.tasks();
|
let tasks = cg.tasks();
|
||||||
assert_eq!(true, !tasks.is_empty());
|
assert!(!tasks.is_empty());
|
||||||
println!("tasks after added: {:?}", tasks);
|
println!("tasks after added: {:?}", tasks);
|
||||||
|
|
||||||
// remove task
|
// remove task
|
||||||
|
|||||||
@@ -29,7 +29,7 @@ fn test_hugetlb_sizes() {
|
|||||||
|
|
||||||
for size in sizes {
|
for size in sizes {
|
||||||
let supported = hugetlb_controller.size_supported(&size);
|
let supported = hugetlb_controller.size_supported(&size);
|
||||||
assert_eq!(supported, true);
|
assert!(supported);
|
||||||
assert_no_error(hugetlb_controller.failcnt(&size));
|
assert_no_error(hugetlb_controller.failcnt(&size));
|
||||||
assert_no_error(hugetlb_controller.limit_in_bytes(&size));
|
assert_no_error(hugetlb_controller.limit_in_bytes(&size));
|
||||||
assert_no_error(hugetlb_controller.usage_in_bytes(&size));
|
assert_no_error(hugetlb_controller.usage_in_bytes(&size));
|
||||||
@@ -40,5 +40,5 @@ fn test_hugetlb_sizes() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn assert_no_error(r: Result<u64>) {
|
fn assert_no_error(r: Result<u64>) {
|
||||||
assert_eq!(!r.is_err(), true)
|
assert!(!r.is_err())
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -17,17 +17,17 @@ fn test_disable_oom_killer() {
|
|||||||
|
|
||||||
// before disable
|
// before disable
|
||||||
let m = mem_controller.memory_stat();
|
let m = mem_controller.memory_stat();
|
||||||
assert_eq!(m.oom_control.oom_kill_disable, false);
|
assert!(!m.oom_control.oom_kill_disable);
|
||||||
|
|
||||||
// now only v1
|
// now only v1
|
||||||
if !mem_controller.v2() {
|
if !mem_controller.v2() {
|
||||||
// disable oom killer
|
// disable oom killer
|
||||||
let r = mem_controller.disable_oom_killer();
|
let r = mem_controller.disable_oom_killer();
|
||||||
assert_eq!(r.is_err(), false);
|
assert!(!r.is_err());
|
||||||
|
|
||||||
// after disable
|
// after disable
|
||||||
let m = mem_controller.memory_stat();
|
let m = mem_controller.memory_stat();
|
||||||
assert_eq!(m.oom_control.oom_kill_disable, true);
|
assert!(m.oom_control.oom_kill_disable);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
cg.delete().unwrap();
|
cg.delete().unwrap();
|
||||||
@@ -60,7 +60,7 @@ fn set_mem_v2() {
|
|||||||
max: None,
|
max: None,
|
||||||
};
|
};
|
||||||
let r = mem_controller.set_mem(m);
|
let r = mem_controller.set_mem(m);
|
||||||
assert_eq!(true, r.is_ok());
|
assert!(r.is_ok());
|
||||||
|
|
||||||
let m = mem_controller.get_mem().unwrap();
|
let m = mem_controller.get_mem().unwrap();
|
||||||
// get
|
// get
|
||||||
@@ -77,7 +77,7 @@ fn set_mem_v2() {
|
|||||||
low: None,
|
low: None,
|
||||||
};
|
};
|
||||||
let r = mem_controller.set_mem(m);
|
let r = mem_controller.set_mem(m);
|
||||||
assert_eq!(true, r.is_ok());
|
assert!(r.is_ok());
|
||||||
|
|
||||||
let m = mem_controller.get_mem().unwrap();
|
let m = mem_controller.get_mem().unwrap();
|
||||||
// get
|
// get
|
||||||
|
|||||||
@@ -24,7 +24,7 @@ fn pid_resources() {
|
|||||||
// verify
|
// verify
|
||||||
let pidcontroller: &PidController = cg.controller_of().unwrap();
|
let pidcontroller: &PidController = cg.controller_of().unwrap();
|
||||||
let pid_max = pidcontroller.get_pid_max();
|
let pid_max = pidcontroller.get_pid_max();
|
||||||
assert_eq!(pid_max.is_ok(), true);
|
assert!(pid_max.is_ok());
|
||||||
assert_eq!(pid_max.unwrap(), MaxValue::Value(512));
|
assert_eq!(pid_max.unwrap(), MaxValue::Value(512));
|
||||||
}
|
}
|
||||||
cg.delete().unwrap();
|
cg.delete().unwrap();
|
||||||
|
|||||||
Reference in New Issue
Block a user