From fc71ca0a4098fa6d206767085db425cc15b815f8 Mon Sep 17 00:00:00 2001 From: Adrien Cacciaguerra Date: Tue, 14 Jan 2025 10:00:21 +0000 Subject: [PATCH] feat(executor): add cmd base env to all executors --- src/run/runner/executor.rs | 19 ++++++++++++++++-- src/run/runner/valgrind/executor.rs | 4 +++- src/run/runner/valgrind/measure.rs | 29 ++++++++++++++-------------- src/run/runner/wall_time/executor.rs | 9 +-------- 4 files changed, 36 insertions(+), 25 deletions(-) diff --git a/src/run/runner/executor.rs b/src/run/runner/executor.rs index 9332596f..b1d65bcf 100644 --- a/src/run/runner/executor.rs +++ b/src/run/runner/executor.rs @@ -1,9 +1,11 @@ -use async_trait::async_trait; - +use super::helpers::env::BASE_INJECTED_ENV; use super::interfaces::{ExecutorName, RunData}; use crate::prelude::*; use crate::run::instruments::mongo_tracer::MongoTracer; use crate::run::{check_system::SystemInfo, config::Config}; +use async_trait::async_trait; +use std::collections::HashMap; +use std::path::Path; #[async_trait(?Send)] pub trait Executor { @@ -32,4 +34,17 @@ pub trait Executor { system_info: &SystemInfo, run_data: &RunData, ) -> Result<()>; + + /// Gets the base environment for the command + /// + /// Later on, we will want to refactor this and create the cmd directly in a trait function + fn get_cmd_base_envs(&self, profile_folder: &Path) -> HashMap<&str, String> { + let mut hashmap = BASE_INJECTED_ENV.clone(); + hashmap.insert("CODSPEED_RUNNER_MODE", self.name().to_string()); + hashmap.insert( + "CODSPEED_PROFILE_FOLDER", + profile_folder.to_str().unwrap().to_string(), + ); + hashmap + } } diff --git a/src/run/runner/valgrind/executor.rs b/src/run/runner/valgrind/executor.rs index c2bbdb12..10fdf0f3 100644 --- a/src/run/runner/valgrind/executor.rs +++ b/src/run/runner/valgrind/executor.rs @@ -36,8 +36,10 @@ impl Executor for ValgrindExecutor { run_data: &RunData, mongo_tracer: &Option, ) -> Result<()> { + let base_env = self.get_cmd_base_envs(&run_data.profile_folder); + //TODO: add valgrind version check - measure::measure(config, &run_data.profile_folder, mongo_tracer)?; + measure::measure(&base_env, config, &run_data.profile_folder, mongo_tracer)?; Ok(()) } diff --git a/src/run/runner/valgrind/measure.rs b/src/run/runner/valgrind/measure.rs index 697da749..f0834d42 100644 --- a/src/run/runner/valgrind/measure.rs +++ b/src/run/runner/valgrind/measure.rs @@ -1,11 +1,11 @@ use crate::prelude::*; -use crate::run::runner::helpers::env::BASE_INJECTED_ENV; use crate::run::runner::helpers::get_bench_command::get_bench_command; use crate::run::runner::helpers::run_command_with_log_pipe::run_command_with_log_pipe; use crate::run::runner::valgrind::helpers::ignored_objects_path::get_objects_path_to_ignore; use crate::run::runner::valgrind::helpers::introspected_nodejs::setup_introspected_nodejs; use crate::run::{config::Config, instruments::mongo_tracer::MongoTracer}; use lazy_static::lazy_static; +use std::collections::HashMap; use std::env; use std::fs::canonicalize; use std::path::Path; @@ -42,27 +42,28 @@ lazy_static! { } pub fn measure( + base_env: &HashMap<&str, String>, config: &Config, profile_folder: &Path, mongo_tracer: &Option, ) -> Result<()> { // Create the command let mut cmd = Command::new("setarch"); + cmd.envs(base_env); cmd.arg(ARCH).arg("-R"); // Configure the environment - cmd.envs(BASE_INJECTED_ENV.iter()) - .env( - "PATH", - format!( - "{}:{}", - setup_introspected_nodejs() - .map_err(|e| anyhow!("failed to setup NodeJS introspection. {}", e))? - .to_str() - .unwrap(), - env::var("PATH").unwrap_or_default(), - ), - ) - .env("PYTHONMALLOC", "malloc"); + cmd.env( + "PATH", + format!( + "{}:{}", + setup_introspected_nodejs() + .map_err(|e| anyhow!("failed to setup NodeJS introspection. {}", e))? + .to_str() + .unwrap(), + env::var("PATH").unwrap_or_default(), + ), + ) + .env("PYTHONMALLOC", "malloc"); if let Some(cwd) = &config.working_directory { let abs_cwd = canonicalize(cwd)?; diff --git a/src/run/runner/wall_time/executor.rs b/src/run/runner/wall_time/executor.rs index ddec14c2..8aa37c4d 100644 --- a/src/run/runner/wall_time/executor.rs +++ b/src/run/runner/wall_time/executor.rs @@ -2,7 +2,6 @@ use crate::prelude::*; use crate::run::instruments::mongo_tracer::MongoTracer; use crate::run::runner::executor::Executor; -use crate::run::runner::helpers::env::BASE_INJECTED_ENV; use crate::run::runner::helpers::get_bench_command::get_bench_command; use crate::run::runner::helpers::run_command_with_log_pipe::run_command_with_log_pipe; use crate::run::runner::{ExecutorName, RunData}; @@ -38,13 +37,7 @@ impl Executor for WallTimeExecutor { _mongo_tracer: &Option, ) -> Result<()> { let mut cmd = Command::new("sh"); - - cmd.env("CODSPEED_RUNNER_MODE", &self.name().to_string()) - .env( - "CODSPEED_PROFILE_FOLDER", - run_data.profile_folder.to_str().unwrap(), - ) - .envs(BASE_INJECTED_ENV.iter()); + cmd.envs(self.get_cmd_base_envs(&run_data.profile_folder)); if let Some(cwd) = &config.working_directory { let abs_cwd = canonicalize(cwd)?;