Skip to content

Commit

Permalink
python: expose RACCT statistics
Browse files Browse the repository at this point in the history
  • Loading branch information
fabianfreyer committed Jul 7, 2018
1 parent 80e2182 commit cef0998
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
1 change: 1 addition & 0 deletions bindings/python/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ crate-type = ["cdylib"]

[dependencies]
jail = "^0"
rctl = "^0"

[dependencies.pyo3]
version = "0.2"
Expand Down
33 changes: 33 additions & 0 deletions bindings/python/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#![feature(proc_macro, proc_macro_path_invoc, specialization, const_fn)]
extern crate jail;
extern crate pyo3;
extern crate rctl;

use std::collections::HashMap;

use pyo3::prelude::*;
use pyo3::py::{class, methods, modinit};
Expand Down Expand Up @@ -66,6 +69,36 @@ impl RunningJail {
.map_err(|_| exc::SystemError::new("Jail stop failed"))?;
Ok(())
}

/// Get RACCT resource accounting information
#[getter]
fn get_racct_usage(&self) -> PyResult<HashMap<String, usize>> {
let usage = self.inner.racct_statistics();
let usage_map = usage.map_err(|e| match e {
native::JailError::RctlError(rctl::Error::InvalidKernelState(s)) => match s {
rctl::State::Disabled => exc::SystemError::new(
"Resource accounting is disabled. To enable resource \
accounting, set the `kern.racct.enable` tunable to 1.",
),
rctl::State::NotPresent => exc::SystemError::new(
"Resource accounting is not enabled in the kernel. \
This feature requires the kernel to be compiled with \
`OPTION RACCT` set. Current GENERIC kernels should \
have this option set.",
),
rctl::State::Enabled => exc::SystemError::new(
"rctl::Error::InvalidKernelState returned but state \
is enabled. This really shouldn't happen.",
),
},
_ => exc::SystemError::new("Could not get RACCT accounting information"),
})?;

Ok(usage_map
.iter()
.map(|(resource, metric)| (format!("{}", resource), *metric))
.collect())
}
}

#[class]
Expand Down

0 comments on commit cef0998

Please sign in to comment.