class Logging::Stats::Tracker
The Tracker class provides synchronized access to a collection of related samplers.
Attributes
Public Class Methods
Create a new Tracker instance. An optional boolean can be passed in to change the “threadsafe” value of the tracker. By default all trackers are created to be threadsafe.
# File lib/logging/stats.rb, line 155 def initialize( threadsafe = true ) @stats = Hash.new do |h,name| h[name] = ::Logging::Stats::Sampler.new(name) end @mutex = threadsafe ? ReentrantMutex.new : nil @runner = nil end
Public Instance Methods
Coalesce the samplers from the other tracker into this one. The other tracker is not modified by this method.
Coalescing the same two trackers multiple times should only be done if one of the trackers is reset between calls to this method. Otherwise statistics will be counted multiple times.
Only this tracker is locked when the coalescing is happening. It is left to the user to lock the other tracker if that is the desired behavior. This is a deliberate choice in order to prevent deadlock situations where two threads are contending on the same mutex.
# File lib/logging/stats.rb, line 175 def coalesce( other ) sync { other.stats.each do |name,sampler| stats[name].coalesce(sampler) end } end
Mark the named event sampler. The sampler will be created if it does not exist.
# File lib/logging/stats.rb, line 193 def mark( event ) sync {stats[event].mark} end
Periodically execute the given block at the given period. The tracker will be locked while the block is executing.
This method is useful for logging statistics at given interval.
Example
periodically_run( 300 ) { logger = Logging::Logger['stats'] tracker.each {|sampler| logger << sampler.to_s} tracker.reset }
# File lib/logging/stats.rb, line 235 def periodically_run( period, &block ) raise ArgumentError, 'a runner already exists' unless @runner.nil? @runner = Thread.new do start = stop = Time.now.to_f loop do seconds = period - (stop-start) seconds = period if seconds <= 0 sleep seconds start = Time.now.to_f break if Thread.current[:stop] == true if @mutex then @mutex.synchronize(&block) else block.call end stop = Time.now.to_f end end end
Reset all the samplers managed by this tracker.
# File lib/logging/stats.rb, line 217 def reset sync {stats.each_value {|sampler| sampler.reset}} self end
Add the given value to the named event sampler. The sampler will be created if it does not exist.
# File lib/logging/stats.rb, line 186 def sample( event, value ) sync {stats[event].sample(value)} end
Stop the current periodic runner if present.
# File lib/logging/stats.rb, line 256 def stop return if @runner.nil? @runner[:stop] = true @runner.wakeup if @runner.status @runner = nil end
Obtains an exclusive lock, runs the block, and releases the lock when the
block completes. This method is re-entrant so that a single thread can call
sync
multiple times without hanging the thread.
# File lib/logging/stats.rb, line 270 def sync return yield if @mutex.nil? @mutex.synchronize {yield} end
Tick the named event sampler. The sampler will be created if it does not exist.
# File lib/logging/stats.rb, line 200 def tick( event ) sync {stats[event].tick} end
Time the execution of the given block and store the results in the named event sampler. The sampler will be created if it does not exist.
# File lib/logging/stats.rb, line 208 def time( event ) sync {stats[event].mark} yield ensure sync {stats[event].tick} end