Skip to content

ZJIT: Create perf map files for profilers #13941

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
merged 10 commits into from
Jul 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 36 additions & 2 deletions zjit/src/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,20 @@ fn gen_iseq_entry_point_body(cb: &mut CodeBlock, iseq: IseqPtr) -> *const u8 {
start_ptr.map(|start_ptr| start_ptr.raw_ptr(cb)).unwrap_or(std::ptr::null())
}

/// Write an entry to the perf map in /tmp
fn register_with_perf(iseq_name: String, start_ptr: usize, code_size: usize) {
use std::io::Write;
let perf_map = format!("/tmp/perf-{}.map", std::process::id());
let Ok(mut file) = std::fs::OpenOptions::new().create(true).append(true).open(&perf_map) else {
debug!("Failed to open perf map file: {perf_map}");
return;
};
let Ok(_) = writeln!(file, "{:#x} {:#x} zjit::{}", start_ptr, code_size, iseq_name) else {
debug!("Failed to write {iseq_name} to perf map file: {perf_map}");
return;
};
}

/// Compile a JIT entry
fn gen_entry(cb: &mut CodeBlock, iseq: IseqPtr, function: &Function, function_ptr: CodePtr, c_stack_bytes: usize) -> Option<CodePtr> {
// Set up registers for CFP, EC, SP, and basic block arguments
Expand All @@ -172,7 +186,17 @@ fn gen_entry(cb: &mut CodeBlock, iseq: IseqPtr, function: &Function, function_pt
asm.frame_teardown();
asm.cret(C_RET_OPND);

asm.compile(cb).map(|(start_ptr, _)| start_ptr)
let result = asm.compile(cb).map(|(start_ptr, _)| start_ptr);
if let Some(start_addr) = result {
if get_option!(perf) {
let start_ptr = start_addr.raw_ptr(cb) as usize;
let end_ptr = cb.get_write_ptr().raw_ptr(cb) as usize;
let code_size = end_ptr - start_ptr;
let iseq_name = iseq_get_location(iseq, 0);
register_with_perf(format!("entry for {iseq_name}"), start_ptr, code_size);
}
}
result
}

/// Compile an ISEQ into machine code
Expand Down Expand Up @@ -255,7 +279,17 @@ fn gen_function(cb: &mut CodeBlock, iseq: IseqPtr, function: &Function) -> Optio
}

// Generate code if everything can be compiled
asm.compile(cb).map(|(start_ptr, gc_offsets)| (start_ptr, gc_offsets, jit))
let result = asm.compile(cb).map(|(start_ptr, gc_offsets)| (start_ptr, gc_offsets, jit));
if let Some((start_ptr, _, _)) = result {
if get_option!(perf) {
let start_usize = start_ptr.raw_ptr(cb) as usize;
let end_usize = cb.get_write_ptr().raw_ptr(cb) as usize;
let code_size = end_usize - start_usize;
let iseq_name = iseq_get_location(iseq, 0);
register_with_perf(iseq_name, start_usize, code_size);
}
}
result
}

/// Compile an instruction
Expand Down
6 changes: 6 additions & 0 deletions zjit/src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ pub struct Options {

/// Dump all compiled machine code.
pub dump_disasm: bool,

/// Dump code map to /tmp for performance profilers.
pub perf: bool,
}

/// Return an Options with default values
Expand All @@ -44,6 +47,7 @@ pub fn init_options() -> Options {
dump_hir_opt: None,
dump_lir: false,
dump_disasm: false,
perf: false,
}
}

Expand Down Expand Up @@ -143,6 +147,8 @@ fn parse_option(options: &mut Options, str_ptr: *const std::os::raw::c_char) ->

("dump-disasm", "") => options.dump_disasm = true,

("perf", "") => options.perf = true,

_ => return None, // Option name not recognized
}

Expand Down
Loading
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy