Class: Benchmark::Malloc
- Inherits:
-
Object
- Object
- Benchmark::Malloc
- Defined in:
- lib/benchmark/malloc.rb,
lib/benchmark/malloc/version.rb,
lib/benchmark/malloc/allocation.rb,
lib/benchmark/malloc/allocation_set.rb,
lib/benchmark/malloc/allocation_result.rb
Defined Under Namespace
Classes: Allocation, AllocationResult, AllocationSet, Error
Constant Summary collapse
- VERSION =
"0.2.0"
Instance Attribute Summary collapse
-
#generation ⇒ Object
readonly
Returns the value of attribute generation.
-
#warmup ⇒ Object
readonly
It runs Ruby VM before tracing object allocations.
Class Method Summary collapse
-
.trace(&work) ⇒ Object
Trace memory allocations.
Instance Method Summary collapse
- #check_running ⇒ Object private
-
#initialize(warmup: 0) ⇒ Malloc
constructor
Create a memory allocation tracer.
-
#run(&work) ⇒ Malloc::Result
Gather allocation stats of Ruby code inside of the block.
-
#start ⇒ Object
Start allocation tracing.
-
#stop ⇒ Object
Stop allocation tracing if currently running.
Constructor Details
#initialize(warmup: 0) ⇒ Malloc
Create a memory allocation tracer
31 32 33 34 35 |
# File 'lib/benchmark/malloc.rb', line 31 def initialize(warmup: 0) @warmup = warmup @running = false @alloc_path = ::File.join(__FILE__[0...-3], "allocation.rb") end |
Instance Attribute Details
#generation ⇒ Object (readonly)
Returns the value of attribute generation.
14 15 16 |
# File 'lib/benchmark/malloc.rb', line 14 def generation @generation end |
#warmup ⇒ Object (readonly)
It runs Ruby VM before tracing object allocations
19 20 21 |
# File 'lib/benchmark/malloc.rb', line 19 def warmup @warmup end |
Class Method Details
Instance Method Details
#check_running ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
38 39 40 41 42 |
# File 'lib/benchmark/malloc.rb', line 38 def check_running unless @running raise Error, "not started yet" end end |
#run(&work) ⇒ Malloc::Result
Gather allocation stats of Ruby code inside of the block
104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/benchmark/malloc.rb', line 104 def run(&work) start warmup.times { yield } begin yield rescue Exception ObjectSpace.trace_object_allocations_stop GC.enable raise else stop end end |
#start ⇒ Object
Start allocation tracing
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/benchmark/malloc.rb', line 51 def start if @running raise Error, "already running" end GC.start GC.disable @generation = GC.count @running = true @existing_ids = [] ObjectSpace.each_object do |object| @existing_ids << object.__id__ end ObjectSpace.trace_object_allocations_start end |
#stop ⇒ Object
Stop allocation tracing if currently running
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/benchmark/malloc.rb', line 73 def stop check_running ObjectSpace.trace_object_allocations_stop allocated = collect_allocations retained = [] @running = false GC.enable GC.start ObjectSpace.each_object do |object| next unless ObjectSpace.allocation_generation(object) == generation next unless allocated.key?(object.__id__) retained << allocated[object.__id__] end ObjectSpace.trace_object_allocations_clear AllocationResult.new(AllocationSet.new(allocated.values), AllocationSet.new(retained)) end |