Class: SamplingProf::Sampling

Inherits:
Object
  • Object
show all
Defined in:
lib/sampling_prof/internal.rb

Instance Method Summary collapse

Constructor Details

#initializeSampling

Returns a new instance of Sampling.



6
7
8
9
10
11
# File 'lib/sampling_prof/internal.rb', line 6

def initialize
  @samples = Hash.new{|h,k| h[k] = [0, 0] }
  @call_graph = Hash.new{|h,k| h[k] = 0}
  @nodes = {}
  @start_at = Time.now
end

Instance Method Details

#call_element(loc) ⇒ Object



58
59
60
# File 'lib/sampling_prof/internal.rb', line 58

def call_element(loc)
  [loc.path, loc.lineno, loc.label].join(":")
end

#node_id(loc) ⇒ Object



54
55
56
# File 'lib/sampling_prof/internal.rb', line 54

def node_id(loc)
  @nodes[call_element(loc)] ||= @nodes.size
end

#process(thread) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/sampling_prof/internal.rb', line 29

def process(thread)
  locations = thread.backtrace_locations
  from = -1
  paths = []
  calls = []
  top_index = locations.size - 1
  locations.reverse.each_with_index do |loc, i|
    node_id = node_id(loc)
    if i == top_index
      @samples[node_id][0] += 1
    end

    path = [from, node_id]
    if !paths.include?(path)
      paths << path
      @call_graph[path] += 1
    end
    if !calls.include?(node_id)
      calls << node_id
      @samples[node_id][1] += 1
    end
    from = node_id
  end
end

#resultObject



21
22
23
24
25
26
27
# File 'lib/sampling_prof/internal.rb', line 21

def result
  ret = [runtime * 1000]
  ret << @nodes.map {|node| node.join(',')}.join("\n")
  ret << @samples.map {|count| count.flatten.join(',')}.join("\n")
  ret << @call_graph.map {|v| v.flatten.join(',')}.join("\n")
  "#{ret.join("\n\n")}\n"
end

#runtimeObject



13
14
15
# File 'lib/sampling_prof/internal.rb', line 13

def runtime
  Time.now - @start_at
end

#sampling_data?Boolean

Returns:

  • (Boolean)


17
18
19
# File 'lib/sampling_prof/internal.rb', line 17

def sampling_data?
  !@nodes.empty?
end