From 7f75a1a810fa031bb6ff31166d8ada09a27e9f35 Mon Sep 17 00:00:00 2001 From: ko1 Date: Fri, 10 Apr 2015 10:38:13 +0000 Subject: * 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. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@50211 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 9 +++++ NEWS | 3 ++ ext/objspace/objspace.c | 77 ++++++++++++++++++++++++++++++++++++++++++ test/objspace/test_objspace.rb | 11 ++++++ 4 files changed, 100 insertions(+) diff --git a/ChangeLog b/ChangeLog index aa83d7fb4b..125cfa9314 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,12 @@ +Fri Apr 10 19:35:51 2015 Koichi Sasada + + * 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 * test/ruby/test_file_exhaustive.rb: Test anonymous pipe. diff --git a/NEWS b/NEWS index 4a17ff2cf5..a7ab711dd1 100644 --- a/NEWS +++ b/NEWS @@ -39,6 +39,9 @@ with all sufficient information, see the ChangeLog file. * 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 diff --git a/ext/objspace/objspace.c b/ext/objspace/objspace.c index ec2840feb5..bf0d31a8e2 100644 --- a/ext/objspace/objspace.c +++ b/ext/objspace/objspace.c @@ -500,6 +500,82 @@ count_tdata_objects(int argc, VALUE *argv, VALUE self) 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) 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); diff --git a/test/objspace/test_objspace.rb b/test/objspace/test_objspace.rb index 3870414d42..665fafe7c8 100644 --- a/test/objspace/test_objspace.rb +++ b/test/objspace/test_objspace.rb @@ -79,6 +79,17 @@ class TestObjSpace < Test::Unit::TestCase 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)) -- cgit v1.2.3