Compare commits

..

18 Commits

Author SHA1 Message Date
Tim Zhang
b4df6016b3 Merge pull request #71 from Tim-Zhang/release-0.2.8
release: v0.2.8
2021-12-17 18:27:13 +08:00
Tim Zhang
0686400268 release: v0.2.8
Bump version from 0.2.7 to 0.2.8

Signed-off-by: Tim Zhang <tim@hyper.sh>
2021-12-17 17:40:45 +08:00
Tim Zhang
6986c49e70 Merge pull request #67 from jodh-intel/update-nix-version
cargo: Update nix to latest version
2021-11-30 17:58:31 +08:00
James O. D. Hunt
e0bf36ae23 cargo: Update nix to latest version
Update the `nix` crate to version `0.23.0` to resolve a dependency
issue related to making `cargo audit` run cleanly for the Kata
Containers agent.

See:

https://github.com/kata-containers/kata-containers/pull/3125

Fixes: #66.

Signed-off-by: James O. D. Hunt <james.o.hunt@intel.com>
2021-11-26 11:17:58 +00:00
Fupan Li
2c68f36488 Merge pull request #65 from Tim-Zhang/release-0.2.7
release: v0.2.7
2021-11-11 11:36:20 +08:00
Tim Zhang
0bc1b9821b release: v0.2.7
Bump version from 0.2.6 to 0.2.7

Signed-off-by: Tim Zhang <tim@hyper.sh>
2021-11-11 11:25:28 +08:00
Tim Zhang
da75ba25d6 Merge pull request #59 from jmagnuson/update-nix-0.20.2
deps: update nix to 0.20.2
2021-11-11 11:23:08 +08:00
Jon Magnuson
01475ad515 clippy: fix needless-borrow in test
Fix needless-borrow in test.

Signed-off-by: Jon Magnuson <jon.magnuson@gmail.com>
2021-11-11 11:20:49 +08:00
Tim Zhang
417badd05f Merge pull request #61 from mjerabek/fix-freezer-for-v2
fix: support FreezerController in cgroups v2
2021-11-11 11:11:38 +08:00
Tim Zhang
1720443d58 Merge pull request #63 from quanweiZhou/fix_read_task_hang
fix: read task hang when the cgroup dir removed after open
2021-11-11 11:09:35 +08:00
quanwei.zqw
fb55383273 fix: read task hang when the cgroup dir removed after open
Fixes #62
Signed-off-by: quanweiZhou <quanweiZhou@linux.alibaba.com>
2021-11-10 19:52:42 +08:00
Tim Zhang
0233c1e046 Merge pull request #64 from Tim-Zhang/action-lock-rust-version
action: Lock rust version and fix commit-body checker
2021-11-10 19:39:00 +08:00
Tim Zhang
bf5af7b195 action: fix the the body line checker
the original regex can not handle pasted error message
rightly. this commit will fix this.

Signed-off-by: Tim Zhang <tim@hyper.sh>
2021-11-10 19:34:30 +08:00
Tim Zhang
2e3719dbd5 action: Lock rust version to 1.52
Got the following error in the clippy:
error: this expression borrows a reference (`&str`) that is immediately dereferenced by the compiler
  --> tests/cpuset.rs:54:37
   |
54 |             let r = cpuset.set_cpus(&cpus);
   |                                     ^^^^^ help: change this to: `cpus`
   |
   = note: `-D clippy::needless-borrow` implied by `-D warnings`
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_borrow

But we did not touch the code, just becuase the clippy rules was updated
follow the rust update.

To avoid this problem we lock the rust version in CI and make it
steady.

Signed-off-by: Tim Zhang <tim@hyper.sh>
2021-11-10 19:29:14 +08:00
Martin Jeřábek
4231f35a44 fix: support FreezerController in cgroups v2
Fixes #60

Signed-off-by: Martin Jeřábek <martin.jerabek01@gmail.com>
2021-11-10 11:05:33 +01:00
Jon Magnuson
88b7aafd05 deps: update nix to 0.20.2
Enforces use of a version of nix which contains a patch for
RUSTSEC-2021-0119.

Fixes #58

Signed-off-by: Jon Magnuson <jon.magnuson@gmail.com>
2021-10-28 22:27:58 -05:00
Fupan Li
63750887c2 Merge pull request #56 from flxo/pr-serde
Add Serialize/Deserialize to resource and stat types
2021-08-04 10:10:28 +08:00
Felix Obenhuber
09499e7614 Add Serialize/Deserialize to resource and stat types
Add feature "serde" that derives all resource and statistics types from
Serialize and Deserialize. The feature is turned off by default.

Fixes #54
Fixes #55
Fixes #56

Signed-off-by: Felix Obenhuber <felix@obenhuber.de>
2021-08-03 09:55:10 +02:00
13 changed files with 77 additions and 17 deletions

View File

@@ -1,20 +1,22 @@
name: BVT
on: [pull_request]
env:
RUST_VERSION: 1.52
jobs:
build:
name: Build
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Build
run: make debug
- uses: actions/checkout@v2
- run: rustup install ${{ env.RUST_VERSION }} && rustup default ${{ env.RUST_VERSION }}
- run: make debug
fmt:
name: Format Check
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- run: rustup install ${{ env.RUST_VERSION }} && rustup default ${{ env.RUST_VERSION }}
- run: rustup component add rustfmt
- run: make fmt
clippy:
@@ -22,6 +24,7 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- run: rustup install ${{ env.RUST_VERSION }} && rustup default ${{ env.RUST_VERSION }}
- run: rustup component add clippy
- run: make clippy
test:
@@ -29,6 +32,7 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- run: rustup install ${{ env.RUST_VERSION }} && rustup default ${{ env.RUST_VERSION }}
- run: make test

View File

@@ -48,6 +48,6 @@ jobs:
uses: tim-actions/commit-message-checker-with-regex@v0.3.1
with:
commits: ${{ steps.get-pr-commits.outputs.commits }}
pattern: '^.+(\n.{0,72})*$|^.+\n\s*[^a-zA-Z\s\n]|^.+\n\S+$'
pattern: '^.+(\n([a-zA-Z].{0,149}|[^a-zA-Z\n].*|Signed-off-by:.*|))+$'
error: 'Body line too long (max 72)'
post_error: ${{ env.error_msg }}

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.6"
version = "0.2.8"
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"
@@ -14,8 +14,12 @@ readme = "README.md"
[dependencies]
log = "0.4"
regex = "1.1"
nix = "0.20.0"
nix = "0.23.0"
libc = "0.2"
serde = { version = "1.0", features = ["derive"], optional = true }
[dev-dependencies]
libc = "0.2.76"
[features]
default = []

View File

@@ -31,6 +31,7 @@ pub struct BlkIoController {
}
#[derive(Eq, PartialEq, Debug)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
/// Per-device information
pub struct BlkIoData {
/// The major number of the device.
@@ -42,6 +43,7 @@ pub struct BlkIoData {
}
#[derive(Eq, PartialEq, Debug)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
/// Per-device activity from the control group.
pub struct IoService {
/// The major number of the device.
@@ -61,6 +63,7 @@ pub struct IoService {
}
#[derive(Eq, PartialEq, Debug)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
/// Per-device activity from the control group.
/// Only for cgroup v2
pub struct IoStat {
@@ -203,6 +206,7 @@ fn parse_blkio_data(s: String) -> Result<Vec<BlkIoData>> {
/// Current state and statistics about how throttled are the block devices when accessed from the
/// controller's control group.
#[derive(Default, Debug)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct BlkIoThrottle {
/// Statistics about the bytes transferred between the block devices by the tasks in this
/// control group.
@@ -238,6 +242,7 @@ pub struct BlkIoThrottle {
/// Statistics and state of the block devices.
#[derive(Default, Debug)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct BlkIo {
/// The number of BIOS requests merged into I/O requests by the control group's tasks.
pub io_merged: Vec<IoService>,

View File

@@ -36,6 +36,7 @@ pub struct CpuController {
/// The current state of the control group and its processes.
#[derive(Debug)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Cpu {
/// Reports CPU time statistics.
///

View File

@@ -27,6 +27,7 @@ pub struct CpuAcctController {
}
/// Represents the statistics retrieved from the control group.
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct CpuAcct {
/// Divides the time used by the tasks into `user` time and `system` time.
pub stat: String,

View File

@@ -33,6 +33,7 @@ pub struct CpuSetController {
}
/// The current state of the `cpuset` controller for this control group.
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct CpuSet {
/// If true, no other control groups can share the CPUs listed in the `cpus` field.
pub cpu_exclusive: bool,

View File

@@ -32,6 +32,11 @@ pub struct DevicesController {
/// An enum holding the different types of devices that can be manipulated using this controller.
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
#[cfg_attr(
feature = "serde",
derive(serde::Serialize, serde::Deserialize),
serde(rename_all = "snake_case")
)]
pub enum DeviceType {
/// The rule applies to all devices.
All,
@@ -71,6 +76,11 @@ impl DeviceType {
/// An enum with the permissions that can be allowed/denied to the control group.
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
#[cfg_attr(
feature = "serde",
derive(serde::Serialize, serde::Deserialize),
serde(rename_all = "snake_case")
)]
pub enum DevicePermissions {
/// Permission to read from the device.
Read,

View File

@@ -32,6 +32,7 @@ pub struct FreezerController {
}
/// The current state of the control group
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum FreezerState {
/// The processes in the control group are _not_ frozen.
Thawed,

View File

@@ -87,7 +87,7 @@ fn mountinfo_file(file: &mut File) -> Vec<Mountinfo> {
}
}
}
Err(_) => continue,
Err(_) => break,
}
}
r
@@ -202,7 +202,12 @@ impl Hierarchy for V2 {
let mut subs = vec![];
let controllers = ret.unwrap().trim().to_string();
let controller_list: Vec<&str> = controllers.split(' ').collect();
let mut controller_list: Vec<&str> = controllers.split(' ').collect();
// The freezer functionality is present in V2, but not as a controller,
// but apparently as a core functionality. FreezerController supports
// that, but we must explicitly fake the controller here.
controller_list.push("freezer");
for s in controller_list {
match s {

View File

@@ -365,9 +365,14 @@ where
.map(|file| {
let bf = BufReader::new(file);
let mut v = Vec::new();
for line in bf.lines().flatten() {
let n = line.trim().parse().unwrap_or(0u64);
v.push(n);
for line in bf.lines() {
match line {
Ok(line) => {
let n = line.trim().parse().unwrap_or(0u64);
v.push(n);
}
Err(_) => break,
}
}
v.into_iter().map(CgroupPid::from).collect()
})
@@ -423,6 +428,7 @@ pub trait Hierarchy: std::fmt::Debug + Send + Sync {
/// Resource limits for the memory subsystem.
#[derive(Debug, Clone, Eq, PartialEq, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct MemoryResources {
/// How much memory (in bytes) can the kernel consume.
pub kernel_memory_limit: Option<i64>,
@@ -446,13 +452,15 @@ pub struct MemoryResources {
/// # Usage:
/// ```
/// let resource = &mut cgroups_rs::Resources::default();
/// resource.memory.attrs.insert("memory.numa_balancing", "true".to_string());
/// resource.memory.attrs.insert("memory.numa_balancing".to_string(), "true".to_string());
/// // apply here
pub attrs: std::collections::HashMap<&'static str, String>,
/// ```
pub attrs: HashMap<String, String>,
}
/// Resources limits on the number of processes.
#[derive(Debug, Clone, Eq, PartialEq, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct PidResources {
/// The maximum number of processes that can exist in the control group.
///
@@ -464,6 +472,7 @@ pub struct PidResources {
/// Resources limits about how the tasks can use the CPU.
#[derive(Debug, Clone, Eq, PartialEq, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct CpuResources {
// cpuset
/// A comma-separated list of CPU IDs where the task in the control group can run. Dashes
@@ -488,14 +497,15 @@ pub struct CpuResources {
/// # Usage:
/// ```
/// let resource = &mut cgroups_rs::Resources::default();
/// resource.cpu.attrs.insert("cpu.cfs_init_buffer_us", "10".to_string());
/// resource.cpu.attrs.insert("cpu.cfs_init_buffer_us".to_string(), "10".to_string());
/// // apply here
/// ```
pub attrs: std::collections::HashMap<&'static str, String>,
pub attrs: HashMap<String, String>,
}
/// A device resource that can be allowed or denied access to.
#[derive(Debug, Clone, Eq, PartialEq, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct DeviceResource {
/// If true, access to the device is allowed, otherwise it's denied.
pub allow: bool,
@@ -511,6 +521,7 @@ pub struct DeviceResource {
/// Limit the usage of devices for the control group's tasks.
#[derive(Debug, Clone, Eq, PartialEq, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct DeviceResources {
/// For each device in the list, the limits in the structure are applied.
pub devices: Vec<DeviceResource>,
@@ -518,6 +529,7 @@ pub struct DeviceResources {
/// Assigned priority for a network device.
#[derive(Debug, Clone, Eq, PartialEq, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct NetworkPriority {
/// The name (as visible in `ifconfig`) of the interface.
pub name: String,
@@ -528,6 +540,7 @@ pub struct NetworkPriority {
/// Collections of limits and tags that can be imposed on packets emitted by the tasks in the
/// control group.
#[derive(Debug, Clone, Eq, PartialEq, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct NetworkResources {
/// The networking class identifier to attach to the packets.
///
@@ -539,6 +552,7 @@ pub struct NetworkResources {
/// A hugepage type and its consumption limit for the control group.
#[derive(Debug, Clone, Eq, PartialEq, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct HugePageResource {
/// The size of the hugepage, i.e. `2MB`, `1GB`, etc.
pub size: String,
@@ -549,6 +563,7 @@ pub struct HugePageResource {
/// Provides the ability to set consumption limit on each type of hugepages.
#[derive(Debug, Clone, Eq, PartialEq, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct HugePageResources {
/// Set a limit of consumption for each hugepages type.
pub limits: Vec<HugePageResource>,
@@ -556,6 +571,7 @@ pub struct HugePageResources {
/// Weight for a particular block device.
#[derive(Debug, Clone, Eq, PartialEq, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct BlkIoDeviceResource {
/// The major number of the device.
pub major: u64,
@@ -569,6 +585,7 @@ pub struct BlkIoDeviceResource {
/// Provides the ability to throttle a device (both byte/sec, and IO op/s)
#[derive(Debug, Clone, Eq, PartialEq, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct BlkIoDeviceThrottleResource {
/// The major number of the device.
pub major: u64,
@@ -580,6 +597,7 @@ pub struct BlkIoDeviceThrottleResource {
/// General block I/O resource limits.
#[derive(Debug, Clone, Eq, PartialEq, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct BlkIoResources {
/// The weight of the control group against descendant nodes.
pub weight: Option<u16>,
@@ -599,6 +617,7 @@ pub struct BlkIoResources {
/// The resource limits and constraints that will be set on the control group.
#[derive(Debug, Clone, Eq, PartialEq, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Resources {
/// Memory usage related limits.
pub memory: MemoryResources,
@@ -724,6 +743,7 @@ impl Subsystem {
/// The values for `memory.hight` or `pids.max`
#[derive(Eq, PartialEq, Copy, Clone, Debug)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum MaxValue {
/// This value is returned when the text is `"max"`.
Max,

View File

@@ -38,6 +38,7 @@ pub struct MemController {
}
#[derive(Default, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct SetMemory {
pub low: Option<MaxValue>,
pub high: Option<MaxValue>,
@@ -47,6 +48,7 @@ pub struct SetMemory {
/// Controls statistics and controls about the OOM killer operating in this control group.
#[derive(Default, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct OomControl {
/// If true, the OOM killer has been disabled for the tasks in this control group.
pub oom_kill_disable: bool,
@@ -87,6 +89,7 @@ fn parse_oom_control(s: String) -> Result<OomControl> {
/// Contains statistics about the NUMA locality of the control group's tasks.
#[derive(Default, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct NumaStat {
/// Total amount of pages used by the control group.
pub total_pages: u64,
@@ -302,6 +305,7 @@ fn parse_numa_stat(s: String) -> Result<NumaStat> {
}
#[derive(Default, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct MemoryStat {
pub cache: u64,
pub rss: u64,
@@ -403,6 +407,7 @@ fn parse_memory_stat(s: String) -> Result<MemoryStat> {
/// Contains statistics about the current usage of memory and swap (together, not seperately) by
/// the control group's tasks.
#[derive(Debug)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct MemSwap {
/// How many times the limit has been hit.
pub fail_cnt: u64,
@@ -417,6 +422,7 @@ pub struct MemSwap {
/// State of and statistics gathered by the kernel about the memory usage of the control group's
/// tasks.
#[derive(Debug)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Memory {
/// How many times the limit has been hit.
pub fail_cnt: u64,
@@ -461,6 +467,7 @@ pub struct Memory {
/// The current state of and gathered statistics about the kernel's memory usage for TCP-related
/// data structures.
#[derive(Debug)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Tcp {
/// How many times the limit has been hit.
pub fail_cnt: u64,
@@ -479,6 +486,7 @@ pub struct Tcp {
/// these tasks if it would think that the limits here would be violated. It's important to note
/// that interrupts in particular might not be able to enforce these limits.
#[derive(Debug)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Kmem {
/// How many times the limit has been hit.
pub fail_cnt: u64,

View File

@@ -51,7 +51,7 @@ fn test_cpuset_set_cpus() {
let cpus = fs::read_to_string("/sys/fs/cgroup/cpuset.cpus.effective").unwrap_or_default();
let cpus = cpus.trim();
if !cpus.is_empty() {
let r = cpuset.set_cpus(&cpus);
let r = cpuset.set_cpus(cpus);
assert!(r.is_ok());
let set = cpuset.cpuset();
assert_eq!(1, set.cpus.len());