[前][次][番号順一覧][スレッド一覧]

ruby-changes:74478

From: Takashi <ko1@a...>
Date: Mon, 14 Nov 2022 02:54:58 +0900 (JST)
Subject: [ruby-changes:74478] 6246788bc4 (master): YJIT: Instrument global allocations on stats build (#6712)

https://git.ruby-lang.org/ruby.git/commit/?id=6246788bc4

From 6246788bc47b5d28e3c612479a2986d84f2aa589 Mon Sep 17 00:00:00 2001
From: Takashi Kokubun <takashikkbn@g...>
Date: Sun, 13 Nov 2022 09:54:41 -0800
Subject: YJIT: Instrument global allocations on stats build (#6712)

* YJIT: Instrument global allocations on stats build

* Just use GLOVAL_ALLOCATOR.stats()
---
 yjit.rb           |  1 +
 yjit/Cargo.lock   |  7 +++++++
 yjit/Cargo.toml   |  3 ++-
 yjit/src/stats.rs | 16 ++++++++++++++++
 4 files changed, 26 insertions(+), 1 deletion(-)

diff --git a/yjit.rb b/yjit.rb
index caa3a035d3..cac2430127 100644
--- a/yjit.rb
+++ b/yjit.rb
@@ -262,6 +262,7 @@ module RubyVM::YJIT https://github.com/ruby/ruby/blob/trunk/yjit.rb#L262
       $stderr.puts "inline_code_size:      " + ("%10d" % stats[:inline_code_size])
       $stderr.puts "outlined_code_size:    " + ("%10d" % stats[:outlined_code_size])
       $stderr.puts "freed_code_size:       " + ("%10d" % stats[:freed_code_size])
+      $stderr.puts "yjit_alloc_size:       " + ("%10d" % stats[:yjit_alloc_size]) if stats.key?(:yjit_alloc_size)
       $stderr.puts "live_page_count:       " + ("%10d" % stats[:live_page_count])
       $stderr.puts "freed_page_count:      " + ("%10d" % stats[:freed_page_count])
       $stderr.puts "code_gc_count:         " + ("%10d" % stats[:code_gc_count])
diff --git a/yjit/Cargo.lock b/yjit/Cargo.lock
index e9a59cb771..08ae1bd487 100644
--- a/yjit/Cargo.lock
+++ b/yjit/Cargo.lock
@@ -34,9 +34,16 @@ version = "0.2.124" https://github.com/ruby/ruby/blob/trunk/yjit/Cargo.lock#L34
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "21a41fed9d98f27ab1c6d161da622a4fa35e8a54a8adc24bbf3ddd0ef70b0e50"
 
+[[package]]
+name = "stats_alloc"
+version = "0.1.10"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "5c0e04424e733e69714ca1bbb9204c1a57f09f5493439520f9f68c132ad25eec"
+
 [[package]]
 name = "yjit"
 version = "0.1.0"
 dependencies = [
  "capstone",
+ "stats_alloc",
 ]
diff --git a/yjit/Cargo.toml b/yjit/Cargo.toml
index 858751b1f6..d7d96d5799 100644
--- a/yjit/Cargo.toml
+++ b/yjit/Cargo.toml
@@ -16,12 +16,13 @@ crate-type = ["staticlib"] https://github.com/ruby/ruby/blob/trunk/yjit/Cargo.toml#L16
 # No required dependencies to simplify build process. TODO: Link to yet to be
 # written rationale. Optional For development and testing purposes
 capstone = { version = "0.10.0", optional = true }
+stats_alloc = { version = "0.1.10", optional = true }
 
 [features]
 # NOTE: Development builds select a set of these via configure.ac
 # For debugging, `make V=1` shows exact cargo invocation.
 disasm = ["capstone"]
-stats = []
+stats = ["stats_alloc"]
 
 [profile.dev]
 opt-level = 0
diff --git a/yjit/src/stats.rs b/yjit/src/stats.rs
index 128672a959..cfce4c9c33 100644
--- a/yjit/src/stats.rs
+++ b/yjit/src/stats.rs
@@ -8,6 +8,11 @@ use crate::cruby::*; https://github.com/ruby/ruby/blob/trunk/yjit/src/stats.rs#L8
 use crate::options::*;
 use crate::yjit::yjit_enabled_p;
 
+// stats_alloc is a middleware to instrument global allocations in Rust.
+#[cfg(feature="stats")]
+#[global_allocator]
+static GLOBAL_ALLOCATOR: &stats_alloc::StatsAlloc<std::alloc::System> = &stats_alloc::INSTRUMENTED_SYSTEM;
+
 // YJIT exit counts for each instruction type
 const VM_INSTRUCTION_SIZE_USIZE:usize = VM_INSTRUCTION_SIZE as usize;
 static mut EXIT_OP_COUNT: [u64; VM_INSTRUCTION_SIZE_USIZE] = [0; VM_INSTRUCTION_SIZE_USIZE];
@@ -396,6 +401,10 @@ fn rb_yjit_gen_stats_dict() -> VALUE { https://github.com/ruby/ruby/blob/trunk/yjit/src/stats.rs#L401
 
         // Code GC count
         hash_aset_usize!(hash, "code_gc_count", CodegenGlobals::get_code_gc_count());
+
+        // Rust global allocations in bytes
+        #[cfg(feature="stats")]
+        hash_aset_usize!(hash, "yjit_alloc_size", global_allocation_size());
     }
 
     // If we're not generating stats, the hash is done
@@ -606,3 +615,10 @@ pub extern "C" fn rb_yjit_count_side_exit_op(exit_pc: *const VALUE) -> *const VA https://github.com/ruby/ruby/blob/trunk/yjit/src/stats.rs#L615
     // This function must return exit_pc!
     return exit_pc;
 }
+
+// Get the size of global allocations in Rust.
+#[cfg(feature="stats")]
+fn global_allocation_size() -> usize {
+    let stats = GLOBAL_ALLOCATOR.stats();
+    stats.bytes_allocated.saturating_sub(stats.bytes_deallocated)
+}
-- 
cgit v1.2.3


--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]