liboj-cgroups 0.1.0

Cgroup wrapping for liboj
Documentation
use super::*;

use std::time::Duration;

#[test]
fn test_cgroup_path() -> io::Result<()> {
    let _ctx = Builder::default().build()?;
    Ok(())
}

#[test]
fn test_cpu_controller() -> io::Result<()> {
    let ctx = Builder::new().add_subsystem(Subsystem::Cpu).build()?;
    let controller = ctx.cpu_controller();
    for (period, quota) in vec![
        (Duration::from_millis(200), Duration::from_millis(80)),
        (Duration::from_millis(150), Duration::from_millis(50)),
        (Duration::from_millis(550), Duration::from_millis(240)),
    ]
    .into_iter()
    {
        controller.cfs_period_us().write(&period)?;
        controller.cfs_quota_us().write(&quota)?;
        assert_eq!(controller.cfs_period_us().read()?, period);
        assert_eq!(controller.cfs_quota_us().read()?, quota);
    }
    Ok(())
}

#[test]
fn test_cpuacct_controller() -> io::Result<()> {
    trait IsZero {
        fn is_zero(&self) -> bool;
    }

    impl IsZero for Duration {
        fn is_zero(&self) -> bool {
            self.as_nanos() == 0
        }
    }

    let ctx = Builder::new().add_subsystem(Subsystem::Cpuacct).build()?;
    let controller = ctx.cpuacct_controller();
    controller.reset().reset()?;
    let stat = controller.stat().read()?;
    assert!(stat.user.is_zero());
    assert!(stat.system.is_zero());
    assert!(controller.usage().read()?.is_zero());
    assert!(controller.usage_user().read()?.is_zero());
    assert!(controller.usage_sys().read()?.is_zero());
    assert!(controller
        .usage_all()
        .read()?
        .iter()
        .enumerate()
        .all(|(index, usage)| usage.cpu == index
            && usage.user.is_zero()
            && usage.system.is_zero()));
    controller
        .usage_percpu()
        .read()?
        .iter()
        .for_each(|time| assert!(time.is_zero()));
    controller
        .usage_percpu_user()
        .read()?
        .iter()
        .for_each(|time| assert!(time.is_zero()));
    controller
        .usage_percpu_sys()
        .read()?
        .iter()
        .for_each(|time| assert!(time.is_zero()));
    Ok(())
}

#[test]
fn test_freezer_controller() -> io::Result<()> {
    use subsystem::freezer::State;
    let ctx = Builder::new().add_subsystem(Subsystem::Freezer).build()?;
    let controller = ctx.freezer_controller();
    assert_eq!(controller.state().read()?, State::Thawed);
    assert!(!controller.self_freezing().read()?);
    assert!(!controller.parent_freezing().read()?);
    controller.state().write(&State::Frozen)?;
    assert_eq!(controller.state().read()?, State::Frozen);
    assert!(controller.self_freezing().read()?);
    assert!(!controller.parent_freezing().read()?);
    Ok(())
}

#[test]
fn test_memory_controller() -> io::Result<()> {
    use subsystem::memory::Bytes;
    const ZERO: Bytes = Bytes::from_bytes(0);
    let ctx = Builder::new().add_subsystem(Subsystem::Memory).build()?;

    let controller = ctx.memory_controller();
    assert_eq!(controller.usage_in_bytes().read()?, ZERO);
    let default_limit = controller.limit_in_bytes().read()?;
    controller.limit_in_bytes().write(&Bytes::infinity())?;
    assert_eq!(controller.limit_in_bytes().read()?, default_limit);
    controller.limit_in_bytes().write(&"64M".parse()?)?;
    assert_eq!(controller.limit_in_bytes().read()?, "64M".parse()?);
    assert_eq!(controller.failcnt().read()?, 0);
    assert_eq!(controller.max_usage_in_bytes().read()?, ZERO);
    assert_eq!(controller.soft_limit_in_bytes().read()?, default_limit);
    let _ = controller.swappiness().read()?;
    controller.swappiness().write(&50)?;
    assert_eq!(controller.swappiness().read()?, 50);

    let swap_controller = controller.swap_memory_controller();
    assert_eq!(swap_controller.usage_in_bytes().read()?, ZERO);
    let default_swap_limit = swap_controller.limit_in_bytes().read()?;
    swap_controller.limit_in_bytes().write(&Bytes::infinity())?;
    assert_eq!(swap_controller.limit_in_bytes().read()?, default_swap_limit);
    swap_controller.limit_in_bytes().write(&"64M".parse()?)?;
    assert_eq!(swap_controller.limit_in_bytes().read()?, "64M".parse()?);
    assert_eq!(swap_controller.failcnt().read()?, 0);
    assert_eq!(swap_controller.max_usage_in_bytes().read()?, ZERO);

    Ok(())
}

#[test]
fn test_pids_controller() -> io::Result<()> {
    let ctx = Builder::new().add_subsystem(Subsystem::Pids).build()?;
    let controller = ctx.pids_controller();
    assert_eq!(controller.current().read()?, 0);
    assert!(controller.max().read()?.is_none());
    assert_eq!(controller.events().read()?, 0);
    Ok(())
}