From 0af3b61ee29a0033d0b681c0b3c353bdf12b61dc Mon Sep 17 00:00:00 2001 From: Benoit Petit Date: Fri, 29 Jan 2021 17:43:25 +0100 Subject: [PATCH] feat: adding optional --prefix option to ask scaph to look in PREFIX/proc and PREFIX/sys/class/powercap instead of the default paths. This enables to run scaph in docker with only RO mountpoins. (see discussion https://github.com/hubblo-org/scaphandre/pull/57 for more details) --- src/lib.rs | 2 ++ src/main.rs | 9 +++++++++ src/sensors/mod.rs | 6 +++--- src/sensors/powercap_rapl.rs | 5 +++-- tests/integration.rs | 2 +- 5 files changed, 18 insertions(+), 6 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 2baf8d0d..41fc9b2f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -31,6 +31,7 @@ fn get_sensor(matches: &ArgMatches) -> Box { .parse() .unwrap(), matches.is_present("vm"), + get_argument(matches, "prefix") ), _ => PowercapRAPLSensor::new( get_argument(matches, "sensor-buffer-per-socket-max-kB") @@ -40,6 +41,7 @@ fn get_sensor(matches: &ArgMatches) -> Box { .parse() .unwrap(), matches.is_present("vm"), + get_argument(matches, "prefix") ), }; Box::new(sensor) diff --git a/src/main.rs b/src/main.rs index 724ead29..234f48ea 100644 --- a/src/main.rs +++ b/src/main.rs @@ -53,8 +53,17 @@ fn main() { .long("vm") .required(false) .takes_value(false) + ).arg( + Arg::with_name("prefix") + .value_name("prefix") + .help("Ask scaphandre to look for data in a specific location instead of the default one (will look in $PREFIX/sys/class/powercap and $PREFIX/proc).") + .long("prefix") + .required(false) + .takes_value(false) + .default_value("") ); + for exp in res { let mut subcmd = SubCommand::with_name(exp).about( match exp { diff --git a/src/sensors/mod.rs b/src/sensors/mod.rs index 32fbcdcd..f3ea80ab 100644 --- a/src/sensors/mod.rs +++ b/src/sensors/mod.rs @@ -1090,14 +1090,14 @@ mod tests { #[test] fn read_topology_stats() { - let mut sensor = powercap_rapl::PowercapRAPLSensor::new(8, 8, false); + let mut sensor = powercap_rapl::PowercapRAPLSensor::new(8, 8, false, String::from("")); let topo = (*sensor.get_topology()).unwrap(); println!("{:?}", topo.read_stats()); } #[test] fn read_core_stats() { - let mut sensor = powercap_rapl::PowercapRAPLSensor::new(8, 8, false); + let mut sensor = powercap_rapl::PowercapRAPLSensor::new(8, 8, false, String::from("")); let mut topo = (*sensor.get_topology()).unwrap(); for s in topo.get_sockets() { for c in s.get_cores() { @@ -1108,7 +1108,7 @@ mod tests { #[test] fn read_socket_stats() { - let mut sensor = powercap_rapl::PowercapRAPLSensor::new(8, 8, false); + let mut sensor = powercap_rapl::PowercapRAPLSensor::new(8, 8, false, String::from("")); let mut topo = (*sensor.get_topology()).unwrap(); for s in topo.get_sockets() { println!("{:?}", s.read_stats()); diff --git a/src/sensors/powercap_rapl.rs b/src/sensors/powercap_rapl.rs index fdf3a146..e3102b0b 100644 --- a/src/sensors/powercap_rapl.rs +++ b/src/sensors/powercap_rapl.rs @@ -21,8 +21,9 @@ impl PowercapRAPLSensor { buffer_per_socket_max_kbytes: u16, buffer_per_domain_max_kbytes: u16, virtual_machine: bool, + prefix: String ) -> PowercapRAPLSensor { - let mut powercap_path = String::from("/sys/class/powercap"); + let mut powercap_path = String::from(format!("{}/sys/class/powercap", prefix)); if virtual_machine { powercap_path = String::from("/var/scaphandre"); if let Ok(val) = env::var("SCAPHANDRE_POWERCAP_PATH") { @@ -127,7 +128,7 @@ mod tests { } #[test] fn get_topology_returns_topology_type() { - let mut sensor = PowercapRAPLSensor::new(1, 1, false); + let mut sensor = PowercapRAPLSensor::new(1, 1, false, String::from("")); let topology = sensor.get_topology(); assert_eq!( "alloc::boxed::Box>", diff --git a/tests/integration.rs b/tests/integration.rs index a3dd4c2e..463ff387 100644 --- a/tests/integration.rs +++ b/tests/integration.rs @@ -5,7 +5,7 @@ use std::fs::{create_dir, read_dir}; #[test] fn exporter_qemu() { - let sensor = PowercapRAPLSensor::new(1, 1, false); + let sensor = PowercapRAPLSensor::new(1, 1, false, String::from("")); let mut exporter = QemuExporter::new(Box::new(sensor)); // Create integration_tests directory if it does not exist let curdir = current_dir().unwrap();