|
| 1 | +use crate::core::BlockId; |
| 2 | +use crate::cruby::*; |
| 3 | +use crate::options::*; |
| 4 | +use crate::yjit::yjit_enabled_p; |
| 5 | + |
| 6 | +use std::fmt::{Display, Formatter}; |
| 7 | +use std::os::raw::c_long; |
| 8 | +use crate::utils::iseq_get_location; |
| 9 | + |
| 10 | +type Timestamp = f64; |
| 11 | + |
| 12 | +#[derive(Clone, Debug)] |
| 13 | +pub struct LogEntry { |
| 14 | + /// The time when the block was compiled. |
| 15 | + pub timestamp: Timestamp, |
| 16 | + |
| 17 | + /// The log message. |
| 18 | + pub message: String, |
| 19 | +} |
| 20 | + |
| 21 | +impl Display for LogEntry { |
| 22 | + fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { |
| 23 | + write!(f, "{:15.6}: {}", self.timestamp, self.message) |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +pub type Log = CircularBuffer<LogEntry, 1024>; |
| 28 | +static mut LOG: Option<Log> = None; |
| 29 | + |
| 30 | +impl Log { |
| 31 | + pub fn init() { |
| 32 | + unsafe { |
| 33 | + LOG = Some(Log::new()); |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + pub fn get_instance() -> &'static mut Log { |
| 38 | + unsafe { |
| 39 | + LOG.as_mut().unwrap() |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + pub fn has_instance() -> bool { |
| 44 | + unsafe { |
| 45 | + LOG.as_mut().is_some() |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + pub fn add_block_with_chain_depth(block_id: BlockId, chain_depth: u8) { |
| 50 | + if !Self::has_instance() { |
| 51 | + return; |
| 52 | + } |
| 53 | + |
| 54 | + let print_log = get_option!(log); |
| 55 | + let timestamp = std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).unwrap().as_secs_f64(); |
| 56 | + |
| 57 | + let location = iseq_get_location(block_id.iseq, block_id.idx); |
| 58 | + let index = block_id.idx; |
| 59 | + let message = if chain_depth > 0 { |
| 60 | + format!("{} (index: {}, chain_depth: {})", location, index, chain_depth) |
| 61 | + } else { |
| 62 | + format!("{} (index: {})", location, index) |
| 63 | + }; |
| 64 | + |
| 65 | + let entry = LogEntry { |
| 66 | + timestamp, |
| 67 | + message |
| 68 | + }; |
| 69 | + |
| 70 | + if let Some(output) = print_log { |
| 71 | + match output { |
| 72 | + LogOutput::Stderr => { |
| 73 | + eprintln!("{}", entry); |
| 74 | + } |
| 75 | + |
| 76 | + LogOutput::File(fd) => { |
| 77 | + use std::os::unix::io::{FromRawFd, IntoRawFd}; |
| 78 | + use std::io::Write; |
| 79 | + |
| 80 | + // Write with the fd opened during boot |
| 81 | + let mut file = unsafe { std::fs::File::from_raw_fd(fd) }; |
| 82 | + writeln!(file, "{}", entry).unwrap(); |
| 83 | + file.flush().unwrap(); |
| 84 | + file.into_raw_fd(); // keep the fd open |
| 85 | + } |
| 86 | + |
| 87 | + LogOutput::MemoryOnly => () // Don't print or write anything |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + Self::get_instance().push(entry); |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +pub struct CircularBuffer<T, const N: usize> { |
| 96 | + buffer: Vec<Option<T>>, |
| 97 | + head: usize, |
| 98 | + tail: usize, |
| 99 | + size: usize |
| 100 | +} |
| 101 | + |
| 102 | +impl<T: Clone, const N: usize> CircularBuffer<T, N> { |
| 103 | + pub fn new() -> Self { |
| 104 | + Self { |
| 105 | + buffer: vec![None; N], |
| 106 | + head: 0, |
| 107 | + tail: 0, |
| 108 | + size: 0 |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + pub fn push(&mut self, value: T) { |
| 113 | + self.buffer[self.head] = Some(value); |
| 114 | + self.head = (self.head + 1) % N; |
| 115 | + if self.size == N { |
| 116 | + self.tail = (self.tail + 1) % N; |
| 117 | + } else { |
| 118 | + self.size += 1; |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + pub fn pop(&mut self) -> Option<T> { |
| 123 | + if self.size == 0 { |
| 124 | + return None; |
| 125 | + } |
| 126 | + |
| 127 | + let value = self.buffer[self.tail].take(); |
| 128 | + self.tail = (self.tail + 1) % N; |
| 129 | + self.size -= 1; |
| 130 | + value |
| 131 | + } |
| 132 | + |
| 133 | + pub fn len(&self) -> usize { |
| 134 | + self.size |
| 135 | + } |
| 136 | +} |
| 137 | + |
| 138 | + |
| 139 | +//=========================================================================== |
| 140 | + |
| 141 | +/// Primitive called in yjit.rb |
| 142 | +/// Check if log generation is enabled |
| 143 | +#[no_mangle] |
| 144 | +pub extern "C" fn rb_yjit_log_enabled_p(_ec: EcPtr, _ruby_self: VALUE) -> VALUE { |
| 145 | + if get_option!(log).is_some() { |
| 146 | + return Qtrue; |
| 147 | + } else { |
| 148 | + return Qfalse; |
| 149 | + } |
| 150 | +} |
| 151 | + |
| 152 | +/// Primitive called in yjit.rb. |
| 153 | +/// Export all YJIT log entries as a Ruby array. |
| 154 | +#[no_mangle] |
| 155 | +pub extern "C" fn rb_yjit_get_log(_ec: EcPtr, _ruby_self: VALUE) -> VALUE { |
| 156 | + with_vm_lock(src_loc!(), || rb_yjit_get_log_array()) |
| 157 | +} |
| 158 | + |
| 159 | +fn rb_yjit_get_log_array() -> VALUE { |
| 160 | + if !yjit_enabled_p() || get_option!(log).is_none() { |
| 161 | + return Qnil; |
| 162 | + } |
| 163 | + |
| 164 | + let log = Log::get_instance(); |
| 165 | + let array = unsafe { rb_ary_new_capa(log.len() as c_long) }; |
| 166 | + |
| 167 | + while log.len() > 0 { |
| 168 | + let entry = log.pop().unwrap(); |
| 169 | + |
| 170 | + unsafe { |
| 171 | + let entry_array = rb_ary_new_capa(2); |
| 172 | + rb_ary_push(entry_array, rb_float_new(entry.timestamp)); |
| 173 | + rb_ary_push(entry_array, entry.message.into()); |
| 174 | + rb_ary_push(array, entry_array); |
| 175 | + } |
| 176 | + } |
| 177 | + |
| 178 | + return array; |
| 179 | +} |
0 commit comments