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

ruby-changes:28918

From: ko1 <ko1@a...>
Date: Tue, 28 May 2013 03:14:40 +0900 (JST)
Subject: [ruby-changes:28918] ko1:r40970 (trunk): * ext/objspace/gc_hook.c, ext/objspace/objspace.c: add new methods to

ko1	2013-05-28 03:14:28 +0900 (Tue, 28 May 2013)

  New Revision: 40970

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

  Log:
    * ext/objspace/gc_hook.c, ext/objspace/objspace.c: add new methods to
      hook GC invocation.
    * ObjectSpace.after_gc_start_hook=(proc)
    * ObjectSpace.after_gc_end_hook=(proc)
      Note that hooks are not kicked immediately. Procs are kicked
      at postponed_job.
      This feature is a sample of new internal event and
      rb_postponed_job API.

  Added files:
    trunk/ext/objspace/gc_hook.c
  Modified files:
    trunk/ChangeLog
    trunk/ext/objspace/objspace.c

Index: ChangeLog
===================================================================
--- ChangeLog	(revision 40969)
+++ ChangeLog	(revision 40970)
@@ -1,3 +1,16 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Tue May 28 03:11:02 2013  Koichi Sasada  <ko1@a...>
+
+	* ext/objspace/gc_hook.c, ext/objspace/objspace.c: add new methods to
+	  hook GC invocation.
+	  * ObjectSpace.after_gc_start_hook=(proc)
+	  * ObjectSpace.after_gc_end_hook=(proc)
+
+	  Note that hooks are not kicked immediately. Procs are kicked
+	  at postponed_job.
+
+	  This feature is a sample of new internal event and
+	  rb_postponed_job API.
+
 Tue May 28 02:56:15 2013  Koichi Sasada  <ko1@a...>
 
 	* gc.c (gc_stat): remove wrong rest_sweep().
Index: ext/objspace/objspace.c
===================================================================
--- ext/objspace/objspace.c	(revision 40969)
+++ ext/objspace/objspace.c	(revision 40970)
@@ -780,6 +780,7 @@ reachable_objects_from(VALUE self, VALUE https://github.com/ruby/ruby/blob/trunk/ext/objspace/objspace.c#L780
 }
 
 void Init_object_tracing(VALUE rb_mObjSpace);
+void Init_gc_hook(VALUE rb_mObjSpace);
 
 /* objspace library extends ObjectSpace module and add several
  * methods to get internal statistic information about
@@ -811,4 +812,5 @@ Init_objspace(void) https://github.com/ruby/ruby/blob/trunk/ext/objspace/objspace.c#L812
     rb_define_method(rb_mInternalObjectWrapper, "internal_object_id", iow_internal_object_id, 0);
 
     Init_object_tracing(rb_mObjSpace);
+    Init_gc_hook(rb_mObjSpace);
 }
Index: ext/objspace/gc_hook.c
===================================================================
--- ext/objspace/gc_hook.c	(revision 0)
+++ ext/objspace/gc_hook.c	(revision 40970)
@@ -0,0 +1,80 @@ https://github.com/ruby/ruby/blob/trunk/ext/objspace/gc_hook.c#L1
+/**********************************************************************
+
+  gc_hook.c - GC hook mechanism/ObjectSpace extender for MRI.
+
+  $Author: sorah $
+  created at: Tue May 28 01:34:25 2013
+
+  NOTE: This extension library is not expected to exist except C Ruby.
+  NOTE: This feature is an example usage of internal event tracing APIs.
+
+  All the files in this distribution are covered under the Ruby's
+  license (see the file COPYING).
+
+**********************************************************************/
+
+#include "ruby/ruby.h"
+#include "ruby/debug.h"
+
+static void
+invoke_proc(void *data)
+{
+    VALUE proc = (VALUE)data;
+    rb_proc_call(proc, rb_ary_new());
+}
+
+static void
+gc_start_end_i(VALUE tpval, void *data)
+{
+    rb_trace_arg_t *tparg = rb_tracearg_from_tracepoint(tpval);
+    if (0) fprintf(stderr, "trace: %s\n", rb_tracearg_event_flag(tparg) == RUBY_INTERNAL_EVENT_GC_START ? "gc_start" : "gc_end");
+    rb_postponed_job_register(0, invoke_proc, data);
+}
+
+static VALUE
+set_gc_hook(VALUE rb_mObjSpace, VALUE proc, rb_event_flag_t event, const char *tp_str, const char *proc_str)
+{
+    VALUE tpval;
+    ID tp_key = rb_intern(tp_str);
+    ID proc_key = rb_intern(proc_str);
+
+    if (RTEST(tpval = rb_ivar_get(rb_mObjSpace, tp_key))) {
+	rb_tracepoint_disable(tpval);
+	rb_ivar_set(rb_mObjSpace, tp_key, Qnil);
+	rb_ivar_set(rb_mObjSpace, proc_key, Qnil);
+    }
+
+    if (RTEST(proc)) {
+	if (!rb_obj_is_proc(proc)) {
+	    rb_raise(rb_eTypeError, "trace_func needs to be Proc");
+	}
+
+	tpval = rb_tracepoint_new(0, event, gc_start_end_i, (void *)proc);
+	rb_ivar_set(rb_mObjSpace, tp_key, tpval);
+	rb_ivar_set(rb_mObjSpace, proc_key, proc); /* GC guard */
+	rb_tracepoint_enable(tpval);
+    }
+
+    return proc;
+}
+
+static VALUE
+set_after_gc_start(VALUE rb_mObjSpace, VALUE proc)
+{
+    return set_gc_hook(rb_mObjSpace, proc, RUBY_INTERNAL_EVENT_GC_START,
+		       "__set_after_gc_start_tpval__", "__set_after_gc_start_proc__");
+}
+
+static VALUE
+set_after_gc_end(VALUE rb_mObjSpace, VALUE proc)
+{
+    return set_gc_hook(rb_mObjSpace, proc, RUBY_INTERNAL_EVENT_GC_END,
+		       "__set_after_gc_end_tpval__", "__set_after_gc_end_proc__");
+}
+
+void
+Init_gc_hook(VALUE rb_mObjSpace)
+{
+    rb_define_module_function(rb_mObjSpace, "after_gc_start_hook=", set_after_gc_start, 1);
+    rb_define_module_function(rb_mObjSpace, "after_gc_end_hook=", set_after_gc_end, 1);
+}

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

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