Class: Arachni::Support::Profiler

Inherits:
Object
  • Object
show all
Defined in:
lib/arachni/support/profiler.rb

Overview

Author:

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.write_samples_to_disk(file, options = {}) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/arachni/support/profiler.rb', line 20

def self.write_samples_to_disk( file, options = {} )
    profiler = Support::Profiler.new

    Thread.new do
        begin
            loop do
                profiler.write_object_space( file, options )
                sleep options[:interval] || 1
            end
        rescue => e
            ap e
            ap e.backtrace
        end
    end
end

Instance Method Details

#count_objects(klass) ⇒ Object



171
172
173
# File 'lib/arachni/support/profiler.rb', line 171

def count_objects( klass )
    ObjectSpace.each_object( klass ){}
end

#find_dependencies(_object_id, _mapped = {}) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/arachni/support/profiler.rb', line 57

def find_dependencies( _object_id, _mapped = {} )
    mapped = _mapped
    points_to_object = find_references( Mass[_object_id] )
    ids = points_to_object.keys.map { |x| /\#(\d*)/.match(x).captures.first.to_i }
    mapped[_object_id] = points_to_object#ids

    unmapped = ids - mapped.keys
    unmapped.each do |x|
        new_deps = find_dependencies( x, mapped )
        mapped.merge( new_deps )
    end
    mapped
end

#find_references(o) ⇒ Object



53
54
55
# File 'lib/arachni/support/profiler.rb', line 53

def find_references( o )
    Mass.references( o )
end

#object_space(options = {}) ⇒ Object



81
82
83
84
85
86
87
88
89
90
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/arachni/support/profiler.rb', line 81

def object_space( options = {} )
    klass       = options[:class]
    namespaces  = options[:namespaces] || [
        Arachni,
        Ethon,
        Typhoeus,
        Watir,
        Selenium,
        Addressable,
        Nokogiri,
        String,
        Hash,
        Array,
        Set,
        Thread
    ]

    max_entries = options[:max_entries] || 50

    object_space    = {}
    @object_space ||= {}

    ObjectSpace.each_object do |o|
        next if o.class != klass && !object_within_namespace?( o, namespaces )

        # if o.class == Thread
        #     ap ObjectSpace.allocation_class_path( o ).to_s
        #     ap "#{ObjectSpace.allocation_sourcefile( o )}:#{ObjectSpace.allocation_sourceline( o )}"
        #     ap Utilities.bytes_to_megabytes( ObjectSpace.memsize_of( o ) )
        #     ap '-' * 120
        # end

        object_space[o.class] ||= {
            memsize: 0,
            count:   0
        }

        object_space[o.class][:memsize] += ObjectSpace.memsize_of(o)
        object_space[o.class][:count]   += 1
    end

    object_space = Hash[object_space.sort_by { |_, v| v[:memsize] }.reverse[0..max_entries]]

    with_deltas = {}
    object_space.each do |k, v|
        @object_space[k] ||= {
            memsize: 0,
            count:   0
        }

        if v[:count].is_a? Numeric
            with_deltas[k] = "#{v[:count]} (#{v[:count] - @object_space[k][:count]})"
            with_deltas[k] << " -- #{Utilities.bytes_to_megabytes v[:memsize]}"
            with_deltas[k] << " (#{Utilities.bytes_to_megabytes v[:memsize] - @object_space[k][:memsize]})"
        else
            with_deltas[k] = "#{v[:count]} -- #{Utilities.bytes_to_megabytes v[:memsize]}"
        end
    end

    @object_space = object_space.dup
    with_deltas

rescue => e
    ap e
    ap e.backtrace
    {}
end


71
72
73
74
# File 'lib/arachni/support/profiler.rb', line 71

def print_dependencies( o )
    ap 'Dependencies'
    ap find_dependencies( o.object_id )
end


41
42
43
44
45
46
47
48
49
50
51
# File 'lib/arachni/support/profiler.rb', line 41

def print_object_allocations( o )
    ap 'Object ID:   ' + o.object_id.to_s
    ap 'Source file: ' + ObjectSpace.allocation_sourcefile(o)
    ap 'Source line: ' + ObjectSpace.allocation_sourceline(o).to_s
    ap 'Generation:  ' + ObjectSpace.allocation_generation(o).to_s
    ap 'Class path:  ' + ObjectSpace.allocation_class_path(o).to_s
    ap 'Method:      ' + ObjectSpace.allocation_method_id(o).to_s
    ap 'Memsize:     ' + ObjectSpace.memsize_of(o).to_s
    #ap 'Reachable:   ' + ObjectSpace.reachable_objects_from(o).to_s  #=> [referenced, objects, ...]
    ap '-' * 200
end


167
168
169
# File 'lib/arachni/support/profiler.rb', line 167

def print_object_space( options = {} )
    ap object_space( options )
end


76
77
78
79
# File 'lib/arachni/support/profiler.rb', line 76

def print_references( o )
    ap 'References'
    ap find_references( o )
end

#resource_consumptionObject



175
176
177
178
179
180
181
182
# File 'lib/arachni/support/profiler.rb', line 175

def resource_consumption
    procinfo = ::Sys::ProcTable.ps( Process.pid )
    {
        cpu_utilization:    procinfo[:pctcpu],
        memory_utilization: procinfo[:pctmem],
        memory_usage:       rss_to_mb( procinfo[:rss] )
    }
end

#rss_to_mb(rss) ⇒ Object



184
185
186
# File 'lib/arachni/support/profiler.rb', line 184

def rss_to_mb( rss )
    rss * 4096.0 / 1024.0 / 1024.0
end

#trace_allocationsObject



36
37
38
39
# File 'lib/arachni/support/profiler.rb', line 36

def trace_allocations
    require 'objspace'
    ObjectSpace.trace_object_allocations_start
end

#write_object_space(file, options = {}) ⇒ Object



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/arachni/support/profiler.rb', line 149

def write_object_space( file, options = {} )
    consumption = resource_consumption

    str = "RAM: #{consumption[:memory_usage].round(3)}MB"
    str << " (#{consumption[:memory_utilization]}%)"
    str << " - CPU: #{consumption[:cpu_utilization]}%\n\n"

    os      = object_space( options )
    maxsize = os.keys.map(&:to_s).map(&:size).sort.reverse.first

    os.each do |klass, info|
        offset = maxsize - klass.to_s.size
        str << "#{klass}: #{' ' * offset}#{info}\n"
    end

    IO.write( file, str )
end