MCPcopy Create free account
hub / github.com/CodSpeedHQ/codspeed / track_command

Function track_command

crates/memtrack/src/cli.rs:74–217  ·  view source on GitHub ↗
(
    cmd_string: &str,
    ipc_server_name: Option<String>,
    out_dir: &Path,
)

Source from the content-addressed store, hash-verified

72}
73
74fn track_command(
75 cmd_string: &str,
76 ipc_server_name: Option<String>,
77 out_dir: &Path,
78) -> anyhow::Result<std::process::ExitStatus> {
79 // First, establish IPC connection if needed to avoid timeouts on the runner because
80 // creating the Tracker instance takes some time.
81 let ipc_channel = if let Some(server_name) = ipc_server_name {
82 debug!("Connecting to IPC server: {server_name}");
83
84 let (tx, rx) = ipc::channel::<MemtrackIpcMessage>()?;
85 let sender = ipc::IpcSender::connect(server_name)?;
86 sender.send(tx)?;
87
88 Some(rx)
89 } else {
90 None
91 };
92
93 let tracker = Arc::new(Tracker::new()?);
94
95 // Spawn IPC handler thread with the now-available tracker
96 let ipc_handle = if let Some(rx) = ipc_channel {
97 let tracker = tracker.clone();
98 Some(thread::spawn(move || {
99 while let Ok(msg) = rx.recv() {
100 handle_ipc_message(msg, &tracker);
101 }
102 }))
103 } else {
104 // Without IPC, nothing toggles the tracking_enabled map, so allocator
105 // events would be dropped by the eBPF is_enabled() check. Enable it up
106 // front.
107 tracker.enable_tracking()?;
108 None
109 };
110
111 // Run the target command through bash to handle shell syntax. Drop
112 // privileges if running under sudo to avoid permission issues when the
113 // target accesses files owned by the original user.
114 let mut cmd = Command::new("bash");
115 cmd.arg("-c").arg(cmd_string);
116 let uid_gid = get_user_uid_gid();
117 if let Some((uid, gid)) = uid_gid {
118 debug!("Running under sudo, dropping privileges to uid={uid}, gid={gid}");
119 }
120
121 let mut session = tracker
122 .spawn(&cmd, uid_gid)
123 .map_err(|e| anyhow!("Failed to spawn child process: {e}"))?;
124 let root_pid = session.pid();
125 let event_rx = session.take_events()?;
126 debug!("Spawned child with pid {root_pid}");
127
128 // Generate output file name and create file for streaming events
129 let file_name = MemtrackArtifact::file_name(Some(root_pid));
130 let out_file = std::fs::File::create(out_dir.join(file_name))?;
131

Callers 1

run_cliFunction · 0.70

Calls 15

handle_ipc_messageFunction · 0.85
get_user_uid_gidFunction · 0.85
encode_eventsFunction · 0.85
argMethod · 0.80
pidMethod · 0.80
take_eventsMethod · 0.80
waitMethod · 0.80
stack_capture_enabledMethod · 0.80
detachMethod · 0.80
enable_trackingMethod · 0.45
spawnMethod · 0.45
disable_trackingMethod · 0.45

Tested by

no test coverage detected