aboutsummaryrefslogtreecommitdiffstats
path: root/gc.c
diff options
context:
space:
mode:
authorko1 <ko1@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2013-05-27 00:21:02 +0000
committerko1 <ko1@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2013-05-27 00:21:02 +0000
commit680f0b5ba4eb4ba4f542ebc8d1204da61b71eb92 (patch)
tree8b49f2f3f6fe57d5d5738402089c792ef4368bc2 /gc.c
parentdc7522f835937152cd3f7a410886d6f8ed8a7ea0 (diff)
downloadruby-680f0b5ba4eb4ba4f542ebc8d1204da61b71eb92.tar.gz
* include/ruby/ruby.h, gc.c, vm_trace.c: add internal events.
* RUBY_INTERNAL_EVENT_NEWOBJ: object created. * RUBY_INTERNAL_EVENT_FREE: object freeed. * RUBY_INTERNAL_EVENT_GC_START: GC started. And rename `RUBY_EVENT_SWITCH' to `RUBY_INTERNAL_EVENT_SWITCH'. Internal events can not invoke any Ruby program because the tracing timing may be critical (under huge restriction). These events can be hooked only by C-extensions. We recommend to use rb_potponed_job_register() API to call Ruby program safely. This change is mostly written by Aman Gupta (tmm1). https://bugs.ruby-lang.org/issues/8107#note-12 [Feature #8107] * include/ruby/debug.h, vm_trace.c: added two new APIs. * rb_tracearg_event_flag() returns rb_event_flag_t of this event. * rb_tracearg_object() returns created/freeed object. * ext/-test-/tracepoint/extconf.rb, ext/-test-/tracepoint/tracepoint.c, test/-ext-/tracepoint/test_tracepoint.rb: add a test. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@40946 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'gc.c')
-rw-r--r--gc.c30
1 files changed, 29 insertions, 1 deletions
diff --git a/gc.c b/gc.c
index a9a92b8bb3..c405110ebf 100644
--- a/gc.c
+++ b/gc.c
@@ -348,6 +348,7 @@ typedef struct rb_objspace {
size_t count;
size_t total_allocated_object_num;
size_t total_freed_object_num;
+ rb_event_flag_t hook_events; /* this place may be affinity with memory cache */
int gc_stress;
struct mark_func_data_struct {
@@ -826,6 +827,27 @@ heaps_increment(rb_objspace_t *objspace)
return FALSE;
}
+void
+rb_objspace_set_event_hook(const rb_event_flag_t event)
+{
+ rb_objspace_t *objspace = &rb_objspace;
+ objspace->hook_events = event & RUBY_INTERNAL_EVENT_OBJSPACE_MASK;
+}
+
+static void
+gc_event_hook_body(rb_objspace_t *objspace, const rb_event_flag_t event, VALUE data)
+{
+ rb_thread_t *th = GET_THREAD();
+ EXEC_EVENT_HOOK(th, event, th->cfp->self, 0, 0, data);
+}
+
+#define gc_event_hook(objspace, event, data) do { \
+ if (UNLIKELY((objspace)->hook_events & (event))) { \
+ gc_event_hook_body((objspace), (event), (data)); \
+ } \
+} while (0)
+
+
static VALUE
newobj_of(VALUE klass, VALUE flags, VALUE v1, VALUE v2, VALUE v3)
{
@@ -870,7 +892,6 @@ newobj_of(VALUE klass, VALUE flags, VALUE v1, VALUE v2, VALUE v3)
RANY(obj)->file = rb_sourcefile();
RANY(obj)->line = rb_sourceline();
#endif
- objspace->total_allocated_object_num++;
#if RGENGC_PROFILE
if (flags & FL_WB_PROTECTED) objspace->profile.generated_sunny_object_count++;
@@ -889,6 +910,9 @@ newobj_of(VALUE klass, VALUE flags, VALUE v1, VALUE v2, VALUE v3)
if (rgengc_remembered(objspace, (VALUE)obj)) rb_bug("newobj: %p (%s) is remembered.\n", (void *)obj, obj_type_name(obj));
#endif
+ objspace->total_allocated_object_num++;
+ gc_event_hook(objspace, RUBY_INTERNAL_EVENT_NEWOBJ, obj);
+
return obj;
}
@@ -1097,6 +1121,8 @@ make_io_deferred(RVALUE *p)
static int
obj_free(rb_objspace_t *objspace, VALUE obj)
{
+ gc_event_hook(objspace, RUBY_INTERNAL_EVENT_FREE, obj);
+
switch (BUILTIN_TYPE(obj)) {
case T_NIL:
case T_FIXNUM:
@@ -3785,6 +3811,8 @@ garbage_collect_body(rb_objspace_t *objspace, int full_mark, int immediate_sweep
objspace->rgengc.oldgen_object_count = 0;
}
+ gc_event_hook(objspace, RUBY_INTERNAL_EVENT_GC_START, 0 /* TODO: pass minor/immediate flag? */);
+
gc_prof_timer_start(objspace, reason | (minor_gc ? GPR_FLAG_MINOR : 0));
{
assert(during_gc > 0);