mirror of
https://github.com/kata-containers/cgroups-rs.git
synced 2026-08-05 02:13:23 +00:00
Compare commits
38 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
2c68f36488 | ||
|
|
0bc1b9821b | ||
|
|
da75ba25d6 | ||
|
|
01475ad515 | ||
|
|
417badd05f | ||
|
|
1720443d58 | ||
|
|
fb55383273 | ||
|
|
0233c1e046 | ||
|
|
bf5af7b195 | ||
|
|
2e3719dbd5 | ||
|
|
4231f35a44 | ||
|
|
88b7aafd05 | ||
|
|
63750887c2 | ||
|
|
09499e7614 | ||
|
|
bcb7c6cd54 | ||
|
|
2554aa65d0 | ||
|
|
34f935be89 | ||
|
|
5485d8dd46 | ||
|
|
ec4cda1dd9 | ||
|
|
4b5a190ecc | ||
|
|
45b626e0c0 | ||
|
|
0e2430fde1 | ||
|
|
5aa7e6c90e | ||
|
|
5bb27a2692 | ||
|
|
0b2a0405e2 | ||
|
|
c4850ef2ef | ||
|
|
aa207edca8 | ||
|
|
af7a75ce9e | ||
|
|
fb56c817ca | ||
|
|
fdabe52401 | ||
|
|
9ec1010a15 | ||
|
|
610071730d | ||
|
|
0a3e4a828f | ||
|
|
60820bfffb | ||
|
|
2209189244 | ||
|
|
8720aed656 | ||
|
|
16d73e1d59 | ||
|
|
b9ca0a51f1 |
1
.clippy.toml
Normal file
1
.clippy.toml
Normal file
@@ -0,0 +1 @@
|
|||||||
|
upper-case-acronyms-aggressive = true
|
||||||
20
.github/workflows/bvt.yaml
vendored
20
.github/workflows/bvt.yaml
vendored
@@ -1,26 +1,38 @@
|
|||||||
name: BVT
|
name: BVT
|
||||||
on: [pull_request]
|
on: [pull_request]
|
||||||
|
env:
|
||||||
|
RUST_VERSION: 1.52
|
||||||
jobs:
|
jobs:
|
||||||
build:
|
build:
|
||||||
name: Build
|
name: Build
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- name: Checkout
|
- uses: actions/checkout@v2
|
||||||
uses: actions/checkout@v2
|
- run: rustup install ${{ env.RUST_VERSION }} && rustup default ${{ env.RUST_VERSION }}
|
||||||
- name: Build
|
- run: make debug
|
||||||
run: make debug
|
|
||||||
|
|
||||||
fmt:
|
fmt:
|
||||||
name: Format Check
|
name: Format Check
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v2
|
- uses: actions/checkout@v2
|
||||||
|
- run: rustup install ${{ env.RUST_VERSION }} && rustup default ${{ env.RUST_VERSION }}
|
||||||
- run: rustup component add rustfmt
|
- run: rustup component add rustfmt
|
||||||
- run: make fmt
|
- run: make fmt
|
||||||
|
clippy:
|
||||||
|
name: Clippy 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 clippy
|
||||||
|
- run: make clippy
|
||||||
test:
|
test:
|
||||||
name: Run Unit Test
|
name: Run Unit Test
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v2
|
- uses: actions/checkout@v2
|
||||||
|
- run: rustup install ${{ env.RUST_VERSION }} && rustup default ${{ env.RUST_VERSION }}
|
||||||
- run: make test
|
- run: make test
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
2
.github/workflows/commit-message-check.yaml
vendored
2
.github/workflows/commit-message-check.yaml
vendored
@@ -48,6 +48,6 @@ jobs:
|
|||||||
uses: tim-actions/commit-message-checker-with-regex@v0.3.1
|
uses: tim-actions/commit-message-checker-with-regex@v0.3.1
|
||||||
with:
|
with:
|
||||||
commits: ${{ steps.get-pr-commits.outputs.commits }}
|
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)'
|
error: 'Body line too long (max 72)'
|
||||||
post_error: ${{ env.error_msg }}
|
post_error: ${{ env.error_msg }}
|
||||||
|
|||||||
@@ -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.3"
|
version = "0.2.7"
|
||||||
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"
|
||||||
@@ -14,8 +14,12 @@ readme = "README.md"
|
|||||||
[dependencies]
|
[dependencies]
|
||||||
log = "0.4"
|
log = "0.4"
|
||||||
regex = "1.1"
|
regex = "1.1"
|
||||||
nix = "0.18.0"
|
nix = "0.20.2"
|
||||||
libc = "0.2"
|
libc = "0.2"
|
||||||
|
serde = { version = "1.0", features = ["derive"], optional = true }
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
libc = "0.2.76"
|
libc = "0.2.76"
|
||||||
|
|
||||||
|
[features]
|
||||||
|
default = []
|
||||||
|
|||||||
138
src/blkio.rs
138
src/blkio.rs
@@ -31,6 +31,7 @@ pub struct BlkIoController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Eq, PartialEq, Debug)]
|
#[derive(Eq, PartialEq, Debug)]
|
||||||
|
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
|
||||||
/// Per-device information
|
/// Per-device information
|
||||||
pub struct BlkIoData {
|
pub struct BlkIoData {
|
||||||
/// The major number of the device.
|
/// The major number of the device.
|
||||||
@@ -42,6 +43,7 @@ pub struct BlkIoData {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Eq, PartialEq, Debug)]
|
#[derive(Eq, PartialEq, Debug)]
|
||||||
|
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
|
||||||
/// Per-device activity from the control group.
|
/// Per-device activity from the control group.
|
||||||
pub struct IoService {
|
pub struct IoService {
|
||||||
/// The major number of the device.
|
/// The major number of the device.
|
||||||
@@ -61,6 +63,7 @@ pub struct IoService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Eq, PartialEq, Debug)]
|
#[derive(Eq, PartialEq, Debug)]
|
||||||
|
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
|
||||||
/// Per-device activity from the control group.
|
/// Per-device activity from the control group.
|
||||||
/// Only for cgroup v2
|
/// Only for cgroup v2
|
||||||
pub struct IoStat {
|
pub struct IoStat {
|
||||||
@@ -84,14 +87,14 @@ pub struct IoStat {
|
|||||||
|
|
||||||
fn parse_io_service(s: String) -> Result<Vec<IoService>> {
|
fn parse_io_service(s: String) -> Result<Vec<IoService>> {
|
||||||
s.lines()
|
s.lines()
|
||||||
.filter(|x| x.split_whitespace().collect::<Vec<_>>().len() == 3)
|
.filter(|x| x.split_whitespace().count() == 3)
|
||||||
.map(|x| {
|
.map(|x| {
|
||||||
let mut spl = x.split_whitespace();
|
let mut spl = x.split_whitespace();
|
||||||
(spl.nth(0).unwrap(), spl.nth(0).unwrap(), spl.nth(0).unwrap())
|
(spl.next().unwrap(), spl.next().unwrap(), spl.next().unwrap())
|
||||||
})
|
})
|
||||||
.map(|(a, b, c)| {
|
.map(|(a, b, c)| {
|
||||||
let mut spl = a.split(":");
|
let mut spl = a.split(':');
|
||||||
(spl.nth(0).unwrap(), spl.nth(0).unwrap(), b, c)
|
(spl.next().unwrap(), spl.next().unwrap(), b, c)
|
||||||
})
|
})
|
||||||
.collect::<Vec<_>>()
|
.collect::<Vec<_>>()
|
||||||
.chunks(5)
|
.chunks(5)
|
||||||
@@ -131,15 +134,14 @@ fn get_value(s: &str) -> String {
|
|||||||
arr[1].to_string()
|
arr[1].to_string()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn parse_io_stat(s: String) -> Result<Vec<IoStat>> {
|
fn parse_io_stat(s: String) -> Vec<IoStat> {
|
||||||
// line:
|
// line:
|
||||||
// 8:0 rbytes=180224 wbytes=0 rios=3 wios=0 dbytes=0 dios=0
|
// 8:0 rbytes=180224 wbytes=0 rios=3 wios=0 dbytes=0 dios=0
|
||||||
let v = s
|
s.lines()
|
||||||
.lines()
|
.filter(|x| x.split_whitespace().count() == 7)
|
||||||
.filter(|x| x.split_whitespace().collect::<Vec<_>>().len() == 7)
|
|
||||||
.map(|x| {
|
.map(|x| {
|
||||||
let arr = x.split_whitespace().collect::<Vec<&str>>();
|
let arr = x.split_whitespace().collect::<Vec<&str>>();
|
||||||
let device = arr[0].split(":").collect::<Vec<&str>>();
|
let device = arr[0].split(':').collect::<Vec<&str>>();
|
||||||
let (major, minor) = (device[0], device[1]);
|
let (major, minor) = (device[0], device[1]);
|
||||||
|
|
||||||
IoStat {
|
IoStat {
|
||||||
@@ -153,14 +155,12 @@ fn parse_io_stat(s: String) -> Result<Vec<IoStat>> {
|
|||||||
dios: get_value(arr[6]).parse::<u64>().unwrap(),
|
dios: get_value(arr[6]).parse::<u64>().unwrap(),
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.collect::<Vec<IoStat>>();
|
.collect::<Vec<IoStat>>()
|
||||||
|
|
||||||
Ok(v)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn parse_io_service_total(s: String) -> Result<u64> {
|
fn parse_io_service_total(s: String) -> Result<u64> {
|
||||||
s.lines()
|
s.lines()
|
||||||
.filter(|x| x.split_whitespace().collect::<Vec<_>>().len() == 2)
|
.filter(|x| x.split_whitespace().count() == 2)
|
||||||
.fold(Err(Error::new(ParseError)), |_, x| {
|
.fold(Err(Error::new(ParseError)), |_, x| {
|
||||||
match x.split_whitespace().collect::<Vec<_>>().as_slice() {
|
match x.split_whitespace().collect::<Vec<_>>().as_slice() {
|
||||||
["Total", val] => val.parse::<u64>().map_err(|_| Error::new(ParseError)),
|
["Total", val] => val.parse::<u64>().map_err(|_| Error::new(ParseError)),
|
||||||
@@ -197,15 +197,16 @@ fn parse_blkio_data(s: String) -> Result<Vec<BlkIoData>> {
|
|||||||
});
|
});
|
||||||
|
|
||||||
if err.is_err() {
|
if err.is_err() {
|
||||||
return Err(Error::new(ParseError));
|
Err(Error::new(ParseError))
|
||||||
} else {
|
} else {
|
||||||
return Ok(res);
|
Ok(res)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Current state and statistics about how throttled are the block devices when accessed from the
|
/// Current state and statistics about how throttled are the block devices when accessed from the
|
||||||
/// controller's control group.
|
/// controller's control group.
|
||||||
#[derive(Default, Debug)]
|
#[derive(Default, Debug)]
|
||||||
|
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
|
||||||
pub struct BlkIoThrottle {
|
pub struct BlkIoThrottle {
|
||||||
/// Statistics about the bytes transferred between the block devices by the tasks in this
|
/// Statistics about the bytes transferred between the block devices by the tasks in this
|
||||||
/// control group.
|
/// control group.
|
||||||
@@ -241,6 +242,7 @@ pub struct BlkIoThrottle {
|
|||||||
|
|
||||||
/// Statistics and state of the block devices.
|
/// Statistics and state of the block devices.
|
||||||
#[derive(Default, Debug)]
|
#[derive(Default, Debug)]
|
||||||
|
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
|
||||||
pub struct BlkIo {
|
pub struct BlkIo {
|
||||||
/// The number of BIOS requests merged into I/O requests by the control group's tasks.
|
/// The number of BIOS requests merged into I/O requests by the control group's tasks.
|
||||||
pub io_merged: Vec<IoService>,
|
pub io_merged: Vec<IoService>,
|
||||||
@@ -407,19 +409,19 @@ impl BlkIoController {
|
|||||||
Self {
|
Self {
|
||||||
base: root.clone(),
|
base: root.clone(),
|
||||||
path: root,
|
path: root,
|
||||||
v2: v2,
|
v2,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn blkio_v2(&self) -> BlkIo {
|
fn blkio_v2(&self) -> BlkIo {
|
||||||
let mut blkio: BlkIo = Default::default();
|
BlkIo {
|
||||||
blkio.io_stat = self
|
io_stat: self
|
||||||
.open_path("io.stat", false)
|
.open_path("io.stat", false)
|
||||||
.and_then(read_string_from)
|
.and_then(read_string_from)
|
||||||
.and_then(parse_io_stat)
|
.map(parse_io_stat)
|
||||||
.unwrap_or(Vec::new());
|
.unwrap_or_default(),
|
||||||
|
..Default::default()
|
||||||
blkio
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Gathers statistics about and reports the state of the block devices used by the control
|
/// Gathers statistics about and reports the state of the block devices used by the control
|
||||||
@@ -433,222 +435,222 @@ impl BlkIoController {
|
|||||||
.open_path("blkio.io_merged", false)
|
.open_path("blkio.io_merged", false)
|
||||||
.and_then(read_string_from)
|
.and_then(read_string_from)
|
||||||
.and_then(parse_io_service)
|
.and_then(parse_io_service)
|
||||||
.unwrap_or(Vec::new()),
|
.unwrap_or_default(),
|
||||||
io_merged_total: self
|
io_merged_total: self
|
||||||
.open_path("blkio.io_merged", false)
|
.open_path("blkio.io_merged", false)
|
||||||
.and_then(read_string_from)
|
.and_then(read_string_from)
|
||||||
.and_then(parse_io_service_total)
|
.and_then(parse_io_service_total)
|
||||||
.unwrap_or(0),
|
.unwrap_or_default(),
|
||||||
io_merged_recursive: self
|
io_merged_recursive: self
|
||||||
.open_path("blkio.io_merged_recursive", false)
|
.open_path("blkio.io_merged_recursive", false)
|
||||||
.and_then(read_string_from)
|
.and_then(read_string_from)
|
||||||
.and_then(parse_io_service)
|
.and_then(parse_io_service)
|
||||||
.unwrap_or(Vec::new()),
|
.unwrap_or_default(),
|
||||||
io_merged_recursive_total: self
|
io_merged_recursive_total: self
|
||||||
.open_path("blkio.io_merged_recursive", false)
|
.open_path("blkio.io_merged_recursive", false)
|
||||||
.and_then(read_string_from)
|
.and_then(read_string_from)
|
||||||
.and_then(parse_io_service_total)
|
.and_then(parse_io_service_total)
|
||||||
.unwrap_or(0),
|
.unwrap_or_default(),
|
||||||
io_queued: self
|
io_queued: self
|
||||||
.open_path("blkio.io_queued", false)
|
.open_path("blkio.io_queued", false)
|
||||||
.and_then(read_string_from)
|
.and_then(read_string_from)
|
||||||
.and_then(parse_io_service)
|
.and_then(parse_io_service)
|
||||||
.unwrap_or(Vec::new()),
|
.unwrap_or_default(),
|
||||||
io_queued_total: self
|
io_queued_total: self
|
||||||
.open_path("blkio.io_queued", false)
|
.open_path("blkio.io_queued", false)
|
||||||
.and_then(read_string_from)
|
.and_then(read_string_from)
|
||||||
.and_then(parse_io_service_total)
|
.and_then(parse_io_service_total)
|
||||||
.unwrap_or(0),
|
.unwrap_or_default(),
|
||||||
io_queued_recursive: self
|
io_queued_recursive: self
|
||||||
.open_path("blkio.io_queued_recursive", false)
|
.open_path("blkio.io_queued_recursive", false)
|
||||||
.and_then(read_string_from)
|
.and_then(read_string_from)
|
||||||
.and_then(parse_io_service)
|
.and_then(parse_io_service)
|
||||||
.unwrap_or(Vec::new()),
|
.unwrap_or_default(),
|
||||||
io_queued_recursive_total: self
|
io_queued_recursive_total: self
|
||||||
.open_path("blkio.io_queued_recursive", false)
|
.open_path("blkio.io_queued_recursive", false)
|
||||||
.and_then(read_string_from)
|
.and_then(read_string_from)
|
||||||
.and_then(parse_io_service_total)
|
.and_then(parse_io_service_total)
|
||||||
.unwrap_or(0),
|
.unwrap_or_default(),
|
||||||
io_service_bytes: self
|
io_service_bytes: self
|
||||||
.open_path("blkio.io_service_bytes", false)
|
.open_path("blkio.io_service_bytes", false)
|
||||||
.and_then(read_string_from)
|
.and_then(read_string_from)
|
||||||
.and_then(parse_io_service)
|
.and_then(parse_io_service)
|
||||||
.unwrap_or(Vec::new()),
|
.unwrap_or_default(),
|
||||||
io_service_bytes_total: self
|
io_service_bytes_total: self
|
||||||
.open_path("blkio.io_service_bytes", false)
|
.open_path("blkio.io_service_bytes", false)
|
||||||
.and_then(read_string_from)
|
.and_then(read_string_from)
|
||||||
.and_then(parse_io_service_total)
|
.and_then(parse_io_service_total)
|
||||||
.unwrap_or(0),
|
.unwrap_or_default(),
|
||||||
io_service_bytes_recursive: self
|
io_service_bytes_recursive: self
|
||||||
.open_path("blkio.io_service_bytes_recursive", false)
|
.open_path("blkio.io_service_bytes_recursive", false)
|
||||||
.and_then(read_string_from)
|
.and_then(read_string_from)
|
||||||
.and_then(parse_io_service)
|
.and_then(parse_io_service)
|
||||||
.unwrap_or(Vec::new()),
|
.unwrap_or_default(),
|
||||||
io_service_bytes_recursive_total: self
|
io_service_bytes_recursive_total: self
|
||||||
.open_path("blkio.io_service_bytes_recursive", false)
|
.open_path("blkio.io_service_bytes_recursive", false)
|
||||||
.and_then(read_string_from)
|
.and_then(read_string_from)
|
||||||
.and_then(parse_io_service_total)
|
.and_then(parse_io_service_total)
|
||||||
.unwrap_or(0),
|
.unwrap_or_default(),
|
||||||
io_serviced: self
|
io_serviced: self
|
||||||
.open_path("blkio.io_serviced", false)
|
.open_path("blkio.io_serviced", false)
|
||||||
.and_then(read_string_from)
|
.and_then(read_string_from)
|
||||||
.and_then(parse_io_service)
|
.and_then(parse_io_service)
|
||||||
.unwrap_or(Vec::new()),
|
.unwrap_or_default(),
|
||||||
io_serviced_total: self
|
io_serviced_total: self
|
||||||
.open_path("blkio.io_serviced", false)
|
.open_path("blkio.io_serviced", false)
|
||||||
.and_then(read_string_from)
|
.and_then(read_string_from)
|
||||||
.and_then(parse_io_service_total)
|
.and_then(parse_io_service_total)
|
||||||
.unwrap_or(0),
|
.unwrap_or_default(),
|
||||||
io_serviced_recursive: self
|
io_serviced_recursive: self
|
||||||
.open_path("blkio.io_serviced_recursive", false)
|
.open_path("blkio.io_serviced_recursive", false)
|
||||||
.and_then(read_string_from)
|
.and_then(read_string_from)
|
||||||
.and_then(parse_io_service)
|
.and_then(parse_io_service)
|
||||||
.unwrap_or(Vec::new()),
|
.unwrap_or_default(),
|
||||||
io_serviced_recursive_total: self
|
io_serviced_recursive_total: self
|
||||||
.open_path("blkio.io_serviced_recursive", false)
|
.open_path("blkio.io_serviced_recursive", false)
|
||||||
.and_then(read_string_from)
|
.and_then(read_string_from)
|
||||||
.and_then(parse_io_service_total)
|
.and_then(parse_io_service_total)
|
||||||
.unwrap_or(0),
|
.unwrap_or_default(),
|
||||||
io_service_time: self
|
io_service_time: self
|
||||||
.open_path("blkio.io_service_time", false)
|
.open_path("blkio.io_service_time", false)
|
||||||
.and_then(read_string_from)
|
.and_then(read_string_from)
|
||||||
.and_then(parse_io_service)
|
.and_then(parse_io_service)
|
||||||
.unwrap_or(Vec::new()),
|
.unwrap_or_default(),
|
||||||
io_service_time_total: self
|
io_service_time_total: self
|
||||||
.open_path("blkio.io_service_time", false)
|
.open_path("blkio.io_service_time", false)
|
||||||
.and_then(read_string_from)
|
.and_then(read_string_from)
|
||||||
.and_then(parse_io_service_total)
|
.and_then(parse_io_service_total)
|
||||||
.unwrap_or(0),
|
.unwrap_or_default(),
|
||||||
io_service_time_recursive: self
|
io_service_time_recursive: self
|
||||||
.open_path("blkio.io_service_time_recursive", false)
|
.open_path("blkio.io_service_time_recursive", false)
|
||||||
.and_then(read_string_from)
|
.and_then(read_string_from)
|
||||||
.and_then(parse_io_service)
|
.and_then(parse_io_service)
|
||||||
.unwrap_or(Vec::new()),
|
.unwrap_or_default(),
|
||||||
io_service_time_recursive_total: self
|
io_service_time_recursive_total: self
|
||||||
.open_path("blkio.io_service_time_recursive", false)
|
.open_path("blkio.io_service_time_recursive", false)
|
||||||
.and_then(read_string_from)
|
.and_then(read_string_from)
|
||||||
.and_then(parse_io_service_total)
|
.and_then(parse_io_service_total)
|
||||||
.unwrap_or(0),
|
.unwrap_or_default(),
|
||||||
io_wait_time: self
|
io_wait_time: self
|
||||||
.open_path("blkio.io_wait_time", false)
|
.open_path("blkio.io_wait_time", false)
|
||||||
.and_then(read_string_from)
|
.and_then(read_string_from)
|
||||||
.and_then(parse_io_service)
|
.and_then(parse_io_service)
|
||||||
.unwrap_or(Vec::new()),
|
.unwrap_or_default(),
|
||||||
io_wait_time_total: self
|
io_wait_time_total: self
|
||||||
.open_path("blkio.io_wait_time", false)
|
.open_path("blkio.io_wait_time", false)
|
||||||
.and_then(read_string_from)
|
.and_then(read_string_from)
|
||||||
.and_then(parse_io_service_total)
|
.and_then(parse_io_service_total)
|
||||||
.unwrap_or(0),
|
.unwrap_or_default(),
|
||||||
io_wait_time_recursive: self
|
io_wait_time_recursive: self
|
||||||
.open_path("blkio.io_wait_time_recursive", false)
|
.open_path("blkio.io_wait_time_recursive", false)
|
||||||
.and_then(read_string_from)
|
.and_then(read_string_from)
|
||||||
.and_then(parse_io_service)
|
.and_then(parse_io_service)
|
||||||
.unwrap_or(Vec::new()),
|
.unwrap_or_default(),
|
||||||
io_wait_time_recursive_total: self
|
io_wait_time_recursive_total: self
|
||||||
.open_path("blkio.io_wait_time_recursive", false)
|
.open_path("blkio.io_wait_time_recursive", false)
|
||||||
.and_then(read_string_from)
|
.and_then(read_string_from)
|
||||||
.and_then(parse_io_service_total)
|
.and_then(parse_io_service_total)
|
||||||
.unwrap_or(0),
|
.unwrap_or_default(),
|
||||||
leaf_weight: self
|
leaf_weight: self
|
||||||
.open_path("blkio.leaf_weight", false)
|
.open_path("blkio.leaf_weight", false)
|
||||||
.and_then(|file| read_u64_from(file))
|
.and_then(read_u64_from)
|
||||||
.unwrap_or(0u64),
|
.unwrap_or(0u64),
|
||||||
leaf_weight_device: self
|
leaf_weight_device: self
|
||||||
.open_path("blkio.leaf_weight_device", false)
|
.open_path("blkio.leaf_weight_device", false)
|
||||||
.and_then(read_string_from)
|
.and_then(read_string_from)
|
||||||
.and_then(parse_blkio_data)
|
.and_then(parse_blkio_data)
|
||||||
.unwrap_or(Vec::new()),
|
.unwrap_or_default(),
|
||||||
sectors: self
|
sectors: self
|
||||||
.open_path("blkio.sectors", false)
|
.open_path("blkio.sectors", false)
|
||||||
.and_then(read_string_from)
|
.and_then(read_string_from)
|
||||||
.and_then(parse_blkio_data)
|
.and_then(parse_blkio_data)
|
||||||
.unwrap_or(Vec::new()),
|
.unwrap_or_default(),
|
||||||
sectors_recursive: self
|
sectors_recursive: self
|
||||||
.open_path("blkio.sectors_recursive", false)
|
.open_path("blkio.sectors_recursive", false)
|
||||||
.and_then(read_string_from)
|
.and_then(read_string_from)
|
||||||
.and_then(parse_blkio_data)
|
.and_then(parse_blkio_data)
|
||||||
.unwrap_or(Vec::new()),
|
.unwrap_or_default(),
|
||||||
throttle: BlkIoThrottle {
|
throttle: BlkIoThrottle {
|
||||||
io_service_bytes: self
|
io_service_bytes: self
|
||||||
.open_path("blkio.throttle.io_service_bytes", false)
|
.open_path("blkio.throttle.io_service_bytes", false)
|
||||||
.and_then(read_string_from)
|
.and_then(read_string_from)
|
||||||
.and_then(parse_io_service)
|
.and_then(parse_io_service)
|
||||||
.unwrap_or(Vec::new()),
|
.unwrap_or_default(),
|
||||||
io_service_bytes_total: self
|
io_service_bytes_total: self
|
||||||
.open_path("blkio.throttle.io_service_bytes", false)
|
.open_path("blkio.throttle.io_service_bytes", false)
|
||||||
.and_then(read_string_from)
|
.and_then(read_string_from)
|
||||||
.and_then(parse_io_service_total)
|
.and_then(parse_io_service_total)
|
||||||
.unwrap_or(0),
|
.unwrap_or_default(),
|
||||||
io_service_bytes_recursive: self
|
io_service_bytes_recursive: self
|
||||||
.open_path("blkio.throttle.io_service_bytes_recursive", false)
|
.open_path("blkio.throttle.io_service_bytes_recursive", false)
|
||||||
.and_then(read_string_from)
|
.and_then(read_string_from)
|
||||||
.and_then(parse_io_service)
|
.and_then(parse_io_service)
|
||||||
.unwrap_or(Vec::new()),
|
.unwrap_or_default(),
|
||||||
io_service_bytes_recursive_total: self
|
io_service_bytes_recursive_total: self
|
||||||
.open_path("blkio.throttle.io_service_bytes_recursive", false)
|
.open_path("blkio.throttle.io_service_bytes_recursive", false)
|
||||||
.and_then(read_string_from)
|
.and_then(read_string_from)
|
||||||
.and_then(parse_io_service_total)
|
.and_then(parse_io_service_total)
|
||||||
.unwrap_or(0),
|
.unwrap_or_default(),
|
||||||
io_serviced: self
|
io_serviced: self
|
||||||
.open_path("blkio.throttle.io_serviced", false)
|
.open_path("blkio.throttle.io_serviced", false)
|
||||||
.and_then(read_string_from)
|
.and_then(read_string_from)
|
||||||
.and_then(parse_io_service)
|
.and_then(parse_io_service)
|
||||||
.unwrap_or(Vec::new()),
|
.unwrap_or_default(),
|
||||||
io_serviced_total: self
|
io_serviced_total: self
|
||||||
.open_path("blkio.throttle.io_serviced", false)
|
.open_path("blkio.throttle.io_serviced", false)
|
||||||
.and_then(read_string_from)
|
.and_then(read_string_from)
|
||||||
.and_then(parse_io_service_total)
|
.and_then(parse_io_service_total)
|
||||||
.unwrap_or(0),
|
.unwrap_or_default(),
|
||||||
io_serviced_recursive: self
|
io_serviced_recursive: self
|
||||||
.open_path("blkio.throttle.io_serviced_recursive", false)
|
.open_path("blkio.throttle.io_serviced_recursive", false)
|
||||||
.and_then(read_string_from)
|
.and_then(read_string_from)
|
||||||
.and_then(parse_io_service)
|
.and_then(parse_io_service)
|
||||||
.unwrap_or(Vec::new()),
|
.unwrap_or_default(),
|
||||||
io_serviced_recursive_total: self
|
io_serviced_recursive_total: self
|
||||||
.open_path("blkio.throttle.io_serviced_recursive", false)
|
.open_path("blkio.throttle.io_serviced_recursive", false)
|
||||||
.and_then(read_string_from)
|
.and_then(read_string_from)
|
||||||
.and_then(parse_io_service_total)
|
.and_then(parse_io_service_total)
|
||||||
.unwrap_or(0),
|
.unwrap_or_default(),
|
||||||
read_bps_device: self
|
read_bps_device: self
|
||||||
.open_path("blkio.throttle.read_bps_device", false)
|
.open_path("blkio.throttle.read_bps_device", false)
|
||||||
.and_then(read_string_from)
|
.and_then(read_string_from)
|
||||||
.and_then(parse_blkio_data)
|
.and_then(parse_blkio_data)
|
||||||
.unwrap_or(Vec::new()),
|
.unwrap_or_default(),
|
||||||
read_iops_device: self
|
read_iops_device: self
|
||||||
.open_path("blkio.throttle.read_iops_device", false)
|
.open_path("blkio.throttle.read_iops_device", false)
|
||||||
.and_then(read_string_from)
|
.and_then(read_string_from)
|
||||||
.and_then(parse_blkio_data)
|
.and_then(parse_blkio_data)
|
||||||
.unwrap_or(Vec::new()),
|
.unwrap_or_default(),
|
||||||
write_bps_device: self
|
write_bps_device: self
|
||||||
.open_path("blkio.throttle.write_bps_device", false)
|
.open_path("blkio.throttle.write_bps_device", false)
|
||||||
.and_then(read_string_from)
|
.and_then(read_string_from)
|
||||||
.and_then(parse_blkio_data)
|
.and_then(parse_blkio_data)
|
||||||
.unwrap_or(Vec::new()),
|
.unwrap_or_default(),
|
||||||
write_iops_device: self
|
write_iops_device: self
|
||||||
.open_path("blkio.throttle.write_iops_device", false)
|
.open_path("blkio.throttle.write_iops_device", false)
|
||||||
.and_then(read_string_from)
|
.and_then(read_string_from)
|
||||||
.and_then(parse_blkio_data)
|
.and_then(parse_blkio_data)
|
||||||
.unwrap_or(Vec::new()),
|
.unwrap_or_default(),
|
||||||
},
|
},
|
||||||
time: self
|
time: self
|
||||||
.open_path("blkio.time", false)
|
.open_path("blkio.time", false)
|
||||||
.and_then(read_string_from)
|
.and_then(read_string_from)
|
||||||
.and_then(parse_blkio_data)
|
.and_then(parse_blkio_data)
|
||||||
.unwrap_or(Vec::new()),
|
.unwrap_or_default(),
|
||||||
time_recursive: self
|
time_recursive: self
|
||||||
.open_path("blkio.time_recursive", false)
|
.open_path("blkio.time_recursive", false)
|
||||||
.and_then(read_string_from)
|
.and_then(read_string_from)
|
||||||
.and_then(parse_blkio_data)
|
.and_then(parse_blkio_data)
|
||||||
.unwrap_or(Vec::new()),
|
.unwrap_or_default(),
|
||||||
weight: self
|
weight: self
|
||||||
.open_path("blkio.weight", false)
|
.open_path("blkio.weight", false)
|
||||||
.and_then(|file| read_u64_from(file))
|
.and_then(read_u64_from)
|
||||||
.unwrap_or(0u64),
|
.unwrap_or(0u64),
|
||||||
weight_device: self
|
weight_device: self
|
||||||
.open_path("blkio.weight_device", false)
|
.open_path("blkio.weight_device", false)
|
||||||
.and_then(read_string_from)
|
.and_then(read_string_from)
|
||||||
.and_then(parse_blkio_data)
|
.and_then(parse_blkio_data)
|
||||||
.unwrap_or(Vec::new()),
|
.unwrap_or_default(),
|
||||||
io_stat: Vec::new(),
|
io_stat: Vec::new(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -62,7 +62,7 @@ impl Cgroup {
|
|||||||
/// Create this control group.
|
/// Create this control group.
|
||||||
fn create(&self) {
|
fn create(&self) {
|
||||||
if self.hier.v2() {
|
if self.hier.v2() {
|
||||||
let _ret = create_v2_cgroup(self.hier.root().clone(), &self.path);
|
let _ret = create_v2_cgroup(self.hier.root(), &self.path);
|
||||||
} else {
|
} else {
|
||||||
for subsystem in &self.subsystems {
|
for subsystem in &self.subsystems {
|
||||||
subsystem.to_controller().create();
|
subsystem.to_controller().create();
|
||||||
@@ -112,13 +112,11 @@ impl Cgroup {
|
|||||||
.collect::<Vec<_>>();
|
.collect::<Vec<_>>();
|
||||||
}
|
}
|
||||||
|
|
||||||
let cg = Cgroup {
|
Cgroup {
|
||||||
path: path.to_str().unwrap().to_string(),
|
path: path.to_str().unwrap().to_string(),
|
||||||
subsystems: subsystems,
|
subsystems,
|
||||||
hier,
|
hier,
|
||||||
};
|
}
|
||||||
|
|
||||||
cg
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Create a handle for a control group in the hierarchy `hier`, with name `path` and `relative_paths`
|
/// Create a handle for a control group in the hierarchy `hier`, with name `path` and `relative_paths`
|
||||||
@@ -146,7 +144,7 @@ impl Cgroup {
|
|||||||
let cn = x.controller_name();
|
let cn = x.controller_name();
|
||||||
if relative_paths.contains_key(&cn) {
|
if relative_paths.contains_key(&cn) {
|
||||||
let rp = relative_paths.get(&cn).unwrap();
|
let rp = relative_paths.get(&cn).unwrap();
|
||||||
let valid_path = rp.trim_start_matches("/").to_string();
|
let valid_path = rp.trim_start_matches('/').to_string();
|
||||||
let mut p = PathBuf::from(valid_path);
|
let mut p = PathBuf::from(valid_path);
|
||||||
p.push(path);
|
p.push(path);
|
||||||
x.enter(p.as_ref())
|
x.enter(p.as_ref())
|
||||||
@@ -157,13 +155,11 @@ impl Cgroup {
|
|||||||
.collect::<Vec<_>>();
|
.collect::<Vec<_>>();
|
||||||
}
|
}
|
||||||
|
|
||||||
let cg = Cgroup {
|
Cgroup {
|
||||||
subsystems: subsystems,
|
subsystems,
|
||||||
hier,
|
hier,
|
||||||
path: path.to_str().unwrap().to_string(),
|
path: path.to_str().unwrap().to_string(),
|
||||||
};
|
}
|
||||||
|
|
||||||
cg
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// The list of subsystems that this control group supports.
|
/// The list of subsystems that this control group supports.
|
||||||
@@ -179,8 +175,8 @@ impl Cgroup {
|
|||||||
/// will change.
|
/// will change.
|
||||||
pub fn delete(&self) -> Result<()> {
|
pub fn delete(&self) -> Result<()> {
|
||||||
if self.v2() {
|
if self.v2() {
|
||||||
if self.path != "" {
|
if !self.path.is_empty() {
|
||||||
let mut p = self.hier.root().clone();
|
let mut p = self.hier.root();
|
||||||
p.push(self.path.clone());
|
p.push(self.path.clone());
|
||||||
return fs::remove_dir(p).map_err(|e| Error::with_cause(RemoveFailed, e));
|
return fs::remove_dir(p).map_err(|e| Error::with_cause(RemoveFailed, e));
|
||||||
}
|
}
|
||||||
@@ -222,7 +218,7 @@ impl Cgroup {
|
|||||||
/// let cpu: &CpuController = control_group.controller_of()
|
/// let cpu: &CpuController = control_group.controller_of()
|
||||||
/// .expect("No cpu controller attached!");
|
/// .expect("No cpu controller attached!");
|
||||||
/// ```
|
/// ```
|
||||||
pub fn controller_of<'a, T>(self: &'a Self) -> Option<&'a T>
|
pub fn controller_of<'a, T>(&'a self) -> Option<&'a T>
|
||||||
where
|
where
|
||||||
&'a T: From<&'a Subsystem>,
|
&'a T: From<&'a Subsystem>,
|
||||||
T: Controller + ControllIdentifier,
|
T: Controller + ControllIdentifier,
|
||||||
@@ -249,7 +245,7 @@ impl Cgroup {
|
|||||||
pub fn add_task(&self, pid: CgroupPid) -> Result<()> {
|
pub fn add_task(&self, pid: CgroupPid) -> Result<()> {
|
||||||
if self.v2() {
|
if self.v2() {
|
||||||
let subsystems = self.subsystems();
|
let subsystems = self.subsystems();
|
||||||
if subsystems.len() > 0 {
|
if !subsystems.is_empty() {
|
||||||
let c = subsystems[0].to_controller();
|
let c = subsystems[0].to_controller();
|
||||||
c.add_task(&pid)
|
c.add_task(&pid)
|
||||||
} else {
|
} else {
|
||||||
@@ -291,7 +287,7 @@ impl Cgroup {
|
|||||||
// Collect the tasks from all subsystems
|
// Collect the tasks from all subsystems
|
||||||
let mut v = if self.v2() {
|
let mut v = if self.v2() {
|
||||||
let subsystems = self.subsystems();
|
let subsystems = self.subsystems();
|
||||||
if subsystems.len() > 0 {
|
if !subsystems.is_empty() {
|
||||||
let c = subsystems[0].to_controller();
|
let c = subsystems[0].to_controller();
|
||||||
c.tasks()
|
c.tasks()
|
||||||
} else {
|
} else {
|
||||||
@@ -313,11 +309,10 @@ impl Cgroup {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub const UNIFIED_MOUNTPOINT: &'static str = "/sys/fs/cgroup";
|
pub const UNIFIED_MOUNTPOINT: &str = "/sys/fs/cgroup";
|
||||||
|
|
||||||
fn enable_controllers(controllers: &Vec<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());
|
||||||
@@ -327,8 +322,8 @@ fn enable_controllers(controllers: &Vec<String>, path: &PathBuf) {
|
|||||||
fn supported_controllers() -> Vec<String> {
|
fn supported_controllers() -> Vec<String> {
|
||||||
let p = format!("{}/{}", UNIFIED_MOUNTPOINT, "cgroup.controllers");
|
let p = format!("{}/{}", UNIFIED_MOUNTPOINT, "cgroup.controllers");
|
||||||
let ret = fs::read_to_string(p.as_str());
|
let ret = fs::read_to_string(p.as_str());
|
||||||
ret.unwrap_or(String::new())
|
ret.unwrap_or_default()
|
||||||
.split(" ")
|
.split(' ')
|
||||||
.map(|x| x.to_string())
|
.map(|x| x.to_string())
|
||||||
.collect::<Vec<String>>()
|
.collect::<Vec<String>>()
|
||||||
}
|
}
|
||||||
@@ -342,16 +337,15 @@ fn create_v2_cgroup(root: PathBuf, path: &str) -> Result<()> {
|
|||||||
enable_controllers(&controllers, &fp);
|
enable_controllers(&controllers, &fp);
|
||||||
|
|
||||||
// path: "a/b/c"
|
// path: "a/b/c"
|
||||||
let elements = path.split("/").collect::<Vec<&str>>();
|
let elements = path.split('/').collect::<Vec<&str>>();
|
||||||
let last_index = elements.len() - 1;
|
let last_index = elements.len() - 1;
|
||||||
for (i, ele) in elements.iter().enumerate() {
|
for (i, ele) in elements.iter().enumerate() {
|
||||||
// ROOT/a
|
// ROOT/a
|
||||||
fp.push(ele);
|
fp.push(ele);
|
||||||
// create dir, need not check if is a file or directory
|
// create dir, need not check if is a file or directory
|
||||||
if !fp.exists() {
|
if !fp.exists() {
|
||||||
match ::std::fs::create_dir(fp.clone()) {
|
if let Err(e) = std::fs::create_dir(fp.clone()) {
|
||||||
Err(e) => return Err(Error::with_cause(ErrorKind::FsError, e)),
|
return Err(Error::with_cause(ErrorKind::FsError, e));
|
||||||
Ok(_) => {}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -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
|
||||||
|
|||||||
14
src/cpu.rs
14
src/cpu.rs
@@ -36,6 +36,7 @@ pub struct CpuController {
|
|||||||
|
|
||||||
/// The current state of the control group and its processes.
|
/// The current state of the control group and its processes.
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
|
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
|
||||||
pub struct Cpu {
|
pub struct Cpu {
|
||||||
/// Reports CPU time statistics.
|
/// Reports CPU time statistics.
|
||||||
///
|
///
|
||||||
@@ -45,7 +46,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,
|
||||||
}
|
}
|
||||||
@@ -116,7 +117,7 @@ impl CpuController {
|
|||||||
Self {
|
Self {
|
||||||
base: root.clone(),
|
base: root.clone(),
|
||||||
path: root,
|
path: root,
|
||||||
v2: v2,
|
v2,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -133,7 +134,7 @@ impl CpuController {
|
|||||||
Err(e) => Err(Error::with_cause(ReadFailed, e)),
|
Err(e) => Err(Error::with_cause(ReadFailed, e)),
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.unwrap_or("".to_string()),
|
.unwrap_or_default(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -284,7 +285,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,8 +300,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 {
|
Ok(CfsQuotaAndPeriod { quota, period })
|
||||||
quota: quota,
|
|
||||||
period: period,
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -27,6 +27,7 @@ pub struct CpuAcctController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Represents the statistics retrieved from the control group.
|
/// Represents the statistics retrieved from the control group.
|
||||||
|
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
|
||||||
pub struct CpuAcct {
|
pub struct CpuAcct {
|
||||||
/// Divides the time used by the tasks into `user` time and `system` time.
|
/// Divides the time used by the tasks into `user` time and `system` time.
|
||||||
pub stat: String,
|
pub stat: String,
|
||||||
@@ -111,35 +112,35 @@ impl CpuAcctController {
|
|||||||
CpuAcct {
|
CpuAcct {
|
||||||
stat: self
|
stat: self
|
||||||
.open_path("cpuacct.stat", false)
|
.open_path("cpuacct.stat", false)
|
||||||
.and_then(|file| read_string_from(file))
|
.and_then(read_string_from)
|
||||||
.unwrap_or("".to_string()),
|
.unwrap_or_default(),
|
||||||
usage: self
|
usage: self
|
||||||
.open_path("cpuacct.usage", false)
|
.open_path("cpuacct.usage", false)
|
||||||
.and_then(|file| read_u64_from(file))
|
.and_then(read_u64_from)
|
||||||
.unwrap_or(0),
|
.unwrap_or(0),
|
||||||
usage_all: self
|
usage_all: self
|
||||||
.open_path("cpuacct.usage_all", false)
|
.open_path("cpuacct.usage_all", false)
|
||||||
.and_then(|file| read_string_from(file))
|
.and_then(read_string_from)
|
||||||
.unwrap_or("".to_string()),
|
.unwrap_or_default(),
|
||||||
usage_percpu: self
|
usage_percpu: self
|
||||||
.open_path("cpuacct.usage_percpu", false)
|
.open_path("cpuacct.usage_percpu", false)
|
||||||
.and_then(|file| read_string_from(file))
|
.and_then(read_string_from)
|
||||||
.unwrap_or("".to_string()),
|
.unwrap_or_default(),
|
||||||
usage_percpu_sys: self
|
usage_percpu_sys: self
|
||||||
.open_path("cpuacct.usage_percpu_sys", false)
|
.open_path("cpuacct.usage_percpu_sys", false)
|
||||||
.and_then(|file| read_string_from(file))
|
.and_then(read_string_from)
|
||||||
.unwrap_or("".to_string()),
|
.unwrap_or_default(),
|
||||||
usage_percpu_user: self
|
usage_percpu_user: self
|
||||||
.open_path("cpuacct.usage_percpu_user", false)
|
.open_path("cpuacct.usage_percpu_user", false)
|
||||||
.and_then(|file| read_string_from(file))
|
.and_then(read_string_from)
|
||||||
.unwrap_or("".to_string()),
|
.unwrap_or_default(),
|
||||||
usage_sys: self
|
usage_sys: self
|
||||||
.open_path("cpuacct.usage_sys", false)
|
.open_path("cpuacct.usage_sys", false)
|
||||||
.and_then(|file| read_u64_from(file))
|
.and_then(read_u64_from)
|
||||||
.unwrap_or(0),
|
.unwrap_or(0),
|
||||||
usage_user: self
|
usage_user: self
|
||||||
.open_path("cpuacct.usage_user", false)
|
.open_path("cpuacct.usage_user", false)
|
||||||
.and_then(|file| read_u64_from(file))
|
.and_then(read_u64_from)
|
||||||
.unwrap_or(0),
|
.unwrap_or(0),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -33,6 +33,7 @@ pub struct CpuSetController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// The current state of the `cpuset` controller for this control group.
|
/// The current state of the `cpuset` controller for this control group.
|
||||||
|
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
|
||||||
pub struct CpuSet {
|
pub struct CpuSet {
|
||||||
/// If true, no other control groups can share the CPUs listed in the `cpus` field.
|
/// If true, no other control groups can share the CPUs listed in the `cpus` field.
|
||||||
pub cpu_exclusive: bool,
|
pub cpu_exclusive: bool,
|
||||||
@@ -146,7 +147,7 @@ fn find_no_empty_parent(from: &str, file: &str) -> Result<(String, Vec<PathBuf>)
|
|||||||
Err(e) => return Err(Error::with_cause(ReadFailed, e)),
|
Err(e) => return Err(Error::with_cause(ReadFailed, e)),
|
||||||
};
|
};
|
||||||
|
|
||||||
if current_value != "" {
|
if !current_value.is_empty() {
|
||||||
return Ok((current_value, v));
|
return Ok((current_value, v));
|
||||||
}
|
}
|
||||||
v.push(current_path.clone());
|
v.push(current_path.clone());
|
||||||
@@ -167,7 +168,7 @@ fn copy_from_parent(current: &str, file: &str) -> Result<()> {
|
|||||||
// find not empty cpus/memes from current directory.
|
// find not empty cpus/memes from current directory.
|
||||||
let (value, parents) = find_no_empty_parent(current, file)?;
|
let (value, parents) = find_no_empty_parent(current, file)?;
|
||||||
|
|
||||||
if value == "" || parents.len() == 0 {
|
if value.is_empty() || parents.is_empty() {
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -208,17 +209,17 @@ impl<'a> From<&'a Subsystem> for &'a CpuSetController {
|
|||||||
fn parse_range(s: String) -> Result<Vec<(u64, u64)>> {
|
fn parse_range(s: String) -> Result<Vec<(u64, u64)>> {
|
||||||
let mut fin = Vec::new();
|
let mut fin = Vec::new();
|
||||||
|
|
||||||
if s == "".to_string() {
|
if s.is_empty() {
|
||||||
return Ok(fin);
|
return Ok(fin);
|
||||||
}
|
}
|
||||||
|
|
||||||
// first split by commas
|
// first split by commas
|
||||||
let comma_split = s.split(",");
|
let comma_split = s.split(',');
|
||||||
|
|
||||||
for sp in comma_split {
|
for sp in comma_split {
|
||||||
if sp.contains("-") {
|
if sp.contains('-') {
|
||||||
// this is a true range
|
// this is a true range
|
||||||
let dash_split = sp.split("-").collect::<Vec<_>>();
|
let dash_split = sp.split('-').collect::<Vec<_>>();
|
||||||
if dash_split.len() != 2 {
|
if dash_split.len() != 2 {
|
||||||
return Err(Error::new(ParseError));
|
return Err(Error::new(ParseError));
|
||||||
}
|
}
|
||||||
@@ -247,7 +248,7 @@ impl CpuSetController {
|
|||||||
Self {
|
Self {
|
||||||
base: root.clone(),
|
base: root.clone(),
|
||||||
path: root,
|
path: root,
|
||||||
v2: v2,
|
v2,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -257,7 +258,7 @@ impl CpuSetController {
|
|||||||
CpuSet {
|
CpuSet {
|
||||||
cpu_exclusive: {
|
cpu_exclusive: {
|
||||||
self.open_path("cpuset.cpu_exclusive", false)
|
self.open_path("cpuset.cpu_exclusive", false)
|
||||||
.and_then(|file| read_u64_from(file))
|
.and_then(read_u64_from)
|
||||||
.map(|x| x == 1)
|
.map(|x| x == 1)
|
||||||
.unwrap_or(false)
|
.unwrap_or(false)
|
||||||
},
|
},
|
||||||
@@ -265,19 +266,19 @@ impl CpuSetController {
|
|||||||
self.open_path("cpuset.cpus", false)
|
self.open_path("cpuset.cpus", false)
|
||||||
.and_then(read_string_from)
|
.and_then(read_string_from)
|
||||||
.and_then(parse_range)
|
.and_then(parse_range)
|
||||||
.unwrap_or(Vec::new())
|
.unwrap_or_default()
|
||||||
},
|
},
|
||||||
effective_cpus: {
|
effective_cpus: {
|
||||||
self.open_path("cpuset.effective_cpus", false)
|
self.open_path("cpuset.effective_cpus", false)
|
||||||
.and_then(read_string_from)
|
.and_then(read_string_from)
|
||||||
.and_then(parse_range)
|
.and_then(parse_range)
|
||||||
.unwrap_or(Vec::new())
|
.unwrap_or_default()
|
||||||
},
|
},
|
||||||
effective_mems: {
|
effective_mems: {
|
||||||
self.open_path("cpuset.effective_mems", false)
|
self.open_path("cpuset.effective_mems", false)
|
||||||
.and_then(read_string_from)
|
.and_then(read_string_from)
|
||||||
.and_then(parse_range)
|
.and_then(parse_range)
|
||||||
.unwrap_or(Vec::new())
|
.unwrap_or_default()
|
||||||
},
|
},
|
||||||
mem_exclusive: {
|
mem_exclusive: {
|
||||||
self.open_path("cpuset.mem_exclusive", false)
|
self.open_path("cpuset.mem_exclusive", false)
|
||||||
@@ -324,7 +325,7 @@ impl CpuSetController {
|
|||||||
self.open_path("cpuset.mems", false)
|
self.open_path("cpuset.mems", false)
|
||||||
.and_then(read_string_from)
|
.and_then(read_string_from)
|
||||||
.and_then(parse_range)
|
.and_then(parse_range)
|
||||||
.unwrap_or(Vec::new())
|
.unwrap_or_default()
|
||||||
},
|
},
|
||||||
sched_load_balance: {
|
sched_load_balance: {
|
||||||
self.open_path("cpuset.sched_load_balance", false)
|
self.open_path("cpuset.sched_load_balance", false)
|
||||||
|
|||||||
@@ -32,6 +32,11 @@ pub struct DevicesController {
|
|||||||
|
|
||||||
/// An enum holding the different types of devices that can be manipulated using this controller.
|
/// An enum holding the different types of devices that can be manipulated using this controller.
|
||||||
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
|
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
|
||||||
|
#[cfg_attr(
|
||||||
|
feature = "serde",
|
||||||
|
derive(serde::Serialize, serde::Deserialize),
|
||||||
|
serde(rename_all = "snake_case")
|
||||||
|
)]
|
||||||
pub enum DeviceType {
|
pub enum DeviceType {
|
||||||
/// The rule applies to all devices.
|
/// The rule applies to all devices.
|
||||||
All,
|
All,
|
||||||
@@ -49,6 +54,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',
|
||||||
@@ -70,6 +76,11 @@ impl DeviceType {
|
|||||||
|
|
||||||
/// An enum with the permissions that can be allowed/denied to the control group.
|
/// An enum with the permissions that can be allowed/denied to the control group.
|
||||||
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
|
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
|
||||||
|
#[cfg_attr(
|
||||||
|
feature = "serde",
|
||||||
|
derive(serde::Serialize, serde::Deserialize),
|
||||||
|
serde(rename_all = "snake_case")
|
||||||
|
)]
|
||||||
pub enum DevicePermissions {
|
pub enum DevicePermissions {
|
||||||
/// Permission to read from the device.
|
/// Permission to read from the device.
|
||||||
Read,
|
Read,
|
||||||
@@ -82,6 +93,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',
|
||||||
@@ -102,7 +114,7 @@ impl DevicePermissions {
|
|||||||
|
|
||||||
/// Checks whether the string is a valid descriptor of DevicePermissions.
|
/// Checks whether the string is a valid descriptor of DevicePermissions.
|
||||||
pub fn is_valid(s: &str) -> bool {
|
pub fn is_valid(s: &str) -> bool {
|
||||||
if s == "" {
|
if s.is_empty() {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
for i in s.chars() {
|
for i in s.chars() {
|
||||||
@@ -110,7 +122,7 @@ impl DevicePermissions {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return true;
|
true
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns a Vec will all the permissions that a device can have.
|
/// Returns a Vec will all the permissions that a device can have.
|
||||||
@@ -123,9 +135,10 @@ impl DevicePermissions {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Convert a string into DevicePermissions.
|
/// Convert a string into DevicePermissions.
|
||||||
|
#[allow(clippy::should_implement_trait)]
|
||||||
pub fn from_str(s: &str) -> Result<Vec<DevicePermissions>> {
|
pub fn from_str(s: &str) -> Result<Vec<DevicePermissions>> {
|
||||||
let mut v = Vec::new();
|
let mut v = Vec::new();
|
||||||
if s == "" {
|
if s.is_empty() {
|
||||||
return Ok(v);
|
return Ok(v);
|
||||||
}
|
}
|
||||||
for e in s.chars() {
|
for e in s.chars() {
|
||||||
@@ -206,7 +219,7 @@ impl DevicesController {
|
|||||||
devtype: DeviceType,
|
devtype: DeviceType,
|
||||||
major: i64,
|
major: i64,
|
||||||
minor: i64,
|
minor: i64,
|
||||||
perm: &Vec<DevicePermissions>,
|
perm: &[DevicePermissions],
|
||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
let perms = perm
|
let perms = perm
|
||||||
.iter()
|
.iter()
|
||||||
@@ -238,7 +251,7 @@ impl DevicesController {
|
|||||||
devtype: DeviceType,
|
devtype: DeviceType,
|
||||||
major: i64,
|
major: i64,
|
||||||
minor: i64,
|
minor: i64,
|
||||||
perm: &Vec<DevicePermissions>,
|
perm: &[DevicePermissions],
|
||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
let perms = perm
|
let perms = perm
|
||||||
.iter()
|
.iter()
|
||||||
@@ -274,13 +287,13 @@ impl DevicesController {
|
|||||||
error!("allowed_devices: acc: {:?}, ls: {:?}", acc, ls);
|
error!("allowed_devices: acc: {:?}, ls: {:?}", acc, ls);
|
||||||
Err(Error::new(ParseError))
|
Err(Error::new(ParseError))
|
||||||
} else {
|
} else {
|
||||||
let devtype = DeviceType::from_char(ls[0].chars().nth(0));
|
let devtype = DeviceType::from_char(ls[0].chars().next());
|
||||||
let mut major = ls[1].parse::<i64>();
|
let mut major = ls[1].parse::<i64>();
|
||||||
let mut minor = ls[2].parse::<i64>();
|
let mut minor = ls[2].parse::<i64>();
|
||||||
if major.is_err() && ls[1] == "*".to_string() {
|
if major.is_err() && ls[1] == "*" {
|
||||||
major = Ok(-1);
|
major = Ok(-1);
|
||||||
}
|
}
|
||||||
if minor.is_err() && ls[2] == "*".to_string() {
|
if minor.is_err() && ls[2] == "*" {
|
||||||
minor = Ok(-1);
|
minor = Ok(-1);
|
||||||
}
|
}
|
||||||
if devtype.is_none() || major.is_err() || minor.is_err() || !DevicePermissions::is_valid(&ls[3]) {
|
if devtype.is_none() || major.is_err() || minor.is_err() || !DevicePermissions::is_valid(&ls[3]) {
|
||||||
@@ -295,7 +308,7 @@ impl DevicesController {
|
|||||||
devtype: devtype.unwrap(),
|
devtype: devtype.unwrap(),
|
||||||
major: major.unwrap(),
|
major: major.unwrap(),
|
||||||
minor: minor.unwrap(),
|
minor: minor.unwrap(),
|
||||||
access: access,
|
access,
|
||||||
});
|
});
|
||||||
Ok(acc)
|
Ok(acc)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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>> {
|
||||||
@@ -53,7 +53,7 @@ fn register_memory_event(
|
|||||||
|
|
||||||
let event_control_path = cg_dir.join("cgroup.event_control");
|
let event_control_path = cg_dir.join("cgroup.event_control");
|
||||||
let data;
|
let data;
|
||||||
if arg == "" {
|
if arg.is_empty() {
|
||||||
data = format!("{} {}", eventfd, event_file.as_raw_fd());
|
data = format!("{} {}", eventfd, event_file.as_raw_fd());
|
||||||
} else {
|
} else {
|
||||||
data = format!("{} {} {}", eventfd, event_file.as_raw_fd(), arg);
|
data = format!("{} {} {}", eventfd, event_file.as_raw_fd(), arg);
|
||||||
@@ -70,11 +70,8 @@ fn register_memory_event(
|
|||||||
thread::spawn(move || {
|
thread::spawn(move || {
|
||||||
loop {
|
loop {
|
||||||
let mut buf = [0; 8];
|
let mut buf = [0; 8];
|
||||||
match eventfd_file.read(&mut buf) {
|
if eventfd_file.read(&mut buf).is_err() {
|
||||||
Err(_err) => {
|
return;
|
||||||
return;
|
|
||||||
}
|
|
||||||
Ok(_) => {}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// When a cgroup is destroyed, an event is sent to eventfd.
|
// When a cgroup is destroyed, an event is sent to eventfd.
|
||||||
|
|||||||
@@ -32,6 +32,7 @@ pub struct FreezerController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// The current state of the control group
|
/// The current state of the control group
|
||||||
|
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
|
||||||
pub enum FreezerState {
|
pub enum FreezerState {
|
||||||
/// The processes in the control group are _not_ frozen.
|
/// The processes in the control group are _not_ frozen.
|
||||||
Thawed,
|
Thawed,
|
||||||
@@ -87,7 +88,7 @@ impl FreezerController {
|
|||||||
Self {
|
Self {
|
||||||
base: root.clone(),
|
base: root.clone(),
|
||||||
path: root,
|
path: root,
|
||||||
v2: v2,
|
v2,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -130,7 +131,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,
|
||||||
@@ -87,7 +87,7 @@ fn mountinfo_file(file: &mut File) -> Vec<Mountinfo> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Err(_) => continue,
|
Err(_) => break,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
r
|
r
|
||||||
@@ -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,
|
||||||
}
|
}
|
||||||
@@ -202,7 +202,12 @@ impl Hierarchy for V2 {
|
|||||||
let mut subs = vec![];
|
let mut subs = vec![];
|
||||||
|
|
||||||
let controllers = ret.unwrap().trim().to_string();
|
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 {
|
for s in controller_list {
|
||||||
match s {
|
match s {
|
||||||
@@ -268,6 +273,12 @@ impl V1 {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Default for V1 {
|
||||||
|
fn default() -> Self {
|
||||||
|
Self::new()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl V2 {
|
impl V2 {
|
||||||
/// Finds where control groups are mounted to and returns a hierarchy in which control groups
|
/// Finds where control groups are mounted to and returns a hierarchy in which control groups
|
||||||
/// can be created.
|
/// can be created.
|
||||||
@@ -278,7 +289,13 @@ impl V2 {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub const UNIFIED_MOUNTPOINT: &'static str = "/sys/fs/cgroup";
|
impl Default for V2 {
|
||||||
|
fn default() -> Self {
|
||||||
|
Self::new()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub const UNIFIED_MOUNTPOINT: &str = "/sys/fs/cgroup";
|
||||||
|
|
||||||
#[cfg(all(target_os = "linux", not(target_env = "musl")))]
|
#[cfg(all(target_os = "linux", not(target_env = "musl")))]
|
||||||
pub fn is_cgroup2_unified_mode() -> bool {
|
pub fn is_cgroup2_unified_mode() -> bool {
|
||||||
@@ -294,7 +311,7 @@ pub fn is_cgroup2_unified_mode() -> bool {
|
|||||||
fs_stat.unwrap().filesystem_type() == statfs::CGROUP2_SUPER_MAGIC
|
fs_stat.unwrap().filesystem_type() == statfs::CGROUP2_SUPER_MAGIC
|
||||||
}
|
}
|
||||||
|
|
||||||
pub const INIT_CGROUP_PATHS: &'static str = "/proc/1/cgroup";
|
pub const INIT_CGROUP_PATHS: &str = "/proc/1/cgroup";
|
||||||
|
|
||||||
#[cfg(all(target_os = "linux", target_env = "musl"))]
|
#[cfg(all(target_os = "linux", target_env = "musl"))]
|
||||||
pub fn is_cgroup2_unified_mode() -> bool {
|
pub fn is_cgroup2_unified_mode() -> bool {
|
||||||
|
|||||||
@@ -92,8 +92,8 @@ impl HugeTlbController {
|
|||||||
Self {
|
Self {
|
||||||
base: root.clone(),
|
base: root.clone(),
|
||||||
path: root,
|
path: root,
|
||||||
sizes: sizes,
|
sizes,
|
||||||
v2: v2,
|
v2,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -115,7 +115,7 @@ impl HugeTlbController {
|
|||||||
self.open_path(&format!("hugetlb.{}.events", hugetlb_size), false)
|
self.open_path(&format!("hugetlb.{}.events", hugetlb_size), false)
|
||||||
.and_then(flat_keyed_to_vec)
|
.and_then(flat_keyed_to_vec)
|
||||||
.and_then(|x| {
|
.and_then(|x| {
|
||||||
if x.len() == 0 {
|
if x.is_empty() {
|
||||||
return Err(Error::from_string(format!(
|
return Err(Error::from_string(format!(
|
||||||
"get empty from hugetlb.{}.events",
|
"get empty from hugetlb.{}.events",
|
||||||
hugetlb_size
|
hugetlb_size
|
||||||
@@ -175,7 +175,7 @@ impl HugeTlbController {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub const HUGEPAGESIZE_DIR: &'static str = "/sys/kernel/mm/hugepages";
|
pub const HUGEPAGESIZE_DIR: &str = "/sys/kernel/mm/hugepages";
|
||||||
use regex::Regex;
|
use regex::Regex;
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::fs;
|
use std::fs;
|
||||||
@@ -264,8 +264,8 @@ fn parse_size(s: &str, m: &HashMap<String, u128>) -> Result<u128> {
|
|||||||
let caps = re.unwrap().captures(s).unwrap();
|
let caps = re.unwrap().captures(s).unwrap();
|
||||||
|
|
||||||
let num = caps.name("num");
|
let num = caps.name("num");
|
||||||
let size: u128 = if num.is_some() {
|
let size: u128 = if let Some(num) = num {
|
||||||
let n = num.unwrap().as_str().trim().parse::<u128>();
|
let n = num.as_str().trim().parse::<u128>();
|
||||||
if n.is_err() {
|
if n.is_err() {
|
||||||
return Err(Error::new(InvalidBytesSize));
|
return Err(Error::new(InvalidBytesSize));
|
||||||
}
|
}
|
||||||
@@ -275,10 +275,10 @@ fn parse_size(s: &str, m: &HashMap<String, u128>) -> Result<u128> {
|
|||||||
};
|
};
|
||||||
|
|
||||||
let q = caps.name("mul");
|
let q = caps.name("mul");
|
||||||
let mul: u128 = if q.is_some() {
|
let mul: u128 = if let Some(q) = q {
|
||||||
let t = m.get(q.unwrap().as_str());
|
let t = m.get(q.as_str());
|
||||||
if t.is_some() {
|
if let Some(t) = t {
|
||||||
*t.unwrap()
|
*t
|
||||||
} else {
|
} else {
|
||||||
return Err(Error::new(InvalidBytesSize));
|
return Err(Error::new(InvalidBytesSize));
|
||||||
}
|
}
|
||||||
@@ -289,7 +289,7 @@ fn parse_size(s: &str, m: &HashMap<String, u128>) -> Result<u128> {
|
|||||||
Ok(size * mul)
|
Ok(size * mul)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn custom_size(mut size: f64, base: f64, m: &Vec<String>) -> String {
|
fn custom_size(mut size: f64, base: f64, m: &[String]) -> String {
|
||||||
let mut i = 0;
|
let mut i = 0;
|
||||||
while size >= base && i < m.len() - 1 {
|
while size >= base && i < m.len() - 1 {
|
||||||
size /= base;
|
size /= base;
|
||||||
|
|||||||
236
src/lib.rs
236
src/lib.rs
@@ -4,9 +4,11 @@
|
|||||||
// SPDX-License-Identifier: Apache-2.0 or MIT
|
// SPDX-License-Identifier: Apache-2.0 or MIT
|
||||||
//
|
//
|
||||||
|
|
||||||
|
#![allow(clippy::unnecessary_unwrap)]
|
||||||
use log::*;
|
use log::*;
|
||||||
|
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
use std::fmt;
|
||||||
use std::fs::{self, File};
|
use std::fs::{self, File};
|
||||||
use std::io::{BufRead, BufReader, Read, Write};
|
use std::io::{BufRead, BufReader, Read, Write};
|
||||||
use std::path::{Path, PathBuf};
|
use std::path::{Path, PathBuf};
|
||||||
@@ -123,23 +125,23 @@ pub enum Controllers {
|
|||||||
Systemd,
|
Systemd,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Controllers {
|
impl fmt::Display for Controllers {
|
||||||
pub fn to_string(&self) -> String {
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
match self {
|
match self {
|
||||||
Controllers::Pids => return "pids".to_string(),
|
Controllers::Pids => write!(f, "pids"),
|
||||||
Controllers::Mem => return "memory".to_string(),
|
Controllers::Mem => write!(f, "memory"),
|
||||||
Controllers::CpuSet => return "cpuset".to_string(),
|
Controllers::CpuSet => write!(f, "cpuset"),
|
||||||
Controllers::CpuAcct => return "cpuacct".to_string(),
|
Controllers::CpuAcct => write!(f, "cpuacct"),
|
||||||
Controllers::Cpu => return "cpu".to_string(),
|
Controllers::Cpu => write!(f, "cpu"),
|
||||||
Controllers::Devices => return "devices".to_string(),
|
Controllers::Devices => write!(f, "devices"),
|
||||||
Controllers::Freezer => return "freezer".to_string(),
|
Controllers::Freezer => write!(f, "freezer"),
|
||||||
Controllers::NetCls => return "net_cls".to_string(),
|
Controllers::NetCls => write!(f, "net_cls"),
|
||||||
Controllers::BlkIo => return "blkio".to_string(),
|
Controllers::BlkIo => write!(f, "blkio"),
|
||||||
Controllers::PerfEvent => return "perf_event".to_string(),
|
Controllers::PerfEvent => write!(f, "perf_event"),
|
||||||
Controllers::NetPrio => return "net_prio".to_string(),
|
Controllers::NetPrio => write!(f, "net_prio"),
|
||||||
Controllers::HugeTlb => return "hugetlb".to_string(),
|
Controllers::HugeTlb => write!(f, "hugetlb"),
|
||||||
Controllers::Rdma => return "rdma".to_string(),
|
Controllers::Rdma => write!(f, "rdma"),
|
||||||
Controllers::Systemd => return "name=systemd".to_string(),
|
Controllers::Systemd => write!(f, "name=systemd"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -179,13 +181,13 @@ mod sealed {
|
|||||||
|
|
||||||
if w {
|
if w {
|
||||||
match File::create(&path) {
|
match File::create(&path) {
|
||||||
Err(e) => return Err(Error::with_cause(ErrorKind::WriteFailed, e)),
|
Err(e) => Err(Error::with_cause(ErrorKind::WriteFailed, e)),
|
||||||
Ok(file) => return Ok(file),
|
Ok(file) => Ok(file),
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
match File::open(&path) {
|
match File::open(&path) {
|
||||||
Err(e) => return Err(Error::with_cause(ErrorKind::ReadFailed, e)),
|
Err(e) => Err(Error::with_cause(ErrorKind::ReadFailed, e)),
|
||||||
Ok(file) => return Ok(file),
|
Ok(file) => Ok(file),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -203,7 +205,7 @@ mod sealed {
|
|||||||
|
|
||||||
#[doc(hidden)]
|
#[doc(hidden)]
|
||||||
fn path_exists(&self, p: &str) -> bool {
|
fn path_exists(&self, p: &str) -> bool {
|
||||||
if let Err(_) = self.verify_path() {
|
if self.verify_path().is_err() {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -295,7 +297,7 @@ where
|
|||||||
/// Create this controller
|
/// Create this controller
|
||||||
fn create(&self) {
|
fn create(&self) {
|
||||||
self.verify_path()
|
self.verify_path()
|
||||||
.expect(format!("path should be valid: {:?}", self.path()).as_str());
|
.unwrap_or_else(|_| panic!("path should be valid: {:?}", self.path()));
|
||||||
|
|
||||||
match ::std::fs::create_dir_all(self.get_path()) {
|
match ::std::fs::create_dir_all(self.get_path()) {
|
||||||
Ok(_) => self.post_create(),
|
Ok(_) => self.post_create(),
|
||||||
@@ -360,18 +362,21 @@ where
|
|||||||
file = "cgroup.procs";
|
file = "cgroup.procs";
|
||||||
}
|
}
|
||||||
self.open_path(file, false)
|
self.open_path(file, false)
|
||||||
.and_then(|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() {
|
||||||
if let Ok(line) = line {
|
match line {
|
||||||
let n = line.trim().parse().unwrap_or(0u64);
|
Ok(line) => {
|
||||||
v.push(n);
|
let n = line.trim().parse().unwrap_or(0u64);
|
||||||
|
v.push(n);
|
||||||
|
}
|
||||||
|
Err(_) => break,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Ok(v.into_iter().map(CgroupPid::from).collect())
|
v.into_iter().map(CgroupPid::from).collect()
|
||||||
})
|
})
|
||||||
.unwrap_or(vec![])
|
.unwrap_or_default()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn v2(&self) -> bool {
|
fn v2(&self) -> bool {
|
||||||
@@ -381,23 +386,21 @@ 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(());
|
||||||
}
|
}
|
||||||
|
|
||||||
if dir.exists() {
|
if dir.exists() && dir.is_dir() {
|
||||||
if dir.is_dir() {
|
for entry in fs::read_dir(dir).map_err(|e| Error::with_cause(ReadFailed, e))? {
|
||||||
for entry in fs::read_dir(dir).map_err(|e| Error::with_cause(ReadFailed, e))? {
|
let entry = entry.map_err(|e| Error::with_cause(ReadFailed, e))?;
|
||||||
let entry = entry.map_err(|e| Error::with_cause(ReadFailed, e))?;
|
let path = entry.path();
|
||||||
let path = entry.path();
|
if path.is_dir() {
|
||||||
if path.is_dir() {
|
remove_dir(&path)?;
|
||||||
remove_dir(&path)?;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
fs::remove_dir(dir).map_err(|e| Error::with_cause(RemoveFailed, e))?;
|
|
||||||
}
|
}
|
||||||
|
fs::remove_dir(dir).map_err(|e| Error::with_cause(RemoveFailed, e))?;
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
@@ -425,6 +428,7 @@ pub trait Hierarchy: std::fmt::Debug + Send + Sync {
|
|||||||
|
|
||||||
/// Resource limits for the memory subsystem.
|
/// Resource limits for the memory subsystem.
|
||||||
#[derive(Debug, Clone, Eq, PartialEq, Default)]
|
#[derive(Debug, Clone, Eq, PartialEq, Default)]
|
||||||
|
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
|
||||||
pub struct MemoryResources {
|
pub struct MemoryResources {
|
||||||
/// How much memory (in bytes) can the kernel consume.
|
/// How much memory (in bytes) can the kernel consume.
|
||||||
pub kernel_memory_limit: Option<i64>,
|
pub kernel_memory_limit: Option<i64>,
|
||||||
@@ -448,13 +452,15 @@ pub struct MemoryResources {
|
|||||||
/// # Usage:
|
/// # Usage:
|
||||||
/// ```
|
/// ```
|
||||||
/// let resource = &mut cgroups_rs::Resources::default();
|
/// 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
|
/// // apply here
|
||||||
pub attrs: std::collections::HashMap<&'static str, String>,
|
/// ```
|
||||||
|
pub attrs: HashMap<String, String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Resources limits on the number of processes.
|
/// Resources limits on the number of processes.
|
||||||
#[derive(Debug, Clone, Eq, PartialEq, Default)]
|
#[derive(Debug, Clone, Eq, PartialEq, Default)]
|
||||||
|
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
|
||||||
pub struct PidResources {
|
pub struct PidResources {
|
||||||
/// The maximum number of processes that can exist in the control group.
|
/// The maximum number of processes that can exist in the control group.
|
||||||
///
|
///
|
||||||
@@ -466,6 +472,7 @@ pub struct PidResources {
|
|||||||
|
|
||||||
/// Resources limits about how the tasks can use the CPU.
|
/// Resources limits about how the tasks can use the CPU.
|
||||||
#[derive(Debug, Clone, Eq, PartialEq, Default)]
|
#[derive(Debug, Clone, Eq, PartialEq, Default)]
|
||||||
|
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
|
||||||
pub struct CpuResources {
|
pub struct CpuResources {
|
||||||
// cpuset
|
// cpuset
|
||||||
/// A comma-separated list of CPU IDs where the task in the control group can run. Dashes
|
/// A comma-separated list of CPU IDs where the task in the control group can run. Dashes
|
||||||
@@ -490,14 +497,15 @@ pub struct CpuResources {
|
|||||||
/// # Usage:
|
/// # Usage:
|
||||||
/// ```
|
/// ```
|
||||||
/// let resource = &mut cgroups_rs::Resources::default();
|
/// 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
|
/// // 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.
|
/// A device resource that can be allowed or denied access to.
|
||||||
#[derive(Debug, Clone, Eq, PartialEq, Default)]
|
#[derive(Debug, Clone, Eq, PartialEq, Default)]
|
||||||
|
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
|
||||||
pub struct DeviceResource {
|
pub struct DeviceResource {
|
||||||
/// If true, access to the device is allowed, otherwise it's denied.
|
/// If true, access to the device is allowed, otherwise it's denied.
|
||||||
pub allow: bool,
|
pub allow: bool,
|
||||||
@@ -513,6 +521,7 @@ pub struct DeviceResource {
|
|||||||
|
|
||||||
/// Limit the usage of devices for the control group's tasks.
|
/// Limit the usage of devices for the control group's tasks.
|
||||||
#[derive(Debug, Clone, Eq, PartialEq, Default)]
|
#[derive(Debug, Clone, Eq, PartialEq, Default)]
|
||||||
|
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
|
||||||
pub struct DeviceResources {
|
pub struct DeviceResources {
|
||||||
/// For each device in the list, the limits in the structure are applied.
|
/// For each device in the list, the limits in the structure are applied.
|
||||||
pub devices: Vec<DeviceResource>,
|
pub devices: Vec<DeviceResource>,
|
||||||
@@ -520,6 +529,7 @@ pub struct DeviceResources {
|
|||||||
|
|
||||||
/// Assigned priority for a network device.
|
/// Assigned priority for a network device.
|
||||||
#[derive(Debug, Clone, Eq, PartialEq, Default)]
|
#[derive(Debug, Clone, Eq, PartialEq, Default)]
|
||||||
|
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
|
||||||
pub struct NetworkPriority {
|
pub struct NetworkPriority {
|
||||||
/// The name (as visible in `ifconfig`) of the interface.
|
/// The name (as visible in `ifconfig`) of the interface.
|
||||||
pub name: String,
|
pub name: String,
|
||||||
@@ -530,6 +540,7 @@ pub struct NetworkPriority {
|
|||||||
/// Collections of limits and tags that can be imposed on packets emitted by the tasks in the
|
/// Collections of limits and tags that can be imposed on packets emitted by the tasks in the
|
||||||
/// control group.
|
/// control group.
|
||||||
#[derive(Debug, Clone, Eq, PartialEq, Default)]
|
#[derive(Debug, Clone, Eq, PartialEq, Default)]
|
||||||
|
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
|
||||||
pub struct NetworkResources {
|
pub struct NetworkResources {
|
||||||
/// The networking class identifier to attach to the packets.
|
/// The networking class identifier to attach to the packets.
|
||||||
///
|
///
|
||||||
@@ -541,6 +552,7 @@ pub struct NetworkResources {
|
|||||||
|
|
||||||
/// A hugepage type and its consumption limit for the control group.
|
/// A hugepage type and its consumption limit for the control group.
|
||||||
#[derive(Debug, Clone, Eq, PartialEq, Default)]
|
#[derive(Debug, Clone, Eq, PartialEq, Default)]
|
||||||
|
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
|
||||||
pub struct HugePageResource {
|
pub struct HugePageResource {
|
||||||
/// The size of the hugepage, i.e. `2MB`, `1GB`, etc.
|
/// The size of the hugepage, i.e. `2MB`, `1GB`, etc.
|
||||||
pub size: String,
|
pub size: String,
|
||||||
@@ -551,6 +563,7 @@ pub struct HugePageResource {
|
|||||||
|
|
||||||
/// Provides the ability to set consumption limit on each type of hugepages.
|
/// Provides the ability to set consumption limit on each type of hugepages.
|
||||||
#[derive(Debug, Clone, Eq, PartialEq, Default)]
|
#[derive(Debug, Clone, Eq, PartialEq, Default)]
|
||||||
|
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
|
||||||
pub struct HugePageResources {
|
pub struct HugePageResources {
|
||||||
/// Set a limit of consumption for each hugepages type.
|
/// Set a limit of consumption for each hugepages type.
|
||||||
pub limits: Vec<HugePageResource>,
|
pub limits: Vec<HugePageResource>,
|
||||||
@@ -558,6 +571,7 @@ pub struct HugePageResources {
|
|||||||
|
|
||||||
/// Weight for a particular block device.
|
/// Weight for a particular block device.
|
||||||
#[derive(Debug, Clone, Eq, PartialEq, Default)]
|
#[derive(Debug, Clone, Eq, PartialEq, Default)]
|
||||||
|
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
|
||||||
pub struct BlkIoDeviceResource {
|
pub struct BlkIoDeviceResource {
|
||||||
/// The major number of the device.
|
/// The major number of the device.
|
||||||
pub major: u64,
|
pub major: u64,
|
||||||
@@ -571,6 +585,7 @@ pub struct BlkIoDeviceResource {
|
|||||||
|
|
||||||
/// Provides the ability to throttle a device (both byte/sec, and IO op/s)
|
/// Provides the ability to throttle a device (both byte/sec, and IO op/s)
|
||||||
#[derive(Debug, Clone, Eq, PartialEq, Default)]
|
#[derive(Debug, Clone, Eq, PartialEq, Default)]
|
||||||
|
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
|
||||||
pub struct BlkIoDeviceThrottleResource {
|
pub struct BlkIoDeviceThrottleResource {
|
||||||
/// The major number of the device.
|
/// The major number of the device.
|
||||||
pub major: u64,
|
pub major: u64,
|
||||||
@@ -582,6 +597,7 @@ pub struct BlkIoDeviceThrottleResource {
|
|||||||
|
|
||||||
/// General block I/O resource limits.
|
/// General block I/O resource limits.
|
||||||
#[derive(Debug, Clone, Eq, PartialEq, Default)]
|
#[derive(Debug, Clone, Eq, PartialEq, Default)]
|
||||||
|
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
|
||||||
pub struct BlkIoResources {
|
pub struct BlkIoResources {
|
||||||
/// The weight of the control group against descendant nodes.
|
/// The weight of the control group against descendant nodes.
|
||||||
pub weight: Option<u16>,
|
pub weight: Option<u16>,
|
||||||
@@ -601,6 +617,7 @@ pub struct BlkIoResources {
|
|||||||
|
|
||||||
/// The resource limits and constraints that will be set on the control group.
|
/// The resource limits and constraints that will be set on the control group.
|
||||||
#[derive(Debug, Clone, Eq, PartialEq, Default)]
|
#[derive(Debug, Clone, Eq, PartialEq, Default)]
|
||||||
|
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
|
||||||
pub struct Resources {
|
pub struct Resources {
|
||||||
/// Memory usage related limits.
|
/// Memory usage related limits.
|
||||||
pub memory: MemoryResources,
|
pub memory: MemoryResources,
|
||||||
@@ -641,75 +658,61 @@ impl<'a> From<&'a std::process::Child> for CgroupPid {
|
|||||||
impl Subsystem {
|
impl Subsystem {
|
||||||
fn enter(self, path: &Path) -> Self {
|
fn enter(self, path: &Path) -> Self {
|
||||||
match self {
|
match self {
|
||||||
Subsystem::Pid(cont) => Subsystem::Pid({
|
Subsystem::Pid(mut cont) => Subsystem::Pid({
|
||||||
let mut c = cont.clone();
|
cont.get_path_mut().push(path);
|
||||||
c.get_path_mut().push(path);
|
cont
|
||||||
c
|
|
||||||
}),
|
}),
|
||||||
Subsystem::Mem(cont) => Subsystem::Mem({
|
Subsystem::Mem(mut cont) => Subsystem::Mem({
|
||||||
let mut c = cont.clone();
|
cont.get_path_mut().push(path);
|
||||||
c.get_path_mut().push(path);
|
cont
|
||||||
c
|
|
||||||
}),
|
}),
|
||||||
Subsystem::CpuSet(cont) => Subsystem::CpuSet({
|
Subsystem::CpuSet(mut cont) => Subsystem::CpuSet({
|
||||||
let mut c = cont.clone();
|
cont.get_path_mut().push(path);
|
||||||
c.get_path_mut().push(path);
|
cont
|
||||||
c
|
|
||||||
}),
|
}),
|
||||||
Subsystem::CpuAcct(cont) => Subsystem::CpuAcct({
|
Subsystem::CpuAcct(mut cont) => Subsystem::CpuAcct({
|
||||||
let mut c = cont.clone();
|
cont.get_path_mut().push(path);
|
||||||
c.get_path_mut().push(path);
|
cont
|
||||||
c
|
|
||||||
}),
|
}),
|
||||||
Subsystem::Cpu(cont) => Subsystem::Cpu({
|
Subsystem::Cpu(mut cont) => Subsystem::Cpu({
|
||||||
let mut c = cont.clone();
|
cont.get_path_mut().push(path);
|
||||||
c.get_path_mut().push(path);
|
cont
|
||||||
c
|
|
||||||
}),
|
}),
|
||||||
Subsystem::Devices(cont) => Subsystem::Devices({
|
Subsystem::Devices(mut cont) => Subsystem::Devices({
|
||||||
let mut c = cont.clone();
|
cont.get_path_mut().push(path);
|
||||||
c.get_path_mut().push(path);
|
cont
|
||||||
c
|
|
||||||
}),
|
}),
|
||||||
Subsystem::Freezer(cont) => Subsystem::Freezer({
|
Subsystem::Freezer(mut cont) => Subsystem::Freezer({
|
||||||
let mut c = cont.clone();
|
cont.get_path_mut().push(path);
|
||||||
c.get_path_mut().push(path);
|
cont
|
||||||
c
|
|
||||||
}),
|
}),
|
||||||
Subsystem::NetCls(cont) => Subsystem::NetCls({
|
Subsystem::NetCls(mut cont) => Subsystem::NetCls({
|
||||||
let mut c = cont.clone();
|
cont.get_path_mut().push(path);
|
||||||
c.get_path_mut().push(path);
|
cont
|
||||||
c
|
|
||||||
}),
|
}),
|
||||||
Subsystem::BlkIo(cont) => Subsystem::BlkIo({
|
Subsystem::BlkIo(mut cont) => Subsystem::BlkIo({
|
||||||
let mut c = cont.clone();
|
cont.get_path_mut().push(path);
|
||||||
c.get_path_mut().push(path);
|
cont
|
||||||
c
|
|
||||||
}),
|
}),
|
||||||
Subsystem::PerfEvent(cont) => Subsystem::PerfEvent({
|
Subsystem::PerfEvent(mut cont) => Subsystem::PerfEvent({
|
||||||
let mut c = cont.clone();
|
cont.get_path_mut().push(path);
|
||||||
c.get_path_mut().push(path);
|
cont
|
||||||
c
|
|
||||||
}),
|
}),
|
||||||
Subsystem::NetPrio(cont) => Subsystem::NetPrio({
|
Subsystem::NetPrio(mut cont) => Subsystem::NetPrio({
|
||||||
let mut c = cont.clone();
|
cont.get_path_mut().push(path);
|
||||||
c.get_path_mut().push(path);
|
cont
|
||||||
c
|
|
||||||
}),
|
}),
|
||||||
Subsystem::HugeTlb(cont) => Subsystem::HugeTlb({
|
Subsystem::HugeTlb(mut cont) => Subsystem::HugeTlb({
|
||||||
let mut c = cont.clone();
|
cont.get_path_mut().push(path);
|
||||||
c.get_path_mut().push(path);
|
cont
|
||||||
c
|
|
||||||
}),
|
}),
|
||||||
Subsystem::Rdma(cont) => Subsystem::Rdma({
|
Subsystem::Rdma(mut cont) => Subsystem::Rdma({
|
||||||
let mut c = cont.clone();
|
cont.get_path_mut().push(path);
|
||||||
c.get_path_mut().push(path);
|
cont
|
||||||
c
|
|
||||||
}),
|
}),
|
||||||
Subsystem::Systemd(cont) => Subsystem::Systemd({
|
Subsystem::Systemd(mut cont) => Subsystem::Systemd({
|
||||||
let mut c = cont.clone();
|
cont.get_path_mut().push(path);
|
||||||
c.get_path_mut().push(path);
|
cont
|
||||||
c
|
|
||||||
}),
|
}),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -740,6 +743,7 @@ impl Subsystem {
|
|||||||
|
|
||||||
/// The values for `memory.hight` or `pids.max`
|
/// The values for `memory.hight` or `pids.max`
|
||||||
#[derive(Eq, PartialEq, Copy, Clone, Debug)]
|
#[derive(Eq, PartialEq, Copy, Clone, Debug)]
|
||||||
|
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
|
||||||
pub enum MaxValue {
|
pub enum MaxValue {
|
||||||
/// This value is returned when the text is `"max"`.
|
/// This value is returned when the text is `"max"`.
|
||||||
Max,
|
Max,
|
||||||
@@ -754,22 +758,25 @@ 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,
|
||||||
MaxValue::Value(num) => *num,
|
MaxValue::Value(num) => *num,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn to_string(&self) -> String {
|
impl fmt::Display for MaxValue {
|
||||||
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
match self {
|
match self {
|
||||||
MaxValue::Max => "max".to_string(),
|
MaxValue::Max => write!(f, "max"),
|
||||||
MaxValue::Value(num) => num.to_string(),
|
MaxValue::Value(num) => write!(f, "{}", num.to_string()),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn parse_max_value(s: &String) -> Result<MaxValue> {
|
pub fn parse_max_value(s: &str) -> Result<MaxValue> {
|
||||||
if s.trim() == "max" {
|
if s.trim() == "max" {
|
||||||
return Ok(MaxValue::Max);
|
return Ok(MaxValue::Max);
|
||||||
}
|
}
|
||||||
@@ -791,11 +798,8 @@ pub fn flat_keyed_to_vec(mut file: File) -> Result<Vec<(String, i64)>> {
|
|||||||
for line in content.lines() {
|
for line in content.lines() {
|
||||||
let parts: Vec<&str> = line.split(' ').collect();
|
let parts: Vec<&str> = line.split(' ').collect();
|
||||||
if parts.len() == 2 {
|
if parts.len() == 2 {
|
||||||
match parts[1].parse::<i64>() {
|
if let Ok(i) = parts[1].parse::<i64>() {
|
||||||
Ok(i) => {
|
v.push((parts[0].to_string(), i));
|
||||||
v.push((parts[0].to_string(), i));
|
|
||||||
}
|
|
||||||
Err(_) => {}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -814,11 +818,8 @@ pub fn flat_keyed_to_hashmap(mut file: File) -> Result<HashMap<String, i64>> {
|
|||||||
for line in content.lines() {
|
for line in content.lines() {
|
||||||
let parts: Vec<&str> = line.split(' ').collect();
|
let parts: Vec<&str> = line.split(' ').collect();
|
||||||
if parts.len() == 2 {
|
if parts.len() == 2 {
|
||||||
match parts[1].parse::<i64>() {
|
if let Ok(i) = parts[1].parse::<i64>() {
|
||||||
Ok(i) => {
|
h.insert(parts[0].to_string(), i);
|
||||||
h.insert(parts[0].to_string(), i);
|
|
||||||
}
|
|
||||||
Err(_) => {}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -836,18 +837,15 @@ pub fn nested_keyed_to_hashmap(mut file: File) -> Result<HashMap<String, HashMap
|
|||||||
let mut h = HashMap::new();
|
let mut h = HashMap::new();
|
||||||
for line in content.lines() {
|
for line in content.lines() {
|
||||||
let parts: Vec<&str> = line.split(' ').collect();
|
let parts: Vec<&str> = line.split(' ').collect();
|
||||||
if parts.len() == 0 {
|
if parts.is_empty() {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
let mut th = HashMap::new();
|
let mut th = HashMap::new();
|
||||||
for item in parts[1..].into_iter() {
|
for item in parts[1..].iter() {
|
||||||
let fields: Vec<&str> = item.split('=').collect();
|
let fields: Vec<&str> = item.split('=').collect();
|
||||||
if fields.len() == 2 {
|
if fields.len() == 2 {
|
||||||
match fields[1].parse::<i64>() {
|
if let Ok(i) = fields[1].parse::<i64>() {
|
||||||
Ok(i) => {
|
th.insert(fields[0].to_string(), i);
|
||||||
th.insert(fields[0].to_string(), i);
|
|
||||||
}
|
|
||||||
Err(_) => {}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
299
src/memory.rs
299
src/memory.rs
@@ -38,6 +38,7 @@ pub struct MemController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Default, Debug, PartialEq, Eq)]
|
#[derive(Default, Debug, PartialEq, Eq)]
|
||||||
|
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
|
||||||
pub struct SetMemory {
|
pub struct SetMemory {
|
||||||
pub low: Option<MaxValue>,
|
pub low: Option<MaxValue>,
|
||||||
pub high: 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.
|
/// Controls statistics and controls about the OOM killer operating in this control group.
|
||||||
#[derive(Default, Debug, PartialEq, Eq)]
|
#[derive(Default, Debug, PartialEq, Eq)]
|
||||||
|
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
|
||||||
pub struct OomControl {
|
pub struct OomControl {
|
||||||
/// If true, the OOM killer has been disabled for the tasks in this control group.
|
/// If true, the OOM killer has been disabled for the tasks in this control group.
|
||||||
pub oom_kill_disable: bool,
|
pub oom_kill_disable: bool,
|
||||||
@@ -56,18 +58,38 @@ pub struct OomControl {
|
|||||||
pub oom_kill: u64,
|
pub oom_kill: u64,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[allow(clippy::unnecessary_wraps)]
|
||||||
fn parse_oom_control(s: String) -> Result<OomControl> {
|
fn parse_oom_control(s: String) -> Result<OomControl> {
|
||||||
let spl = s.split_whitespace().collect::<Vec<_>>();
|
let spl = s.split_whitespace().collect::<Vec<_>>();
|
||||||
|
|
||||||
|
let oom_kill_disable = if spl.len() > 1 {
|
||||||
|
spl[1].parse::<u64>().unwrap() == 1
|
||||||
|
} else {
|
||||||
|
false
|
||||||
|
};
|
||||||
|
|
||||||
|
let under_oom = if spl.len() > 3 {
|
||||||
|
spl[3].parse::<u64>().unwrap() == 1
|
||||||
|
} else {
|
||||||
|
false
|
||||||
|
};
|
||||||
|
|
||||||
|
let oom_kill = if spl.len() > 5 {
|
||||||
|
spl[5].parse::<u64>().unwrap()
|
||||||
|
} else {
|
||||||
|
0
|
||||||
|
};
|
||||||
|
|
||||||
Ok(OomControl {
|
Ok(OomControl {
|
||||||
oom_kill_disable: spl[1].parse::<u64>().unwrap() == 1,
|
oom_kill_disable,
|
||||||
under_oom: spl[3].parse::<u64>().unwrap() == 1,
|
under_oom,
|
||||||
oom_kill: spl[5].parse::<u64>().unwrap(),
|
oom_kill,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Contains statistics about the NUMA locality of the control group's tasks.
|
/// Contains statistics about the NUMA locality of the control group's tasks.
|
||||||
#[derive(Default, Debug, PartialEq, Eq)]
|
#[derive(Default, Debug, PartialEq, Eq)]
|
||||||
|
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
|
||||||
pub struct NumaStat {
|
pub struct NumaStat {
|
||||||
/// Total amount of pages used by the control group.
|
/// Total amount of pages used by the control group.
|
||||||
pub total_pages: u64,
|
pub total_pages: u64,
|
||||||
@@ -104,18 +126,19 @@ pub struct NumaStat {
|
|||||||
pub hierarchical_unevictable_pages_per_node: Vec<u64>,
|
pub hierarchical_unevictable_pages_per_node: Vec<u64>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[allow(clippy::unnecessary_wraps)]
|
||||||
fn parse_numa_stat(s: String) -> Result<NumaStat> {
|
fn parse_numa_stat(s: String) -> Result<NumaStat> {
|
||||||
// Parse the number of nodes
|
// Parse the number of nodes
|
||||||
let _nodes = (s.split_whitespace().collect::<Vec<_>>().len() - 8) / 8;
|
let _nodes = (s.split_whitespace().count() - 8) / 8;
|
||||||
let mut ls = s.lines();
|
let mut ls = s.lines();
|
||||||
let total_line = ls.next().unwrap();
|
let total_line = ls.next().unwrap();
|
||||||
let file_line = ls.next().unwrap();
|
let file_line = ls.next().unwrap();
|
||||||
let anon_line = ls.next().unwrap();
|
let anon_line = ls.next().unwrap();
|
||||||
let unevict_line = ls.next().unwrap();
|
let unevict_line = ls.next().unwrap();
|
||||||
let hier_total_line = ls.next().unwrap();
|
let hier_total_line = ls.next().unwrap_or_default();
|
||||||
let hier_file_line = ls.next().unwrap();
|
let hier_file_line = ls.next().unwrap_or_default();
|
||||||
let hier_anon_line = ls.next().unwrap();
|
let hier_anon_line = ls.next().unwrap_or_default();
|
||||||
let hier_unevict_line = ls.next().unwrap();
|
let hier_unevict_line = ls.next().unwrap_or_default();
|
||||||
|
|
||||||
Ok(NumaStat {
|
Ok(NumaStat {
|
||||||
total_pages: total_line
|
total_pages: total_line
|
||||||
@@ -124,10 +147,10 @@ fn parse_numa_stat(s: String) -> Result<NumaStat> {
|
|||||||
.parse::<u64>()
|
.parse::<u64>()
|
||||||
.unwrap_or(0),
|
.unwrap_or(0),
|
||||||
total_pages_per_node: {
|
total_pages_per_node: {
|
||||||
let spl = &total_line.split(" ").collect::<Vec<_>>()[1..];
|
let spl = &total_line.split(' ').collect::<Vec<_>>()[1..];
|
||||||
spl.iter()
|
spl.iter()
|
||||||
.map(|x| {
|
.map(|x| {
|
||||||
x.split("=").collect::<Vec<_>>()[1]
|
x.split('=').collect::<Vec<_>>()[1]
|
||||||
.parse::<u64>()
|
.parse::<u64>()
|
||||||
.unwrap_or(0)
|
.unwrap_or(0)
|
||||||
})
|
})
|
||||||
@@ -139,10 +162,10 @@ fn parse_numa_stat(s: String) -> Result<NumaStat> {
|
|||||||
.parse::<u64>()
|
.parse::<u64>()
|
||||||
.unwrap_or(0),
|
.unwrap_or(0),
|
||||||
file_pages_per_node: {
|
file_pages_per_node: {
|
||||||
let spl = &file_line.split(" ").collect::<Vec<_>>()[1..];
|
let spl = &file_line.split(' ').collect::<Vec<_>>()[1..];
|
||||||
spl.iter()
|
spl.iter()
|
||||||
.map(|x| {
|
.map(|x| {
|
||||||
x.split("=").collect::<Vec<_>>()[1]
|
x.split('=').collect::<Vec<_>>()[1]
|
||||||
.parse::<u64>()
|
.parse::<u64>()
|
||||||
.unwrap_or(0)
|
.unwrap_or(0)
|
||||||
})
|
})
|
||||||
@@ -154,10 +177,10 @@ fn parse_numa_stat(s: String) -> Result<NumaStat> {
|
|||||||
.parse::<u64>()
|
.parse::<u64>()
|
||||||
.unwrap_or(0),
|
.unwrap_or(0),
|
||||||
anon_pages_per_node: {
|
anon_pages_per_node: {
|
||||||
let spl = &anon_line.split(" ").collect::<Vec<_>>()[1..];
|
let spl = &anon_line.split(' ').collect::<Vec<_>>()[1..];
|
||||||
spl.iter()
|
spl.iter()
|
||||||
.map(|x| {
|
.map(|x| {
|
||||||
x.split("=").collect::<Vec<_>>()[1]
|
x.split('=').collect::<Vec<_>>()[1]
|
||||||
.parse::<u64>()
|
.parse::<u64>()
|
||||||
.unwrap_or(0)
|
.unwrap_or(0)
|
||||||
})
|
})
|
||||||
@@ -169,79 +192,120 @@ fn parse_numa_stat(s: String) -> Result<NumaStat> {
|
|||||||
.parse::<u64>()
|
.parse::<u64>()
|
||||||
.unwrap_or(0),
|
.unwrap_or(0),
|
||||||
unevictable_pages_per_node: {
|
unevictable_pages_per_node: {
|
||||||
let spl = &unevict_line.split(" ").collect::<Vec<_>>()[1..];
|
let spl = &unevict_line.split(' ').collect::<Vec<_>>()[1..];
|
||||||
spl.iter()
|
spl.iter()
|
||||||
.map(|x| {
|
.map(|x| {
|
||||||
x.split("=").collect::<Vec<_>>()[1]
|
x.split('=').collect::<Vec<_>>()[1]
|
||||||
.parse::<u64>()
|
.parse::<u64>()
|
||||||
.unwrap_or(0)
|
.unwrap_or(0)
|
||||||
})
|
})
|
||||||
.collect()
|
.collect()
|
||||||
},
|
},
|
||||||
hierarchical_total_pages: hier_total_line
|
hierarchical_total_pages: {
|
||||||
.split(|x| x == ' ' || x == '=')
|
if !hier_total_line.is_empty() {
|
||||||
.collect::<Vec<_>>()[1]
|
hier_total_line
|
||||||
.parse::<u64>()
|
.split(|x| x == ' ' || x == '=')
|
||||||
.unwrap_or(0),
|
.collect::<Vec<_>>()[1]
|
||||||
|
.parse::<u64>()
|
||||||
|
.unwrap_or(0)
|
||||||
|
} else {
|
||||||
|
0
|
||||||
|
}
|
||||||
|
},
|
||||||
hierarchical_total_pages_per_node: {
|
hierarchical_total_pages_per_node: {
|
||||||
let spl = &hier_total_line.split(" ").collect::<Vec<_>>()[1..];
|
if !hier_total_line.is_empty() {
|
||||||
spl.iter()
|
let spl = &hier_total_line.split(' ').collect::<Vec<_>>()[1..];
|
||||||
.map(|x| {
|
spl.iter()
|
||||||
x.split("=").collect::<Vec<_>>()[1]
|
.map(|x| {
|
||||||
.parse::<u64>()
|
x.split('=').collect::<Vec<_>>()[1]
|
||||||
.unwrap_or(0)
|
.parse::<u64>()
|
||||||
})
|
.unwrap_or(0)
|
||||||
.collect()
|
})
|
||||||
|
.collect()
|
||||||
|
} else {
|
||||||
|
Vec::new()
|
||||||
|
}
|
||||||
|
},
|
||||||
|
hierarchical_file_pages: {
|
||||||
|
if !hier_file_line.is_empty() {
|
||||||
|
hier_file_line
|
||||||
|
.split(|x| x == ' ' || x == '=')
|
||||||
|
.collect::<Vec<_>>()[1]
|
||||||
|
.parse::<u64>()
|
||||||
|
.unwrap_or(0)
|
||||||
|
} else {
|
||||||
|
0
|
||||||
|
}
|
||||||
},
|
},
|
||||||
hierarchical_file_pages: hier_file_line
|
|
||||||
.split(|x| x == ' ' || x == '=')
|
|
||||||
.collect::<Vec<_>>()[1]
|
|
||||||
.parse::<u64>()
|
|
||||||
.unwrap_or(0),
|
|
||||||
hierarchical_file_pages_per_node: {
|
hierarchical_file_pages_per_node: {
|
||||||
let spl = &hier_file_line.split(" ").collect::<Vec<_>>()[1..];
|
if !hier_file_line.is_empty() {
|
||||||
spl.iter()
|
let spl = &hier_file_line.split(' ').collect::<Vec<_>>()[1..];
|
||||||
.map(|x| {
|
spl.iter()
|
||||||
x.split("=").collect::<Vec<_>>()[1]
|
.map(|x| {
|
||||||
.parse::<u64>()
|
x.split('=').collect::<Vec<_>>()[1]
|
||||||
.unwrap_or(0)
|
.parse::<u64>()
|
||||||
})
|
.unwrap_or(0)
|
||||||
.collect()
|
})
|
||||||
|
.collect()
|
||||||
|
} else {
|
||||||
|
Vec::new()
|
||||||
|
}
|
||||||
|
},
|
||||||
|
hierarchical_anon_pages: {
|
||||||
|
if !hier_anon_line.is_empty() {
|
||||||
|
hier_anon_line
|
||||||
|
.split(|x| x == ' ' || x == '=')
|
||||||
|
.collect::<Vec<_>>()[1]
|
||||||
|
.parse::<u64>()
|
||||||
|
.unwrap_or(0)
|
||||||
|
} else {
|
||||||
|
0
|
||||||
|
}
|
||||||
},
|
},
|
||||||
hierarchical_anon_pages: hier_anon_line
|
|
||||||
.split(|x| x == ' ' || x == '=')
|
|
||||||
.collect::<Vec<_>>()[1]
|
|
||||||
.parse::<u64>()
|
|
||||||
.unwrap_or(0),
|
|
||||||
hierarchical_anon_pages_per_node: {
|
hierarchical_anon_pages_per_node: {
|
||||||
let spl = &hier_anon_line.split(" ").collect::<Vec<_>>()[1..];
|
if !hier_anon_line.is_empty() {
|
||||||
spl.iter()
|
let spl = &hier_anon_line.split(' ').collect::<Vec<_>>()[1..];
|
||||||
.map(|x| {
|
spl.iter()
|
||||||
x.split("=").collect::<Vec<_>>()[1]
|
.map(|x| {
|
||||||
.parse::<u64>()
|
x.split('=').collect::<Vec<_>>()[1]
|
||||||
.unwrap_or(0)
|
.parse::<u64>()
|
||||||
})
|
.unwrap_or(0)
|
||||||
.collect()
|
})
|
||||||
|
.collect()
|
||||||
|
} else {
|
||||||
|
Vec::new()
|
||||||
|
}
|
||||||
|
},
|
||||||
|
hierarchical_unevictable_pages: {
|
||||||
|
if !hier_unevict_line.is_empty() {
|
||||||
|
hier_unevict_line
|
||||||
|
.split(|x| x == ' ' || x == '=')
|
||||||
|
.collect::<Vec<_>>()[1]
|
||||||
|
.parse::<u64>()
|
||||||
|
.unwrap_or(0)
|
||||||
|
} else {
|
||||||
|
0
|
||||||
|
}
|
||||||
},
|
},
|
||||||
hierarchical_unevictable_pages: hier_unevict_line
|
|
||||||
.split(|x| x == ' ' || x == '=')
|
|
||||||
.collect::<Vec<_>>()[1]
|
|
||||||
.parse::<u64>()
|
|
||||||
.unwrap_or(0),
|
|
||||||
hierarchical_unevictable_pages_per_node: {
|
hierarchical_unevictable_pages_per_node: {
|
||||||
let spl = &hier_unevict_line.split(" ").collect::<Vec<_>>()[1..];
|
if !hier_unevict_line.is_empty() {
|
||||||
spl.iter()
|
let spl = &hier_unevict_line.split(' ').collect::<Vec<_>>()[1..];
|
||||||
.map(|x| {
|
spl.iter()
|
||||||
x.split("=").collect::<Vec<_>>()[1]
|
.map(|x| {
|
||||||
.parse::<u64>()
|
x.split('=').collect::<Vec<_>>()[1]
|
||||||
.unwrap_or(0)
|
.parse::<u64>()
|
||||||
})
|
.unwrap_or(0)
|
||||||
.collect()
|
})
|
||||||
|
.collect()
|
||||||
|
} else {
|
||||||
|
Vec::new()
|
||||||
|
}
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Default, Debug, PartialEq, Eq)]
|
#[derive(Default, Debug, PartialEq, Eq)]
|
||||||
|
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
|
||||||
pub struct MemoryStat {
|
pub struct MemoryStat {
|
||||||
pub cache: u64,
|
pub cache: u64,
|
||||||
pub rss: u64,
|
pub rss: u64,
|
||||||
@@ -282,6 +346,7 @@ pub struct MemoryStat {
|
|||||||
pub raw: HashMap<String, u64>,
|
pub raw: HashMap<String, u64>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[allow(clippy::unnecessary_wraps)]
|
||||||
fn parse_memory_stat(s: String) -> Result<MemoryStat> {
|
fn parse_memory_stat(s: String) -> Result<MemoryStat> {
|
||||||
let mut raw = HashMap::new();
|
let mut raw = HashMap::new();
|
||||||
|
|
||||||
@@ -335,13 +400,14 @@ fn parse_memory_stat(s: String) -> Result<MemoryStat> {
|
|||||||
total_inactive_file: *raw.get("total_inactive_file").unwrap_or(&0),
|
total_inactive_file: *raw.get("total_inactive_file").unwrap_or(&0),
|
||||||
total_active_file: *raw.get("total_active_file").unwrap_or(&0),
|
total_active_file: *raw.get("total_active_file").unwrap_or(&0),
|
||||||
total_unevictable: *raw.get("total_unevictable").unwrap_or(&0),
|
total_unevictable: *raw.get("total_unevictable").unwrap_or(&0),
|
||||||
raw: raw,
|
raw,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Contains statistics about the current usage of memory and swap (together, not seperately) by
|
/// Contains statistics about the current usage of memory and swap (together, not seperately) by
|
||||||
/// the control group's tasks.
|
/// the control group's tasks.
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
|
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
|
||||||
pub struct MemSwap {
|
pub struct MemSwap {
|
||||||
/// How many times the limit has been hit.
|
/// How many times the limit has been hit.
|
||||||
pub fail_cnt: u64,
|
pub fail_cnt: u64,
|
||||||
@@ -356,6 +422,7 @@ pub struct MemSwap {
|
|||||||
/// State of and statistics gathered by the kernel about the memory usage of the control group's
|
/// State of and statistics gathered by the kernel about the memory usage of the control group's
|
||||||
/// tasks.
|
/// tasks.
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
|
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
|
||||||
pub struct Memory {
|
pub struct Memory {
|
||||||
/// How many times the limit has been hit.
|
/// How many times the limit has been hit.
|
||||||
pub fail_cnt: u64,
|
pub fail_cnt: u64,
|
||||||
@@ -400,6 +467,7 @@ pub struct Memory {
|
|||||||
/// The current state of and gathered statistics about the kernel's memory usage for TCP-related
|
/// The current state of and gathered statistics about the kernel's memory usage for TCP-related
|
||||||
/// data structures.
|
/// data structures.
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
|
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
|
||||||
pub struct Tcp {
|
pub struct Tcp {
|
||||||
/// How many times the limit has been hit.
|
/// How many times the limit has been hit.
|
||||||
pub fail_cnt: u64,
|
pub fail_cnt: u64,
|
||||||
@@ -418,6 +486,7 @@ pub struct Tcp {
|
|||||||
/// these tasks if it would think that the limits here would be violated. It's important to note
|
/// 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.
|
/// that interrupts in particular might not be able to enforce these limits.
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
|
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
|
||||||
pub struct Kmem {
|
pub struct Kmem {
|
||||||
/// How many times the limit has been hit.
|
/// How many times the limit has been hit.
|
||||||
pub fail_cnt: u64,
|
pub fail_cnt: u64,
|
||||||
@@ -474,7 +543,7 @@ impl MemController {
|
|||||||
Self {
|
Self {
|
||||||
base: root.clone(),
|
base: root.clone(),
|
||||||
path: root,
|
path: root,
|
||||||
v2: v2,
|
v2,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -489,8 +558,8 @@ impl MemController {
|
|||||||
for value in values {
|
for value in values {
|
||||||
let v = value.0;
|
let v = value.0;
|
||||||
let f = value.1;
|
let f = value.1;
|
||||||
if v.is_some() {
|
if let Some(v) = v {
|
||||||
let v = v.unwrap().to_string();
|
let v = v.to_string();
|
||||||
self.open_path(f, true).and_then(|mut file| {
|
self.open_path(f, true).and_then(|mut file| {
|
||||||
file.write_all(v.as_ref())
|
file.write_all(v.as_ref())
|
||||||
.map_err(|e| Error::with_cause(WriteFailed, e))
|
.map_err(|e| Error::with_cause(WriteFailed, e))
|
||||||
@@ -531,7 +600,7 @@ impl MemController {
|
|||||||
.open_path("memory.stat", false)
|
.open_path("memory.stat", false)
|
||||||
.and_then(read_string_from)
|
.and_then(read_string_from)
|
||||||
.and_then(parse_memory_stat)
|
.and_then(parse_memory_stat)
|
||||||
.unwrap_or(MemoryStat::default()),
|
.unwrap_or_default(),
|
||||||
swappiness: self
|
swappiness: self
|
||||||
.open_path("memory.swap.current", false)
|
.open_path("memory.swap.current", false)
|
||||||
.and_then(read_u64_from)
|
.and_then(read_u64_from)
|
||||||
@@ -575,12 +644,12 @@ impl MemController {
|
|||||||
.open_path("memory.numa_stat", false)
|
.open_path("memory.numa_stat", false)
|
||||||
.and_then(read_string_from)
|
.and_then(read_string_from)
|
||||||
.and_then(parse_numa_stat)
|
.and_then(parse_numa_stat)
|
||||||
.unwrap_or(NumaStat::default()),
|
.unwrap_or_default(),
|
||||||
oom_control: self
|
oom_control: self
|
||||||
.open_path("memory.oom_control", false)
|
.open_path("memory.oom_control", false)
|
||||||
.and_then(read_string_from)
|
.and_then(read_string_from)
|
||||||
.and_then(parse_oom_control)
|
.and_then(parse_oom_control)
|
||||||
.unwrap_or(OomControl::default()),
|
.unwrap_or_default(),
|
||||||
soft_limit_in_bytes: self
|
soft_limit_in_bytes: self
|
||||||
.open_path("memory.soft_limit_in_bytes", false)
|
.open_path("memory.soft_limit_in_bytes", false)
|
||||||
.and_then(read_i64_from)
|
.and_then(read_i64_from)
|
||||||
@@ -589,7 +658,7 @@ impl MemController {
|
|||||||
.open_path("memory.stat", false)
|
.open_path("memory.stat", false)
|
||||||
.and_then(read_string_from)
|
.and_then(read_string_from)
|
||||||
.and_then(parse_memory_stat)
|
.and_then(parse_memory_stat)
|
||||||
.unwrap_or(MemoryStat::default()),
|
.unwrap_or_default(),
|
||||||
swappiness: self
|
swappiness: self
|
||||||
.open_path("memory.swappiness", false)
|
.open_path("memory.swappiness", false)
|
||||||
.and_then(read_u64_from)
|
.and_then(read_u64_from)
|
||||||
@@ -623,7 +692,7 @@ impl MemController {
|
|||||||
slabinfo: self
|
slabinfo: self
|
||||||
.open_path("memory.kmem.slabinfo", false)
|
.open_path("memory.kmem.slabinfo", false)
|
||||||
.and_then(read_string_from)
|
.and_then(read_string_from)
|
||||||
.unwrap_or("".to_string()),
|
.unwrap_or_default(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -655,7 +724,7 @@ impl MemController {
|
|||||||
fail_cnt: self
|
fail_cnt: self
|
||||||
.open_path("memory.swap.events", false)
|
.open_path("memory.swap.events", false)
|
||||||
.and_then(flat_keyed_to_hashmap)
|
.and_then(flat_keyed_to_hashmap)
|
||||||
.and_then(|x| Ok(*x.get("fail").unwrap_or(&0) as u64))
|
.map(|x| *x.get("fail").unwrap_or(&0) as u64)
|
||||||
.unwrap(),
|
.unwrap(),
|
||||||
limit_in_bytes: self
|
limit_in_bytes: self
|
||||||
.open_path("memory.swap.max", false)
|
.open_path("memory.swap.max", false)
|
||||||
@@ -878,6 +947,7 @@ mod tests {
|
|||||||
use crate::memory::{
|
use crate::memory::{
|
||||||
parse_memory_stat, parse_numa_stat, parse_oom_control, MemoryStat, NumaStat, OomControl,
|
parse_memory_stat, parse_numa_stat, parse_oom_control, MemoryStat, NumaStat, OomControl,
|
||||||
};
|
};
|
||||||
|
|
||||||
static GOOD_VALUE: &str = "\
|
static GOOD_VALUE: &str = "\
|
||||||
total=51189 N0=51189 N1=123
|
total=51189 N0=51189 N1=123
|
||||||
file=50175 N0=50175 N1=123
|
file=50175 N0=50175 N1=123
|
||||||
@@ -889,7 +959,24 @@ hierarchical_anon=770402 N0=770402 N1=123
|
|||||||
hierarchical_unevictable=20 N0=20 N1=123
|
hierarchical_unevictable=20 N0=20 N1=123
|
||||||
";
|
";
|
||||||
|
|
||||||
static GOOD_OOMCONTROL_VAL: &str = "\
|
static GOOD_VALUE_NON_HIERARCHICAL: &str = "\
|
||||||
|
total=51189 N0=51189 N1=123
|
||||||
|
file=50175 N0=50175 N1=123
|
||||||
|
anon=1014 N0=1014 N1=123
|
||||||
|
unevictable=0 N0=0 N1=123
|
||||||
|
";
|
||||||
|
|
||||||
|
static GOOD_OOMCONTROL_VAL_1: &str = "\
|
||||||
|
oom_kill_disable 0
|
||||||
|
oom_kill 1337
|
||||||
|
";
|
||||||
|
|
||||||
|
static GOOD_OOMCONTROL_VAL_2: &str = "\
|
||||||
|
oom_kill_disable 0
|
||||||
|
under_oom 1
|
||||||
|
";
|
||||||
|
|
||||||
|
static GOOD_OOMCONTROL_VAL_3: &str = "\
|
||||||
oom_kill_disable 0
|
oom_kill_disable 0
|
||||||
under_oom 1
|
under_oom 1
|
||||||
oom_kill 1337
|
oom_kill 1337
|
||||||
@@ -959,11 +1046,61 @@ total_unevictable 81920
|
|||||||
hierarchical_unevictable_pages_per_node: vec![20, 123],
|
hierarchical_unevictable_pages_per_node: vec![20, 123],
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
let ok = parse_numa_stat(GOOD_VALUE_NON_HIERARCHICAL.to_string()).unwrap();
|
||||||
|
assert_eq!(
|
||||||
|
ok,
|
||||||
|
NumaStat {
|
||||||
|
total_pages: 51189,
|
||||||
|
total_pages_per_node: vec![51189, 123],
|
||||||
|
file_pages: 50175,
|
||||||
|
file_pages_per_node: vec![50175, 123],
|
||||||
|
anon_pages: 1014,
|
||||||
|
anon_pages_per_node: vec![1014, 123],
|
||||||
|
unevictable_pages: 0,
|
||||||
|
unevictable_pages_per_node: vec![0, 123],
|
||||||
|
|
||||||
|
hierarchical_total_pages: 0,
|
||||||
|
hierarchical_total_pages_per_node: vec![],
|
||||||
|
hierarchical_file_pages: 0,
|
||||||
|
hierarchical_file_pages_per_node: vec![],
|
||||||
|
hierarchical_anon_pages: 0,
|
||||||
|
hierarchical_anon_pages_per_node: vec![],
|
||||||
|
hierarchical_unevictable_pages: 0,
|
||||||
|
hierarchical_unevictable_pages_per_node: vec![],
|
||||||
|
}
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_parse_oom_control() {
|
fn test_parse_oom_control() {
|
||||||
let ok = parse_oom_control(GOOD_OOMCONTROL_VAL.to_string()).unwrap();
|
let ok = parse_oom_control("".to_string()).unwrap();
|
||||||
|
assert_eq!(
|
||||||
|
ok,
|
||||||
|
OomControl {
|
||||||
|
oom_kill_disable: false,
|
||||||
|
under_oom: false,
|
||||||
|
oom_kill: 0,
|
||||||
|
}
|
||||||
|
);
|
||||||
|
let ok = parse_oom_control(GOOD_OOMCONTROL_VAL_1.to_string()).unwrap();
|
||||||
|
assert_eq!(
|
||||||
|
ok,
|
||||||
|
OomControl {
|
||||||
|
oom_kill_disable: false,
|
||||||
|
under_oom: false,
|
||||||
|
oom_kill: 0,
|
||||||
|
}
|
||||||
|
);
|
||||||
|
let ok = parse_oom_control(GOOD_OOMCONTROL_VAL_2.to_string()).unwrap();
|
||||||
|
assert_eq!(
|
||||||
|
ok,
|
||||||
|
OomControl {
|
||||||
|
oom_kill_disable: false,
|
||||||
|
under_oom: true,
|
||||||
|
oom_kill: 0,
|
||||||
|
}
|
||||||
|
);
|
||||||
|
let ok = parse_oom_control(GOOD_OOMCONTROL_VAL_3.to_string()).unwrap();
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
ok,
|
ok,
|
||||||
OomControl {
|
OomControl {
|
||||||
@@ -1017,7 +1154,7 @@ total_unevictable 81920
|
|||||||
total_inactive_file: 1272135680,
|
total_inactive_file: 1272135680,
|
||||||
total_active_file: 2338816000,
|
total_active_file: 2338816000,
|
||||||
total_unevictable: 81920,
|
total_unevictable: 81920,
|
||||||
raw: raw,
|
raw,
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -49,7 +49,7 @@ impl ControllerInternal for NetClsController {
|
|||||||
|
|
||||||
update_and_test!(self, set_class, res.class_id, get_class);
|
update_and_test!(self, set_class, res.class_id, get_class);
|
||||||
|
|
||||||
return Ok(());
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -96,6 +96,6 @@ impl NetClsController {
|
|||||||
/// Get the network class id of the outgoing packets of the control group's tasks.
|
/// Get the network class id of the outgoing packets of the control group's tasks.
|
||||||
pub fn get_class(&self) -> Result<u64> {
|
pub fn get_class(&self) -> Result<u64> {
|
||||||
self.open_path("net_cls.classid", false)
|
self.open_path("net_cls.classid", false)
|
||||||
.and_then(|file| read_u64_from(file))
|
.and_then(read_u64_from)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -94,6 +94,7 @@ impl NetPrioController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// A map of priorities for each network interface.
|
/// A map of priorities for each network interface.
|
||||||
|
#[allow(clippy::iter_nth_zero, clippy::unnecessary_unwrap)]
|
||||||
pub fn ifpriomap(&self) -> Result<HashMap<String, u64>> {
|
pub fn ifpriomap(&self) -> Result<HashMap<String, u64>> {
|
||||||
self.open_path("net_prio.ifpriomap", false)
|
self.open_path("net_prio.ifpriomap", false)
|
||||||
.and_then(|file| {
|
.and_then(|file| {
|
||||||
@@ -105,6 +106,7 @@ impl NetPrioController {
|
|||||||
let mut acc = acc.unwrap();
|
let mut acc = acc.unwrap();
|
||||||
let l = line.unwrap();
|
let l = line.unwrap();
|
||||||
let mut sp = l.split_whitespace();
|
let mut sp = l.split_whitespace();
|
||||||
|
|
||||||
let ifname = sp.nth(0);
|
let ifname = sp.nth(0);
|
||||||
let ifprio = sp.nth(1);
|
let ifprio = sp.nth(1);
|
||||||
if ifname.is_none() || ifprio.is_none() {
|
if ifname.is_none() || ifprio.is_none() {
|
||||||
|
|||||||
@@ -96,7 +96,7 @@ impl PidController {
|
|||||||
Self {
|
Self {
|
||||||
base: root.clone(),
|
base: root.clone(),
|
||||||
path: root,
|
path: root,
|
||||||
v2: v2,
|
v2,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -66,7 +66,7 @@ impl SystemdController {
|
|||||||
Self {
|
Self {
|
||||||
base: root.clone(),
|
base: root.clone(),
|
||||||
path: root,
|
path: root,
|
||||||
v2: v2,
|
v2,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -48,7 +48,7 @@ fn test_cgroup_with_relative_paths() {
|
|||||||
let cg = Cgroup::load(h, String::from(cgroup_name));
|
let cg = Cgroup::load(h, String::from(cgroup_name));
|
||||||
{
|
{
|
||||||
let subsystems = cg.subsystems();
|
let subsystems = cg.subsystems();
|
||||||
subsystems.into_iter().for_each(|sub| match sub {
|
subsystems.iter().for_each(|sub| match sub {
|
||||||
Subsystem::Pid(c) => {
|
Subsystem::Pid(c) => {
|
||||||
let cgroup_path = c.path().to_str().unwrap();
|
let cgroup_path = c.path().to_str().unwrap();
|
||||||
let relative_path = "/pids/";
|
let relative_path = "/pids/";
|
||||||
|
|||||||
@@ -36,24 +36,23 @@ 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.len() > 0);
|
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());
|
||||||
assert_eq!((0, 0), set.cpus[0]);
|
assert_eq!((0, 0), set.cpus[0]);
|
||||||
|
|
||||||
// all cpus in system
|
// all cpus in system
|
||||||
let cpus =
|
let cpus = fs::read_to_string("/sys/fs/cgroup/cpuset.cpus.effective").unwrap_or_default();
|
||||||
fs::read_to_string("/sys/fs/cgroup/cpuset.cpus.effective").unwrap_or("".to_string());
|
|
||||||
let cpus = cpus.trim();
|
let cpus = cpus.trim();
|
||||||
if cpus != "" {
|
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);
|
||||||
@@ -73,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.len() > 0);
|
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.len() > 0);
|
assert!(!tasks.is_empty());
|
||||||
println!("tasks after added: {:?}", tasks);
|
println!("tasks after added: {:?}", tasks);
|
||||||
|
|
||||||
// remove task
|
// remove task
|
||||||
|
|||||||
@@ -27,7 +27,7 @@ fn test_devices_parsing() {
|
|||||||
DeviceType::All,
|
DeviceType::All,
|
||||||
-1,
|
-1,
|
||||||
-1,
|
-1,
|
||||||
&vec![
|
&[
|
||||||
DevicePermissions::Read,
|
DevicePermissions::Read,
|
||||||
DevicePermissions::Write,
|
DevicePermissions::Write,
|
||||||
DevicePermissions::MkNod,
|
DevicePermissions::MkNod,
|
||||||
@@ -42,7 +42,7 @@ fn test_devices_parsing() {
|
|||||||
|
|
||||||
// Now add mknod access to /dev/null device
|
// Now add mknod access to /dev/null device
|
||||||
devices
|
devices
|
||||||
.allow_device(DeviceType::Char, 1, 3, &vec![DevicePermissions::MkNod])
|
.allow_device(DeviceType::Char, 1, 3, &[DevicePermissions::MkNod])
|
||||||
.unwrap();
|
.unwrap();
|
||||||
let allowed_devices = devices.allowed_devices();
|
let allowed_devices = devices.allowed_devices();
|
||||||
assert!(allowed_devices.is_ok());
|
assert!(allowed_devices.is_ok());
|
||||||
|
|||||||
@@ -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
|
||||||
|
|||||||
@@ -62,7 +62,7 @@ fn test_pid_events_is_not_zero() {
|
|||||||
let before = pids.get_pid_events();
|
let before = pids.get_pid_events();
|
||||||
let before = before.unwrap();
|
let before = before.unwrap();
|
||||||
|
|
||||||
match fork() {
|
match unsafe { fork() } {
|
||||||
Ok(ForkResult::Parent { child, .. }) => {
|
Ok(ForkResult::Parent { child, .. }) => {
|
||||||
// move the process into the control group
|
// move the process into the control group
|
||||||
let _ = pids.add_task(&(pid_t::from(child) as u64).into());
|
let _ = pids.add_task(&(pid_t::from(child) as u64).into());
|
||||||
@@ -89,7 +89,7 @@ fn test_pid_events_is_not_zero() {
|
|||||||
Ok(ForkResult::Child) => loop {
|
Ok(ForkResult::Child) => loop {
|
||||||
let pids_max = pids.get_pid_max();
|
let pids_max = pids.get_pid_max();
|
||||||
if pids_max.is_ok() && pids_max.unwrap() == MaxValue::Value(1) {
|
if pids_max.is_ok() && pids_max.unwrap() == MaxValue::Value(1) {
|
||||||
if let Err(_) = fork() {
|
if unsafe { fork() }.is_err() {
|
||||||
unsafe { libc::exit(0) };
|
unsafe { libc::exit(0) };
|
||||||
} else {
|
} else {
|
||||||
unsafe { libc::exit(1) };
|
unsafe { libc::exit(1) };
|
||||||
|
|||||||
@@ -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