Class: TimeBandits::TimeConsumers::GarbageCollection

Inherits:
Object
  • Object
show all
Defined in:
lib/time_bandits/time_consumers/garbage_collection.rb

Constant Summary collapse

GCFORMAT =
"GC: %.3f(%d) | HP: %d(%d,%d,%d,%d)"
@@heap_dumps_enabled =
false

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.heap_dumps_enabled=(v) ⇒ Object



6
7
8
# File 'lib/time_bandits/time_consumers/garbage_collection.rb', line 6

def self.heap_dumps_enabled=(v)
  @@heap_dumps_enabled = v
end

.instanceObject



16
17
18
# File 'lib/time_bandits/time_consumers/garbage_collection.rb', line 16

def self.instance
  @instance ||= new
end

Instance Method Details

#_get_allocated_objectsObject



45
# File 'lib/time_bandits/time_consumers/garbage_collection.rb', line 45

def _get_allocated_objects; ObjectSpace.allocated_objects; end

#_get_allocated_sizeObject



55
# File 'lib/time_bandits/time_consumers/garbage_collection.rb', line 55

def _get_allocated_size; GC.allocated_size; end

#_get_collectionsObject



37
# File 'lib/time_bandits/time_consumers/garbage_collection.rb', line 37

def _get_collections; GC.collections; end

#_get_gc_timeObject



31
# File 'lib/time_bandits/time_consumers/garbage_collection.rb', line 31

def _get_gc_time; GC.time; end

#_get_heap_slotsObject



63
# File 'lib/time_bandits/time_consumers/garbage_collection.rb', line 63

def _get_heap_slots; GC.heap_slots; end

#allocated_objectsObject



103
104
105
# File 'lib/time_bandits/time_consumers/garbage_collection.rb', line 103

def allocated_objects
  _get_allocated_objects - @allocated_objects
end

#allocated_sizeObject



107
108
109
# File 'lib/time_bandits/time_consumers/garbage_collection.rb', line 107

def allocated_size
  _get_allocated_size - @allocated_size
end

#collectionsObject



99
100
101
# File 'lib/time_bandits/time_consumers/garbage_collection.rb', line 99

def collections
  _get_collections - @collections
end

#consumedObject Also known as: current_runtime



90
91
92
# File 'lib/time_bandits/time_consumers/garbage_collection.rb', line 90

def consumed
  0.0
end

#consumed_gc_timeObject

ms



95
96
97
# File 'lib/time_bandits/time_consumers/garbage_collection.rb', line 95

def consumed_gc_time # ms
  (_get_gc_time - @consumed).to_f / 1000
end

#enable_statsObject



20
21
22
23
24
25
26
27
28
# File 'lib/time_bandits/time_consumers/garbage_collection.rb', line 20

def enable_stats
  return unless GC.respond_to? :enable_stats
  GC.enable_stats
  if defined?(PhusionPassenger)
    PhusionPassenger.on_event(:starting_worker_process) do |forked|
      GC.enable_stats if forked
    end
  end
end

#heap_growthObject



111
112
113
# File 'lib/time_bandits/time_consumers/garbage_collection.rb', line 111

def heap_growth
  _get_heap_slots - @heap_slots
end

#live_data_set_sizeObject



73
# File 'lib/time_bandits/time_consumers/garbage_collection.rb', line 73

def live_data_set_size; GC.heap_slots_live_after_last_gc; end

#metricsObject



126
127
128
129
130
131
132
133
134
135
136
# File 'lib/time_bandits/time_consumers/garbage_collection.rb', line 126

def metrics
  {
    :gc_time => consumed_gc_time,
    :gc_calls => collections,
    :heap_growth => heap_growth,
    :heap_size => _get_heap_slots,
    :allocated_objects => allocated_objects,
    :allocated_bytes => allocated_size,
    :live_data_set_size => live_data_set_size
  }
end

#resetObject



82
83
84
85
86
87
88
# File 'lib/time_bandits/time_consumers/garbage_collection.rb', line 82

def reset
  @consumed = _get_gc_time
  @collections = _get_collections
  @allocated_objects = _get_allocated_objects
  @allocated_size = _get_allocated_size
  @heap_slots = _get_heap_slots
end

#runtimeObject



117
118
119
120
121
122
123
124
# File 'lib/time_bandits/time_consumers/garbage_collection.rb', line 117

def runtime
  heap_slots = _get_heap_slots
  heap_growth = self.heap_growth
  allocated_objects = self.allocated_objects
  allocated_size = self.allocated_size
  GCHacks.heap_dump if heap_growth > 0 && @@heap_dumps_enabled && defined?(GCHacks)
  GCFORMAT % [consumed_gc_time, collections, heap_growth, heap_slots, allocated_objects, allocated_size, live_data_set_size]
end