Skip to content
Merged
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
1 change: 1 addition & 0 deletions src/run/runner/valgrind/helpers/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod ignored_objects_path;
pub mod perf_maps;
pub mod python;
pub mod venv_compat;
21 changes: 21 additions & 0 deletions src/run/runner/valgrind/helpers/python.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
use std::process::Command;

/// Checks if the Python interpreter supports free-threaded mode.
/// Returns true if Python is free-threaded (GIL disabled), false otherwise.
pub fn is_free_threaded_python() -> bool {
// Use sysconfig.get_config_var("Py_GIL_DISABLED") as recommended by Python docs at https://docs.python.org/3/howto/free-threading-python.html#identifying-free-threaded-python
let output = Command::new("python")
.args([
"-c",
"import sysconfig; print(sysconfig.get_config_var('Py_GIL_DISABLED') or 0)",
])
.output();

match output {
Ok(output) if output.status.success() => {
let stdout = String::from_utf8_lossy(&output.stdout);
stdout.trim() == "1"
}
_ => false, // If Python is not available or command fails, assume not free-threaded
}
}
14 changes: 11 additions & 3 deletions src/run/runner/valgrind/measure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use crate::run::runner::helpers::introspected_golang;
use crate::run::runner::helpers::introspected_nodejs;
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::python::is_free_threaded_python;
use crate::run::{config::Config, instruments::mongo_tracer::MongoTracer};
use lazy_static::lazy_static;
use std::env;
Expand Down Expand Up @@ -87,9 +88,16 @@ pub async fn measure(
cmd.envs(get_base_injected_env(
RunnerMode::Simulation,
profile_folder,
))
.env("PYTHONMALLOC", "malloc")
.env(
));

// Only set PYTHONMALLOC=malloc for non-free-threaded Python builds.
// Free-threaded Python (with GIL disabled) manages memory differently and
// should not have PYTHONMALLOC overridden.
if !is_free_threaded_python() {
cmd.env("PYTHONMALLOC", "malloc");
}

cmd.env(
"PATH",
format!(
"{}:{}:{}",
Expand Down