Class: OneApm::Support::VM::RubiniusVM

Inherits:
Object
  • Object
show all
Defined in:
lib/one_apm/support/vm/rubinius_vm.rb

Constant Summary collapse

OA_SUPPORTED_KEYS_GC_RBX_METRICS =
[
  :gc_runs,
  :heap_live,
  :major_gc_count,
  :minor_gc_count,
  :method_cache_invalidations,
  :thread_count,
  :total_allocated_object
].freeze

Instance Method Summary collapse

Instance Method Details

#gather_gc_stats(snap) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/one_apm/support/vm/rubinius_vm.rb', line 20

def gather_gc_stats(snap)
  snap.gc_runs = GC.count

  # Rubinius::Metrics is available since Rubinius 2.3
  if has_metrics?
    gather_stats_from_metrics(snap)
  else
    gather_stats_from_gc_stat(snap)
  end

  gather_gc_time(snap)
end

#gather_gc_time(snap) ⇒ Object



62
63
64
65
66
67
# File 'lib/one_apm/support/vm/rubinius_vm.rb', line 62

def gather_gc_time(snap)
  if GC.respond_to?(:time)
    # On Rubinius GC.time returns a time in miliseconds, not seconds.
    snap.gc_total_time = GC.time / 1000
  end
end

#gather_stats(snap) ⇒ Object



15
16
17
18
# File 'lib/one_apm/support/vm/rubinius_vm.rb', line 15

def gather_stats(snap)
  gather_gc_stats(snap)
  gather_thread_stats(snap)
end

#gather_stats_from_gc_stat(snap) ⇒ Object



53
54
55
56
57
58
59
60
# File 'lib/one_apm/support/vm/rubinius_vm.rb', line 53

def gather_stats_from_gc_stat(snap)
  gc_stats = GC.stat[:gc]

  if gc_stats
    snap.major_gc_count = gc_stats[:full][:count] if gc_stats[:full]
    snap.minor_gc_count = gc_stats[:young][:count] if gc_stats[:young]
  end
end

#gather_stats_from_metrics(snap) ⇒ Object

The ‘+ 0’ bits in here are a workaround for a Rubinius bug wherein Rubinius::Metrics.data returns 0’s as Bignums, and these Bignums cannot be safely subtraced from Fixnums. The + 0 has the effect of truncating to a Fixnum if possible without loss of precision.



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/one_apm/support/vm/rubinius_vm.rb', line 37

def gather_stats_from_metrics(snap)
  snap.major_gc_count = metric(:'gc.immix.count') + 0
  snap.minor_gc_count = metric(:'gc.young.count') + 0

  snap.heap_live = metric(:'memory.large.objects.current') +
    metric(:'memory.young.objects.current') +
    metric(:'memory.immix.objects.current')

  snap.total_allocated_object =
    metric(:'memory.large.objects.total') +
    metric(:'memory.young.objects.total') +
    metric(:'memory.immix.objects.total')

  snap.method_cache_invalidations = metric(:'vm.inline_cache.resets') + 0
end

#gather_thread_stats(snap) ⇒ Object



69
70
71
# File 'lib/one_apm/support/vm/rubinius_vm.rb', line 69

def gather_thread_stats(snap)
  snap.thread_count = Thread.list.size
end

#has_metrics?Boolean

Returns:

  • (Boolean)


73
74
75
# File 'lib/one_apm/support/vm/rubinius_vm.rb', line 73

def has_metrics?
  Rubinius.const_defined?(:Metrics)
end

#metric(key) ⇒ Object



77
78
79
# File 'lib/one_apm/support/vm/rubinius_vm.rb', line 77

def metric(key)
  Rubinius::Metrics.data[key]
end

#snapshotObject



9
10
11
12
13
# File 'lib/one_apm/support/vm/rubinius_vm.rb', line 9

def snapshot
  snap = Snapshot.new
  gather_stats(snap)
  snap
end

#supports?(key) ⇒ Boolean

Returns:

  • (Boolean)


91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/one_apm/support/vm/rubinius_vm.rb', line 91

def supports?(key)
  if has_metrics?
    case key
    when :major_gc_count
      true
    when :minor_gc_count
      true
    when :heap_live
      true
    when :total_allocated_object
      true
    when :method_cache_invalidations
      true
    when :gc_runs
      true
    when :gc_total_time
      GC.respond_to?(:time)
    when :thread_count
      true
    else
      false
    end
  else
    case key
    when :major_gc_count
      true
    when :minor_gc_count
      true
    when :thread_count
      true
    else
      false
    end
  end
end