Created
June 26, 2013 21:28
-
-
Save bradhe/5871879 to your computer and use it in GitHub Desktop.
This patch adds a new function to the Ruby VM for getting the total number of allocations performed, as well as adds a method to GC to get that same number.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
From 2cbc1b959af0b40b0723b3fb586ea70d0324fe59 Mon Sep 17 00:00:00 2001 | |
From: Brad Heller <[email protected]> | |
Date: Wed, 26 Jun 2013 13:17:02 -0700 | |
Subject: [PATCH] Add support for total allocations measurement. | |
--- | |
gc.c | 16 ++++++++++++++++ | |
include/ruby/intern.h | 1 + | |
test/ruby/test_gc.rb | 4 ++++ | |
3 files changed, 21 insertions(+) | |
diff --git a/gc.c b/gc.c | |
index f307ecb..59c04ab 100644 | |
--- a/gc.c | |
+++ b/gc.c | |
@@ -518,6 +518,13 @@ rb_objspace_free(rb_objspace_t *objspace) | |
} | |
#endif | |
+static unsigned long total_allocations = 0; | |
+ | |
+unsigned long | |
+rb_total_allocations() { | |
+ return total_allocations; | |
+} | |
+ | |
/* tiny heap size */ | |
/* 32KB */ | |
/*#define HEAP_SIZE 0x8000 */ | |
@@ -593,6 +600,12 @@ rb_memerror(void) | |
rb_exc_raise(nomem_error); | |
} | |
+static VALUE | |
+gc_total_allocations_get(VALUE self) | |
+{ | |
+ return ULONG2NUM(total_allocations); | |
+} | |
+ | |
/* | |
* call-seq: | |
* GC.stress -> true or false | |
@@ -1220,6 +1233,8 @@ rb_newobj(void) | |
#endif | |
GC_PROF_INC_LIVE_NUM; | |
+ total_allocations++; | |
+ | |
return obj; | |
} | |
@@ -3693,6 +3708,7 @@ Init_GC(void) | |
rb_define_singleton_method(rb_mGC, "stress=", gc_stress_set, 1); | |
rb_define_singleton_method(rb_mGC, "count", gc_count, 0); | |
rb_define_singleton_method(rb_mGC, "stat", gc_stat, -1); | |
+ rb_define_singleton_method(rb_mGC, "total_allocations", gc_total_allocations_get, 0); | |
rb_define_method(rb_mGC, "garbage_collect", rb_gc_start, 0); | |
rb_mProfiler = rb_define_module_under(rb_mGC, "Profiler"); | |
diff --git a/include/ruby/intern.h b/include/ruby/intern.h | |
index 9745afd..14620b2 100644 | |
--- a/include/ruby/intern.h | |
+++ b/include/ruby/intern.h | |
@@ -426,6 +426,7 @@ void rb_gc_call_finalizer_at_exit(void); | |
VALUE rb_gc_enable(void); | |
VALUE rb_gc_disable(void); | |
VALUE rb_gc_start(void); | |
+unsigned long rb_total_allocations _((void)); | |
#define Init_stack(addr) ruby_init_stack(addr) | |
void rb_gc_set_params(void); | |
/* hash.c */ | |
diff --git a/test/ruby/test_gc.rb b/test/ruby/test_gc.rb | |
index f04e7e0..19fa426 100644 | |
--- a/test/ruby/test_gc.rb | |
+++ b/test/ruby/test_gc.rb | |
@@ -54,6 +54,10 @@ class TestGc < Test::Unit::TestCase | |
assert_operator(c, :<, GC.count) | |
end | |
+ def test_total_allocations | |
+ assert_kind_of(Fixnum, GC.total_allocations) | |
+ end | |
+ | |
def test_stat | |
res = GC.stat | |
assert_equal(false, res.empty?) | |
-- | |
1.8.2.3 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment