Class: Silhouette::DefaultProfiler

Inherits:
Processor
  • Object
show all
Defined in:
lib/silhouette/processor.rb

Direct Known Subclasses

EntryPointProfiler

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Processor

#process_line, #run

Constructor Details

#initialize(per_callsite = false) ⇒ DefaultProfiler

Returns a new instance of DefaultProfiler.



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

def initialize(per_callsite=false)
    @threads = Hash.new
    @map = Hash.new
    @cs_map = Hash.new
    @timestamps = []
    @clocks = []
    @pid = nil
    @total = 0.0
    @filters = []
    @methods = Hash.new
    @files = Hash.new
    @per_callsite = per_callsite
end

Instance Attribute Details

#directoryObject (readonly)

Returns the value of attribute directory.



112
113
114
# File 'lib/silhouette/processor.rb', line 112

def directory
  @directory
end

#timestampsObject (readonly)

Returns the value of attribute timestamps.



112
113
114
# File 'lib/silhouette/processor.rb', line 112

def timestamps
  @timestamps
end

Instance Method Details

#collapse_children(cl, map) ⇒ Object



247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
# File 'lib/silhouette/processor.rb', line 247

def collapse_children(cl, map)
    out = Hash.new { |h,k| h[k] = 0 }

    # p cl
    cl.each do |c|
        out[c] += 1
    end
    
    data = []
    out.each do |meth,times|
        # ch_ms = (map[meth][1] * 1000) / map[meth][0]
        data << [meth, times, map[meth]]
    end
    
    data.sort! { |a,b| b[2].self_sec <=> a[2].self_sec }
    data
end

#dataObject



114
115
116
# File 'lib/silhouette/processor.rb', line 114

def data
    @map
end

#data=(d) ⇒ Object



118
119
120
# File 'lib/silhouette/processor.rb', line 118

def data=(d)
    @map = d
end

#get_name(info, per_cs = false) ⇒ Object



317
318
319
320
321
322
323
# File 'lib/silhouette/processor.rb', line 317

def get_name(info, per_cs=false)
    if per_cs
        @methods[info.first].to_s + " @ " + @files[info[1]].to_s + ":#{info[2]}"
    else
        @methods[info].to_s
    end
end


199
200
201
202
# File 'lib/silhouette/processor.rb', line 199

def print(f=STDERR, max=nil)
    print_flat_profile(f, max)
    print_tree_profile(f, max)
end


208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
# File 'lib/silhouette/processor.rb', line 208

def print_flat_profile(f=STDERR, max=nil)
    f.puts "Number of threads: #{@threads.size}"
    if @per_callsite
        f.puts "Profiling based on method call and call site."
    else
        f.puts "Profiling based on method call." 
    end
    f.puts "Cost of profiler: #{@profiler_cost.to_f / @clock_per_sec} seconds."
    
    total = total_seconds
    total = 0.01 if total == 0
    f.puts "\nFlat profile (#{total} total seconds):"
    data = @map.values
    data.sort! { |a,b| b.self_sec <=> a.self_sec }
    # data.sort! { |a,b| b[1] <=> a[1] }
    sum = 0
    f.puts "  %      total     self              self     total"
    f.puts " time   seconds   seconds    calls  ms/call  ms/call  name"
    count = 0
    data.each do |node|
    #data.each do |calls, self_ms, total_ms, sig|
        sum += node.self_sec
        
        prec = node.percentage(total)
        next if prec < 0.01
        
        f.printf "%6.2f ",  prec
        f.printf "%8.2f ",  sum
        f.printf "%8.2f ",  node.self_sec
        f.printf "%8d ",    node.calls
        f.printf "%8.2f ",  node.self_ms_per_call
        f.printf "%8.2f ",  node.total_ms_per_call
        f.puts get_name(node.method)
    
        count += 1
        return if max and count > max
    end
end


268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
# File 'lib/silhouette/processor.rb', line 268

def print_tree_profile(f=STDERR, max=nil)
    width = 40
    f.puts
    f.puts "Call Tree Profile: "
    f.puts "index       calls      ms/     self    total"
    f.puts "                      call     sec      sec"
    map = @map

    data = map.values
    data.sort! { |a,b| b.self_sec <=> a.self_sec }
    data.each do |pn|
        next if pn.total_ms_per_call < 0.01
        data = collapse_children(pn.callers, map)

        data.each do |meth, called_times, cn|
            times = map[meth].children.find_all { |i| i == pn.method }.size
            if times == 0
                times = "?"
            else
                called_times = called_times / times
            end
            vars = ["", "#{times}/#{called_times}", "-", "-", 
                "-", get_name(cn.method)]
            f.puts "%-2s %14s %8s %8s %8s    %s [#{meth}]" % vars
        end

        cl = collapse_children(pn.children, map)
        chlines = []
        sum = 0
        cl.each do |meth, times, cn|
            self_ms = cn.total_ms_per_call * times
            sum += self_ms
            self_sec = self_ms.to_f / 1000
            total = self_sec * pn.calls
            
            next if total < 0.01
            vars = ["", times, cn.total_ms_per_call, 
                self_sec, total, get_name(cn.method)]
            chlines << "%-8s %8d %8.2f %8.2f %8.2f    %s [#{meth}]" % vars
        end
        vars = ["[#{pn.method}]", pn.calls, 
            pn.self_ms_per_call, pn.self_sec, pn.total_sec, 
            get_name(pn.method)]
        f.puts "%-8s %8d %8.2f %8.2f %8.2f  %s" % vars
        f.puts *chlines if chlines.size > 0
        f.puts "-" * 70
    end
end

#process_call(thread, method, file, line, clock) ⇒ Object



146
147
148
149
# File 'lib/silhouette/processor.rb', line 146

def process_call(thread, method, file, line, clock)
    st = (@threads[thread] ||= [])
    st.push [clock, 0.0, [method, file, line], []]
end

#process_end(*args) ⇒ Object



132
133
134
# File 'lib/silhouette/processor.rb', line 132

def process_end(*args)
    @final_clock, @profiler_cost = *args
end

#process_file(*args) ⇒ Object



141
142
143
144
# File 'lib/silhouette/processor.rb', line 141

def process_file(*args)
    idx = args.shift
    @files[idx] = args
end

#process_method(*args) ⇒ Object



136
137
138
139
# File 'lib/silhouette/processor.rb', line 136

def process_method(*args)
    idx = args.shift
    @methods[idx] = args
end

#process_return(thread, method, file, line, clock) ⇒ Object



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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/silhouette/processor.rb', line 151

def process_return(thread, method, file, line, clock)
    st = (@threads[thread] ||= [])
    if tick = st.pop
=begin
        if tick.last != method
            STDERR.puts "Unmatched return for #{method} (#{tick.last})"
            return
        end
        if @per_callsite
            key = [method, loc]
        else
            key = method
        end
=end
        cost = (clock.to_f - tick[0]) / @clock_per_sec

        # Add to the callers callee cost and child list.
        if last = st.last
            last[1] += cost
            last[3] << method
            caller = last[2]
        else
            caller = nil
        end

        # Record the data for the method.
        key = method
        node = (@map[key] ||= ProfileNode.new(key, tick.last))
        node.inc_call!
        node.add_cost cost, tick[1]
        node.callers << caller.first if caller
        
#                 data = (@map[key] ||= [0, 0.0, 0.0, key, tick.last, []])
#                 data[0] += 1
#                 data[2] += cost
#                 data[1] += cost - tick[1]
#                 data[5] << caller.first if caller # Just the method index

        # Record the data for the method at callsite
#                 key = [method, file, line]
#                 data = (@cs_map[key] ||= [0, 0.0, 0.0, key, tick.last, caller])
#                 data[0] += 1
#                 data[2] += cost
#                 data[1] += cost - tick[1]
    
    end
end

#process_start(*args) ⇒ Object



128
129
130
# File 'lib/silhouette/processor.rb', line 128

def process_start(*args)
    @directory, @clock_per_sec, @start_clock = *args
end

#save(file) ⇒ Object



122
123
124
125
126
# File 'lib/silhouette/processor.rb', line 122

def save(file)
    File.open(file, "w") do |f|
        f << Marshal.dump(self)
    end
end

#show_callers(callers, map) ⇒ Object



265
266
# File 'lib/silhouette/processor.rb', line 265

def show_callers(callers, map)
end

#stack(thread) ⇒ Object



108
109
110
# File 'lib/silhouette/processor.rb', line 108

def stack(thread)
    @threads[thread] ||= []
end

#total_secondsObject



204
205
206
# File 'lib/silhouette/processor.rb', line 204

def total_seconds
    (@final_clock - @start_clock  ).to_f / @clock_per_sec
end