Class: HeapScope::Runtime::MRI

Inherits:
Base
  • Object
show all
Defined in:
lib/heapscope/runtime/mri.rb

Instance Attribute Summary

Attributes inherited from Base

#engine

Instance Method Summary collapse

Methods inherited from Base

#gc_stat, #gc_stat?, #process_metadata, #rss_bytes, #rss_tracking?

Constructor Details

#initializeMRI

Returns a new instance of MRI.



9
10
11
# File 'lib/heapscope/runtime/mri.rb', line 9

def initialize
  super(engine: "ruby")
end

Instance Method Details

#allocation_info(object) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/heapscope/runtime/mri.rb', line 67

def allocation_info(object)
  return nil unless allocation_tracing?

  file = ObjectSpace.allocation_sourcefile(object)
  line = ObjectSpace.allocation_sourceline(object)
  return nil unless file

  {
    file: file,
    line: line,
    generation: (ObjectSpace.allocation_generation(object) if ObjectSpace.respond_to?(:allocation_generation)),
    class_path: (ObjectSpace.allocation_class_path(object) if ObjectSpace.respond_to?(:allocation_class_path)),
    method_id: (ObjectSpace.allocation_method_id(object) if ObjectSpace.respond_to?(:allocation_method_id))
  }.compact
rescue StandardError
  nil
end

#allocation_tracing?Boolean

Returns:

  • (Boolean)


13
14
15
# File 'lib/heapscope/runtime/mri.rb', line 13

def allocation_tracing?
  ObjectSpace.respond_to?(:trace_object_allocations_start)
end

#each_object(klass = nil) ⇒ Object

Raises:



29
30
31
32
33
34
35
36
37
# File 'lib/heapscope/runtime/mri.rb', line 29

def each_object(klass = nil, &)
  raise CapabilityError, "ObjectSpace.each_object unavailable" unless each_object?

  if klass
    ObjectSpace.each_object(klass, &)
  else
    ObjectSpace.each_object(&)
  end
end

#each_object?Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/heapscope/runtime/mri.rb', line 25

def each_object?
  ObjectSpace.respond_to?(:each_object)
end

#memsize?Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/heapscope/runtime/mri.rb', line 21

def memsize?
  ObjectSpace.respond_to?(:memsize_of)
end

#memsize_of(object) ⇒ Object



39
40
41
42
43
44
45
# File 'lib/heapscope/runtime/mri.rb', line 39

def memsize_of(object)
  return nil unless memsize?

  ObjectSpace.memsize_of(object)
rescue StandardError
  nil
end

#reachable_objects?Boolean

Returns:

  • (Boolean)


17
18
19
# File 'lib/heapscope/runtime/mri.rb', line 17

def reachable_objects?
  ObjectSpace.respond_to?(:reachable_objects_from)
end

#reachable_objects_from(object) ⇒ Object



47
48
49
50
51
52
53
# File 'lib/heapscope/runtime/mri.rb', line 47

def reachable_objects_from(object)
  return [] unless reachable_objects?

  ObjectSpace.reachable_objects_from(object) || []
rescue StandardError
  []
end

#start_allocation_tracingObject

Raises:



55
56
57
58
59
# File 'lib/heapscope/runtime/mri.rb', line 55

def start_allocation_tracing
  raise CapabilityError, "Allocation tracing unavailable" unless allocation_tracing?

  ObjectSpace.trace_object_allocations_start
end

#stop_allocation_tracingObject



61
62
63
64
65
# File 'lib/heapscope/runtime/mri.rb', line 61

def stop_allocation_tracing
  return unless allocation_tracing?

  ObjectSpace.trace_object_allocations_stop
end