|
| 1 | +use crate::{prelude::*, run::runner::wall_time::perf::unwind_data::UnwindData}; |
| 2 | +use linux_perf_data::jitdump::{JitDumpReader, JitDumpRecord}; |
| 3 | +use std::{ |
| 4 | + collections::HashSet, |
| 5 | + path::{Path, PathBuf}, |
| 6 | +}; |
| 7 | + |
| 8 | +struct JitDump { |
| 9 | + path: PathBuf, |
| 10 | +} |
| 11 | + |
| 12 | +impl JitDump { |
| 13 | + pub fn new(path: PathBuf) -> Self { |
| 14 | + Self { path } |
| 15 | + } |
| 16 | + |
| 17 | + /// Parses the JIT dump file and converts it into a list of `UnwindData`. |
| 18 | + /// |
| 19 | + /// The JIT dump file contains synthetic `eh_frame` data for jitted functions. This can be parsed and |
| 20 | + /// then converted to `UnwindData` which is used for stack unwinding. |
| 21 | + /// |
| 22 | + /// See: https://github.com/python/cpython/blob/main/Python/perf_jit_trampoline.c |
| 23 | + pub fn into_unwind_data(self) -> Result<Vec<UnwindData>> { |
| 24 | + let file = std::fs::File::open(self.path)?; |
| 25 | + |
| 26 | + let mut jit_unwind_data = Vec::new(); |
| 27 | + let mut current_unwind_info: Option<(Vec<u8>, Vec<u8>)> = None; |
| 28 | + |
| 29 | + let mut reader = JitDumpReader::new(file)?; |
| 30 | + while let Some(raw_record) = reader.next_record()? { |
| 31 | + // The first recording is always the unwind info, followed by the code load event |
| 32 | + // (see `perf_map_jit_write_entry` in https://github.com/python/cpython/blob/9743d069bd53e9d3a8f09df899ec1c906a79da24/Python/perf_jit_trampoline.c#L1163C13-L1163C37) |
| 33 | + match raw_record.parse()? { |
| 34 | + JitDumpRecord::CodeLoad(record) => { |
| 35 | + let name = record.function_name.as_slice(); |
| 36 | + let name = String::from_utf8_lossy(&name); |
| 37 | + |
| 38 | + let avma_start = record.vma; |
| 39 | + let code_size = record.code_bytes.len() as u64; |
| 40 | + let avma_end = avma_start + code_size; |
| 41 | + |
| 42 | + let Some((eh_frame, eh_frame_hdr)) = current_unwind_info.take() else { |
| 43 | + warn!("No unwind info available for JIT code load: {name}"); |
| 44 | + continue; |
| 45 | + }; |
| 46 | + |
| 47 | + jit_unwind_data.push(UnwindData { |
| 48 | + path: format!("jit_{name}"), |
| 49 | + avma_range: avma_start..avma_end, |
| 50 | + base_avma: avma_start, |
| 51 | + eh_frame_hdr, |
| 52 | + eh_frame_hdr_svma: 0..0, // TODO: Do we need this? |
| 53 | + eh_frame, |
| 54 | + eh_frame_svma: 0..0, // TODO: Do we need this? |
| 55 | + }); |
| 56 | + } |
| 57 | + JitDumpRecord::CodeUnwindingInfo(record) => { |
| 58 | + // Store unwind info for the next code loads |
| 59 | + current_unwind_info = Some(( |
| 60 | + record.eh_frame.as_slice().to_vec(), |
| 61 | + record.eh_frame_hdr.as_slice().to_vec(), |
| 62 | + )); |
| 63 | + } |
| 64 | + JitDumpRecord::CodeMove(_) => { |
| 65 | + panic!("CodeMove not implemented yet"); |
| 66 | + } |
| 67 | + _ => { |
| 68 | + warn!("Unhandled JIT dump record: {raw_record:?}"); |
| 69 | + } |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + Ok(jit_unwind_data) |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +/// Converts all the `jit-<pid>.dump` into unwind data and copies it to the profile folder. |
| 78 | +pub async fn harvest_perf_jit_for_pids(profile_folder: &Path, pids: &HashSet<i32>) -> Result<()> { |
| 79 | + for pid in pids { |
| 80 | + let name = format!("jit-{pid}.dump"); |
| 81 | + let path = PathBuf::from("/tmp").join(&name); |
| 82 | + |
| 83 | + if !path.exists() { |
| 84 | + continue; |
| 85 | + } |
| 86 | + debug!("Found JIT dump file: {path:?}"); |
| 87 | + |
| 88 | + let unwind_data = match JitDump::new(path).into_unwind_data() { |
| 89 | + Ok(unwind_data) => unwind_data, |
| 90 | + Err(error) => { |
| 91 | + warn!("Failed to convert jit dump into unwind data: {error:?}"); |
| 92 | + continue; |
| 93 | + } |
| 94 | + }; |
| 95 | + |
| 96 | + for module in unwind_data { |
| 97 | + module.save_to(profile_folder, *pid as _)?; |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + Ok(()) |
| 102 | +} |
0 commit comments