Implement ::std::error::Error

This commit is contained in:
Sam Wilson
2018-10-10 14:03:28 -04:00
committed by Levente Kurusa
parent 84ae587360
commit 7826b798bd
18 changed files with 392 additions and 293 deletions
+24 -21
View File
@@ -7,8 +7,11 @@ use std::fs::File;
use std::io::{Read, Write};
use std::path::PathBuf;
use error::*;
use error::ErrorKind::*;
use {
CgroupError, ControllIdentifier, Controller, Controllers, CpuResources, Resources, Subsystem,
ControllIdentifier, Controller, Controllers, CpuResources, Resources, Subsystem,
};
/// A controller that allows controlling the `cpu` subsystem of a Cgroup.
@@ -48,25 +51,25 @@ impl Controller for CpuController {
&self.base
}
fn apply(&self, res: &Resources) -> Result<(), CgroupError> {
fn apply(&self, res: &Resources) -> Result<()> {
// get the resources that apply to this controller
let res: &CpuResources = &res.cpu;
if res.update_values {
// apply pid_max
let _ = self.set_shares(res.shares);
if self.shares() != Ok(res.shares as u64) {
return Err(CgroupError::Unknown);
if self.shares()? != res.shares as u64 {
return Err(Error::new(ErrorKind::Other));
}
let _ = self.set_cfs_period(res.period);
if self.cfs_period() != Ok(res.period as u64) {
return Err(CgroupError::Unknown);
if self.cfs_period()? != res.period as u64 {
return Err(Error::new(ErrorKind::Other));
}
let _ = self.set_cfs_quota(res.quota as u64);
if self.cfs_quota() != Ok(res.quota as u64) {
return Err(CgroupError::Unknown);
if self.cfs_quota()? != res.quota as u64 {
return Err(Error::new(ErrorKind::Other));
}
// TODO: rt properties (CONFIG_RT_GROUP_SCHED) are not yet supported
@@ -96,11 +99,11 @@ impl<'a> From<&'a Subsystem> for &'a CpuController {
}
}
fn read_u64_from(mut file: File) -> Result<u64, CgroupError> {
fn read_u64_from(mut file: File) -> Result<u64> {
let mut string = String::new();
match file.read_to_string(&mut string) {
Ok(_) => string.trim().parse().map_err(|_| CgroupError::ParseError),
Err(e) => Err(CgroupError::ReadError(e)),
Ok(_) => string.trim().parse().map_err(|e| Error::with_cause(ParseError, e)),
Err(e) => Err(Error::with_cause(ReadFailed, e)),
}
}
@@ -125,7 +128,7 @@ impl CpuController {
let res = file.read_to_string(&mut s);
match res {
Ok(_) => Ok(s),
Err(e) => Err(CgroupError::ReadError(e)),
Err(e) => Err(Error::with_cause(ReadFailed, e)),
}
}).unwrap_or("".to_string()),
}
@@ -137,49 +140,49 @@ impl CpuController {
/// For example, setting control group `A`'s `shares` to `100`, and control group `B`'s
/// `shares` to `200` ensures that control group `B` receives twice as much as CPU bandwidth.
/// (Assuming both `A` and `B` are of the same parent)
pub fn set_shares(&self, shares: u64) -> Result<(), CgroupError> {
pub fn set_shares(&self, shares: u64) -> Result<()> {
self.open_path("cpu.shares", true).and_then(|mut file| {
file.write_all(shares.to_string().as_ref())
.map_err(CgroupError::WriteError)
.map_err(|e| Error::with_cause(WriteFailed, e))
})
}
/// Retrieve the CPU bandwidth that this control group (relative to other control groups and
/// this control group's parent) can use.
pub fn shares(&self) -> Result<u64, CgroupError> {
pub fn shares(&self) -> Result<u64> {
self.open_path("cpu.shares", false).and_then(read_u64_from)
}
/// Specify a period (when using the CFS scheduler) of time in microseconds for how often this
/// control group's access to the CPU should be reallocated.
pub fn set_cfs_period(&self, us: u64) -> Result<(), CgroupError> {
pub fn set_cfs_period(&self, us: u64) -> Result<()> {
self.open_path("cpu.cfs_period_us", true)
.and_then(|mut file| {
file.write_all(us.to_string().as_ref())
.map_err(CgroupError::WriteError)
.map_err(|e| Error::with_cause(WriteFailed, e))
})
}
/// Retrieve the period of time of how often this cgroup's access to the CPU should be
/// reallocated in microseconds.
pub fn cfs_period(&self) -> Result<u64, CgroupError> {
pub fn cfs_period(&self) -> Result<u64> {
self.open_path("cpu.cfs_period_us", false)
.and_then(read_u64_from)
}
/// Specify a quota (when using the CFS scheduler) of time in microseconds for which all tasks
/// in this control group can run during one period (see: `set_cfs_period()`).
pub fn set_cfs_quota(&self, us: u64) -> Result<(), CgroupError> {
pub fn set_cfs_quota(&self, us: u64) -> Result<()> {
self.open_path("cpu.cfs_quota_us", true)
.and_then(|mut file| {
file.write_all(us.to_string().as_ref())
.map_err(CgroupError::WriteError)
.map_err(|e| Error::with_cause(WriteFailed, e))
})
}
/// Retrieve the quota of time for which all tasks in this cgroup can run during one period, in
/// microseconds.
pub fn cfs_quota(&self) -> Result<u64, CgroupError> {
pub fn cfs_quota(&self) -> Result<u64> {
self.open_path("cpu.cfs_quota_us", false)
.and_then(read_u64_from)
}