Eric Wong <normalperson / yhbt.net> wrote:
> Marc Heiler <shevegen / linuxmail.org> wrote:
> > Here is an idea, not sure if you'd like to pick it up because it would 
> > mean extra work for you. :)
> > 
> > But, I was wondering... this could be a nice small tutorial which could 
> > teach some more people about both ruby and the new Linux API at the same 
> > time. It is all decently documented but it is also the first time I have 
> > heard of it.
> 
> Heh, I haven't used it myself much (nor most of the things I publish).
> 
> I suppose most of the test cases are reasonable, maybe I'll use TimerFD
> in an upcoming release of raindrops[1] somewhere...

I just pushed the following change out to raindrops.git.  Not the
most useful example, but I like timerfd much more than timer_*
in POSIX (not sure if there are Ruby bindings for those, but in
many cases Kernel#sleep is perfectly adequate :)

> [1] http://raindrops.bogomips.org/ , git://bogomips.org/raindrops

From b0be66c37bb7080b0f06ab76b0e7d3404e2f9059 Mon Sep 17 00:00:00 2001
From: Eric Wong <normalperson / yhbt.net>
Date: Mon, 14 Mar 2011 14:58:40 -0700
Subject: [PATCH] linux-listener-stats: use timerfd if available

It is more accurate than sleep and doesn't require calculating
times
---
 examples/linux-listener-stats.rb |   16 +++++++++++++++-
 1 files changed, 15 insertions(+), 1 deletions(-)

diff --git a/examples/linux-listener-stats.rb b/examples/linux-listener-stats.rb
index 3494dff..ed58d27 100755
--- a/examples/linux-listener-stats.rb
+++ b/examples/linux-listener-stats.rb
@@ -8,6 +8,10 @@ require 'raindrops'
 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 @@ rescue LoadError
   $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 @@ begin
     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)
-- 
Eric Wong