Class: MemoryTracker::GcStat

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/memory_tracker/gc_stat.rb

Constant Summary collapse

RUBY22_TO_CANONICAL_MAPPING =
{ total_allocated_objects: :total_allocated_object,
  total_freed_objects:     :total_freed_object,
  heap_allocated_pages:    :heap_used,
  heap_live_slots:         :heap_live_num,
  heap_free_slots:         :heap_free_num,
  heap_final_slots:        :heap_final_num,
  heap_sorted_length:      :heap_length
}
CANONICAL_TO_RUBY22_MAPPING =
RUBY22_TO_CANONICAL_MAPPING.invert
RUBY23_TO_CANONICAL_MAPPING =
RUBY22_TO_CANONICAL_MAPPING
CANONICAL_TO_RUBY23_MAPPING =
CANONICAL_TO_RUBY22_MAPPING

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(rss, vsize) ⇒ GcStat

Returns a new instance of GcStat.



19
20
21
# File 'lib/memory_tracker/gc_stat.rb', line 19

def initialize(rss, vsize)
  @stats = GC.stat.merge({ :rss => rss, :vsize => vsize})
end

Class Method Details

.canonical_key_name(key) ⇒ Object



55
56
57
58
59
60
61
62
63
64
# File 'lib/memory_tracker/gc_stat.rb', line 55

def self.canonical_key_name(key)
  canonical = key
  case RUBY_VERSION
  when /\A2\.2\./
    canonical = RUBY22_TO_CANONICAL_MAPPING.fetch(key, key)
  when /\A2\.3\./
    canonical = RUBY23_TO_CANONICAL_MAPPING.fetch(key, key)
  end
  canonical
end

.current_version_key_name(key) ⇒ Object



66
67
68
69
70
71
72
73
74
75
# File 'lib/memory_tracker/gc_stat.rb', line 66

def self.current_version_key_name(key)
  current_key_name = key
  case RUBY_VERSION
  when /\A2\.2\./
    current_key_name = CANONICAL_TO_RUBY22_MAPPING.fetch(key, key)
  when /\A2\.3\./
    current_key_name = CANONICAL_TO_RUBY23_MAPPING.fetch(key, key)
  end
  current_key_name
end

.gcdiff(before, after) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/memory_tracker/gc_stat.rb', line 81

def self.gcdiff(before, after)
  return {} unless (before && before[:total_allocated_object] && before[:total_freed_object])
  return {} unless (after && after[:total_allocated_object] && after[:total_freed_object])
  diff = {}
  b = before.clone
  a = after.clone
  diff[:num_alloc] = a[:total_allocated_object] - b[:total_allocated_object]
  diff[:num_heaps] = a[:heap_used]
  [ a, b ].each do |x|
    x.delete(:heap_increment)
    x.delete(:heap_length)
    x.delete(:heap_final_num)
    x[:in_use] = x.delete(:total_allocated_object) - x.delete(:total_freed_object)
  end
  b.each_key do |key|
    diff[key] = a[key] - b[key]
  end
  diff
end

.heap_usedObject



77
78
79
# File 'lib/memory_tracker/gc_stat.rb', line 77

def self.heap_used
  GC.stat[current_version_key_name(:heap_used)]
end

Instance Method Details

#[](key) ⇒ Object



43
44
45
# File 'lib/memory_tracker/gc_stat.rb', line 43

def [](key)
  @stats[current_version_key_name(key)]
end

#canonical_key_name(key) ⇒ Object



47
48
49
# File 'lib/memory_tracker/gc_stat.rb', line 47

def canonical_key_name(key)
  self.class.canonical_key_name(key)
end

#current_version_key_name(key) ⇒ Object



51
52
53
# File 'lib/memory_tracker/gc_stat.rb', line 51

def current_version_key_name(key)
  self.class.current_version_key_name(key)
end

#each(&block) ⇒ Object



23
24
25
26
27
# File 'lib/memory_tracker/gc_stat.rb', line 23

def each(&block)
  @stats.each do |k, v|
    yield canonical_key_name(k), v
  end
end

#keysObject



29
30
31
# File 'lib/memory_tracker/gc_stat.rb', line 29

def keys
  @stats.keys
end

#ordered_keysObject



33
34
35
# File 'lib/memory_tracker/gc_stat.rb', line 33

def ordered_keys
  @stats.keys.map { |k| canonical_key_name(k) }.sort
end

#ordered_values(ordered_columns = ordered_keys) ⇒ Object



37
38
39
40
41
# File 'lib/memory_tracker/gc_stat.rb', line 37

def ordered_values(ordered_columns = ordered_keys)
  ordered_columns.inject([]) do |vals, key|
    vals << @stats[current_version_key_name(key)]
  end
end