Class: Arachni::Support::Profiler

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

Overview

Author:

  • Tasos “Zapotek” Laskos <tasos.laskos@arachni-scanner.com>

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
35
# File 'lib/arachni/support/profiler.rb', line 20

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

    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



212
213
214
# File 'lib/arachni/support/profiler.rb', line 212

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

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



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

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



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

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

#object_space(options = {}) ⇒ Object



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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/arachni/support/profiler.rb', line 82

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

    namespaces = Set.new( namespaces )

    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 == Page
        #     # print_object_allocations( o )
        #
        #     # ap ObjectSpace.allocation_class_path( o ).to_s
        #     # ap "#{ObjectSpace.allocation_sourcefile( o )}:#{ObjectSpace.allocation_sourceline( o )}"
        #     ap find_references( o )
        # end

        # if o.class == String && o.size > 1_000_000
        #     print_object_allocations( o )
        #     # print_references( o )
        #     # print_dependencies( o )
        #
        #     Process.kill 'KILL', Process.pid
        # end

        # if o.class == Thread
        #     print_object_allocations( o )
        #     # print_references( o )
        #     # print_dependencies( o )
        #
        #     # Process.kill 'KILL', Process.pid
        # end

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

        if o.class == String
            object_space[o.class][:memsize] += o.bytesize
        else
            object_space[o.class][:memsize] += ObjectSpace.memsize_of(o)
        end

        object_space[o.class][:count] += 1
    end

    ap '-' * 120

    object_space['All'] = {
        memsize: ObjectSpace.memsize_of_all,
        count:   ObjectSpace.count_objects_size[:TOTAL]
    }

    object_space = Hash[object_space.sort_by { |_, v| v[:count] }.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


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

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


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

def print_object_allocations( o )
    ap 'Reachable:   ' + ObjectSpace.reachable_objects_from(o).to_s  #=> [referenced, objects, ...]
    ap 'Object ID:   ' + o.object_id.to_s
    ap 'Source file: ' + ObjectSpace.allocation_sourcefile(o).to_s
    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 '-' * 200
end


208
209
210
# File 'lib/arachni/support/profiler.rb', line 208

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


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

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

#resource_consumptionObject



216
217
218
219
220
221
222
223
# File 'lib/arachni/support/profiler.rb', line 216

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



225
226
227
# File 'lib/arachni/support/profiler.rb', line 225

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

#trace_allocationsObject



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

def trace_allocations
    require 'objspace'
    ObjectSpace.trace_object_allocations_start
end

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



181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# File 'lib/arachni/support/profiler.rb', line 181

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

    mem = consumption[:memory_usage].round(3)

    delta_mem = 0
    if @consumption
        delta_mem = (mem - @consumption[:memory_usage]).round(3)
    end

    str = "RAM: #{mem}MB (#{delta_mem}MB)"
    str << " (#{consumption[:memory_utilization]}%)"
    str << " - CPU: #{consumption[:cpu_utilization]}%\n\n"

    @consumption = consumption

    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