|
| 1 | +use crate::prelude::*; |
| 2 | +use crate::run::instruments::mongo_tracer::MongoTracer; |
| 3 | +use crate::run::runner::executor::Executor; |
| 4 | +use crate::run::runner::helpers::command::CommandBuilder; |
| 5 | +use crate::run::runner::helpers::get_bench_command::get_bench_command; |
| 6 | +use crate::run::runner::helpers::run_command_with_log_pipe::run_command_with_log_pipe_and_callback; |
| 7 | +use crate::run::runner::helpers::run_with_sudo::wrap_with_sudo; |
| 8 | +use crate::run::runner::shared::fifo::RunnerFifo; |
| 9 | +use crate::run::runner::{ExecutorName, RunData}; |
| 10 | +use crate::run::{check_system::SystemInfo, config::Config}; |
| 11 | +use async_trait::async_trait; |
| 12 | +use ipc_channel::ipc; |
| 13 | +use memtrack::MemtrackIpcClient; |
| 14 | +use memtrack::MemtrackIpcServer; |
| 15 | +use runner_shared::benchmark_results::{ArtifactExt, ExecutionTimestamps}; |
| 16 | +use runner_shared::fifo::Command as FifoCommand; |
| 17 | +use runner_shared::fifo::IntegrationMode; |
| 18 | +use std::path::Path; |
| 19 | +use std::process::Command; |
| 20 | +use std::rc::Rc; |
| 21 | + |
| 22 | +pub struct MemoryExecutor; |
| 23 | + |
| 24 | +impl MemoryExecutor { |
| 25 | + fn build_memtrack_command( |
| 26 | + config: &Config, |
| 27 | + run_data: &RunData, |
| 28 | + ) -> Result<(MemtrackIpcServer, CommandBuilder)> { |
| 29 | + // FIXME: We only support native languages for now |
| 30 | + |
| 31 | + // Find memtrack binary - check env variable or use default command name |
| 32 | + let memtrack_path = std::env::var("CODSPEED_MEMTRACK_BINARY") |
| 33 | + .unwrap_or_else(|_| "codspeed-memtrack".to_string()); |
| 34 | + |
| 35 | + let mut cmd_builder = CommandBuilder::new(memtrack_path); |
| 36 | + cmd_builder.arg("track"); |
| 37 | + cmd_builder.arg(get_bench_command(config)?); |
| 38 | + cmd_builder.arg("--output"); |
| 39 | + cmd_builder.arg(run_data.profile_folder.join("results")); |
| 40 | + |
| 41 | + // Setup memtrack IPC server |
| 42 | + let (ipc_server, server_name) = ipc::IpcOneShotServer::new()?; |
| 43 | + cmd_builder.arg("--ipc-server"); |
| 44 | + cmd_builder.arg(server_name); |
| 45 | + |
| 46 | + Ok((ipc_server, cmd_builder)) |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +#[async_trait(?Send)] |
| 51 | +impl Executor for MemoryExecutor { |
| 52 | + fn name(&self) -> ExecutorName { |
| 53 | + ExecutorName::Memory |
| 54 | + } |
| 55 | + |
| 56 | + async fn setup( |
| 57 | + &self, |
| 58 | + _system_info: &SystemInfo, |
| 59 | + _setup_cache_dir: Option<&Path>, |
| 60 | + ) -> Result<()> { |
| 61 | + // Validate that the codspeed-memtrack command is available |
| 62 | + let memtrack_path = std::env::var("CODSPEED_MEMTRACK_BINARY") |
| 63 | + .unwrap_or_else(|_| "codspeed-memtrack".to_string()); |
| 64 | + |
| 65 | + info!("Validating memtrack binary at path: {}", memtrack_path); |
| 66 | + let output = Command::new(&memtrack_path).arg("--version").output()?; |
| 67 | + if !output.status.success() { |
| 68 | + bail!("codspeed-memtrack command is not available or failed to execute"); |
| 69 | + } |
| 70 | + |
| 71 | + Ok(()) |
| 72 | + } |
| 73 | + |
| 74 | + async fn run( |
| 75 | + &self, |
| 76 | + config: &Config, |
| 77 | + _system_info: &SystemInfo, |
| 78 | + run_data: &RunData, |
| 79 | + _mongo_tracer: &Option<MongoTracer>, |
| 80 | + ) -> Result<()> { |
| 81 | + // Create the results/ directory inside the profile folder to avoid having memtrack create it with wrong permissions |
| 82 | + std::fs::create_dir_all(run_data.profile_folder.join("results"))?; |
| 83 | + |
| 84 | + let (ipc, cmd_builder) = Self::build_memtrack_command(config, run_data)?; |
| 85 | + let cmd = wrap_with_sudo(cmd_builder)?.build(); |
| 86 | + debug!("cmd: {cmd:?}"); |
| 87 | + |
| 88 | + let runner_fifo = RunnerFifo::new()?; |
| 89 | + let on_process_started = async |pid| -> anyhow::Result<()> { |
| 90 | + let marker_result = Self::handle_fifo(runner_fifo, pid, ipc).await?; |
| 91 | + |
| 92 | + // Directly write to the profile folder, to avoid having to define another field |
| 93 | + marker_result |
| 94 | + .save_to(run_data.profile_folder.join("results")) |
| 95 | + .unwrap(); |
| 96 | + |
| 97 | + Ok(()) |
| 98 | + }; |
| 99 | + |
| 100 | + let status = run_command_with_log_pipe_and_callback(cmd, on_process_started).await?; |
| 101 | + debug!("cmd exit status: {:?}", status); |
| 102 | + |
| 103 | + if !status.success() { |
| 104 | + bail!("failed to execute memory tracker process: {status}"); |
| 105 | + } |
| 106 | + |
| 107 | + Ok(()) |
| 108 | + } |
| 109 | + |
| 110 | + async fn teardown( |
| 111 | + &self, |
| 112 | + _config: &Config, |
| 113 | + _system_info: &SystemInfo, |
| 114 | + _run_data: &RunData, |
| 115 | + ) -> Result<()> { |
| 116 | + Ok(()) |
| 117 | + } |
| 118 | +} |
| 119 | + |
| 120 | +impl MemoryExecutor { |
| 121 | + async fn handle_fifo( |
| 122 | + mut runner_fifo: RunnerFifo, |
| 123 | + pid: u32, |
| 124 | + ipc: MemtrackIpcServer, |
| 125 | + ) -> anyhow::Result<ExecutionTimestamps> { |
| 126 | + debug!("handle_fifo called with PID {pid}"); |
| 127 | + |
| 128 | + // Accept the IPC connection from memtrack and get the sender it sends us |
| 129 | + let (_, memtrack_sender) = ipc.accept()?; |
| 130 | + let ipc_client = Rc::new(MemtrackIpcClient::from_accepted(memtrack_sender)); |
| 131 | + |
| 132 | + let ipc_client_health = Rc::clone(&ipc_client); |
| 133 | + let health_check = async move || { |
| 134 | + // Ping memtrack via IPC to check if it's still responding |
| 135 | + match ipc_client_health.ping() { |
| 136 | + Ok(()) => Ok(true), |
| 137 | + Err(_) => Ok(false), |
| 138 | + } |
| 139 | + }; |
| 140 | + |
| 141 | + let on_cmd = async move |cmd: &FifoCommand| { |
| 142 | + match cmd { |
| 143 | + FifoCommand::StartBenchmark => { |
| 144 | + debug!("Enabling memtrack via IPC"); |
| 145 | + if let Err(e) = ipc_client.enable() { |
| 146 | + error!("Failed to enable memtrack: {e}"); |
| 147 | + return Ok(FifoCommand::Err); |
| 148 | + } |
| 149 | + } |
| 150 | + FifoCommand::StopBenchmark => { |
| 151 | + debug!("Disabling memtrack via IPC"); |
| 152 | + if let Err(e) = ipc_client.disable() { |
| 153 | + // There's a chance that memtrack has already exited here, so just log as debug |
| 154 | + debug!("Failed to disable memtrack: {e}"); |
| 155 | + return Ok(FifoCommand::Err); |
| 156 | + } |
| 157 | + } |
| 158 | + FifoCommand::GetIntegrationMode => { |
| 159 | + return Ok(FifoCommand::IntegrationModeResponse( |
| 160 | + IntegrationMode::Analysis, |
| 161 | + )); |
| 162 | + } |
| 163 | + _ => { |
| 164 | + warn!("Unhandled FIFO command: {cmd:?}"); |
| 165 | + return Ok(FifoCommand::Err); |
| 166 | + } |
| 167 | + } |
| 168 | + |
| 169 | + Ok(FifoCommand::Ack) |
| 170 | + }; |
| 171 | + |
| 172 | + let (marker_result, _) = runner_fifo |
| 173 | + .handle_fifo_messages(health_check, on_cmd) |
| 174 | + .await?; |
| 175 | + Ok(marker_result) |
| 176 | + } |
| 177 | +} |
0 commit comments