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

ruby-changes:38130

From: ko1 <ko1@a...>
Date: Fri, 10 Apr 2015 19:38:26 +0900 (JST)
Subject: [ruby-changes:38130] ko1:r50211 (trunk): * ext/objspace/objspace.c: add ObjectSpace.count_imemo_objects method

ko1	2015-04-10 19:38:13 +0900 (Fri, 10 Apr 2015)

  New Revision: 50211

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

  Log:
    * ext/objspace/objspace.c: add ObjectSpace.count_imemo_objects method
      to count imemo objects for each type.
    * test/objspace/test_objspace.rb: add a test.
    * NEWS: describe about this addition.

  Modified files:
    trunk/ChangeLog
    trunk/NEWS
    trunk/ext/objspace/objspace.c
    trunk/test/objspace/test_objspace.rb
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 50210)
+++ ChangeLog	(revision 50211)
@@ -1,3 +1,12 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Fri Apr 10 19:35:51 2015  Koichi Sasada  <ko1@a...>
+
+	* ext/objspace/objspace.c: add ObjectSpace.count_imemo_objects method
+	  to count imemo objects for each type.
+
+	* test/objspace/test_objspace.rb: add a test.
+
+	* NEWS: describe about this addition.
+
 Fri Apr 10 19:34:24 2015  Tanaka Akira  <akr@f...>
 
 	* test/ruby/test_file_exhaustive.rb: Test anonymous pipe.
Index: ext/objspace/objspace.c
===================================================================
--- ext/objspace/objspace.c	(revision 50210)
+++ ext/objspace/objspace.c	(revision 50211)
@@ -500,6 +500,82 @@ count_tdata_objects(int argc, VALUE *arg https://github.com/ruby/ruby/blob/trunk/ext/objspace/objspace.c#L500
     return hash;
 }
 
+static ID imemo_type_ids[imemo_mask];
+
+static int
+count_imemo_objects_i(void *vstart, void *vend, size_t stride, void *data)
+{
+    VALUE hash = (VALUE)data;
+    VALUE v = (VALUE)vstart;
+
+    for (; v != (VALUE)vend; v += stride) {
+	if (RBASIC(v)->flags && BUILTIN_TYPE(v) == T_IMEMO) {
+	    VALUE counter;
+	    VALUE key = ID2SYM(imemo_type_ids[imemo_type(v)]);
+
+	    counter = rb_hash_aref(hash, key);
+
+	    if (NIL_P(counter)) {
+		counter = INT2FIX(1);
+	    }
+	    else {
+		counter = INT2FIX(FIX2INT(counter) + 1);
+	    }
+
+	    rb_hash_aset(hash, key, counter);
+	}
+    }
+
+    return 0;
+}
+
+/*
+ *  call-seq:
+ *     ObjectSpace.count_imemo_objects([result_hash]) -> hash
+ *
+ *  Counts objects for each +T_IMEMO+ type.
+ *
+ *  This method is only for MRI developers interested in performance and memory
+ *  usage of Ruby programs.
+ *
+ *  It returns a hash as:
+ *
+ *       {:imemo_ifunc=>8,
+ *        :imemo_svar=>7,
+ *        :imemo_cref=>509,
+ *        :imemo_memo=>1,
+ *        :imemo_throw_data=>1}
+ *
+ *  If the optional argument, result_hash, is given, it is overwritten and
+ *  returned. This is intended to avoid probe effect.
+ *
+ *  The contents of the returned hash is implementation specific and may change
+ *  in the future.
+ *
+ *  In this version, keys are symbol objects.
+ *
+ *  This method is only expected to work with C Ruby.
+ */
+
+static VALUE
+count_imemo_objects(int argc, VALUE *argv, VALUE self)
+{
+    VALUE hash = setup_hash(argc, argv);
+
+    if (imemo_type_ids[0] == 0) {
+	imemo_type_ids[0] = rb_intern("imemo_none");
+	imemo_type_ids[1] = rb_intern("imemo_cref");
+	imemo_type_ids[2] = rb_intern("imemo_svar");
+	imemo_type_ids[3] = rb_intern("imemo_throw_data");
+	imemo_type_ids[4] = rb_intern("imemo_ifunc");
+	imemo_type_ids[5] = rb_intern("imemo_memo");
+    }
+
+    rb_objspace_each_objects(count_imemo_objects_i, (void *)hash);
+
+    return hash;
+}
+
 static void
 iow_mark(void *ptr)
 {
@@ -749,6 +825,7 @@ Init_objspace(void) https://github.com/ruby/ruby/blob/trunk/ext/objspace/objspace.c#L825
     rb_define_module_function(rb_mObjSpace, "count_objects_size", count_objects_size, -1);
     rb_define_module_function(rb_mObjSpace, "count_nodes", count_nodes, -1);
     rb_define_module_function(rb_mObjSpace, "count_tdata_objects", count_tdata_objects, -1);
+    rb_define_module_function(rb_mObjSpace, "count_imemo_objects", count_imemo_objects, -1);
 
     rb_define_module_function(rb_mObjSpace, "reachable_objects_from", reachable_objects_from, 1);
     rb_define_module_function(rb_mObjSpace, "reachable_objects_from_root", reachable_objects_from_root, 0);
Index: NEWS
===================================================================
--- NEWS	(revision 50210)
+++ NEWS	(revision 50211)
@@ -39,6 +39,9 @@ with all sufficient information, see the https://github.com/ruby/ruby/blob/trunk/NEWS#L39
   * Socket#accept_nonblock supports `exception :false` to return symbols
     Ditto for TCPServer#accept_nonblock, UNIXServer#accept_nonblock
 
+* ObjectSpace (objspace)
+  * ObjectSpace.count_imemo_objects is added.
+
 * OpenSSL
   * OpenSSL::SSL::SSLSocket#accept_nonblock supports `exception: false`
     to return symbols
Index: test/objspace/test_objspace.rb
===================================================================
--- test/objspace/test_objspace.rb	(revision 50210)
+++ test/objspace/test_objspace.rb	(revision 50211)
@@ -79,6 +79,17 @@ class TestObjSpace < Test::Unit::TestCas https://github.com/ruby/ruby/blob/trunk/test/objspace/test_objspace.rb#L79
     assert_not_empty(arg)
   end
 
+  def test_count_imemo_objects
+    res = ObjectSpace.count_imemo_objects
+    puts
+    pp res
+    assert_not_empty(res)
+    assert_not_nil(res[:imemo_cref])
+    arg = {}
+    res = ObjectSpace.count_imemo_objects(arg)
+    assert_not_empty(res)
+  end
+
   def test_reachable_objects_from
     assert_separately %w[--disable-gem -robjspace], __FILE__, __LINE__, <<-'eom'
     assert_equal(nil, ObjectSpace.reachable_objects_from(nil))

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

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