Class: HeapScope::Collector

Inherits:
Object
  • Object
show all
Defined in:
lib/heapscope/collector.rb

Overview

Collects heap snapshots at configurable fidelity with pause/object budgets.

Constant Summary collapse

RUNTIME_NOISE =
%w[
  Class Module Object BasicObject String Array Hash Integer Float Symbol
  Proc Method UnboundMethod Binding Encoding Range Regexp MatchData
  Thread Fiber Mutex Rational Complex NilClass TrueClass FalseClass
  RubyVM RubyVM::InstructionSequence IO File Dir Time Random
].freeze

Instance Method Summary collapse

Constructor Details

#initialize(runtime: Runtime.current, config: HeapScope.config) ⇒ Collector

Returns a new instance of Collector.



15
16
17
18
# File 'lib/heapscope/collector.rb', line 15

def initialize(runtime: Runtime.current, config: HeapScope.config)
  @runtime = runtime
  @config = config
end

Instance Method Details

#capture(mode: nil, metadata: {}, max_objects: nil, max_pause: nil, sample_rate: nil) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/heapscope/collector.rb', line 20

def capture(mode: nil, metadata: {}, max_objects: nil, max_pause: nil, sample_rate: nil)
  mode = @config.effective_snapshot_mode(mode)
  max_objects ||= @config.max_objects
  sample_rate ||= @config.object_sample_rate
  max_pause_ms = normalize_pause(max_pause || @config.max_pause_ms)

  started = monotonic_ms
  limitations = []

  gc_stat = @runtime.gc_stat
  rss = safe_rss
  limitations << "rss_tracking_unavailable" if rss.nil? && !@runtime.rss_tracking?

  class_stats, allocation_sites, object_sample, truncated, more_limits =
    collect_objects(mode, max_objects, sample_rate, max_pause_ms, started)
  limitations.concat(more_limits)
  limitations << "analysis_truncated_pause_budget" if truncated && max_pause_ms

  duration = monotonic_ms - started

  Snapshot.new(
    id: SecureRandom.hex(8),
    timestamp: Time.now.utc,
    mode: mode,
    gc_stat: gc_stat,
    rss_bytes: rss,
    class_stats: class_stats,
    allocation_sites: allocation_sites,
    metadata: ,
    limitations: limitations.uniq,
    sampled: sample_rate < 1.0 || mode != :lightweight,
    process: @runtime.,
    duration_ms: duration.round(2),
    object_sample: object_sample
  )
rescue CapabilityError
  raise
rescue StandardError => e
  raise SnapshotError, "Failed to capture snapshot: #{e.class}: #{e.message}"
end