Class: Benchmark::Malloc

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

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

#generationObject (readonly)

Returns the value of attribute generation.



14
15
16
# File 'lib/benchmark/malloc.rb', line 14

def generation
  @generation
end

#warmupObject (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

.trace(&work) ⇒ Object

Trace memory allocations



24
25
26
# File 'lib/benchmark/malloc.rb', line 24

def self.trace(&work)
  Malloc.new.run(&work)
end

Instance Method Details

#check_runningObject

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

Examples:

malloc = Malloc.new
malloc.run { ... }

Returns:

  • (Malloc::Result)


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

#startObject

Start allocation tracing

Examples:

malloc = Malloc.new
malloc.start


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

#stopObject

Stop allocation tracing if currently running

Examples:

Malloc.stop


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