Class: SamplingProf::Sampling

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

Instance Method Summary collapse

Constructor Details

#initialize(threads) ⇒ Sampling

Returns a new instance of Sampling.



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

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

Instance Method Details

#call_element(loc) ⇒ Object



61
62
63
# File 'lib/sampling_prof/internal.rb', line 61

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

#node_id(loc) ⇒ Object



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

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

#processObject



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

def process
  @threads.sample_threads.each do |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
end

#resultObject



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

def result
  ret = [@threads.sampling_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



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

def runtime
  Time.now - @start_at
end

#sampling_data?Boolean

Returns:

  • (Boolean)


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

def sampling_data?
  !@nodes.empty?
end