summaryrefslogtreecommitdiff
path: root/gc.rb
diff options
context:
space:
mode:
authorKoichi Sasada <ko1@atdot.net>2021-11-10 16:52:22 +0900
committerKoichi Sasada <ko1@atdot.net>2021-11-19 08:32:07 +0900
commitc347038d4ea246d686fcd4ddc8761a91e4dd25ab (patch)
treef2644c2b94dbe93b4d76df90db1cd085485a9426 /gc.rb
parent349a1797828a1fa6acc3c0d30a2a24e884d02907 (diff)
GC measurement feature
* `GC.measure_total_time = true` enables total time measurement (default: true) * `GC.measure_total_time` returns current flag. * `GC.total_time` returns measured total time in nano seconds. * `GC.stat(:time)` (and Hash) returns measured total time in milli seconds.
Notes
Notes: Merged: https://github.com/ruby/ruby/pull/4757
Diffstat (limited to 'gc.rb')
-rw-r--r--gc.rb45
1 files changed, 45 insertions, 0 deletions
diff --git a/gc.rb b/gc.rb
index e80d6635a7..d3f80dbaa8 100644
--- a/gc.rb
+++ b/gc.rb
@@ -266,6 +266,51 @@ module GC
def self.using_rvargc?
GC::INTERNAL_CONSTANTS[:SIZE_POOL_COUNT] > 1
end
+
+
+ # call-seq:
+ # GC.measure_total_time = true/false
+ #
+ # Enable to measure GC time.
+ # You can get the result with `GC.stat(:time)`.
+ # Note that the GC time measurement can introduce the performance regression.
+ def self.measure_total_time=(flag)
+ Primitive.cstmt! %{
+ rb_objspace.flags.measure_gc = RTEST(flag) ? TRUE : FALSE;
+ return flag;
+ }
+ end
+
+ # call-seq:
+ # GC.measure_total_time -> true/false
+ #
+ # Return measure_total_time flag (default: true).
+ # Note that measurement can affect the application performance.
+ def self.measure_total_time
+ Primitive.cexpr! %{
+ RBOOL(rb_objspace.flags.measure_gc)
+ }
+ end
+
+ Primitive.cinit! %{
+ #if SIZEOF_LONG == 8
+ #define UINT64_2NUM RB_ULONG2NUM
+ #elif SIZEOF_LONG_LONG == 8
+ #define UINT64_2NUM RB_ULL2NUM
+ #else
+ #error Can not make UINT64_2NUM
+ #endif
+ }
+
+ # call-seq:
+ # GC.total_time -> int
+ #
+ # Return measured GC total time in nano seconds.
+ def self.total_time
+ Primitive.cexpr! %{
+ UINT64_2NUM(rb_objspace.profile.total_time_ns)
+ }
+ end
end
module ObjectSpace