Skip to content

Commit

Permalink
Wait for 'perf record' to finish
Browse files Browse the repository at this point in the history
  • Loading branch information
janaknat committed Apr 23, 2024
1 parent fb135b5 commit 3813164
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 2 deletions.
19 changes: 19 additions & 0 deletions src/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,13 @@ impl DataType {
Ok(())
}

pub fn finish_data_collection(&mut self) -> Result<()> {
trace!("Finish data collection...");
self.data
.finish_data_collection(self.collector_params.clone())?;
Ok(())
}

pub fn after_data_collection(&mut self) -> Result<()> {
trace!("Running post collection actions...");
self.data
Expand Down Expand Up @@ -202,6 +209,14 @@ macro_rules! data {
Ok(())
}

fn finish_data_collection(&mut self, params: CollectorParams) -> Result<()> {
match self {
$(
Data::$x(ref mut value) => value.finish_data_collection(params)?,
)*
}
Ok(())
}
fn after_data_collection(&mut self, params: CollectorParams) -> Result<()> {
match self {
$(
Expand Down Expand Up @@ -301,6 +316,10 @@ pub trait CollectData {
noop!();
Ok(())
}
fn finish_data_collection(&mut self, _params: CollectorParams) -> Result<()> {
noop!();
Ok(())
}
fn after_data_collection(&mut self, _params: CollectorParams) -> Result<()> {
noop!();
Ok(())
Expand Down
30 changes: 28 additions & 2 deletions src/data/perf_profile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,15 @@ use ctor::ctor;
use log::{error, trace};
use serde::{Deserialize, Serialize};
use std::io::ErrorKind;
use std::process::Command;
use std::process::{Child, Command};
use std::sync::Mutex;

pub static PERF_PROFILE_FILE_NAME: &str = "perf_profile";

lazy_static! {
pub static ref PERF_CHILD: Mutex<Option<Child>> = Mutex::new(None);
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct PerfProfileRaw {
pub data: String,
Expand Down Expand Up @@ -55,14 +60,35 @@ impl CollectData for PerfProfileRaw {
}
error!("Skipping Perf profile collection.");
}
Ok(_) => trace!("Recording Perf profiling data."),
Ok(child) => {
trace!("Recording Perf profiling data.");
*PERF_CHILD.lock().unwrap() = Some(child);
}
}
Ok(())
}

fn collect_data(&mut self) -> Result<()> {
Ok(())
}

fn finish_data_collection(&mut self, _params: CollectorParams) -> Result<()> {
let mut child = PERF_CHILD.lock().unwrap();
match child.as_ref() {
None => return Ok(()),
Some(_) => {}
}

trace!("Waiting for perf profile collection to complete...");
match child.as_mut().unwrap().wait() {
Err(e) => {
error!("'perf' did not exit successfully: {}", e);
return Ok(());
}
Ok(_) => trace!("'perf record' executed successfully."),
}
Ok(())
}
}

#[derive(Serialize, Deserialize, Debug, Clone)]
Expand Down
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,9 @@ impl PerformanceData {
let data_collection_time = time::Instant::now() - current;
debug!("Collection time: {:?}", data_collection_time);
}
for (_name, datatype) in self.collectors.iter_mut() {
datatype.finish_data_collection()?;
}
for (_name, datatype) in self.collectors.iter_mut() {
datatype.after_data_collection()?;
}
Expand Down

0 comments on commit 3813164

Please sign in to comment.