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

ruby-changes:17610

From: ko1 <ko1@a...>
Date: Thu, 28 Oct 2010 04:02:32 +0900 (JST)
Subject: [ruby-changes:17610] Ruby:r29616 (trunk): * gc.c (GC.stat): added.

ko1	2010-10-28 04:02:24 +0900 (Thu, 28 Oct 2010)

  New Revision: 29616

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=29616

  Log:
    * gc.c (GC.stat): added.  [ruby-dev:38607]
    * test/ruby/test_gc.rb: add a test for above.

  Modified files:
    trunk/ChangeLog
    trunk/gc.c
    trunk/test/ruby/test_gc.rb

Index: ChangeLog
===================================================================
--- ChangeLog	(revision 29615)
+++ ChangeLog	(revision 29616)
@@ -1,3 +1,9 @@
+Thu Oct 28 04:00:08 2010  Koichi Sasada  <ko1@a...>
+
+	* gc.c (GC.stat): added.  [ruby-dev:38607]
+
+	* test/ruby/test_gc.rb: add a test for above.
+
 Thu Oct 28 03:13:06 2010  Koichi Sasada  <ko1@a...>
 
 	* ext/objspace/objspace.c (memsize_of): fix rdoc.
Index: gc.c
===================================================================
--- gc.c	(revision 29615)
+++ gc.c	(revision 29616)
@@ -329,6 +329,7 @@
 	size_t live_num;
 	size_t free_num;
 	size_t free_min;
+	size_t final_num;
 	size_t do_heap_free;
     } heap;
     struct {
@@ -353,7 +354,7 @@
 	double invoke_time;
     } profile;
     struct gc_list *global_list;
-    unsigned int count;
+    size_t count;
     int gc_stress;
 } rb_objspace_t;
 
@@ -1995,6 +1996,7 @@
     else {
         objspace->heap.free_num += free_num;
     }
+    objspace->heap.final_num += final_num;
 }
 
 static int
@@ -2024,6 +2026,7 @@
     }
     objspace->heap.sweep_slots = heaps;
     objspace->heap.free_num = 0;
+    objspace->heap.final_num = 0;
 }
 
 static void
@@ -3276,6 +3279,52 @@
     return UINT2NUM((&rb_objspace)->count);
 }
 
+/*
+ *  call-seq:
+ *     GC.stat -> Hash
+ *
+ *  Return information about GC.
+ *
+ *  It returns the hash includes information about internal statistisc
+ *  about GC such as: {:count => 42, ...}
+ *
+ *  The contents of the returned hash is implementation defined.
+ *  It may be changed in future.
+ *
+ *  This method is not expected to work except C Ruby.
+ *
+ */
+
+static VALUE
+gc_stat(int argc, VALUE *argv, VALUE self)
+{
+    rb_objspace_t *objspace = &rb_objspace;
+    VALUE hash;
+
+    if (rb_scan_args(argc, argv, "01", &hash) == 1) {
+        if (TYPE(hash) != T_HASH)
+            rb_raise(rb_eTypeError, "non-hash given");
+    }
+
+    if (hash == Qnil) {
+        hash = rb_hash_new();
+    }
+
+    rest_sweep(objspace);
+
+    rb_hash_aset(hash, ID2SYM(rb_intern("count")), SIZET2NUM(objspace->count));
+
+    /* implementation dependent counters */
+    rb_hash_aset(hash, ID2SYM(rb_intern("heap_used")), SIZET2NUM(objspace->heap.used));
+    rb_hash_aset(hash, ID2SYM(rb_intern("heap_length")), SIZET2NUM(objspace->heap.length));
+    rb_hash_aset(hash, ID2SYM(rb_intern("heap_increment")), SIZET2NUM(objspace->heap.increment));
+    rb_hash_aset(hash, ID2SYM(rb_intern("heap_live_num")), SIZET2NUM(objspace->heap.live_num));
+    rb_hash_aset(hash, ID2SYM(rb_intern("heap_free_num")), SIZET2NUM(objspace->heap.free_num));
+    rb_hash_aset(hash, ID2SYM(rb_intern("heap_final_num")), SIZET2NUM(objspace->heap.final_num));
+    return hash;
+}
+
+
 #if CALC_EXACT_MALLOC_SIZE
 /*
  *  call-seq:
@@ -3474,6 +3523,7 @@
     rb_define_singleton_method(rb_mGC, "stress", gc_stress_get, 0);
     rb_define_singleton_method(rb_mGC, "stress=", gc_stress_set, 1);
     rb_define_singleton_method(rb_mGC, "count", gc_count, 0);
+    rb_define_singleton_method(rb_mGC, "stat", gc_stat, -1);
     rb_define_method(rb_mGC, "garbage_collect", rb_gc_start, 0);
 
     rb_mProfiler = rb_define_module_under(rb_mGC, "Profiler");
Index: test/ruby/test_gc.rb
===================================================================
--- test/ruby/test_gc.rb	(revision 29615)
+++ test/ruby/test_gc.rb	(revision 29616)
@@ -51,4 +51,16 @@
     GC.start
     assert_operator(c, :<, GC.count)
   end
+
+  def test_stat
+    res = GC.stat
+    assert_equal(false, res.empty?)
+    assert_kind_of(Integer, res[:count])
+
+    arg = Hash.new
+    res = GC.stat(arg)
+    assert_equal(arg, res)
+    assert_equal(false, res.empty?)
+    assert_kind_of(Integer, res[:count])
+  end
 end

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

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