Permalink
Browse files

linux-listener-stats: use timerfd if available

It is more accurate than sleep and doesn't require calculating
times
  • Loading branch information...
1 parent 65dc45b commit b0be66c37bb7080b0f06ab76b0e7d3404e2f9059 Eric Wong committed Mar 14, 2011
Showing with 15 additions and 1 deletion.
  1. +15 −1 examples/linux-listener-stats.rb
@@ -8,6 +8,10 @@
require 'optparse'
require 'ipaddr'
require 'time'
+begin
+ require 'sleepy_penguin'
+rescue LoadError
+end
usage = "Usage: #$0 [-d DELAY] [-t QUEUED_THRESHOLD] ADDR..."
ARGV.size > 0 or abort usage
delay = false
@@ -31,6 +35,16 @@
$stderr.puts "Aggregate missing, USR1 and USR2 handlers unavailable"
end if delay
+if delay && defined?(SleepyPenguin::TimerFD)
+ @tfd = SleepyPenguin::TimerFD.new
+ @tfd.settime nil, delay, delay
+ def delay_for(seconds)
+ @tfd.expirations
+ end
+else
+ alias delay_for sleep
+end
+
agg_active = agg_queued = nil
if delay && defined?(Aggregate)
agg_active = Aggregate.new
@@ -109,4 +123,4 @@ def dump_aggregate(label, agg)
next if queued < queued_thresh
printf fmt, now ||= Time.now.utc.iso8601, addr, active, queued
end
-end while delay && sleep(delay)
+end while delay && delay_for(delay)

0 comments on commit b0be66c

Please sign in to comment.