3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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
|
# File 'lib/fast_stack.rb', line 3
def self.profile(millisecs=1, &blk)
done = false
stacks = []
thread = Thread.current
delta = millisecs / 1000.0
last_time = Time.new
new_api = thread.respond_to?(:backtrace_locations)
profile_thread = Thread.new do
until done
start = Time.new
if last_time < start - delta
last_time = start
stacks << (new_api ? thread.backtrace_locations : thread.backtrace)
end
sleep(delta)
end
end
trap('PROF') do
start = Time.new
if last_time < start - delta
last_time = start
stack = (new_api ? thread.backtrace_locations : thread.backtrace)
if thread == Thread.current
stack = stack[2..-1]
start = Time.new
end
stacks << stack
end
end
begin
profile_block(millisecs * 1000, &blk)
ensure
done = true
profile_thread.join
end
stacks
end
|