Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add debug sensor for development workflow on hardware that does… #176

Open
wants to merge 4 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ k8s-sync = { version = "0.2.3", optional = true }
#k8s-sync = { path = "../rs-k8s-sync", optional = true }
hyper = { version = "0.14", features = ["full"], optional = true }
tokio = { version = "1", features = ["full"], optional = true}
dyn-clone = "1.0.5"

[features]
default = ["prometheus", "riemann", "warp10", "containers", "json"]
Expand Down
6 changes: 3 additions & 3 deletions src/exporters/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ impl JSONExporter {
.filter_map(|socket| {
if let Some(metric) = metrics_iter.find(|x| {
if x.name == "scaph_socket_power_microwatts" {
socket.id
socket.get_id()
== x.attributes
.get("socket_id")
.unwrap()
Expand All @@ -247,7 +247,7 @@ impl JSONExporter {
.unwrap()
.parse::<u16>()
.unwrap()
== socket.id
== socket.get_id()
})
.map(|d| Domain {
name: d.name.clone(),
Expand All @@ -257,7 +257,7 @@ impl JSONExporter {
.collect::<Vec<_>>();

Some(Socket {
id: socket.id,
id: socket.get_id(),
consumption: (socket_power as f32),
domains,
timestamp: metric.timestamp.as_secs_f64(),
Expand Down
12 changes: 6 additions & 6 deletions src/exporters/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ impl MetricGenerator {

for socket in &self.topology.sockets {
let mut attributes = HashMap::new();
attributes.insert("socket_id".to_string(), socket.id.to_string());
attributes.insert("socket_id".to_string(), socket.get_id().to_string());

self.data.push(Metric {
name: String::from("scaph_self_socket_stats_nb"),
Expand All @@ -331,7 +331,7 @@ impl MetricGenerator {
tags: vec!["scaphandre".to_string()],
attributes: attributes.clone(),
description: String::from("Number of CPUStat traces stored for each socket"),
metric_value: MetricValueType::IntUnsigned(socket.stat_buffer.len() as u64),
metric_value: MetricValueType::IntUnsigned(socket.get_stat_buffer_passive().len() as u64),
});

self.data.push(Metric {
Expand All @@ -346,10 +346,10 @@ impl MetricGenerator {
description: String::from(
"Number of energy consumption Records stored for each socket",
),
metric_value: MetricValueType::IntUnsigned(socket.record_buffer.len() as u64),
metric_value: MetricValueType::IntUnsigned(socket.get_record_buffer_passive().len() as u64),
});

for domain in &socket.domains {
for domain in socket.get_domains_passive() {
attributes.insert("rapl_domain_name".to_string(), domain.name.to_string());

self.data.push(Metric {
Expand Down Expand Up @@ -422,7 +422,7 @@ impl MetricGenerator {
let metric_timestamp = metric.timestamp;

let mut attributes = HashMap::new();
attributes.insert("socket_id".to_string(), socket.id.to_string());
attributes.insert("socket_id".to_string(), socket.get_id().to_string());

self.data.push(Metric {
name: String::from("scaph_socket_energy_microjoules"),
Expand Down Expand Up @@ -466,7 +466,7 @@ impl MetricGenerator {
let mut attributes = HashMap::new();
attributes.insert("domain_name".to_string(), domain.name.clone());
attributes.insert("domain_id".to_string(), domain.id.to_string());
attributes.insert("socket_id".to_string(), socket.id.to_string());
attributes.insert("socket_id".to_string(), socket.get_id().to_string());

self.data.push(Metric {
name: String::from("scaph_domain_energy_microjoules"),
Expand Down
8 changes: 4 additions & 4 deletions src/exporters/warpten.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,16 +235,16 @@ impl Warp10Exporter {

for socket in &self.topology.sockets {
let mut metric_labels = labels.clone();
metric_labels.push(warp10::Label::new("socket_id", &socket.id.to_string()));
let metric_value = socket.stat_buffer.len();
metric_labels.push(warp10::Label::new("socket_id", &socket.get_id().to_string()));
let metric_value = socket.get_stat_buffer_passive().len();
data.push(warp10::Data::new(
time::OffsetDateTime::now_utc(),
None,
String::from("scaph_self_socket_stats_nb"),
metric_labels.clone(),
warp10::Value::Int(metric_value as i32),
));
let metric_value = socket.record_buffer.len();
let metric_value = socket.get_record_buffer_passive().len();
data.push(warp10::Data::new(
time::OffsetDateTime::now_utc(),
None,
Expand Down Expand Up @@ -277,7 +277,7 @@ impl Warp10Exporter {
}
}

for domain in &socket.domains {
for domain in socket.get_domains_passive() {
let mut metric_labels = labels.clone();
metric_labels.push(warp10::Label::new("rapl_domain_name", &domain.name));
let metric_value = domain.record_buffer.len();
Expand Down
20 changes: 12 additions & 8 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use exporters::{
json::JSONExporter, prometheus::PrometheusExporter, qemu::QemuExporter,
riemann::RiemannExporter, stdout::StdoutExporter, warpten::Warp10Exporter, Exporter,
};
use sensors::{powercap_rapl::PowercapRAPLSensor, Sensor};
use sensors::{powercap_rapl::PowercapRAPLSensor, Sensor, debug::DebugSensor};
use std::collections::HashMap;
use std::time::{Duration, SystemTime};

Expand All @@ -27,27 +27,31 @@ fn get_argument(matches: &ArgMatches, arg: &'static str) -> String {

/// Helper function to get a Sensor instance from ArgMatches
fn get_sensor(matches: &ArgMatches) -> Box<dyn Sensor> {
let sensor = match &get_argument(matches, "sensor")[..] {
"powercap_rapl" => PowercapRAPLSensor::new(
match &get_argument(matches, "sensor")[..] {
"powercap_rapl" => Box::new(PowercapRAPLSensor::new(
get_argument(matches, "sensor-buffer-per-socket-max-kB")
.parse()
.unwrap(),
get_argument(matches, "sensor-buffer-per-domain-max-kB")
.parse()
.unwrap(),
matches.is_present("vm"),
),
_ => PowercapRAPLSensor::new(
)),
"debug" => Box::new(DebugSensor::new(
get_argument(matches, "sensor-buffer-per-socket-max-kB")
.parse()
.unwrap(),
)),
_ => Box::new(PowercapRAPLSensor::new(
get_argument(matches, "sensor-buffer-per-socket-max-kB")
.parse()
.unwrap(),
get_argument(matches, "sensor-buffer-per-domain-max-kB")
.parse()
.unwrap(),
matches.is_present("vm"),
),
};
Box::new(sensor)
))
}
}

/// Matches the sensor and exporter name and options requested from the command line and
Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
use clap::{crate_authors, crate_version, App, AppSettings, Arg, SubCommand};
use scaphandre::{get_exporters_options, run};
fn main() {
let sensors = ["powercap_rapl"];
let sensors = ["powercap_rapl", "debug"];
let exporters_options = get_exporters_options();
let exporters = exporters_options.keys();
let exporters: Vec<&str> = exporters.into_iter().map(|x| x.as_str()).collect();
Expand Down
127 changes: 127 additions & 0 deletions src/sensors/debug.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
use crate::sensors::{Sensor,Topology,Domain,Socket,Record,CPUStat,CPUCore};
use std::error::Error;
use super::{utils::{current_system_time_since_epoch}, units};

pub struct DebugSensor {
buffer_per_socket_max_kbytes: u16,
}

impl DebugSensor {
pub fn new(buffer_per_socket_max_kbytes: u16,) -> DebugSensor {
DebugSensor {
buffer_per_socket_max_kbytes
}
}
}

impl Sensor for DebugSensor {
fn generate_topology(&self) -> Result<Topology, Box<dyn Error>> {
let mut topo = Topology::new();
topo.safe_add_socket(DebugSocket::new(1234, self.buffer_per_socket_max_kbytes));
topo.safe_add_domain_to_socket(
1234,
4321,
"debug domain",
"debug domain uj_counter",
self.buffer_per_socket_max_kbytes
);
Ok(topo)
}

fn get_topology(&mut self) -> Box<Option<Topology>> {
Box::new(self.generate_topology().ok())
}
}

#[derive(Debug, Clone)]
pub struct DebugSocket {
/// Numerical ID of the CPU socket (physical_id in /proc/cpuinfo)
pub id: u16,
/// RAPL domains attached to the socket
pub domains: Vec<Domain>,
/// Comsumption records measured and stored by scaphandre for this socket.
pub record_buffer: Vec<Record>,
/// Maximum size of the record_buffer in kilobytes.
pub buffer_max_kbytes: u16,
/// CPU cores (core_id in /proc/cpuinfo) attached to the socket.
pub cpu_cores: Vec<CPUCore>,
/// Usage statistics records stored for this socket.
pub stat_buffer: Vec<CPUStat>,
}

impl DebugSocket {
pub fn new(id: u16, buffer_max_kbytes: u16) -> DebugSocket {
DebugSocket {
id,
domains: vec![],
record_buffer: vec![],
buffer_max_kbytes,
cpu_cores: vec![],
stat_buffer: vec![]
}
}
}

impl Socket for DebugSocket {
fn read_record_uj(&self) -> Result<Record, Box<dyn Error>> {
Ok(Record::new(
current_system_time_since_epoch(),
String::from("7081760374"),
units::Unit::MicroJoule,
))
}

/// Combines stats from all CPU cores owned byu the socket and returns
/// a CpuTime struct containing stats for the whole socket.
fn read_stats(&self) -> Option<CPUStat> {
None
}

fn get_id(&self) -> u16 {
self.id
}

fn get_record_buffer(&mut self) -> &mut Vec<Record> {
&mut self.record_buffer
}

fn get_record_buffer_passive(&self) -> &Vec<Record> {
&self.record_buffer
}

fn get_buffer_max_kbytes(&self) -> u16 {
self.buffer_max_kbytes
}

/// Returns a mutable reference to the domains vector.
fn get_domains(&mut self) -> &mut Vec<Domain> {
&mut self.domains
}

/// Returns a immutable reference to the domains vector.
fn get_domains_passive(&self) -> &Vec<Domain> {
&self.domains
}

/// Returns a mutable reference to the CPU cores vector.
fn get_cores(&mut self) -> &mut Vec<CPUCore> {
&mut self.cpu_cores
}

/// Returns a immutable reference to the CPU cores vector.
fn get_cores_passive(&self) -> &Vec<CPUCore> {
&self.cpu_cores
}

fn get_stat_buffer(&mut self) -> &mut Vec<CPUStat> {
&mut self.stat_buffer
}

fn get_stat_buffer_passive(&self) -> &Vec<CPUStat> {
&self.stat_buffer
}

fn get_debug_type(&self) -> String {
String::from("Debug")
}
}
Loading