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



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

def _get_allocated_objects; GC.stat(:total_allocated_objects); end

#_get_allocated_sizeObject

this will wrap around :malloc_increase_bytes_limit so is not really correct



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

def _get_allocated_size; GC.total_malloced_bytes; end

#_get_collectionsObject



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

def _get_collections; GC.count; 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



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

def _get_heap_slots; GC.heap_slots; end

#allocated_objectsObject



82
83
84
# File 'lib/time_bandits/time_consumers/garbage_collection.rb', line 82

def allocated_objects
  _get_allocated_objects - @allocated_objects
end

#allocated_sizeObject



86
87
88
89
90
# File 'lib/time_bandits/time_consumers/garbage_collection.rb', line 86

def allocated_size
  new_size = _get_allocated_size
  old_size = @allocated_size
  new_size <= old_size ? new_size : new_size - old_size
end

#collectionsObject



78
79
80
# File 'lib/time_bandits/time_consumers/garbage_collection.rb', line 78

def collections
  _get_collections - @collections
end

#consumedObject Also known as: current_runtime



69
70
71
# File 'lib/time_bandits/time_consumers/garbage_collection.rb', line 69

def consumed
  0.0
end

#consumed_gc_timeObject

ms



74
75
76
# File 'lib/time_bandits/time_consumers/garbage_collection.rb', line 74

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



92
93
94
# File 'lib/time_bandits/time_consumers/garbage_collection.rb', line 92

def heap_growth
  _get_heap_slots - @heap_slots
end

#live_data_set_sizeObject



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

def live_data_set_size; GC.heap_slots_live_after_last_gc; end

#metricsObject



107
108
109
110
111
112
113
114
115
116
117
# File 'lib/time_bandits/time_consumers/garbage_collection.rb', line 107

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



61
62
63
64
65
66
67
# File 'lib/time_bandits/time_consumers/garbage_collection.rb', line 61

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



98
99
100
101
102
103
104
105
# File 'lib/time_bandits/time_consumers/garbage_collection.rb', line 98

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