about summary refs log tree commit homepage
path: root/examples
diff options
context:
space:
mode:
authorEric Wong <normalperson@yhbt.net>2011-03-08 16:45:01 -0800
committerEric Wong <normalperson@yhbt.net>2011-03-08 16:48:24 -0800
commitd5cbd604badd58ebd99a5e19953d20d7672da4dc (patch)
tree8c1c544b5b7228093754adbcc94ead47d8dca545 /examples
parent93bb8af761dbaa8e19c4660c520db1ee7ba1c70b (diff)
downloadraindrops-d5cbd604badd58ebd99a5e19953d20d7672da4dc.tar.gz
USR1 dumps histograms, and USR2 resets the counters
Diffstat (limited to 'examples')
-rwxr-xr-xexamples/linux-listener-stats.rb40
1 files changed, 37 insertions, 3 deletions
diff --git a/examples/linux-listener-stats.rb b/examples/linux-listener-stats.rb
index cb48ef7..9493931 100755
--- a/examples/linux-listener-stats.rb
+++ b/examples/linux-listener-stats.rb
@@ -8,15 +8,44 @@ require 'raindrops'
 require 'optparse'
 require 'ipaddr'
 require 'time'
+begin
+  require 'aggregate'
+rescue LoadError
+  $stderr.puts "Aggregate missing, USR1 and USR2 handlers unavailable"
+end
+
 usage = "Usage: #$0 [-d DELAY] [-t QUEUED_THRESHOLD] ADDR..."
 ARGV.size > 0 or abort usage
 delay = false
 queued_thresh = -1
-
 # "normal" exits when driven on the command-line
 trap(:INT) { exit 130 }
 trap(:PIPE) { exit 0 }
 
+agg_active = agg_queued = nil
+if defined?(Aggregate)
+  agg_active = Aggregate.new
+  agg_queued = Aggregate.new
+
+  def dump_aggregate(label, agg)
+    $stderr.write "--- #{label} ---\n"
+    %w(count min max outliers_low outliers_high mean std_dev).each do |f|
+      $stderr.write "#{f}=#{agg.__send__ f}\n"
+    end
+    $stderr.write "#{agg}\n\n"
+  end
+
+  trap(:USR1) do
+    dump_aggregate "active", agg_active
+    dump_aggregate "queued", agg_queued
+  end
+  trap(:USR2) do
+    agg_active = Aggregate.new
+    agg_queued = Aggregate.new
+  end
+  $stderr.puts "USR1(dump_aggregate) and USR2(reset) handlers ready for PID=#$$"
+end
+
 opts = OptionParser.new('', 24, '  ') do |opts|
   opts.banner = usage
   opts.on('-d', '--delay=DELAY', Float) { |n| delay = n }
@@ -63,7 +92,12 @@ begin
   tcp and combined.merge! Raindrops::Linux.tcp_listener_stats(tcp)
   unix and combined.merge! Raindrops::Linux.unix_listener_stats(unix)
   combined.each do |addr,stats|
-    next if stats.queued < queued_thresh
-    printf fmt, now ||= Time.now.utc.iso8601, addr, stats.active, stats.queued
+    active, queued = stats.active, stats.queued
+    if agg_active
+      agg_active << active
+      agg_queued << queued
+    end
+    next if queued < queued_thresh
+    printf fmt, now ||= Time.now.utc.iso8601, addr, active, queued
   end
 end while delay && sleep(delay)