Skip to content

Commit

Permalink
YJIT: Add counter to measure how often we compile "cold" ISEQs (#535)
Browse files Browse the repository at this point in the history
  • Loading branch information
maximecb committed Sep 27, 2023
1 parent 175ac32 commit 775585b
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 0 deletions.
2 changes: 2 additions & 0 deletions yjit.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
// Expose these as declarations since we are building YJIT.
bool rb_yjit_enabled_p(void);
bool rb_yjit_compile_new_iseqs(void);
unsigned rb_yjit_call_threshold(void);
bool rb_yjit_threshold_hit(const rb_iseq_t *const iseq, unsigned long total_calls);
void rb_yjit_invalidate_all_method_lookup_assumptions(void);
void rb_yjit_cme_invalidate(rb_callable_method_entry_t *cme);
Expand All @@ -49,6 +50,7 @@ void rb_yjit_tracing_invalidate_all(void);

static inline bool rb_yjit_enabled_p(void) { return false; }
static inline bool rb_yjit_compile_new_iseqs(void) { return false; }
static inline unsigned rb_yjit_call_threshold(void) { return UINT_MAX; }
static inline bool rb_yjit_threshold_hit(const rb_iseq_t *const iseq, unsigned long total_calls) { return false; }
static inline void rb_yjit_invalidate_all_method_lookup_assumptions(void) {}
static inline void rb_yjit_cme_invalidate(rb_callable_method_entry_t *cme) {}
Expand Down
3 changes: 3 additions & 0 deletions yjit/src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -980,6 +980,9 @@ pub struct IseqPayload {
// Blocks that are invalidated but are not yet deallocated.
// The code GC will free them later.
pub dead_blocks: Vec<BlockRef>,

// Number of calls when we hit the threshold for this ISEQs
pub call_count_at_threshold: u64,
}

impl IseqPayload {
Expand Down
2 changes: 2 additions & 0 deletions yjit/src/stats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ macro_rules! make_counters {
pub const DEFAULT_COUNTERS: [Counter; 7] = [
Counter::code_gc_count,
Counter::compiled_iseq_entry,
Counter::compiled_iseq_entry_cold,
Counter::compiled_iseq_count,
Counter::compiled_blockid_count,
Counter::compiled_block_count,
Expand Down Expand Up @@ -441,6 +442,7 @@ make_counters! {
binding_set,

compiled_iseq_entry,
compiled_entry_cold,
compiled_iseq_count,
compiled_blockid_count,
compiled_block_count,
Expand Down
37 changes: 37 additions & 0 deletions yjit/src/yjit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use crate::options::*;
use crate::stats::YjitExitLocations;
use crate::stats::incr_counter;
use crate::stats::with_compile_time;
use crate::stats::incr_counter;

use std::os::raw;
use std::sync::atomic::{AtomicBool, Ordering};
Expand Down Expand Up @@ -53,6 +54,42 @@ pub extern "C" fn rb_yjit_threshold_hit(_iseq: IseqPtr, total_calls: u64) -> boo
return total_calls == call_threshold;
}

// Counter to serve as a proxy for execution time, total number of calls
static mut TOTAL_ENTRY_HITS: u64 = 0;

/// Test whether we are ready to compile an ISEQ or not
#[no_mangle]
pub extern "C" fn rb_yjit_threshold_hit(iseq: IseqPtr, total_calls: u64) -> bool {
let call_threshold = get_option!(call_threshold) as u64;

unsafe { TOTAL_ENTRY_HITS += 1; }

if total_calls >= call_threshold {

if total_calls == call_threshold {
let payload = get_or_create_iseq_payload(iseq);
let call_count = unsafe { TOTAL_ENTRY_HITS };
payload.call_count_at_threshold = call_count;
}

// Try to estimate the total time taken (total number of calls) to reach 20 calls to this ISEQ
// This give us a ratio of how hot/cold this ISEQ is
if total_calls == call_threshold + 20 {
let payload = get_or_create_iseq_payload(iseq);
let call_count = unsafe { TOTAL_ENTRY_HITS };
let num_calls = call_count - payload.call_count_at_threshold;

if num_calls > 50_000 {
incr_counter!(compiled_entry_cold);
}

return true;
}
}

return false;
}

/// This function is called from C code
#[no_mangle]
pub extern "C" fn rb_yjit_init_rust() {
Expand Down

0 comments on commit 775585b

Please sign in to comment.