Skip to content

Commit

Permalink
Add support for catching signals
Browse files Browse the repository at this point in the history
This is used if the user ends an Aperf run using Ctrl+c or kill <pid>.
We send the same signal to any child processes launched by aperf.
  • Loading branch information
janaknat committed Jul 26, 2024
1 parent a5df597 commit 4b910d2
Show file tree
Hide file tree
Showing 6 changed files with 122 additions and 85 deletions.
84 changes: 28 additions & 56 deletions Cargo.lock

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

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ name = "aperf"
path = "src/bin/aperf.rs"

[dependencies]
nix = { version = "0.29.0", features = ["signal", "poll"] }
clap = { version = "4.2.5", features = ["derive"] }
serde = { version = "1.0", features = ["derive"] }
chrono = { version = "0.4", features = ["serde"] }
Expand All @@ -29,7 +30,7 @@ thiserror = "1.0"
log = "0.4.21"
env_logger = "0.10.0"
lazy_static = "1.4.0"
timerfd = "1.3.0"
timerfd = "1.6.0"
procfs = "0.12.0"
ctor = "0.2.6"
sysinfo = "0.26.2"
Expand Down
7 changes: 7 additions & 0 deletions src/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ use kernel_config::KernelConfig;
use log::trace;
use meminfodata::{MeminfoData, MeminfoDataRaw};
use netstat::{Netstat, NetstatRaw};
use nix::sys::{signal, signal::Signal};
use perf_profile::{PerfProfile, PerfProfileRaw};
use perf_stat::{PerfStat, PerfStatRaw};
use processes::{Processes, ProcessesRaw};
Expand All @@ -60,6 +61,7 @@ pub struct CollectorParams {
pub run_name: String,
pub profile: HashMap<String, String>,
pub tmp_dir: PathBuf,
pub signal: Signal,
}

impl CollectorParams {
Expand All @@ -72,6 +74,7 @@ impl CollectorParams {
run_name: String::new(),
profile: HashMap::new(),
tmp_dir: PathBuf::new(),
signal: signal::SIGTERM,
}
}
}
Expand Down Expand Up @@ -109,6 +112,10 @@ impl DataType {
self.is_profile_option = true;
}

pub fn set_signal(&mut self, signal: Signal) {
self.collector_params.signal = signal;
}

pub fn init_data_type(&mut self, param: InitParams) -> Result<()> {
trace!("Initializing data type...");
let name = format!(
Expand Down
5 changes: 5 additions & 0 deletions src/data/java_profile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use crate::{PDError, PERFORMANCE_DATA, VISUALIZATION_DATA};
use anyhow::Result;
use ctor::ctor;
use log::{debug, error, trace};
use nix::{sys::signal, unistd::Pid};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::io::Write;
Expand Down Expand Up @@ -201,6 +202,10 @@ impl CollectData for JavaProfileRaw {
}

fn finish_data_collection(&mut self, params: CollectorParams) -> Result<()> {
for child in ASPROF_CHILDREN.lock().unwrap().iter() {
signal::kill(Pid::from_raw(child.id() as i32), params.signal)?;
}

trace!("Waiting for asprof profile collection to complete...");
while ASPROF_CHILDREN.lock().unwrap().len() > 0 {
match ASPROF_CHILDREN.lock().unwrap().pop().unwrap().wait() {
Expand Down
6 changes: 6 additions & 0 deletions src/data/perf_profile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use crate::{PDError, PERFORMANCE_DATA, VISUALIZATION_DATA};
use anyhow::Result;
use ctor::ctor;
use log::{error, trace};
use nix::{sys::signal, unistd::Pid};
use serde::{Deserialize, Serialize};
use std::fs;
use std::io::Write;
Expand Down Expand Up @@ -79,6 +80,11 @@ impl CollectData for PerfProfileRaw {
Some(_) => {}
}

signal::kill(
Pid::from_raw(child.as_mut().unwrap().id() as i32),
params.signal,
)?;

trace!("Waiting for perf profile collection to complete...");
match child.as_mut().unwrap().wait() {
Err(e) => {
Expand Down
Loading

0 comments on commit 4b910d2

Please sign in to comment.