Skip to content

Commit

Permalink
YJIT: Combine the compilation log enable flag with the output target …
Browse files Browse the repository at this point in the history
…to simplify the state.
  • Loading branch information
nirvdrum committed Oct 9, 2024
1 parent 8b5e2bf commit 2f9b591
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 35 deletions.
34 changes: 16 additions & 18 deletions yjit/src/compilation_log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ impl CompilationLog {
return;
}

let print_compilation_log = get_option!(print_compilation_log);
let timestamp = std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).unwrap().as_secs_f64();

let location = iseq_get_location(block_id.iseq, block_id.idx);
Expand All @@ -66,24 +65,23 @@ impl CompilationLog {
message
};

match get_option!(compilation_log) {
CompilationLogOutput::Stderr => {
eprintln!("{}", entry);
}

if let Some(output) = print_compilation_log {
match output {
CompilationLogOutput::Stderr => {
eprintln!("{}", entry);
}

CompilationLogOutput::File(fd) => {
use std::os::unix::io::{FromRawFd, IntoRawFd};
use std::io::Write;
CompilationLogOutput::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) };
writeln!(file, "{}", entry).unwrap();
file.flush().unwrap();
file.into_raw_fd(); // keep the fd open
}
// Write with the fd opened during boot
let mut file = unsafe { std::fs::File::from_raw_fd(fd) };
writeln!(file, "{}", entry).unwrap();
file.flush().unwrap();
file.into_raw_fd(); // keep the fd open
}

_ => () // Don't write/print anything
}

Self::get_instance().push(entry);
Expand Down Expand Up @@ -184,7 +182,7 @@ impl<'a, T: Clone, const N: usize> Iterator for CircularBufferIterator<'a, T, N>
/// Check if compilation log generation is enabled
#[no_mangle]
pub extern "C" fn rb_yjit_compilation_log_enabled_p(_ec: EcPtr, _ruby_self: VALUE) -> VALUE {
if get_option!(gen_compilation_log) {
if get_option!(compilation_log) != CompilationLogOutput::Disabled {
return Qtrue;
} else {
return Qfalse;
Expand All @@ -199,7 +197,7 @@ pub extern "C" fn rb_yjit_get_compilation_log(_ec: EcPtr, _ruby_self: VALUE) ->
}

fn rb_yjit_get_compilation_log_array() -> VALUE {
if !yjit_enabled_p() || !get_option!(gen_compilation_log) {
if !yjit_enabled_p() || get_option!(compilation_log) == CompilationLogOutput::Disabled {
return Qnil;
}

Expand Down
25 changes: 12 additions & 13 deletions yjit/src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,8 @@ pub struct Options {
/// Enable writing /tmp/perf-{pid}.map for Linux perf
pub perf_map: Option<PerfMap>,

// Log compilations
pub gen_compilation_log: bool,

// Print compilation log on exit (when gen_compilation_log is also true)
pub print_compilation_log: Option<CompilationLogOutput>,
// Where to store the compilation log. `None` disables the log
pub compilation_log: CompilationLogOutput,
}

// Initialize the options to default values
Expand All @@ -109,8 +106,7 @@ pub static mut OPTIONS: Options = Options {
frame_pointer: false,
code_gc: false,
perf_map: None,
gen_compilation_log: false,
print_compilation_log: None,
compilation_log: CompilationLogOutput::Disabled,
};

/// YJIT option descriptions for `ruby --help`.
Expand Down Expand Up @@ -139,10 +135,14 @@ pub enum TraceExits {

#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum CompilationLogOutput {
// Dump to stderr when the process exits
Stderr,
// Disable the log entirely
Disabled,
// Dump to the log file as compilation events occur.
File(std::os::unix::io::RawFd)
File(std::os::unix::io::RawFd),
// Keep the log in memory only
MemoryOnly,
// Dump to stderr when the process exits
Stderr
}

#[derive(Debug)]
Expand Down Expand Up @@ -330,8 +330,7 @@ pub fn parse_option(str_ptr: *const std::os::raw::c_char) -> Option<()> {
},
("compilation-log", _) => match opt_val {
"" => unsafe {
OPTIONS.gen_compilation_log = true;
OPTIONS.print_compilation_log = Some(CompilationLogOutput::Stderr);
OPTIONS.compilation_log = CompilationLogOutput::Stderr;
CompilationLog::init();
},
arg_value => {
Expand All @@ -345,7 +344,7 @@ pub fn parse_option(str_ptr: *const std::os::raw::c_char) -> Option<()> {
Ok(file) => {
use std::os::unix::io::IntoRawFd;
eprintln!("YJIT compilation log: {log_file_path}");
unsafe { OPTIONS.print_compilation_log = Some(CompilationLogOutput::File(file.into_raw_fd())) }
unsafe { OPTIONS.compilation_log = CompilationLogOutput::File(file.into_raw_fd()) }
}
Err(err) => panic!("Failed to create {log_file_path}: {err}"),
}
Expand Down
6 changes: 2 additions & 4 deletions yjit/src/yjit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,12 +180,10 @@ pub extern "C" fn rb_yjit_enable(_ec: EcPtr, _ruby_self: VALUE, gen_stats: VALUE

if gen_compilation_log.test() {
unsafe {
OPTIONS.gen_compilation_log = gen_compilation_log.test();

if print_compilation_log.test() {
OPTIONS.print_compilation_log = Some(CompilationLogOutput::Stderr);
OPTIONS.compilation_log = CompilationLogOutput::Stderr;
} else {
OPTIONS.print_compilation_log = None;
OPTIONS.compilation_log = CompilationLogOutput::MemoryOnly;
}

CompilationLog::init();
Expand Down

0 comments on commit 2f9b591

Please sign in to comment.