Skip to content

Commit a6f64fc

Browse files
committed
refactor: process memory mappings in separate function
1 parent 36094bc commit a6f64fc

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,
@@ -257,59 +313,11 @@ impl PerfRunner {
257313
#[cfg(target_os = "linux")]
258314
if !symbols_by_pid.contains_key(&pid) && !unwind_data_by_pid.contains_key(&pid)
259315
{
260-
use procfs::process::MMPermissions;
261-
262-
let bench_proc = procfs::process::Process::new(pid as _)
263-
.expect("Failed to find benchmark process");
264-
let exe_maps = bench_proc.maps().expect("Failed to read /proc/{pid}/maps");
265-
266-
for map in &exe_maps {
267-
let page_offset = map.offset;
268-
let (base_addr, end_addr) = map.address;
269-
let path = match &map.pathname {
270-
procfs::process::MMapPath::Path(path) => Some(path.clone()),
271-
_ => None,
272-
};
273-
274-
if let Some(path) = &path {
275-
symbols_by_pid
276-
.entry(pid)
277-
.or_insert(ProcessSymbols::new(pid))
278-
.add_mapping(pid, path, base_addr, end_addr);
279-
debug!("Added mapping for module {path:?}");
280-
281-
if map.perms.contains(MMPermissions::EXECUTE) {
282-
match UnwindData::new(
283-
path.to_string_lossy().as_bytes(),
284-
page_offset,
285-
base_addr,
286-
end_addr - base_addr,
287-
None,
288-
) {
289-
Ok(unwind_data) => {
290-
unwind_data_by_pid
291-
.entry(pid)
292-
.or_default()
293-
.push(unwind_data);
294-
debug!(
295-
"Added unwind data for {path:?} ({base_addr:x} - {end_addr:x})"
296-
);
297-
}
298-
Err(error) => {
299-
debug!(
300-
"Failed to create unwind data for module {}: {}",
301-
path.display(),
302-
error
303-
);
304-
}
305-
}
306-
}
307-
} else if map.perms.contains(MMPermissions::EXECUTE) {
308-
debug!(
309-
"Found executable mapping without path: {base_addr:x} - {end_addr:x}"
310-
);
311-
}
312-
}
316+
Self::process_memory_mappings(
317+
pid,
318+
&mut symbols_by_pid,
319+
&mut unwind_data_by_pid,
320+
)?;
313321
}
314322

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

0 commit comments

Comments
 (0)