Class: AllocationStats

Inherits:
Object
  • Object
show all
Defined in:
lib/allocation_stats.rb,
lib/allocation_stats/allocation.rb,
lib/allocation_stats/allocations_proxy.rb

Overview

Copyright 2013 Google Inc. All Rights Reserved. Licensed under the Apache License, Version 2.0, found in the LICENSE file.

Defined Under Namespace

Classes: Allocation, AllocationsProxy

Constant Summary collapse

RUBYLIBDIR =

a convenience constant

RbConfig::CONFIG["rubylibdir"]
GEMDIR =

a convenience constant

Gem.dir

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(burn: 0) ⇒ AllocationStats



39
40
41
# File 'lib/allocation_stats.rb', line 39

def initialize(burn: 0)
  @burn = burn
end

Instance Attribute Details

#burnFixnum

burn count for block tracing. Defaults to 0. When called with a block,

trace will yield the block @burn-times before actually tracing the object

allocations. This offers the benefit of pre-memoizing objects, and loading any required Ruby files before tracing.



26
27
28
# File 'lib/allocation_stats.rb', line 26

def burn
  @burn
end

#gc_profiler_reportObject

Returns the value of attribute gc_profiler_report.



28
29
30
# File 'lib/allocation_stats.rb', line 28

def gc_profiler_report
  @gc_profiler_report
end

#new_allocationsArray (readonly)

allocation data for all new objects that were allocated during the #initialize block. It is better to use #allocations, which returns an AllocationsProxy, which has a much more convenient, domain-specific API for filtering, sorting, and grouping Allocation objects, than this plain Array object.



37
38
39
# File 'lib/allocation_stats.rb', line 37

def new_allocations
  @new_allocations
end

Class Method Details

.trace(&block) ⇒ Object



43
44
45
46
# File 'lib/allocation_stats.rb', line 43

def self.trace(&block)
  allocation_stats = AllocationStats.new
  allocation_stats.trace(&block)
end

Instance Method Details

#allocations(alias_paths: false) ⇒ Object

Proxy for the @new_allocations array that allows for individual filtering, sorting, and grouping of the Allocation objects.



122
123
124
# File 'lib/allocation_stats.rb', line 122

def allocations(alias_paths: false)
  AllocationsProxy.new(@new_allocations, alias_paths: alias_paths)
end

#collect_new_allocationsObject



96
97
98
99
100
101
102
103
104
105
106
# File 'lib/allocation_stats.rb', line 96

def collect_new_allocations
  @new_allocations = []
  ObjectSpace.each_object.to_a.each do |object|
    next if ObjectSpace.allocation_sourcefile(object).nil?
    next if ObjectSpace.allocation_sourcefile(object) == __FILE__
    next if @existing_object_ids[object.object_id / 1000] &&
            @existing_object_ids[object.object_id / 1000].include?(object.object_id)

    @new_allocations << Allocation.new(object)
  end
end

#inspectObject

Inspect @new_allocations, the canonical array of Allocation objects.



116
117
118
# File 'lib/allocation_stats.rb', line 116

def inspect
  @new_allocations.inspect
end

#startObject



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/allocation_stats.rb', line 80

def start
  GC.start
  GC.disable

  @existing_object_ids = {}

  ObjectSpace.each_object.to_a.each do |object|
    @existing_object_ids[object.object_id / 1000] ||= []
    @existing_object_ids[object.object_id / 1000] << object.object_id
  end

  ObjectSpace.trace_object_allocations_start

  return self
end

#stopObject



108
109
110
111
112
113
# File 'lib/allocation_stats.rb', line 108

def stop
  collect_new_allocations
  ObjectSpace.trace_object_allocations_stop
  ObjectSpace.trace_object_allocations_clear
  profile_and_start_gc
end

#trace(&block) ⇒ Object



48
49
50
51
52
53
54
# File 'lib/allocation_stats.rb', line 48

def trace(&block)
  if block_given?
    trace_block(&block)
  else
    start
  end
end

#trace_blockObject



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/allocation_stats.rb', line 56

def trace_block
  @burn.times { yield }

  GC.start
  GC.disable

  @existing_object_ids = {}

  ObjectSpace.each_object.to_a.each do |object|
    @existing_object_ids[object.object_id / 1000] ||= []
    @existing_object_ids[object.object_id / 1000] << object.object_id
  end

  ObjectSpace.trace_object_allocations {
    yield
  }

  collect_new_allocations
  ObjectSpace.trace_object_allocations_clear
  profile_and_start_gc

  return self
end