Skip to content

Commit 958c16e

Browse files
committed
fix: unify environments between the two modes
1 parent a237959 commit 958c16e

7 files changed

Lines changed: 75 additions & 53 deletions

File tree

src/run/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,8 @@ pub async fn run(args: RunArgs, api_client: &CodSpeedAPIClient) -> Result<()> {
116116
let system_info = SystemInfo::new()?;
117117
check_system::check_system(&system_info)?;
118118

119-
let executor = runner::get_executor()?;
119+
let mode = runner::get_mode()?;
120+
let executor = runner::get_executor_from_mode(mode);
120121

121122
let run_data = get_run_data()?;
122123

src/run/runner/executor.rs

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
1-
use super::helpers::env::BASE_INJECTED_ENV;
21
use super::interfaces::{ExecutorName, RunData};
32
use crate::prelude::*;
43
use crate::run::instruments::mongo_tracer::MongoTracer;
54
use crate::run::{check_system::SystemInfo, config::Config};
65
use async_trait::async_trait;
7-
use std::collections::HashMap;
8-
use std::path::Path;
96

107
#[async_trait(?Send)]
118
pub trait Executor {
@@ -34,17 +31,4 @@ pub trait Executor {
3431
system_info: &SystemInfo,
3532
run_data: &RunData,
3633
) -> Result<()>;
37-
38-
/// Gets the base environment for the command
39-
///
40-
/// Later on, we will want to refactor this and create the cmd directly in a trait function
41-
fn get_cmd_base_envs(&self, profile_folder: &Path) -> HashMap<&str, String> {
42-
let mut hashmap = BASE_INJECTED_ENV.clone();
43-
hashmap.insert("CODSPEED_RUNNER_MODE", self.name().to_string());
44-
hashmap.insert(
45-
"CODSPEED_PROFILE_FOLDER",
46-
profile_folder.to_str().unwrap().to_string(),
47-
);
48-
hashmap
49-
}
5034
}

src/run/runner/helpers/env.rs

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
1-
use std::{collections::HashMap, env::consts::ARCH};
1+
use std::{collections::HashMap, env::consts::ARCH, path::Path};
22

3-
use lazy_static::lazy_static;
3+
use crate::run::runner::RunnerMode;
44

5-
lazy_static! {
6-
pub static ref BASE_INJECTED_ENV: HashMap<&'static str, String> = {
7-
HashMap::from([
8-
("PYTHONHASHSEED", "0".into()),
9-
("ARCH", ARCH.into()),
10-
("CODSPEED_ENV", "runner".into()),
11-
])
12-
};
5+
pub fn get_base_injected_env(
6+
mode: RunnerMode,
7+
profile_folder: &Path,
8+
) -> HashMap<&'static str, String> {
9+
HashMap::from([
10+
("PYTHONHASHSEED", "0".into()),
11+
("ARCH", ARCH.into()),
12+
("CODSPEED_ENV", "runner".into()),
13+
("CODSPEED_RUNNER_MODE", mode.to_string()),
14+
(
15+
"CODSPEED_PROFILE_FOLDER",
16+
profile_folder.to_string_lossy().to_string(),
17+
),
18+
])
1319
}

src/run/runner/mod.rs

Lines changed: 40 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::env;
1+
use std::{env, fmt::Display};
22

33
use crate::prelude::*;
44

@@ -12,22 +12,51 @@ use anyhow::bail;
1212
use executor::Executor;
1313
use helpers::profile_folder::create_profile_folder;
1414
pub use interfaces::{ExecutorName, RunData};
15-
use valgrind::executor::{ValgrindExecutor, INSTRUMENTATION_RUNNER_MODE};
16-
use wall_time::executor::{WallTimeExecutor, WALL_TIME_RUNNER_MODE};
15+
use valgrind::executor::ValgrindExecutor;
16+
use wall_time::executor::WallTimeExecutor;
17+
18+
pub enum RunnerMode {
19+
Instrumentation,
20+
WallTime,
21+
}
22+
23+
impl Display for RunnerMode {
24+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
25+
match self {
26+
RunnerMode::Instrumentation => write!(f, "instrumentation"),
27+
RunnerMode::WallTime => write!(f, "walltime"),
28+
}
29+
}
30+
}
31+
32+
impl TryFrom<&str> for RunnerMode {
33+
type Error = anyhow::Error;
34+
35+
fn try_from(value: &str) -> Result<Self> {
36+
match value {
37+
"instrumentation" => Ok(RunnerMode::Instrumentation),
38+
"walltime" => Ok(RunnerMode::WallTime),
39+
_ => bail!("Unknown runner mode: {}", value),
40+
}
41+
}
42+
}
1743

1844
pub const EXECUTOR_TARGET: &str = "executor";
1945

20-
pub fn get_executor() -> Result<Box<dyn Executor>> {
46+
pub fn get_mode() -> Result<RunnerMode> {
2147
if let Ok(runner_mode) = env::var("CODSPEED_RUNNER_MODE") {
2248
debug!("CODSPEED_RUNNER_MODE is set to {}", runner_mode);
23-
match runner_mode.as_str() {
24-
INSTRUMENTATION_RUNNER_MODE => Ok(Box::new(ValgrindExecutor)),
25-
WALL_TIME_RUNNER_MODE => Ok(Box::new(WallTimeExecutor)),
26-
_ => bail!("Unknown codspeed runner mode"),
27-
}
49+
RunnerMode::try_from(runner_mode.as_str())
2850
} else {
29-
debug!("CODSPEED_RUNNER_MODE is not set, using valgrind");
30-
Ok(Box::new(ValgrindExecutor))
51+
debug!("CODSPEED_RUNNER_MODE is not set, using instrumentation");
52+
Ok(RunnerMode::Instrumentation)
53+
}
54+
}
55+
56+
pub fn get_executor_from_mode(mode: RunnerMode) -> Box<dyn Executor> {
57+
match mode {
58+
RunnerMode::Instrumentation => Box::new(ValgrindExecutor),
59+
RunnerMode::WallTime => Box::new(WallTimeExecutor),
3160
}
3261
}
3362

src/run/runner/valgrind/executor.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ use crate::run::{check_system::SystemInfo, config::Config};
88

99
use super::{helpers::perf_maps::harvest_perf_maps, measure, setup::setup};
1010

11-
pub const INSTRUMENTATION_RUNNER_MODE: &str = "instrumentation";
12-
1311
pub struct ValgrindExecutor;
1412

1513
#[async_trait(?Send)]
@@ -36,10 +34,8 @@ impl Executor for ValgrindExecutor {
3634
run_data: &RunData,
3735
mongo_tracer: &Option<MongoTracer>,
3836
) -> Result<()> {
39-
let base_env = self.get_cmd_base_envs(&run_data.profile_folder);
40-
4137
//TODO: add valgrind version check
42-
measure::measure(&base_env, config, &run_data.profile_folder, mongo_tracer)?;
38+
measure::measure(config, &run_data.profile_folder, mongo_tracer)?;
4339

4440
Ok(())
4541
}

src/run/runner/valgrind/measure.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
use crate::prelude::*;
2+
use crate::run::runner::helpers::env::get_base_injected_env;
23
use crate::run::runner::helpers::get_bench_command::get_bench_command;
34
use crate::run::runner::helpers::run_command_with_log_pipe::run_command_with_log_pipe;
45
use crate::run::runner::valgrind::helpers::ignored_objects_path::get_objects_path_to_ignore;
56
use crate::run::runner::valgrind::helpers::introspected_nodejs::setup_introspected_nodejs;
7+
use crate::run::runner::RunnerMode;
68
use crate::run::{config::Config, instruments::mongo_tracer::MongoTracer};
79
use lazy_static::lazy_static;
8-
use std::collections::HashMap;
910
use std::env;
1011
use std::fs::canonicalize;
1112
use std::path::Path;
@@ -42,17 +43,20 @@ lazy_static! {
4243
}
4344

4445
pub fn measure(
45-
base_env: &HashMap<&str, String>,
4646
config: &Config,
4747
profile_folder: &Path,
4848
mongo_tracer: &Option<MongoTracer>,
4949
) -> Result<()> {
5050
// Create the command
5151
let mut cmd = Command::new("setarch");
52-
cmd.envs(base_env);
5352
cmd.arg(ARCH).arg("-R");
5453
// Configure the environment
55-
cmd.env(
54+
cmd.envs(get_base_injected_env(
55+
RunnerMode::Instrumentation,
56+
profile_folder,
57+
))
58+
.env("PYTHONMALLOC", "malloc")
59+
.env(
5660
"PATH",
5761
format!(
5862
"{}:{}",
@@ -62,8 +66,7 @@ pub fn measure(
6266
.unwrap(),
6367
env::var("PATH").unwrap_or_default(),
6468
),
65-
)
66-
.env("PYTHONMALLOC", "malloc");
69+
);
6770

6871
if let Some(cwd) = &config.working_directory {
6972
let abs_cwd = canonicalize(cwd)?;

src/run/runner/wall_time/executor.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,15 @@ use crate::prelude::*;
22

33
use crate::run::instruments::mongo_tracer::MongoTracer;
44
use crate::run::runner::executor::Executor;
5+
use crate::run::runner::helpers::env::get_base_injected_env;
56
use crate::run::runner::helpers::get_bench_command::get_bench_command;
67
use crate::run::runner::helpers::run_command_with_log_pipe::run_command_with_log_pipe;
7-
use crate::run::runner::{ExecutorName, RunData};
8+
use crate::run::runner::{ExecutorName, RunData, RunnerMode};
89
use crate::run::{check_system::SystemInfo, config::Config};
910
use async_trait::async_trait;
1011
use std::fs::canonicalize;
1112
use std::process::Command;
1213

13-
pub const WALL_TIME_RUNNER_MODE: &str = "walltime";
14-
1514
pub struct WallTimeExecutor;
1615

1716
#[async_trait(?Send)]
@@ -37,7 +36,11 @@ impl Executor for WallTimeExecutor {
3736
_mongo_tracer: &Option<MongoTracer>,
3837
) -> Result<()> {
3938
let mut cmd = Command::new("sh");
40-
cmd.envs(self.get_cmd_base_envs(&run_data.profile_folder));
39+
40+
cmd.envs(get_base_injected_env(
41+
RunnerMode::WallTime,
42+
&run_data.profile_folder,
43+
));
4144

4245
if let Some(cwd) = &config.working_directory {
4346
let abs_cwd = canonicalize(cwd)?;

0 commit comments

Comments
 (0)