Skip to content

Commit dc9cf9c

Browse files
committed
refactor: process memory mappings in separate function
1 parent a0b891e commit dc9cf9c

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
@@ -213,6 +213,62 @@ impl PerfRunner {
213213
Ok(())
214214
}
215215

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

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

0 commit comments

Comments
 (0)