-
Notifications
You must be signed in to change notification settings - Fork 5
feat: run python with perf jit dump #103
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
not-matthias
merged 7 commits into
main
from
cod-992-perf-works-in-github-action-but-not-locally
Sep 12, 2025
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
b09e541
feat: run python with perf jit dump
not-matthias 72f17f6
feat: add unwind data tests
not-matthias a8d042f
fix: create perf map for jitdump
not-matthias 85cf656
chore: add debug log for /proc/<pid>/maps
not-matthias 1936c1f
fix: codspeed debug check
not-matthias b980718
fix: ignore statically linked python
not-matthias 3323c5a
feat: detect stack size at runtime
not-matthias File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
use crate::{ | ||
prelude::*, | ||
run::runner::wall_time::perf::{ | ||
perf_map::{ModuleSymbols, Symbol}, | ||
unwind_data::UnwindData, | ||
}, | ||
}; | ||
use linux_perf_data::jitdump::{JitDumpReader, JitDumpRecord}; | ||
use std::{ | ||
collections::HashSet, | ||
path::{Path, PathBuf}, | ||
}; | ||
|
||
struct JitDump { | ||
path: PathBuf, | ||
} | ||
|
||
impl JitDump { | ||
pub fn new(path: PathBuf) -> Self { | ||
Self { path } | ||
} | ||
|
||
pub fn into_perf_map(self) -> Result<ModuleSymbols> { | ||
let mut symbols = Vec::new(); | ||
|
||
let file = std::fs::File::open(self.path)?; | ||
let mut reader = JitDumpReader::new(file)?; | ||
while let Some(raw_record) = reader.next_record()? { | ||
let JitDumpRecord::CodeLoad(record) = raw_record.parse()? else { | ||
continue; | ||
}; | ||
|
||
let name = record.function_name.as_slice(); | ||
let name = String::from_utf8_lossy(&name); | ||
|
||
symbols.push(Symbol { | ||
addr: record.vma, | ||
size: record.code_bytes.len() as u64, | ||
name: name.to_string(), | ||
}); | ||
} | ||
debug!("Extracted {} JIT symbols", symbols.len()); | ||
|
||
Ok(ModuleSymbols::from_symbols(symbols)) | ||
} | ||
|
||
/// Parses the JIT dump file and converts it into a list of `UnwindData`. | ||
/// | ||
/// The JIT dump file contains synthetic `eh_frame` data for jitted functions. This can be parsed and | ||
/// then converted to `UnwindData` which is used for stack unwinding. | ||
/// | ||
/// See: https://github.com/python/cpython/blob/main/Python/perf_jit_trampoline.c | ||
pub fn into_unwind_data(self) -> Result<Vec<UnwindData>> { | ||
let file = std::fs::File::open(self.path)?; | ||
|
||
let mut jit_unwind_data = Vec::new(); | ||
let mut current_unwind_info: Option<(Vec<u8>, Vec<u8>)> = None; | ||
|
||
let mut reader = JitDumpReader::new(file)?; | ||
while let Some(raw_record) = reader.next_record()? { | ||
// The first recording is always the unwind info, followed by the code load event | ||
// (see `perf_map_jit_write_entry` in https://github.com/python/cpython/blob/9743d069bd53e9d3a8f09df899ec1c906a79da24/Python/perf_jit_trampoline.c#L1163C13-L1163C37) | ||
match raw_record.parse()? { | ||
JitDumpRecord::CodeLoad(record) => { | ||
let name = record.function_name.as_slice(); | ||
let name = String::from_utf8_lossy(&name); | ||
|
||
let avma_start = record.vma; | ||
let code_size = record.code_bytes.len() as u64; | ||
let avma_end = avma_start + code_size; | ||
|
||
let Some((eh_frame, eh_frame_hdr)) = current_unwind_info.take() else { | ||
warn!("No unwind info available for JIT code load: {name}"); | ||
continue; | ||
}; | ||
|
||
jit_unwind_data.push(UnwindData { | ||
path: format!("jit_{name}"), | ||
avma_range: avma_start..avma_end, | ||
base_avma: 0, | ||
eh_frame_hdr, | ||
eh_frame_hdr_svma: 0..0, | ||
eh_frame, | ||
eh_frame_svma: 0..0, | ||
}); | ||
} | ||
JitDumpRecord::CodeUnwindingInfo(record) => { | ||
// Store unwind info for the next code loads | ||
current_unwind_info = Some(( | ||
record.eh_frame.as_slice().to_vec(), | ||
record.eh_frame_hdr.as_slice().to_vec(), | ||
)); | ||
} | ||
_ => { | ||
warn!("Unhandled JIT dump record: {raw_record:?}"); | ||
} | ||
} | ||
} | ||
|
||
Ok(jit_unwind_data) | ||
} | ||
} | ||
|
||
/// Converts all the `jit-<pid>.dump` into unwind data and copies it to the profile folder. | ||
pub async fn harvest_perf_jit_for_pids(profile_folder: &Path, pids: &HashSet<i32>) -> Result<()> { | ||
for pid in pids { | ||
let name = format!("jit-{pid}.dump"); | ||
let path = PathBuf::from("/tmp").join(&name); | ||
|
||
if !path.exists() { | ||
continue; | ||
} | ||
debug!("Found JIT dump file: {path:?}"); | ||
|
||
// Append the symbols to the existing perf map file | ||
let symbols = match JitDump::new(path.clone()).into_perf_map() { | ||
Ok(symbols) => symbols, | ||
Err(error) => { | ||
warn!("Failed to convert jit dump into perf map: {error:?}"); | ||
continue; | ||
} | ||
}; | ||
symbols.append_to_file(profile_folder.join(format!("perf-{pid}.map")))?; | ||
|
||
let unwind_data = match JitDump::new(path).into_unwind_data() { | ||
Ok(unwind_data) => unwind_data, | ||
Err(error) => { | ||
warn!("Failed to convert jit dump into unwind data: {error:?}"); | ||
continue; | ||
} | ||
}; | ||
|
||
for module in unwind_data { | ||
module.save_to(profile_folder, *pid as _)?; | ||
} | ||
} | ||
|
||
Ok(()) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
...napshots/codspeed__run__runner__wall_time__perf__unwind_data__tests__cpp_unwind_data.snap
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
--- | ||
source: src/run/runner/wall_time/perf/unwind_data.rs | ||
expression: "UnwindData::new(MODULE_PATH.as_bytes(), 0x0, start_addr, size, None)" | ||
--- | ||
Ok( | ||
UnwindData { | ||
path: "testdata/perf_map/cpp_my_benchmark.bin", | ||
avma_range: 400000..459000, | ||
base_avma: 0, | ||
eh_frame_hdr_svma: 4577bc..458b30, | ||
eh_frame_hdr_hash: 4b4eac90f7f5e60d, | ||
eh_frame_hash: 233bdd4ae9fe4ba4, | ||
eh_frame_svma: 451098..4577bc, | ||
}, | ||
) |
15 changes: 15 additions & 0 deletions
15
...shots/codspeed__run__runner__wall_time__perf__unwind_data__tests__golang_unwind_data.snap
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
--- | ||
source: src/run/runner/wall_time/perf/unwind_data.rs | ||
expression: "UnwindData::new(MODULE_PATH.as_bytes(), 0x2000, start_addr, size, None)" | ||
--- | ||
Ok( | ||
UnwindData { | ||
path: "testdata/perf_map/go_fib.bin", | ||
avma_range: 402000..50f000, | ||
base_avma: 0, | ||
eh_frame_hdr_svma: 6498b0..649b94, | ||
eh_frame_hdr_hash: f1f69beb959a08d7, | ||
eh_frame_hash: a8727039dd21b51c, | ||
eh_frame_svma: 649b98..64aa70, | ||
}, | ||
) |
15 changes: 15 additions & 0 deletions
15
...s/codspeed__run__runner__wall_time__perf__unwind_data__tests__rust_divan_unwind_data.snap
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
--- | ||
source: src/run/runner/wall_time/perf/unwind_data.rs | ||
expression: unwind_data | ||
--- | ||
Ok( | ||
UnwindData { | ||
path: "testdata/perf_map/divan_sleep_benches.bin", | ||
avma_range: 5555555a2000..555555692000, | ||
base_avma: 555555554000, | ||
eh_frame_hdr_svma: 2ac74..2ea60, | ||
eh_frame_hdr_hash: f579da4368e627c1, | ||
eh_frame_hash: 791501d5a9c438d, | ||
eh_frame_svma: 11540..2ac74, | ||
}, | ||
) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Won't this be too much logs?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It doesn't add that much (around 40-50 lines). However, it's one of the most important things needed for debugging any perf related issues (e.g. if we fail to resolve the symbol we can look at which module it belongs to).
And since we only enable it when running with
CODSPEED_LOG=debug
it shouldn't be an issue.