Skip to content

Commit

Permalink
Implement events command for cgroup v1 stats
Browse files Browse the repository at this point in the history
  • Loading branch information
Furisto committed Jul 31, 2021
1 parent 95abb16 commit 7087b44
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 11 deletions.
12 changes: 7 additions & 5 deletions src/cgroups/v1/blkio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,11 +138,13 @@ impl Blkio {
}

fn get_throttling_policy_stats(cgroup_path: &Path) -> Result<BlkioStats> {
let mut stats = BlkioStats::default();

stats.service_bytes =
Self::parse_blkio_file(&cgroup_path.join(BLKIO_THROTTLE_IO_SERVICE_BYTES))?;
stats.serviced = Self::parse_blkio_file(&cgroup_path.join(BLKIO_THROTTLE_IO_SERVICED))?;
let stats = BlkioStats {
service_bytes: Self::parse_blkio_file(
&cgroup_path.join(BLKIO_THROTTLE_IO_SERVICE_BYTES),
)?,
serviced: Self::parse_blkio_file(&cgroup_path.join(BLKIO_THROTTLE_IO_SERVICED))?,
..Default::default()
};

Ok(stats)
}
Expand Down
5 changes: 1 addition & 4 deletions src/cgroups/v1/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,10 +166,7 @@ impl Memory {
fn hierarchy_enabled(cgroup_path: &Path) -> Result<bool> {
let hierarchy_path = cgroup_path.join(MEMORY_USE_HIERARCHY);
let hierarchy = common::read_cgroup_file(hierarchy_path)?;
let enabled = match hierarchy.trim() {
"1" => true,
_ => false,
};
let enabled = matches!(hierarchy.trim(), "1");

Ok(enabled)
}
Expand Down
2 changes: 1 addition & 1 deletion src/cgroups/v2/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,6 @@ impl CgroupManager for Manager {
}

fn stats(&self) -> Result<Stats> {
todo!();
Ok(Stats::default())
}
}
2 changes: 1 addition & 1 deletion src/cgroups/v2/systemd_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ impl CgroupManager for SystemDCGroupManager {
}

fn stats(&self) -> Result<Stats> {
todo!();
Ok(Stats::default())
}
}

Expand Down
57 changes: 57 additions & 0 deletions src/commands/events.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
use crate::{cgroups::common, utils};
use clap::Clap;
use std::{path::PathBuf, thread, time::Duration};

use anyhow::{bail, Context, Result};

use crate::container::{Container, ContainerStatus};

#[derive(Clap, Debug)]
pub struct Events {
/// Sets the stats collection interval in seconds (default: 5s)
#[clap(long, default_value = "5")]
pub interval: u32,
/// Display the container stats only once
#[clap(long)]
pub stats: bool,
/// Name of the container instance
pub container_id: String,
}

impl Events {
pub fn exec(&self, root_path: PathBuf) -> Result<()> {
let container_dir = root_path.join(&self.container_id);
if !container_dir.exists() {
log::debug!("{:?}", container_dir);
bail!("{} doesn't exist.", self.container_id)
}

let container = Container::load(container_dir)?.refresh_status()?;
if !container.state.status.eq(&ContainerStatus::Running) {
bail!("{} is not in running state", self.container_id);
}

let cgroups_path = utils::get_cgroup_path(
&container
.spec()?
.linux
.context("no linux in spec")?
.cgroups_path,
&self.container_id,
);
let use_systemd = container
.systemd()
.context("Could not determine cgroup manager")?;

let cgroup_manager = common::create_cgroup_manager(cgroups_path, use_systemd)?;
match self.stats {
true => println!("{:#?}", cgroup_manager.stats()),
false => loop {
println!("{:#?}", cgroup_manager.stats());
thread::sleep(Duration::from_secs(self.interval as u64));
},
}

Ok(())
}
}
1 change: 1 addition & 0 deletions src/commands/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
pub mod create;
pub mod delete;
pub mod events;
pub mod exec;
pub mod info;
pub mod kill;
Expand Down
4 changes: 4 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use clap::Clap;

use youki::commands::create;
use youki::commands::delete;
use youki::commands::events;
use youki::commands::exec;
use youki::commands::info;
use youki::commands::kill;
Expand Down Expand Up @@ -71,6 +72,8 @@ enum SubCommand {
Pause(pause::Pause),
#[clap(version = "0.0.1", author = "utam0k <k0ma@utam0k.jp>")]
Resume(resume::Resume),
#[clap(version = "0.0.1", author = "utam0k <k0ma@utam0k.jp>")]
Events(events::Events),
}

/// This is the entry point in the container runtime. The binary is run by a high-level container runtime,
Expand Down Expand Up @@ -104,5 +107,6 @@ fn main() -> Result<()> {
SubCommand::Spec(spec) => spec.exec(),
SubCommand::Pause(pause) => pause.exec(root_path, systemd_cgroup),
SubCommand::Resume(resume) => resume.exec(root_path, systemd_cgroup),
SubCommand::Events(events) => events.exec(root_path),
}
}

0 comments on commit 7087b44

Please sign in to comment.