examples/capture/capture.rb in async-1.20.1 vs examples/capture/capture.rb in async-1.21.0

- old
+ new

@@ -32,32 +32,38 @@ value end end end -def strace(pid, duration = 10) +def strace(pid, duration = 60) input, output = IO.pipe - pid = Process.spawn("strace", "-p", pid.to_s, "-cqf", "-w", "-e", "trace=read,write,sendto,recvfrom", err: output) + pid = Process.spawn("strace", "-p", pid.to_s, "-cqf", "-w", "-e", "!futex", err: output) output.close Signal.trap(:INT) do Process.kill(:INT, pid) Signal.trap(:INT, :DEFAULT) end + Thread.new do + sleep duration + Process.kill(:INT, pid) + end + summary = {} - if line = input.gets - rule = input.gets # horizontal separator - pattern = Regexp.new( - rule.split(/\s/).map{|s| "(.{1,#{s.size}})"}.join(' ') - ) + if first_line = input.gets + if rule = input.gets # horizontal separator + pattern = Regexp.new( + rule.split(/\s/).map{|s| "(.{1,#{s.size}})"}.join(' ') + ) + + header = pattern.match(first_line).captures.map{|key| key.strip.to_sym} + end - header = pattern.match(line).captures.map{|key| key.strip.to_sym} - while line = input.gets break if line == rule row = pattern.match(line).captures.map{|value| parse(value)} fields = header.zip(row).to_h @@ -69,19 +75,24 @@ fields = header.zip(row).to_h summary[:total] = fields end end - Process.waitpid(pid) + _, status = Process.waitpid2(pid) + Console.logger.error(status) do |buffer| + buffer.puts first_line + end unless status.success? + return summary end pids.each do |pid| start_times = getrusage(pid) Console.logger.info("Process #{pid} start times:", start_times) + # sleep 60 summary = strace(pid) Console.logger.info("strace -p #{pid}") do |buffer| summary.each do |fields| buffer.puts fields.inspect @@ -92,10 +103,10 @@ Console.logger.info("Process #{pid} end times:", end_times) if total = summary[:total] process_duration = end_times.utime - start_times.utime wait_duration = summary[:total][:seconds] - + Console.logger.info("Process Waiting: #{wait_duration.round(4)}s out of #{process_duration.round(4)}s") do |buffer| buffer.puts "Wait percentage: #{(wait_duration / process_duration * 100.0).round(2)}%" end else Console.logger.warn("No system calls detected.")