Permalink
Browse files

Control the GC runs.

  • Loading branch information...
1 parent 92dc5c5 commit a77bc26285d9ee523b84791613d33311c71d2ef5 Evan Weaver committed Oct 2, 2009
Showing with 28 additions and 5 deletions.
  1. +2 −0 CHANGELOG
  2. +5 −4 ext/snapshot.c
  3. +14 −0 lib/bleak_house.rb
  4. +7 −1 test/unit/test_bleak_house.rb
View
@@ -1,4 +1,6 @@
+v4.6. Allow snapshotting without running the GC first.
+
v4.5. Enable SET_CURRENT_SOURCE() for better reporting (tmm1, joedamato).
v4.4. Switch to Ruby 1.8.7p174.
View
@@ -14,9 +14,10 @@ static VALUE heaps_length(VALUE self) {
return INT2FIX(rb_gc_heaps_length());
}
-/* Walk the live, instrumented objects on the heap and write them to <tt>_logfile</tt>. */
-static VALUE snapshot(VALUE self, VALUE _logfile) {
+/* Inner method; call BleakHouse.snapshot instead. */
+static VALUE ext_snapshot(VALUE self, VALUE _logfile, VALUE _gc_runs) {
Check_Type(_logfile, T_STRING);
+ Check_Type(_gc_runs, T_FIXNUM);
RVALUE *obj, *obj_end;
st_table_entry *sym;
@@ -40,7 +41,7 @@ static VALUE snapshot(VALUE self, VALUE _logfile) {
int i;
char * chr;
- for (i = 0; i < 3; i++) {
+ for (i = 0; i < FIX2INT(_gc_runs); i++) {
/* request GC run */
rb_funcall(rb_mGC, rb_intern("start"), 0);
rb_thread_schedule();
@@ -144,7 +145,7 @@ void
Init_snapshot()
{
rb_mB = rb_define_module("BleakHouse");
- rb_define_singleton_method(rb_mB, "snapshot", snapshot, 1);
+ rb_define_singleton_method(rb_mB, "ext_snapshot", ext_snapshot, 2);
rb_define_singleton_method(rb_mB, "heaps_used", heaps_used, 0);
rb_define_singleton_method(rb_mB, "heaps_length", heaps_length, 0);
}
View
@@ -6,3 +6,17 @@
RUBY_VERSION = `ruby -v`.split(" ")[1]
require 'snapshot'
require 'bleak_house/hook'
+
+class << BleakHouse
+ private :ext_snapshot
+end
+
+module BleakHouse
+
+ # Walk the live, instrumented objects on the heap and write them to
+ # <tt>logfile</tt>. Accepts an optional number of GC runs to perform
+ # before dumping the heap.
+ def self.snapshot(logfile, gc_runs = 3)
+ ext_snapshot(logfile, gc_runs)
+ end
+end
@@ -18,13 +18,19 @@ def setup
end
def test_snapshot
- symbol_count = Symbol.all_symbols.size
BleakHouse.snapshot(FILE)
assert File.exist?(FILE)
assert BleakHouse.heaps_used > 0
assert BleakHouse.heaps_length > 0
end
+ def test_snapshot_gc_runs
+ BleakHouse.snapshot(FILE, 0)
+ assert File.exist?(FILE)
+ assert BleakHouse.heaps_used > 0
+ assert BleakHouse.heaps_length > 0
+ end
+
def test_exception
assert_raises(RuntimeError) do
BleakHouse.snapshot("/")

0 comments on commit a77bc26

Please sign in to comment.