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

ruby-changes:69939

From: Peter <ko1@a...>
Date: Fri, 26 Nov 2021 00:33:30 +0900 (JST)
Subject: [ruby-changes:69939] 6157619bb6 (master): Add GC.stat_size_pool to get stats for a size pool

https://git.ruby-lang.org/ruby.git/commit/?id=6157619bb6

From 6157619bb68e4307cdf065cb73d5bfcec30d042d Mon Sep 17 00:00:00 2001
From: Peter Zhu <peter@p...>
Date: Thu, 25 Nov 2021 09:31:58 -0500
Subject: Add GC.stat_size_pool to get stats for a size pool

GC.stat_size_pool will return stats for a particular size pool. This is
used for the Variable Width Allocation feature.
---
 gc.c                 | 95 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 gc.rb                | 18 ++++++++++
 test/ruby/test_gc.rb | 48 ++++++++++++++++++++++++++
 3 files changed, 161 insertions(+)

diff --git a/gc.c b/gc.c
index e0ba25860ca..3b94e8ded0c 100644
--- a/gc.c
+++ b/gc.c
@@ -10675,6 +10675,101 @@ rb_gc_stat(VALUE key) https://github.com/ruby/ruby/blob/trunk/gc.c#L10675
     }
 }
 
+
+enum gc_stat_size_pool_sym {
+    gc_stat_size_pool_sym_slot_size,
+    gc_stat_size_pool_sym_heap_allocatable_pages,
+    gc_stat_size_pool_sym_heap_eden_pages,
+    gc_stat_size_pool_sym_heap_eden_slots,
+    gc_stat_size_pool_sym_heap_tomb_pages,
+    gc_stat_size_pool_sym_heap_tomb_slots,
+    gc_stat_size_pool_sym_last
+};
+
+static VALUE gc_stat_size_pool_symbols[gc_stat_size_pool_sym_last];
+
+static void
+setup_gc_stat_size_pool_symbols(void)
+{
+    if (gc_stat_size_pool_symbols[0] == 0) {
+#define S(s) gc_stat_size_pool_symbols[gc_stat_size_pool_sym_##s] = ID2SYM(rb_intern_const(#s))
+        S(slot_size);
+        S(heap_allocatable_pages);
+        S(heap_eden_pages);
+        S(heap_eden_slots);
+        S(heap_tomb_pages);
+        S(heap_tomb_slots);
+#undef S
+    }
+}
+
+static size_t
+gc_stat_size_pool_internal(int size_pool_idx, VALUE hash_or_sym)
+{
+    rb_objspace_t *objspace = &rb_objspace;
+    VALUE hash = Qnil, key = Qnil;
+
+    setup_gc_stat_size_pool_symbols();
+
+    if (RB_TYPE_P(hash_or_sym, T_HASH)) {
+        hash = hash_or_sym;
+    }
+    else if (SYMBOL_P(hash_or_sym)) {
+        key = hash_or_sym;
+    }
+    else {
+        rb_raise(rb_eTypeError, "non-hash or symbol argument");
+    }
+
+    if (size_pool_idx < 0 || size_pool_idx >= SIZE_POOL_COUNT) {
+        rb_raise(rb_eArgError, "size pool index out of range");
+    }
+
+    rb_size_pool_t *size_pool = &size_pools[size_pool_idx];
+
+#define SET(name, attr) \
+    if (key == gc_stat_size_pool_symbols[gc_stat_size_pool_sym_##name]) \
+        return attr; \
+    else if (hash != Qnil) \
+        rb_hash_aset(hash, gc_stat_size_pool_symbols[gc_stat_size_pool_sym_##name], SIZET2NUM(attr));
+
+    SET(slot_size, size_pool->slot_size);
+    SET(heap_allocatable_pages, size_pool->allocatable_pages);
+    SET(heap_eden_pages, SIZE_POOL_EDEN_HEAP(size_pool)->total_pages);
+    SET(heap_eden_slots, SIZE_POOL_EDEN_HEAP(size_pool)->total_slots);
+    SET(heap_tomb_pages, SIZE_POOL_TOMB_HEAP(size_pool)->total_pages);
+    SET(heap_tomb_slots, SIZE_POOL_TOMB_HEAP(size_pool)->total_slots);
+#undef SET
+
+    if (!NIL_P(key)) { /* matched key should return above */
+        rb_raise(rb_eArgError, "unknown key: %"PRIsVALUE, rb_sym2str(key));
+    }
+
+    return 0;
+}
+
+static VALUE
+gc_stat_size_pool(rb_execution_context_t *ec, VALUE self, VALUE size_pool_idx, VALUE arg)
+{
+    if (NIL_P(arg)) {
+        arg = rb_hash_new();
+    }
+    else if (SYMBOL_P(arg)) {
+        size_t value = gc_stat_internal(arg);
+        return SIZET2NUM(value);
+    }
+    else if (RB_TYPE_P(arg, T_HASH)) {
+        // ok
+    }
+    else {
+        rb_raise(rb_eTypeError, "non-hash or symbol given");
+    }
+
+    gc_stat_size_pool_internal(FIX2INT(size_pool_idx), arg);
+
+    return arg;
+}
+
 static VALUE
 gc_stress_get(rb_execution_context_t *ec, VALUE self)
 {
diff --git a/gc.rb b/gc.rb
index 7a60710f66d..47beab8cbcf 100644
--- a/gc.rb
+++ b/gc.rb
@@ -196,6 +196,24 @@ module GC https://github.com/ruby/ruby/blob/trunk/gc.rb#L196
     Primitive.gc_stat hash_or_key
   end
 
+  # :nodoc:
+  # call-seq:
+  #    GC.stat_size_pool(size_pool_idx) -> Hash
+  #    GC.stat_size_pool(size_pool_idx, hash) -> Hash
+  #    GC.stat_size_pool(size_pool_idx, :key) -> Numeric
+  #
+  # Returns a Hash containing information about a size pool in the GC.
+  #
+  # The contents of the hash are implementation specific and may be changed in
+  # the future.
+  #
+  # If the optional argument, hash, is given, it is overwritten and returned.
+  #
+  # This method is only expected to work on C Ruby.
+  def self.stat_size_pool size_pool_idx, hash_or_key = nil
+    Primitive.gc_stat_size_pool size_pool_idx, hash_or_key
+  end
+
   #  call-seq:
   #     GC.latest_gc_info -> {:gc_by=>:newobj}
   #     GC.latest_gc_info(hash) -> hash
diff --git a/test/ruby/test_gc.rb b/test/ruby/test_gc.rb
index baf9971c483..1cb030e76ab 100644
--- a/test/ruby/test_gc.rb
+++ b/test/ruby/test_gc.rb
@@ -139,6 +139,54 @@ class TestGc < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_gc.rb#L139
     end
   end
 
+  def test_stat_size_pool
+    skip 'stress' if GC.stress
+
+    stat_size_pool = {}
+    stat = {}
+    # Initialize to prevent GC in future calls
+    GC.stat_size_pool(0, stat_size_pool)
+    GC.stat(stat)
+
+    GC::INTERNAL_CONSTANTS[:SIZE_POOL_COUNT].times do |i|
+      GC.stat_size_pool(i, stat_size_pool)
+      GC.stat(stat)
+
+      assert_equal GC::INTERNAL_CONSTANTS[:RVALUE_SIZE] * (2**i), stat_size_pool[:slot_size]
+      assert_operator stat_size_pool[:heap_allocatable_pages], :<=, stat[:heap_allocatable_pages]
+      assert_operator stat_size_pool[:heap_eden_pages], :<=, stat[:heap_eden_pages]
+      assert_operator stat_size_pool[:heap_eden_slots], :>=, 0
+      assert_operator stat_size_pool[:heap_tomb_pages], :<=, stat[:heap_tomb_pages]
+      assert_operator stat_size_pool[:heap_tomb_slots], :>=, 0
+    end
+
+    assert_raise(ArgumentError) { GC.stat_size_pool(-1) }
+    assert_raise(ArgumentError) { GC.stat_size_pool(GC::INTERNAL_CONSTANTS[:SIZE_POOL_COUNT]) }
+  end
+
+  def test_stat_size_pool_constraints
+    skip 'stress' if GC.stress
+
+    stat = GC.stat
+    stat_size_pools = []
+    # Initialize to prevent GC in future calls
+    GC::INTERNAL_CONSTANTS[:SIZE_POOL_COUNT].times do |i|
+      stat_size_pools << GC.stat_size_pool(i)
+    end
+    # Get actual stats
+    GC.stat(stat)
+    GC::INTERNAL_CONSTANTS[:SIZE_POOL_COUNT].times do |i|
+      GC.stat_size_pool(i, stat_size_pools[i])
+    end
+
+    stat_size_pools_sum = stat_size_pools[0].keys.to_h { |k| [k, stat_size_pools.sum { |e| e[k] }] }
+
+    assert_equal stat[:heap_allocatable_pages], stat_size_pools_sum[:heap_allocatable_pages]
+    assert_equal stat[:heap_eden_pages], stat_size_pools_sum[:heap_eden_pages]
+    assert_equal stat[:heap_tomb_pages], stat_size_pools_sum[:heap_tomb_pages]
+    assert_equal stat[:heap_available_slots], stat_size_pools_sum[:heap_eden_slots] + stat_size_pools_sum[:heap_tomb_slots]
+  end
+
   def test_latest_gc_info
     skip 'stress' if GC.stress
 
-- 
cgit v1.2.1


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

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