about summary refs log tree commit homepage
diff options
context:
space:
mode:
authorEric Wong <normalperson@yhbt.net>2009-07-27 01:28:37 -0700
committerEric Wong <normalperson@yhbt.net>2009-07-27 01:28:37 -0700
commite9ce9e63a56dbf2afa190206625ef839a8ac3670 (patch)
treeab7f1d169ba4c3de2cc9b8c1242c54a0e0ca0388
parentac095b59f07a93c20269f87d7c5995c3fcdce37a (diff)
downloadmall-e9ce9e63a56dbf2afa190206625ef839a8ac3670.tar.gz
Add Mall.dump_stats class method
This allows memory instpection without relying on (the currently
unavailable) glibc or expecially any version of it.
-rw-r--r--ext/mall/extconf.rb1
-rw-r--r--ext/mall/mall.c.erb11
-rw-r--r--test/test_mall.rb18
3 files changed, 30 insertions, 0 deletions
diff --git a/ext/mall/extconf.rb b/ext/mall/extconf.rb
index 4b89e1e..9ba1c32 100644
--- a/ext/mall/extconf.rb
+++ b/ext/mall/extconf.rb
@@ -13,5 +13,6 @@ end
 have_header('malloc.h') or abort "malloc.h header missing"
 have_type('struct mallinfo', 'malloc.h') or abort 'struct mallinfo missing'
 have_func('malloc_trim', 'malloc.h')
+have_func('malloc_stats', 'malloc.h')
 dir_config('mall')
 create_makefile('mall')
diff --git a/ext/mall/mall.c.erb b/ext/mall/mall.c.erb
index 59734f2..1b0a675 100644
--- a/ext/mall/mall.c.erb
+++ b/ext/mall/mall.c.erb
@@ -66,6 +66,14 @@ static VALUE trim(VALUE klass, VALUE pad)
 }
 #endif /* HAVE_MALLOC_TRIM */
 
+#ifdef HAVE_MALLOC_STATS
+static VALUE dump_stats(VALUE klass)
+{
+        malloc_stats();
+        return Qnil;
+}
+#endif /* HAVE_MALLOC_STATS */
+
 void Init_mall(void)
 {
         cMall = rb_define_class("Mall", rb_cObject);
@@ -74,6 +82,9 @@ void Init_mall(void)
 #ifdef HAVE_MALLOC_TRIM
         rb_define_singleton_method(cMall, "trim", trim, 1);
 #endif /* HAVE_MALLOC_TRIM */
+#ifdef HAVE_MALLOC_STATS
+        rb_define_singleton_method(cMall, "dump_stats", dump_stats, 0);
+#endif /* HAVE_MALLOC_STATS */
 
 <% %w(
 mxfast nlblks grain keep
diff --git a/test/test_mall.rb b/test/test_mall.rb
index 91d541b..0cd8bd1 100644
--- a/test/test_mall.rb
+++ b/test/test_mall.rb
@@ -1,3 +1,4 @@
+require "tempfile"
 require "test/unit"
 require "mall"
 
@@ -27,4 +28,21 @@ class TestMall < Test::Unit::TestCase
     end
   end
 
+  def test_dump_stats
+    if Mall.respond_to?(:dump_stats)
+      olderr = $stderr.dup
+      begin
+        tmp = Tempfile.new('mall_dump_stats')
+        $stderr.sync = tmp.sync = true
+        $stderr.reopen(tmp)
+        assert_nil Mall.dump_stats
+        assert tmp.stat.size != 0
+      ensure
+        $stderr.reopen(olderr)
+      end
+    else
+      warn "Mall.dump_stats not supported"
+    end
+  end
+
 end