Skip to content

Commit

Permalink
YJIT: Add a live ISeq counter
Browse files Browse the repository at this point in the history
It's an estimator for application size and could be used as a
compilation heuristic later.

Co-authored-by: Maxime Chevalier-Boisvert <[email protected]>
Co-authored-by: Takashi Kokubun <[email protected]>
  • Loading branch information
3 people committed Oct 17, 2023
1 parent d458b41 commit ce8f699
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 0 deletions.
1 change: 1 addition & 0 deletions common.mk
Original file line number Diff line number Diff line change
Expand Up @@ -3429,6 +3429,7 @@ compile.$(OBJEXT): {$(VPATH)}vm_callinfo.h
compile.$(OBJEXT): {$(VPATH)}vm_core.h
compile.$(OBJEXT): {$(VPATH)}vm_debug.h
compile.$(OBJEXT): {$(VPATH)}vm_opts.h
compile.$(OBJEXT): {$(VPATH)}yjit.h
complex.$(OBJEXT): $(CCAN_DIR)/check_type/check_type.h
complex.$(OBJEXT): $(CCAN_DIR)/container_of/container_of.h
complex.$(OBJEXT): $(CCAN_DIR)/list/list.h
Expand Down
6 changes: 6 additions & 0 deletions compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
#include "vm_core.h"
#include "vm_callinfo.h"
#include "vm_debug.h"
#include "yjit.h"

#include "builtin.h"
#include "insns.inc"
Expand Down Expand Up @@ -1019,6 +1020,11 @@ rb_iseq_translate_threaded_code(rb_iseq_t *iseq)
}
FL_SET((VALUE)iseq, ISEQ_TRANSLATED);
#endif

#if USE_YJIT
rb_yjit_live_iseq_count++;
#endif

return COMPILE_OK;
}

Expand Down
2 changes: 2 additions & 0 deletions iseq.c
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,8 @@ rb_iseq_free(const rb_iseq_t *iseq)
rb_rjit_free_iseq(iseq); /* Notify RJIT */
#if USE_YJIT
rb_yjit_iseq_free(body->yjit_payload);
RUBY_ASSERT(rb_yjit_live_iseq_count > 0);
rb_yjit_live_iseq_count--;
#endif
ruby_xfree((void *)body->iseq_encoded);
ruby_xfree((void *)body->insns_info.body);
Expand Down
1 change: 1 addition & 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.
extern uint64_t rb_yjit_call_threshold;
extern uint64_t rb_yjit_cold_threshold;
extern uint64_t rb_yjit_live_iseq_count;
void rb_yjit_incr_counter(const char *counter_name);
bool rb_yjit_enabled_p(void);
bool rb_yjit_compile_new_iseqs(void);
Expand Down
1 change: 1 addition & 0 deletions yjit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,7 @@ def _print_stats(out: $stderr) # :nodoc:
out.puts "compilation_failure: " + format_number(13, compilation_failure) if compilation_failure != 0
out.puts "compiled_iseq_entry: " + format_number(13, stats[:compiled_iseq_entry])
out.puts "cold_iseq_entry: " + format_number_pct(13, stats[:cold_iseq_entry], stats[:compiled_iseq_entry] + stats[:cold_iseq_entry])
out.puts "live_iseq_count: " + format_number(13, stats[:live_iseq_count])
out.puts "compiled_iseq_count: " + format_number(13, stats[:compiled_iseq_count])
out.puts "compiled_blockid_count:" + format_number(13, stats[:compiled_blockid_count])
out.puts "compiled_block_count: " + format_number(13, stats[:compiled_block_count])
Expand Down
6 changes: 6 additions & 0 deletions yjit/src/stats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ use crate::cruby::*;
use crate::options::*;
use crate::yjit::yjit_enabled_p;

/// A running total of how many ISeqs are in the system.
#[no_mangle]
pub static mut rb_yjit_live_iseq_count: u64 = 0;

/// A middleware to count Rust-allocated bytes as yjit_alloc_size.
#[global_allocator]
static GLOBAL_ALLOCATOR: StatsAlloc = StatsAlloc { alloc_size: AtomicUsize::new(0) };
Expand Down Expand Up @@ -635,6 +639,8 @@ fn rb_yjit_gen_stats_dict(context: bool) -> VALUE {

// VM instructions count
hash_aset_usize!(hash, "vm_insns_count", rb_vm_insns_count as usize);

hash_aset_usize!(hash, "live_iseq_count", rb_yjit_live_iseq_count as usize);
}

// If we're not generating stats, put only default counters
Expand Down

0 comments on commit ce8f699

Please sign in to comment.