Compare commits

..

9 Commits

Author SHA1 Message Date
Fupan Li
bcb7c6cd54 Merge pull request #51 from Tim-Zhang/release-0.2.6
release: v0.2.6
2021-06-30 20:53:39 +08:00
Tim Zhang
2554aa65d0 release: v0.2.6
Bump version from 0.2.5 to 0.2.6

Signed-off-by: Tim Zhang <tim@hyper.sh>
2021-06-30 20:36:34 +08:00
Tim Zhang
34f935be89 Merge pull request #49 from lifupan/master
freezer: fix the issue of missing trim the str
2021-06-30 20:35:22 +08:00
Fupan Li
5485d8dd46 Merge pull request #50 from Tim-Zhang/fix-clippy-for-rust-1.53
Fix clippy for rust 1.53
2021-06-30 19:58:48 +08:00
Tim Zhang
ec4cda1dd9 Fix clippy for rust 1.53
There are new lints are added in clippy for rust 1.53

Signed-off-by: Tim Zhang <tim@hyper.sh>
2021-06-30 19:07:56 +08:00
fupan.lfp
4b5a190ecc freezer: fix the issue of missing trim the str
When reading from the freezer file, it should trim it
first, and the string may container an '\n'.

Fixes: #48

Signed-off-by: fupan.lfp <fupan.lfp@antgroup.com>
2021-06-30 18:39:33 +08:00
Fupan Li
45b626e0c0 Merge pull request #46 from Tim-Zhang/fix-clippy-for-rust-1.52
Fix clippy for rust 1.52
2021-05-21 16:24:16 +08:00
Tim Zhang
0e2430fde1 clippy: turn on lint upper_case_acronyms
cargo-clippy has moved the upper_case_acronyms lint to
pedantic(removed from the default list), but we need
the lint to keep names consistent and follow the rust
naming conventions.

Signed-off-by: Tim Zhang <tim@hyper.sh>
2021-05-21 00:48:50 +08:00
Tim Zhang
5aa7e6c90e Fix clippy for rust 1.52
There are new lints are added in clippy for rust 1.52

Signed-off-by: Tim Zhang <tim@hyper.sh>
2021-05-21 00:45:27 +08:00
14 changed files with 36 additions and 34 deletions

1
.clippy.toml Normal file
View File

@@ -0,0 +1 @@
upper-case-acronyms-aggressive = true

View File

@@ -5,7 +5,7 @@ repository = "https://github.com/kata-containers/cgroups-rs"
keywords = ["linux", "cgroup", "containers", "isolation"]
categories = ["os", "api-bindings", "os::unix-apis"]
license = "MIT OR Apache-2.0"
version = "0.2.5"
version = "0.2.6"
authors = ["The Kata Containers community <kata-dev@lists.katacontainers.io>", "Levente Kurusa <lkurusa@acm.org>", "Sam Wilson <tecywiz121@hotmail.com>"]
edition = "2018"
homepage = "https://github.com/kata-containers/cgroups-rs"

View File

@@ -311,9 +311,8 @@ impl Cgroup {
pub const UNIFIED_MOUNTPOINT: &str = "/sys/fs/cgroup";
fn enable_controllers(controllers: &[String], path: &PathBuf) {
let mut f = path.clone();
f.push("cgroup.subtree_control");
fn enable_controllers(controllers: &[String], path: &Path) {
let f = path.join("cgroup.subtree_control");
for c in controllers {
let body = format!("+{}", c);
let _rest = fs::write(f.as_path(), body.as_bytes());

View File

@@ -240,10 +240,10 @@ impl DeviceResourceBuilder {
access: Vec<crate::devices::DevicePermissions>,
) -> DeviceResourceBuilder {
self.cgroup.resources.devices.devices.push(DeviceResource {
allow,
devtype,
major,
minor,
devtype,
allow,
access,
});
self

View File

@@ -45,7 +45,7 @@ pub struct Cpu {
/// The current state of the control group and its processes.
#[derive(Debug)]
struct CFSQuotaAndPeriod {
struct CfsQuotaAndPeriod {
quota: MaxValue,
period: u64,
}
@@ -284,7 +284,7 @@ impl 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();
file.read_to_string(&mut content)
.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>()
.map_err(|e| Error::with_cause(ParseError, e))?;
Ok(CFSQuotaAndPeriod { quota, period })
Ok(CfsQuotaAndPeriod { quota, period })
}

View File

@@ -49,6 +49,7 @@ impl Default for DeviceType {
impl DeviceType {
/// 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 {
match self {
DeviceType::All => 'a',
@@ -82,6 +83,7 @@ pub enum DevicePermissions {
impl DevicePermissions {
/// 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 {
match self {
DevicePermissions::Read => 'r',

View File

@@ -76,6 +76,7 @@ impl fmt::Display for Error {
impl StdError for Error {
fn cause(&self) -> Option<&dyn StdError> {
#[allow(clippy::manual_map)]
match self.cause {
Some(ref x) => Some(&**x),
None => None,

View File

@@ -8,7 +8,7 @@ use nix::sys::eventfd;
use std::fs::{self, File};
use std::io::Read;
use std::os::unix::io::{AsRawFd, FromRawFd};
use std::path::{Path, PathBuf};
use std::path::Path;
use std::sync::mpsc::{self, Receiver};
use std::thread;
@@ -17,18 +17,18 @@ use crate::error::*;
// notify_on_oom returns channel on which you can expect event about OOM,
// 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", "")
}
// notify_on_oom returns channel on which you can expect event about OOM,
// 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", "")
}
// 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" {
return Err(Error::from_string(format!(
"invalid pressure level {}",
@@ -41,7 +41,7 @@ pub fn notify_memory_pressure(key: &str, dir: &PathBuf, level: &str) -> Result<R
fn register_memory_event(
key: &str,
cg_dir: &PathBuf,
cg_dir: &Path,
event_name: &str,
arg: &str,
) -> Result<Receiver<String>> {

View File

@@ -130,7 +130,7 @@ impl FreezerController {
let mut s = String::new();
let res = file.read_to_string(&mut s);
match res {
Ok(_) => match s.as_ref() {
Ok(_) => match s.trim() {
"FROZEN" => Ok(FreezerState::Frozen),
"THAWED" => Ok(FreezerState::Thawed),
"1" => Ok(FreezerState::Frozen),

View File

@@ -365,11 +365,9 @@ where
.map(|file| {
let bf = BufReader::new(file);
let mut v = Vec::new();
for line in bf.lines() {
if let Ok(line) = line {
let n = line.trim().parse().unwrap_or(0u64);
v.push(n);
}
for line in bf.lines().flatten() {
let n = line.trim().parse().unwrap_or(0u64);
v.push(n);
}
v.into_iter().map(CgroupPid::from).collect()
})
@@ -383,7 +381,7 @@ where
// remove_dir aims to remove cgroup path. It does so recursively,
// by removing any subdirectories (sub-cgroups) first.
fn remove_dir(dir: &PathBuf) -> Result<()> {
fn remove_dir(dir: &Path) -> Result<()> {
// try the fast path first.
if fs::remove_dir(dir).is_ok() {
return Ok(());
@@ -740,6 +738,7 @@ impl Default for MaxValue {
}
impl MaxValue {
#[allow(clippy::should_implement_trait, clippy::wrong_self_convention)]
fn to_i64(&self) -> i64 {
match self {
MaxValue::Max => -1,

View File

@@ -36,12 +36,12 @@ fn test_cpuset_set_cpus() {
assert_eq!(0, set.cpus.len());
} else {
// for cgroup v1, cpuset is copied from parent.
assert_eq!(true, !set.cpus.is_empty());
assert!(!set.cpus.is_empty());
}
// 0
let r = cpuset.set_cpus("0");
assert_eq!(true, r.is_ok());
assert!(r.is_ok());
let set = cpuset.cpuset();
assert_eq!(1, set.cpus.len());
@@ -52,7 +52,7 @@ fn test_cpuset_set_cpus() {
let cpus = cpus.trim();
if !cpus.is_empty() {
let r = cpuset.set_cpus(&cpus);
assert_eq!(true, r.is_ok());
assert!(r.is_ok());
let set = cpuset.cpuset();
assert_eq!(1, set.cpus.len());
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());
} else {
// 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.
let pid_i = libc::pid_t::from(nix::unistd::getpid()) as u64;
let _ = cg.add_task(CgroupPid::from(pid_i));
let tasks = cg.tasks();
assert_eq!(true, !tasks.is_empty());
assert!(!tasks.is_empty());
println!("tasks after added: {:?}", tasks);
// remove task

View File

@@ -29,7 +29,7 @@ fn test_hugetlb_sizes() {
for size in sizes {
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.limit_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>) {
assert_eq!(!r.is_err(), true)
assert!(!r.is_err())
}

View File

@@ -17,17 +17,17 @@ fn test_disable_oom_killer() {
// before disable
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
if !mem_controller.v2() {
// disable oom killer
let r = mem_controller.disable_oom_killer();
assert_eq!(r.is_err(), false);
assert!(!r.is_err());
// after disable
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();
@@ -60,7 +60,7 @@ fn set_mem_v2() {
max: None,
};
let r = mem_controller.set_mem(m);
assert_eq!(true, r.is_ok());
assert!(r.is_ok());
let m = mem_controller.get_mem().unwrap();
// get
@@ -77,7 +77,7 @@ fn set_mem_v2() {
low: None,
};
let r = mem_controller.set_mem(m);
assert_eq!(true, r.is_ok());
assert!(r.is_ok());
let m = mem_controller.get_mem().unwrap();
// get

View File

@@ -24,7 +24,7 @@ fn pid_resources() {
// verify
let pidcontroller: &PidController = cg.controller_of().unwrap();
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));
}
cg.delete().unwrap();