use std::fs::File;
use std::io::{Read, Write};
use std::path::PathBuf;
use error::*;
use error::ErrorKind::*;
use {
ControllIdentifier, ControllerInternal, Controllers, CpuResources, Resources, Subsystem,
};
#[derive(Debug, Clone)]
pub struct CpuController {
base: PathBuf,
path: PathBuf,
}
#[derive(Debug)]
pub struct Cpu {
pub stat: String,
}
impl ControllerInternal for CpuController {
fn control_type(&self) -> Controllers {
Controllers::Cpu
}
fn get_path(&self) -> &PathBuf {
&self.path
}
fn get_path_mut(&mut self) -> &mut PathBuf {
&mut self.path
}
fn get_base(&self) -> &PathBuf {
&self.base
}
fn apply(&self, res: &Resources) -> Result<()> {
let res: &CpuResources = &res.cpu;
if res.update_values {
let _ = self.set_shares(res.shares);
if self.shares()? != res.shares as u64 {
return Err(Error::new(ErrorKind::Other));
}
let _ = self.set_cfs_period(res.period);
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()? != res.quota as u64 {
return Err(Error::new(ErrorKind::Other));
}
}
Ok(())
}
}
impl ControllIdentifier for CpuController {
fn controller_type() -> Controllers {
Controllers::Cpu
}
}
impl<'a> From<&'a Subsystem> for &'a CpuController {
fn from(sub: &'a Subsystem) -> &'a CpuController {
unsafe {
match sub {
Subsystem::Cpu(c) => c,
_ => {
assert_eq!(1, 0);
::std::mem::uninitialized()
}
}
}
}
}
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(|e| Error::with_cause(ParseError, e)),
Err(e) => Err(Error::with_cause(ReadFailed, e)),
}
}
impl CpuController {
pub fn new(oroot: PathBuf) -> Self {
let mut root = oroot;
root.push(Self::controller_type().to_string());
Self {
base: root.clone(),
path: root,
}
}
pub fn cpu(&self) -> Cpu {
Cpu {
stat: self
.open_path("cpu.stat", false)
.and_then(|mut file| {
let mut s = String::new();
let res = file.read_to_string(&mut s);
match res {
Ok(_) => Ok(s),
Err(e) => Err(Error::with_cause(ReadFailed, e)),
}
}).unwrap_or("".to_string()),
}
}
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(|e| Error::with_cause(WriteFailed, e))
})
}
pub fn shares(&self) -> Result<u64> {
self.open_path("cpu.shares", false).and_then(read_u64_from)
}
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(|e| Error::with_cause(WriteFailed, e))
})
}
pub fn cfs_period(&self) -> Result<u64> {
self.open_path("cpu.cfs_period_us", false)
.and_then(read_u64_from)
}
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(|e| Error::with_cause(WriteFailed, e))
})
}
pub fn cfs_quota(&self) -> Result<u64> {
self.open_path("cpu.cfs_quota_us", false)
.and_then(read_u64_from)
}
}