Skip to content

Commit 223b6a5

Browse files
committed
refactor: process memory mappings in separate function
1 parent 1c241f3 commit 223b6a5

1 file changed

Lines changed: 61 additions & 53 deletions

File tree

  • src/run/runner/wall_time/perf

src/run/runner/wall_time/perf/mod.rs

Lines changed: 61 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,62 @@ impl PerfRunner {
210210
Ok(())
211211
}
212212

213+
#[cfg(target_os = "linux")]
214+
fn process_memory_mappings(
215+
pid: u32,
216+
symbols_by_pid: &mut HashMap<u32, ProcessSymbols>,
217+
unwind_data_by_pid: &mut HashMap<u32, Vec<UnwindData>>,
218+
) -> anyhow::Result<()> {
219+
use procfs::process::MMPermissions;
220+
221+
let bench_proc =
222+
procfs::process::Process::new(pid as _).expect("Failed to find benchmark process");
223+
let exe_maps = bench_proc.maps().expect("Failed to read /proc/{pid}/maps");
224+
225+
for map in &exe_maps {
226+
let page_offset = map.offset;
227+
let (base_addr, end_addr) = map.address;
228+
let path = match &map.pathname {
229+
procfs::process::MMapPath::Path(path) => Some(path.clone()),
230+
_ => None,
231+
};
232+
233+
if let Some(path) = &path {
234+
symbols_by_pid
235+
.entry(pid)
236+
.or_insert(ProcessSymbols::new(pid))
237+
.add_mapping(pid, path, base_addr, end_addr);
238+
debug!("Added mapping for module {path:?}");
239+
240+
if map.perms.contains(MMPermissions::EXECUTE) {
241+
match UnwindData::new(
242+
path.to_string_lossy().as_bytes(),
243+
page_offset,
244+
base_addr,
245+
end_addr - base_addr,
246+
None,
247+
) {
248+
Ok(unwind_data) => {
249+
unwind_data_by_pid.entry(pid).or_default().push(unwind_data);
250+
debug!("Added unwind data for {path:?} ({base_addr:x} - {end_addr:x})");
251+
}
252+
Err(error) => {
253+
debug!(
254+
"Failed to create unwind data for module {}: {}",
255+
path.display(),
256+
error
257+
);
258+
}
259+
}
260+
}
261+
} else if map.perms.contains(MMPermissions::EXECUTE) {
262+
debug!("Found executable mapping without path: {base_addr:x} - {end_addr:x}");
263+
}
264+
}
265+
266+
Ok(())
267+
}
268+
213269
async fn handle_fifo(
214270
mut runner_fifo: RunnerFifo,
215271
mut perf_fifo: PerfFifo,
@@ -254,59 +310,11 @@ impl PerfRunner {
254310
#[cfg(target_os = "linux")]
255311
if !symbols_by_pid.contains_key(&pid) && !unwind_data_by_pid.contains_key(&pid)
256312
{
257-
use procfs::process::MMPermissions;
258-
259-
let bench_proc = procfs::process::Process::new(pid as _)
260-
.expect("Failed to find benchmark process");
261-
let exe_maps = bench_proc.maps().expect("Failed to read /proc/{pid}/maps");
262-
263-
for map in &exe_maps {
264-
let page_offset = map.offset;
265-
let (base_addr, end_addr) = map.address;
266-
let path = match &map.pathname {
267-
procfs::process::MMapPath::Path(path) => Some(path.clone()),
268-
_ => None,
269-
};
270-
271-
if let Some(path) = &path {
272-
symbols_by_pid
273-
.entry(pid)
274-
.or_insert(ProcessSymbols::new(pid))
275-
.add_mapping(pid, path, base_addr, end_addr);
276-
debug!("Added mapping for module {path:?}");
277-
278-
if map.perms.contains(MMPermissions::EXECUTE) {
279-
match UnwindData::new(
280-
path.to_string_lossy().as_bytes(),
281-
page_offset,
282-
base_addr,
283-
end_addr - base_addr,
284-
None,
285-
) {
286-
Ok(unwind_data) => {
287-
unwind_data_by_pid
288-
.entry(pid)
289-
.or_default()
290-
.push(unwind_data);
291-
debug!(
292-
"Added unwind data for {path:?} ({base_addr:x} - {end_addr:x})"
293-
);
294-
}
295-
Err(error) => {
296-
debug!(
297-
"Failed to create unwind data for module {}: {}",
298-
path.display(),
299-
error
300-
);
301-
}
302-
}
303-
}
304-
} else if map.perms.contains(MMPermissions::EXECUTE) {
305-
debug!(
306-
"Found executable mapping without path: {base_addr:x} - {end_addr:x}"
307-
);
308-
}
309-
}
313+
Self::process_memory_mappings(
314+
pid,
315+
&mut symbols_by_pid,
316+
&mut unwind_data_by_pid,
317+
)?;
310318
}
311319

312320
runner_fifo.send_cmd(FifoCommand::Ack).await?;

0 commit comments

Comments
 (0)