Skip to content
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
48 changes: 0 additions & 48 deletions crates/exec-harness/src/analysis/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use crate::BenchmarkCommand;
use crate::constants;
use crate::uri;
use instrument_hooks_bindings::InstrumentHooks;
use std::path::PathBuf;
use std::process::Command;

mod ld_preload_check;
Expand Down Expand Up @@ -65,57 +64,10 @@ pub fn perform_with_valgrind(commands: Vec<BenchmarkCommand>) -> Result<()> {

let status = child.wait().context("Failed to execute command")?;

bail_if_command_spawned_subprocesses_under_valgrind(child.id())?;

if !status.success() {
bail!("Command exited with non-zero status: {status}");
}
}

Ok(())
}

/// Checks if the benchmark process spawned subprocesses under valgrind by looking for <pid>.out
/// files in the profile folder.
///
/// The presence of <pid>.out files where <pid> is greater than the benchmark process pid indicates
/// that the benchmark process spawned subprocesses. This .out file will be almost empty, with a 0
/// cost reported due to the disabled instrumentation.
///
/// We currently do not support measuring processes that spawn subprocesses under valgrind, because
/// valgrind will not have its instrumentation in the new process.
/// The LD_PRELOAD trick that we use to inject our instrumentation into the benchmark process only
/// works for the first process.
///
/// TODO(COD-2163): Remove this once we support nested processes under valgrind
fn bail_if_command_spawned_subprocesses_under_valgrind(pid: u32) -> Result<()> {
let Some(profile_folder) = std::env::var_os("CODSPEED_PROFILE_FOLDER") else {
debug!("CODSPEED_PROFILE_FOLDER is not set, skipping subprocess detection");
return Ok(());
};

let profile_folder = PathBuf::from(profile_folder);

// Bail if any <pid>.out where <pid> > pid of the benchmark process exists in the profile
// folder, which indicates that the benchmark process spawned subprocesses.
for entry in std::fs::read_dir(profile_folder)? {
let entry = entry?;
let file_name = entry.file_name();
let file_name = file_name.to_string_lossy();

if let Some(stripped) = file_name.strip_suffix(".out") {
if let Ok(subprocess_pid) = stripped.parse::<u32>() {
if subprocess_pid > pid {
bail!(
"The codspeed CLI in CPU Simulation mode does not support measuring processes that spawn other processes yet.\n\n\
Please either:\n\
- Use the walltime measurement mode, or\n\
- Benchmark a process that does not create subprocesses"
)
}
}
}
}

Ok(())
}
1 change: 1 addition & 0 deletions src/cli/exec/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ fn build_orchestrator_config(
fair_sched: args.shared.experimental.experimental_fair_sched,
cycle_estimation: args.shared.cycle_estimation,
exclude_allocations: args.shared.exclude_allocations,
track_simulation_subprocesses: args.shared.track_simulation_subprocesses,
})
}

Expand Down
2 changes: 2 additions & 0 deletions src/cli/run/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ impl RunArgs {
base: None,
cycle_estimation: true,
exclude_allocations: true,
track_simulation_subprocesses: false,
profiler_run_args: ProfilerRunArgs {
enable_profiler: false,
enable_perf: None,
Expand Down Expand Up @@ -133,6 +134,7 @@ fn build_orchestrator_config(
fair_sched: args.shared.experimental.experimental_fair_sched,
cycle_estimation: args.shared.cycle_estimation,
exclude_allocations: args.shared.exclude_allocations,
track_simulation_subprocesses: args.shared.track_simulation_subprocesses,
})
}

Expand Down
9 changes: 9 additions & 0 deletions src/cli/shared.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,15 @@ pub struct ExecAndRunSharedArgs {
)]
pub exclude_allocations: bool,

/// Measure the subprocesses spawned by the benchmarked process in simulation mode.
#[arg(
long,
env = "CODSPEED_TRACK_SIMULATION_SUBPROCESSES",
default_value_t = false,
action = clap::ArgAction::Set
)]
pub track_simulation_subprocesses: bool,

#[command(flatten)]
pub profiler_run_args: ProfilerRunArgs,

Expand Down
8 changes: 8 additions & 0 deletions src/executor/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,9 @@ pub struct OrchestratorConfig {
pub cycle_estimation: bool,
/// Signal the backend to exclude memory allocation time from simulation results.
pub exclude_allocations: bool,
/// Inherit valgrind's instrumentation state across a traced exec, so the cost of
/// subprocesses spawned by a benchmark is measured too.
pub track_simulation_subprocesses: bool,
}

/// Per-execution configuration passed to executors.
Expand Down Expand Up @@ -132,6 +135,9 @@ pub struct ExecutorConfig {
pub cycle_estimation: bool,
/// Signal the backend to exclude memory allocation time from simulation results.
pub exclude_allocations: bool,
/// Inherit valgrind's instrumentation state across a traced exec, so the cost of
/// subprocesses spawned by a benchmark is measured too.
pub track_simulation_subprocesses: bool,
}

#[derive(Debug, Clone, PartialEq)]
Expand Down Expand Up @@ -203,6 +209,7 @@ impl OrchestratorConfig {
fair_sched: self.fair_sched,
cycle_estimation: self.cycle_estimation,
exclude_allocations: self.exclude_allocations,
track_simulation_subprocesses: self.track_simulation_subprocesses,
}
}
}
Expand Down Expand Up @@ -237,6 +244,7 @@ impl OrchestratorConfig {
fair_sched: false,
cycle_estimation: true,
exclude_allocations: true,
track_simulation_subprocesses: false,
}
}
}
Expand Down
23 changes: 8 additions & 15 deletions src/executor/valgrind/measure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,19 @@ fn get_valgrind_args(tool: &SimulationTool, config: &ExecutorConfig) -> Vec<Stri
"--I1=32768,8,64",
"--D1=32768,8,64",
"--LL=8388608,16,64",
"--instr-atstart=no",
"--collect-systime=nsec",
"--read-inline-info=yes",
]
.iter()
.map(|x| x.to_string())
.collect();

args.push(if config.track_simulation_subprocesses {
"--instr-atstart=inherit".to_string()
} else {
"--instr-atstart=no".to_string()
});

match tool {
SimulationTool::Callgrind => {
if config.cycle_estimation {
Expand All @@ -44,14 +49,14 @@ fn get_valgrind_args(tool: &SimulationTool, config: &ExecutorConfig) -> Vec<Stri
args.push("--compress-strings=no".to_string());
args.push("--combine-dumps=yes".to_string());
args.push("--dump-line=no".to_string());
args.push("--separate-threads=yes".to_string());
}
SimulationTool::Tracegrind => {
args.push("--tool=tracegrind".to_string());
}
}

// Valgrind changes argv[0] when tracing Nix's rustup wrapper, which breaks rustup proxy detection.
let children_skip_patterns = ["*esbuild", "*.rustup-wrapped"];
let children_skip_patterns = ["*esbuild"];
args.push(format!(
"--trace-children-skip={}",
children_skip_patterns.join(",")
Expand Down Expand Up @@ -240,18 +245,6 @@ mod tests {
(script_status, out_status)
}

#[test]
fn test_valgrind_skips_rustup_wrapped_proxy() {
let config = ExecutorConfig::test();
let args = get_valgrind_args(&SimulationTool::Callgrind, &config);
let skip_arg = args
.iter()
.find(|arg| arg.starts_with("--trace-children-skip="))
.unwrap();

assert!(skip_arg.contains("*.rustup-wrapped"));
}

#[test]
fn test_run_wrapper_script() {
temp_env::with_var("TEST_ENV_VAR", "test_value".into(), || {
Expand Down