1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

* ext/objspace/object_tracing.c: add new 3 methods to control tracing.

* ObjectSpace::trace_object_allocations_start
* ObjectSpace::trace_object_allocations_stop
* ObjectSpace::trace_object_allocations_clear
  And some refactoring.
* test/objspace/test_objspace.rb: add a test for new methods.
* NEWS: add a description for new methods.



git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@43095 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
ko1 2013-09-30 08:17:30 +00:00
parent 6a1a08c945
commit 1450e0b5ac
4 changed files with 168 additions and 39 deletions

View file

@ -1,3 +1,15 @@
Mon Sep 30 16:46:58 2013 Koichi Sasada <ko1@atdot.net>
* ext/objspace/object_tracing.c: add new 3 methods to control tracing.
* ObjectSpace::trace_object_allocations_start
* ObjectSpace::trace_object_allocations_stop
* ObjectSpace::trace_object_allocations_clear
And some refactoring.
* test/objspace/test_objspace.rb: add a test for new methods.
* NEWS: add a description for new methods.
Mon Sep 30 11:18:04 2013 Koichi Sasada <ko1@atdot.net> Mon Sep 30 11:18:04 2013 Koichi Sasada <ko1@atdot.net>
* gc.c (rb_gc_disable): do rest_sweep() before disable GC. * gc.c (rb_gc_disable): do rest_sweep() before disable GC.

12
NEWS
View file

@ -239,6 +239,18 @@ with all sufficient information, see the ChangeLog file.
=== Stdlib compatibility issues (excluding feature bug fixes) === Stdlib compatibility issues (excluding feature bug fixes)
* objspace
* new method:
* ObjectSpace.trace_object_allocations
* ObjectSpace.trace_object_allocations_start
* ObjectSpace.trace_object_allocations_stop
* ObjectSpace.trace_object_allocations_clear
* ObjectSpace.allocation_sourcefile
* ObjectSpace.allocation_sourceline
* ObjectSpace.allocation_class_path
* ObjectSpace.allocation_method_id
* ObjectSpace.allocation_generation
* Set * Set
* incompatible changes: * incompatible changes:
* Set#to_set now returns self instead of generating a copy. * Set#to_set now returns self instead of generating a copy.

View file

@ -19,15 +19,15 @@
size_t rb_gc_count(void); /* from gc.c */ size_t rb_gc_count(void); /* from gc.c */
struct traceobj_arg { struct traceobj_arg {
int running;
VALUE newobj_trace; VALUE newobj_trace;
VALUE freeobj_trace; VALUE freeobj_trace;
st_table *object_table; st_table *object_table; /* obj (VALUE) -> allocation_info */
st_table *str_table; st_table *str_table; /* cstr -> refcount */
struct traceobj_arg *prev_traceobj_arg; struct traceobj_arg *prev_traceobj_arg;
}; };
struct traceobj_arg *traceobj_arg; /* TODO: do not use GLOBAL VARIABLE!!! */ /* all of information don't need marking. */
struct allocation_info { struct allocation_info {
const char *path; const char *path;
unsigned long line; unsigned long line;
@ -129,17 +129,94 @@ free_values_i(st_data_t key, st_data_t value, void *data)
return ST_CONTINUE; return ST_CONTINUE;
} }
static VALUE static struct traceobj_arg *tmp_trace_arg; /* TODO: Do not use global variables */
stop_trace_object_allocations(void *data)
static struct traceobj_arg *
get_traceobj_arg(void)
{ {
struct traceobj_arg *arg = (struct traceobj_arg *)data; if (tmp_trace_arg == 0) {
rb_tracepoint_disable(arg->newobj_trace); tmp_trace_arg = ALLOC_N(struct traceobj_arg, 1);
rb_tracepoint_disable(arg->freeobj_trace); tmp_trace_arg->running = 0;
tmp_trace_arg->newobj_trace = 0;
tmp_trace_arg->freeobj_trace = 0;
tmp_trace_arg->object_table = st_init_numtable();
tmp_trace_arg->str_table = st_init_strtable();
}
return tmp_trace_arg;
}
/*
* call-seq: trace_object_allocations_start
*
* Starts tracing object allocations.
*
*/
static VALUE
trace_object_allocations_start(VALUE self)
{
struct traceobj_arg *arg = get_traceobj_arg();
if (arg->running++ > 0) {
/* do nothing */
}
else {
if (arg->newobj_trace == 0) {
arg->newobj_trace = rb_tracepoint_new(0, RUBY_INTERNAL_EVENT_NEWOBJ, newobj_i, arg);
arg->freeobj_trace = rb_tracepoint_new(0, RUBY_INTERNAL_EVENT_FREEOBJ, freeobj_i, arg);
}
rb_tracepoint_enable(arg->newobj_trace);
rb_tracepoint_enable(arg->freeobj_trace);
}
return Qnil;
}
/*
* call-seq: trace_object_allocations_stop
*
* Stop tracing object allocations.
*
* Note that if trace_object_allocations_start is called n-times, then
* stop tracing after calling trace_object_allocations_stop n-times.
*
*/
static VALUE
trace_object_allocations_stop(VALUE self)
{
struct traceobj_arg *arg = get_traceobj_arg();
if (arg->running > 0) {
arg->running--;
}
if (arg->running == 0) {
rb_tracepoint_disable(arg->newobj_trace);
rb_tracepoint_disable(arg->freeobj_trace);
arg->newobj_trace = 0;
arg->freeobj_trace = 0;
}
return Qnil;
}
/*
* call-seq: trace_object_allocations_clear
*
* Clear recorded tracing information.
*
*/
static VALUE
trace_object_allocations_clear(VALUE self)
{
struct traceobj_arg *arg = get_traceobj_arg();
/* clear tables */
st_foreach(arg->object_table, free_values_i, 0); st_foreach(arg->object_table, free_values_i, 0);
st_clear(arg->object_table);
st_foreach(arg->str_table, free_keys_i, 0); st_foreach(arg->str_table, free_keys_i, 0);
st_free_table(arg->object_table); st_clear(arg->str_table);
st_free_table(arg->str_table);
traceobj_arg = arg->prev_traceobj_arg; /* do not touch TracePoints */
return Qnil; return Qnil;
} }
@ -171,30 +248,18 @@ stop_trace_object_allocations(void *data)
* "<code>ObjectSpace::trace_object_allocations</code>" notation. * "<code>ObjectSpace::trace_object_allocations</code>" notation.
*/ */
static VALUE static VALUE
trace_object_allocations(VALUE objspace) trace_object_allocations(VALUE self)
{ {
struct traceobj_arg arg; trace_object_allocations_start(self);
return rb_ensure(rb_yield, Qnil, trace_object_allocations_stop, self);
arg.newobj_trace = rb_tracepoint_new(0, RUBY_INTERNAL_EVENT_NEWOBJ, newobj_i, &arg);
arg.freeobj_trace = rb_tracepoint_new(0, RUBY_INTERNAL_EVENT_FREEOBJ, freeobj_i, &arg);
arg.object_table = st_init_numtable();
arg.str_table = st_init_strtable();
arg.prev_traceobj_arg = traceobj_arg;
traceobj_arg = &arg;
rb_tracepoint_enable(arg.newobj_trace);
rb_tracepoint_enable(arg.freeobj_trace);
return rb_ensure(rb_yield, Qnil, stop_trace_object_allocations, (VALUE)&arg);
} }
static struct allocation_info * static struct allocation_info *
lookup_allocation_info(VALUE obj) lookup_allocation_info(VALUE obj)
{ {
if (traceobj_arg) { if (tmp_trace_arg) {
struct allocation_info *info; struct allocation_info *info;
if (st_lookup(traceobj_arg->object_table, obj, (st_data_t *)&info)) { if (st_lookup(tmp_trace_arg->object_table, obj, (st_data_t *)&info)) {
return info; return info;
} }
} }
@ -209,11 +274,12 @@ lookup_allocation_info(VALUE obj)
* See ::trace_object_allocations for more information and examples. * See ::trace_object_allocations for more information and examples.
*/ */
static VALUE static VALUE
allocation_sourcefile(VALUE objspace, VALUE obj) allocation_sourcefile(VALUE self, VALUE obj)
{ {
struct allocation_info *info = lookup_allocation_info(obj); struct allocation_info *info = lookup_allocation_info(obj);
if (info) {
return info->path ? rb_str_new2(info->path) : Qnil; if (info && info->path) {
return rb_str_new2(info->path);
} }
else { else {
return Qnil; return Qnil;
@ -228,9 +294,10 @@ allocation_sourcefile(VALUE objspace, VALUE obj)
* See ::trace_object_allocations for more information and examples. * See ::trace_object_allocations for more information and examples.
*/ */
static VALUE static VALUE
allocation_sourceline(VALUE objspace, VALUE obj) allocation_sourceline(VALUE self, VALUE obj)
{ {
struct allocation_info *info = lookup_allocation_info(obj); struct allocation_info *info = lookup_allocation_info(obj);
if (info) { if (info) {
return INT2FIX(info->line); return INT2FIX(info->line);
} }
@ -258,11 +325,12 @@ allocation_sourceline(VALUE objspace, VALUE obj)
* See ::trace_object_allocations for more information and examples. * See ::trace_object_allocations for more information and examples.
*/ */
static VALUE static VALUE
allocation_class_path(VALUE objspace, VALUE obj) allocation_class_path(VALUE self, VALUE obj)
{ {
struct allocation_info *info = lookup_allocation_info(obj); struct allocation_info *info = lookup_allocation_info(obj);
if (info) {
return info->class_path ? rb_str_new2(info->class_path) : Qnil; if (info && info->class_path) {
return rb_str_new2(info->class_path);
} }
else { else {
return Qnil; return Qnil;
@ -290,7 +358,7 @@ allocation_class_path(VALUE objspace, VALUE obj)
* See ::trace_object_allocations for more information and examples. * See ::trace_object_allocations for more information and examples.
*/ */
static VALUE static VALUE
allocation_method_id(VALUE objspace, VALUE obj) allocation_method_id(VALUE self, VALUE obj)
{ {
struct allocation_info *info = lookup_allocation_info(obj); struct allocation_info *info = lookup_allocation_info(obj);
if (info) { if (info) {
@ -322,7 +390,7 @@ allocation_method_id(VALUE objspace, VALUE obj)
* See ::trace_object_allocations for more information and examples. * See ::trace_object_allocations for more information and examples.
*/ */
static VALUE static VALUE
allocation_generation(VALUE objspace, VALUE obj) allocation_generation(VALUE self, VALUE obj)
{ {
struct allocation_info *info = lookup_allocation_info(obj); struct allocation_info *info = lookup_allocation_info(obj);
if (info) { if (info) {
@ -341,6 +409,10 @@ Init_object_tracing(VALUE rb_mObjSpace)
#endif #endif
rb_define_module_function(rb_mObjSpace, "trace_object_allocations", trace_object_allocations, 0); rb_define_module_function(rb_mObjSpace, "trace_object_allocations", trace_object_allocations, 0);
rb_define_module_function(rb_mObjSpace, "trace_object_allocations_start", trace_object_allocations_start, 0);
rb_define_module_function(rb_mObjSpace, "trace_object_allocations_stop", trace_object_allocations_stop, 0);
rb_define_module_function(rb_mObjSpace, "trace_object_allocations_clear", trace_object_allocations_clear, 0);
rb_define_module_function(rb_mObjSpace, "allocation_sourcefile", allocation_sourcefile, 1); rb_define_module_function(rb_mObjSpace, "allocation_sourcefile", allocation_sourcefile, 1);
rb_define_module_function(rb_mObjSpace, "allocation_sourceline", allocation_sourceline, 1); rb_define_module_function(rb_mObjSpace, "allocation_sourceline", allocation_sourceline, 1);
rb_define_module_function(rb_mObjSpace, "allocation_class_path", allocation_class_path, 1); rb_define_module_function(rb_mObjSpace, "allocation_class_path", allocation_class_path, 1);

View file

@ -109,7 +109,7 @@ class TestObjSpace < Test::Unit::TestCase
eom eom
end end
def test_traceobject def test_trace_object_allocations
o0 = Object.new o0 = Object.new
ObjectSpace.trace_object_allocations{ ObjectSpace.trace_object_allocations{
o1 = Object.new; line1 = __LINE__; c1 = GC.count o1 = Object.new; line1 = __LINE__; c1 = GC.count
@ -139,4 +139,37 @@ class TestObjSpace < Test::Unit::TestCase
assert_equal(__method__, ObjectSpace.allocation_method_id(o3)) assert_equal(__method__, ObjectSpace.allocation_method_id(o3))
} }
end end
def test_trace_object_allocations_start_stop_clear
begin
ObjectSpace.trace_object_allocations_start
begin
ObjectSpace.trace_object_allocations_start
begin
ObjectSpace.trace_object_allocations_start
obj0 = Object.new
ensure
ObjectSpace.trace_object_allocations_stop
obj1 = Object.new
end
ensure
ObjectSpace.trace_object_allocations_stop
obj2 = Object.new
end
ensure
ObjectSpace.trace_object_allocations_stop
obj3 = Object.new
end
assert_equal(__FILE__, ObjectSpace.allocation_sourcefile(obj0))
assert_equal(__FILE__, ObjectSpace.allocation_sourcefile(obj1))
assert_equal(__FILE__, ObjectSpace.allocation_sourcefile(obj2))
assert_equal(nil , ObjectSpace.allocation_sourcefile(obj3)) # after tracing
ObjectSpace.trace_object_allocations_clear
assert_equal(nil, ObjectSpace.allocation_sourcefile(obj0))
assert_equal(nil, ObjectSpace.allocation_sourcefile(obj1))
assert_equal(nil, ObjectSpace.allocation_sourcefile(obj2))
assert_equal(nil, ObjectSpace.allocation_sourcefile(obj3))
end
end end