Compare commits

..

8 Commits

Author SHA1 Message Date
Fupan Li
225be2cdbb Merge pull request #111 from Tim-Zhang/release-0.3.1
release: v0.3.1
2023-02-07 16:34:03 +08:00
Tim Zhang
51779d6915 release: v0.3.1
To include patches #105, #108, #110.

Signed-off-by: Tim Zhang <tim@hyper.sh>
2023-02-07 16:22:48 +08:00
Bin Liu
5ea28f076c Merge pull request #105 from amitlevy/patch-1
Fixed documentation for cpu quota and period
2023-02-07 15:10:48 +08:00
Fupan Li
aa74f34a91 Merge pull request #110 from liubin/fix/add-discard-to-blkio
blkio: add discard field to IoService
2023-02-06 14:59:08 +08:00
Amit Levy
328428ace4 Fixed documentation for cpu quota and period
nanoseconds -> microseonds

Noticed while debugging, also fits Red Hat documentation
https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/6/html/resource_management_guide/sec-cpu
, and I read the implementation to confirm

Signed-off-by: Amit Levy <amitlevy49@gmail.com>
2023-02-06 11:40:42 +08:00
Chao Wu
c8bb7e1c7e Merge pull request #108 from jongwu/error_cause
error: replace cause method with source
2023-02-05 23:05:04 +08:00
bin liu
25a1340123 blkio: add discard field to IoService
Some system has the `Discard` field in io service data, current the
library can't handle it correctly.

With this commit the blkio can get metrics whether it has the discard field.

Fixes: #109

Signed-off-by: bin liu <liubin0329@gmail.com>
2023-02-02 22:38:46 +08:00
Jianyong Wu
4203075f19 error: replace cause method with source
cause method for std::error::Error is depricated by rust, source method
is recommended.

Fixes: #107
Signed-off-by: Jianyong Wu <jianyong.wu@arm.com>
2023-02-02 14:00:11 +08:00
4 changed files with 84 additions and 60 deletions

View File

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

View File

@@ -43,7 +43,7 @@ pub struct BlkIoData {
pub data: u64,
}
#[derive(Eq, PartialEq, Debug)]
#[derive(Eq, PartialEq, Debug, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
/// Per-device activity from the control group.
pub struct IoService {
@@ -59,6 +59,8 @@ pub struct IoService {
pub sync: u64,
/// How many items were asynchronously transferred.
pub r#async: u64,
/// How many items were discarded.
pub discard: u64,
/// Total number of items transferred.
pub total: u64,
}
@@ -87,44 +89,62 @@ pub struct IoStat {
}
fn parse_io_service(s: String) -> Result<Vec<IoService>> {
s.lines()
let mut io_services = Vec::<IoService>::new();
let mut io_service = IoService::default();
let lines = s
.lines()
.filter(|x| x.split_whitespace().count() == 3)
.map(|x| {
let mut spl = x.split_whitespace();
(spl.next().unwrap(), spl.next().unwrap(), spl.next().unwrap())
(
spl.next().unwrap(),
spl.next().unwrap(),
spl.next().unwrap(),
)
})
.map(|(a, b, c)| {
let mut spl = a.split(':');
(spl.next().unwrap(), spl.next().unwrap(), b, c)
})
.collect::<Vec<_>>()
.chunks(5)
.map(|x| {
match x {
[(major, minor, "Read", read_val), (_, _, "Write", write_val),
(_, _, "Sync", sync_val), (_, _, "Async", async_val),
(_, _, "Total", total_val)] =>
Some(IoService {
major: major.parse::<i16>().unwrap(),
minor: minor.parse::<i16>().unwrap(),
read: read_val.parse::<u64>().unwrap(),
write: write_val.parse::<u64>().unwrap(),
sync: sync_val.parse::<u64>().unwrap(),
r#async: async_val.parse::<u64>().unwrap(),
total: total_val.parse::<u64>().unwrap(),
}),
_ => None,
}
})
.fold(Ok(Vec::new()), |acc, x| {
if acc.is_err() || x.is_none() {
Err(Error::new(ParseError))
} else {
let mut acc = acc.unwrap();
acc.push(x.unwrap());
Ok(acc)
}
(
spl.next().unwrap().parse::<i16>(),
spl.next().unwrap().parse::<i16>(),
b,
c,
)
})
.collect::<Vec<_>>();
for (major_num, minor_num, op, val) in lines.iter() {
let major = *major_num.as_ref().map_err(|_| Error::new(ParseError))?;
let minor = *minor_num.as_ref().map_err(|_| Error::new(ParseError))?;
if (major != io_service.major || minor != io_service.minor) && io_service.major != 0 {
// new block device
io_services.push(io_service);
io_service = IoService::default();
}
io_service.major = major;
io_service.minor = minor;
let val = val.parse::<u64>().map_err(|_| Error::new(ParseError))?;
match *op {
"Read" => io_service.read = val,
"Write" => io_service.write = val,
"Sync" => io_service.sync = val,
"Async" => io_service.r#async = val,
"Discard" => io_service.discard = val,
"Total" => io_service.total = val,
_ => {}
}
}
if io_service.major != 0 {
io_services.push(io_service);
}
Ok(io_services)
}
fn get_value(s: &str) -> String {
@@ -817,6 +837,7 @@ mod test {
8:32 Write 0
8:32 Sync 4280320
8:32 Async 0
8:32 Discard 1
8:32 Total 4280320
8:48 Read 5705479168
8:48 Write 56096055296
@@ -833,28 +854,6 @@ mod test {
8:0 Sync 7192576
8:0 Async 0
8:0 Total 7192576
Total 61823067136
";
static TEST_WRONG_VALUE: &str = "\
8:32 Read 4280320
8:32 Write 0
8:32 Async 0
8:32 Total 4280320 8:48 Read 5705479168
8:48 Write 56096055296
8:48 Sync 11213923328
8:48 Async 50587611136
8:48 Total 61801534464
8:16 Read 10059776
8:16 Write 0
8:16 Sync 10059776
8:16 Async 0
8:16 Total 10059776
8:0 Read 7192576
8:0 Write 0
8:0 Sync 7192576
8:0 Async 0
8:0 Total 7192576
Total 61823067136
";
@@ -884,6 +883,7 @@ Total 61823067136
write: 0,
sync: 4280320,
r#async: 0,
discard: 1,
total: 4280320,
},
IoService {
@@ -893,6 +893,7 @@ Total 61823067136
write: 56096055296,
sync: 11213923328,
r#async: 50587611136,
discard: 0,
total: 61801534464,
},
IoService {
@@ -902,6 +903,7 @@ Total 61823067136
write: 0,
sync: 10059776,
r#async: 0,
discard: 0,
total: 10059776,
},
IoService {
@@ -911,12 +913,34 @@ Total 61823067136
write: 0,
sync: 7192576,
r#async: 0,
discard: 0,
total: 7192576,
}
]
);
let err = parse_io_service(TEST_WRONG_VALUE.to_string()).unwrap_err();
assert_eq!(err.kind(), &ErrorKind::ParseError,);
let invalid_values = vec![
"\
8:32 Read 4280320
8:32 Write a
8:32 Async 1
",
"\
8:32 Read 4280320
b:32 Write 1
8:32 Async 1
",
"\
8:32 Read 4280320
8:32 Write 1
8:c Async 1
",
];
for value in invalid_values {
let err = parse_io_service(value.to_string()).unwrap_err();
assert_eq!(err.kind(), &ErrorKind::ParseError,);
}
}
#[test]

View File

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

View File

@@ -604,9 +604,9 @@ pub struct CpuResources {
/// Weight of how much of the total CPU time should this control group get. Note that this is
/// hierarchical, so this is weighted against the siblings of this control group.
pub shares: Option<u64>,
/// In one `period`, how much can the tasks run in nanoseconds.
/// In one `period`, how much can the tasks run in microseconds.
pub quota: Option<i64>,
/// Period of time in nanoseconds.
/// Period of time in microseconds.
pub period: Option<u64>,
/// This is currently a no-operation.
pub realtime_runtime: Option<i64>,