Parent

Logging::Stats::Tracker

The Tracker class provides synchronized access to a collection of related samplers.

Attributes

stats[R]

Public Class Methods

new( threadsafe = true ) click to toggle source

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
155:     def initialize( threadsafe = true )
156:       @stats = Hash.new do |h,name|
157:         h[name] = ::Logging::Stats::Sampler.new(name)
158:       end
159:       @mutex = threadsafe ? ReentrantMutex.new : nil
160:       @runner = nil
161:     end

Public Instance Methods

coalesce( other ) click to toggle source

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
175:     def coalesce( other )
176:       sync {
177:         other.stats.each do |name,sampler|
178:           stats[name].coalesce(sampler)
179:         end
180:       }
181:     end
mark( event ) click to toggle source

Mark the named event sampler. The sampler will be created if it does not exist.

     # File lib/logging/stats.rb, line 193
193:     def mark( event )
194:       sync {stats[event].mark}
195:     end
periodically_run( period, &block ) click to toggle source

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
235:     def periodically_run( period, &block )
236:       raise ArgumentError, 'a runner already exists' unless @runner.nil?
237: 
238:       @runner = Thread.new do
239:         start = stop = Time.now.to_f
240:         loop do
241:           seconds = period - (stop-start)
242:           seconds = period if seconds <= 0
243:           sleep seconds
244: 
245:           start = Time.now.to_f
246:           break if Thread.current[:stop] == true
247:           if @mutex then @mutex.synchronize(&block)
248:           else block.call end
249:           stop = Time.now.to_f
250:         end
251:       end
252:     end
reset() click to toggle source

Reset all the samplers managed by this tracker.

     # File lib/logging/stats.rb, line 217
217:     def reset
218:       sync {stats.each_value {|sampler| sampler.reset}}
219:       self
220:     end
sample( event, value ) click to toggle source

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
186:     def sample( event, value )
187:       sync {stats[event].sample(value)}
188:     end
stop() click to toggle source

Stop the current periodic runner if present.

     # File lib/logging/stats.rb, line 256
256:     def stop
257:       return if @runner.nil?
258:       @runner[:stop] = true
259:       @runner.wakeup if @runner.status
260:       @runner = nil
261:     end
sync { block } click to toggle source

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
270:     def sync
271:       return yield if @mutex.nil?
272:       @mutex.synchronize {yield}
273:     end
tick( event ) click to toggle source

Tick the named event sampler. The sampler will be created if it does not exist.

     # File lib/logging/stats.rb, line 200
200:     def tick( event )
201:       sync {stats[event].tick}
202:     end
time( event ) click to toggle source

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
208:     def time( event )
209:       sync {stats[event].mark}
210:       yield
211:     ensure
212:       sync {stats[event].tick}
213:     end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.