Skip to content

Commit

Permalink
fix send trait
Browse files Browse the repository at this point in the history
  • Loading branch information
sfishel18 committed Jun 4, 2022
1 parent 9b8657c commit d8e3e0e
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/exporters/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.get_stat_buffer().len() as u64),
metric_value: MetricValueType::IntUnsigned(socket.get_stat_buffer_passive().len() as u64),
});

self.data.push(Metric {
Expand Down
2 changes: 1 addition & 1 deletion src/exporters/warpten.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ impl Warp10Exporter {
for socket in &self.topology.sockets {
let mut metric_labels = labels.clone();
metric_labels.push(warp10::Label::new("socket_id", &socket.get_id().to_string()));
let metric_value = socket.get_stat_buffer().len();
let metric_value = socket.get_stat_buffer_passive().len();
data.push(warp10::Data::new(
time::OffsetDateTime::now_utc(),
None,
Expand Down
4 changes: 4 additions & 0 deletions src/sensors/debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,10 @@ impl Socket for DebugSocket {
&mut self.stat_buffer
}

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

fn get_debug_type(&self) -> String {
String::from("Debug")
}
Expand Down
4 changes: 4 additions & 0 deletions src/sensors/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -648,6 +648,10 @@ impl Socket for CPUSocket {
&mut self.stat_buffer
}

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

fn get_debug_type(&self) -> String {
String::from("CPU")
}
Expand Down
24 changes: 14 additions & 10 deletions src/sensors/socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use dyn_clone::DynClone;
use crate::sensors::units;
use super::{Record, Domain, CPUStat, CPUCore, RecordGenerator, StatsGenerator};

pub trait Socket: DynClone {
pub trait Socket: DynClone + Send {
fn read_record_uj(&self) -> Result<Record, Box<dyn Error>>;
fn get_record_buffer(&mut self) -> &mut Vec<Record>;
fn get_record_buffer_passive(&self) -> &Vec<Record>;
Expand All @@ -16,6 +16,7 @@ pub trait Socket: DynClone {
fn get_domains_passive(&self) -> &Vec<Domain>;
fn get_domains(&mut self) -> &mut Vec<Domain>;
fn get_stat_buffer(&mut self) -> &mut Vec<CPUStat>;
fn get_stat_buffer_passive(&self) -> &Vec<CPUStat>;
fn read_stats(&self) -> Option<CPUStat>;
fn get_cores(&mut self) -> &mut Vec<CPUCore>;
fn get_cores_passive(&self) -> &Vec<CPUCore>;
Expand Down Expand Up @@ -133,38 +134,41 @@ impl StatsGenerator for dyn Socket {
/// Generates a new CPUStat object storing current usage statistics of the socket
/// and stores it in the stat_buffer.
fn refresh_stats(&mut self) {
let stat_buffer = self.get_stat_buffer();
let stat_buffer = self.get_stat_buffer_passive();
if !stat_buffer.is_empty() {
self.clean_old_stats();
}
stat_buffer.insert(0, self.read_stats().unwrap());
let stats = self.read_stats();
self.get_stat_buffer().insert(0, stats.unwrap());
}

/// Checks the size in memory of stats_buffer and deletes as many CPUStat
/// instances from the buffer to make it smaller in memory than buffer_max_kbytes.
fn clean_old_stats(&mut self) {
let id = self.get_id();
let buffer_max_kbytes = self.get_buffer_max_kbytes();
let stat_buffer = self.get_stat_buffer();
let stat_ptr = &stat_buffer[0];
let size_of_stat = size_of_val(stat_ptr);
let curr_size = size_of_stat * stat_buffer.len();
trace!("current_size of stats in socket {}: {}", self.get_id(), curr_size);
trace!("current_size of stats in socket {}: {}", id, curr_size);
trace!(
"estimated max nb of socket stats: {}",
self.get_buffer_max_kbytes() as f32 * 1000.0 / size_of_stat as f32
buffer_max_kbytes as f32 * 1000.0 / size_of_stat as f32
);
if curr_size > (self.get_buffer_max_kbytes() * 1000) as usize {
let size_diff = curr_size - (self.get_buffer_max_kbytes() * 1000) as usize;
if curr_size > (buffer_max_kbytes * 1000) as usize {
let size_diff = curr_size - (buffer_max_kbytes * 1000) as usize;
trace!(
"socket {} size_diff: {} size of: {}",
self.get_id(),
id,
size_diff,
size_of_stat
);
if size_diff > size_of_stat {
let nb_stats_to_delete = size_diff as f32 / size_of_stat as f32;
trace!(
"socket {} nb_stats_to_delete: {} size_diff: {} size of: {}",
self.get_id(),
id,
nb_stats_to_delete,
size_diff,
size_of_stat
Expand All @@ -175,7 +179,7 @@ impl StatsGenerator for dyn Socket {
let res = stat_buffer.pop();
debug!(
"Cleaning stat buffer of socket {}, removing: {:?}",
self.get_id(), res
id, res
);
}
}
Expand Down

0 comments on commit d8e3e0e

Please sign in to comment.