Skip to content

Commit

Permalink
YJIT: --yjit-dump-disasm=dir: Hold descriptor for dump file
Browse files Browse the repository at this point in the history
This mainly aims to make `--yjit-dump-disasm=<relative_path>` more
usable. Previously, it crashed if the program did chdir(2), since it
opened the dump file every time when appending.

Tested with:

    ./miniruby --yjit-dump-disasm=. --yjit-call-threshold=1 -e 'Dir.chdir("/") {}'

And the `lobsters` benchmark.
  • Loading branch information
XrXr committed Jun 17, 2024
1 parent a119b5f commit 657c8db
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 11 deletions.
15 changes: 9 additions & 6 deletions yjit/src/disasm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,19 +115,22 @@ pub fn disasm_iseq_insn_range(iseq: IseqPtr, start_idx: u16, end_idx: u16) -> St
return out;
}

/// Dump dissassembly for a range in a [CodeBlock]. VM lock required.
#[cfg(feature = "disasm")]
pub fn dump_disasm_addr_range(cb: &CodeBlock, start_addr: CodePtr, end_addr: CodePtr, dump_disasm: &DumpDisasm) {
use std::fs::File;
use std::io::Write;

for (start_addr, end_addr) in cb.writable_addrs(start_addr, end_addr) {
let disasm = disasm_addr_range(cb, start_addr, end_addr);
if disasm.len() > 0 {
match dump_disasm {
DumpDisasm::Stdout => println!("{disasm}"),
DumpDisasm::File(path) => {
let mut f = File::options().create(true).append(true).open(path).unwrap();
f.write_all(disasm.as_bytes()).unwrap();
DumpDisasm::File(fd) => {
use std::os::unix::io::{FromRawFd, IntoRawFd};
use std::io::Write;

// Write with the fd opened during boot
let mut file = unsafe { std::fs::File::from_raw_fd(*fd) };
file.write_all(disasm.as_bytes()).unwrap();
file.into_raw_fd(); // keep the fd open
}
};
}
Expand Down
11 changes: 6 additions & 5 deletions yjit/src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ pub static mut rb_yjit_call_threshold: u64 = SMALL_CALL_THRESHOLD;
pub static mut rb_yjit_cold_threshold: u64 = 200_000;

// Command-line options
#[derive(Clone, PartialEq, Eq, Debug)]
#[derive(Debug)]
#[repr(C)]
pub struct Options {
// Size of the executable memory block to allocate in bytes
Expand Down Expand Up @@ -120,12 +120,12 @@ pub enum TraceExits {
CountedExit(Counter),
}

#[derive(Clone, PartialEq, Eq, Debug)]
#[derive(Debug)]
pub enum DumpDisasm {
// Dump to stdout
Stdout,
// Dump to "yjit_{pid}.log" file under the specified directory
File(String),
File(std::os::unix::io::RawFd),
}

/// Type of symbols to dump into /tmp/perf-{pid}.map
Expand Down Expand Up @@ -257,9 +257,10 @@ pub fn parse_option(str_ptr: *const std::os::raw::c_char) -> Option<()> {
directory => {
let path = format!("{directory}/yjit_{}.log", std::process::id());
match File::options().create(true).append(true).open(&path) {
Ok(_) => {
Ok(file) => {
use std::os::unix::io::IntoRawFd;
eprintln!("YJIT disasm dump: {path}");
unsafe { OPTIONS.dump_disasm = Some(DumpDisasm::File(path)) }
unsafe { OPTIONS.dump_disasm = Some(DumpDisasm::File(file.into_raw_fd())) }
}
Err(err) => eprintln!("Failed to create {path}: {err}"),
}
Expand Down

0 comments on commit 657c8db

Please sign in to comment.