From cef09988ac6451c0a0d314340f0c6697f61ef41f Mon Sep 17 00:00:00 2001 From: Fabian Freyer Date: Sat, 7 Jul 2018 07:47:21 +0200 Subject: [PATCH] python: expose RACCT statistics --- bindings/python/Cargo.toml | 1 + bindings/python/src/lib.rs | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/bindings/python/Cargo.toml b/bindings/python/Cargo.toml index 288ca3075..1f64f9d1e 100644 --- a/bindings/python/Cargo.toml +++ b/bindings/python/Cargo.toml @@ -12,6 +12,7 @@ crate-type = ["cdylib"] [dependencies] jail = "^0" +rctl = "^0" [dependencies.pyo3] version = "0.2" diff --git a/bindings/python/src/lib.rs b/bindings/python/src/lib.rs index 27df79561..b2a90475c 100644 --- a/bindings/python/src/lib.rs +++ b/bindings/python/src/lib.rs @@ -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}; @@ -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> { + 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]