* 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
This commit is contained in:
ko1 2015-04-10 10:38:13 +00:00
Родитель deab71578b
Коммит 7f75a1a810
4 изменённых файлов: 100 добавлений и 0 удалений

Просмотреть файл

@ -1,3 +1,12 @@
Fri Apr 10 19:35:51 2015 Koichi Sasada <ko1@atdot.net>
* 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@fsij.org>
* test/ruby/test_file_exhaustive.rb: Test anonymous pipe.

3
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

Просмотреть файл

@ -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);

Просмотреть файл

@ -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))