class EventMachine::Monitor

The monitor object itself deals with maintaining lspace and timers.

Constants

DEFAULT_BUCKETS

Which buckets to include in the histogram by default

DEFAULT_INTERVAL

How long (by default) between calls to monitors

Public Class Methods

new(&block) click to toggle source

Create a new monitor.

The block will be called in the monitor’s LSpace, all further EM work should happen within the block to ensure that we measure everything.

@param [Proc] block The block during which to monitor events @yieldparam [Monitor] self

# File lib/em-monitor.rb, line 145
def initialize(&block)
  create_lspace.enter do
    block.call self
  end
end

Public Instance Methods

monitor_spans(interval, &block) click to toggle source

Attach a listener to this monitor.

Only one listener can be active at a time, and the interval set here will take affect after the next tick of the previous interval.

@param [Number] interval The (lower bound of) time in seconds between calls to block @param [Proc] block The block to call @yieldparam [Array<Float>] spans Each number of seconds the event loop spent processing. @yieldparam [Time] from The time at which the block was previously called @yieldparam [Time] to The current time @see EM.monitor_spans

# File lib/em-monitor.rb, line 162
def monitor_spans(interval, &block)
  @periodic_timer ||= create_timer(interval)
  @periodic_timer.interval = interval
  @monitor = block
end

Private Instance Methods

create_lspace() click to toggle source

Add an around_filter to LSpace that wraps every CPU-span in a timing function.

@return [LSpace]

# File lib/em-monitor.rb, line 173
def create_lspace
  LSpace.new.around_filter do |&block|
    start = Time.now
    begin
      block.call
    ensure
      @timings << Time.now - start if @timings
    end
  end
end
create_timer(interval) click to toggle source

Add a periodic timer that will periodically call @monitor with the contents of @timings.

@param [Number] interval The interval to use for the timer. @return [EM::PeriodicTimer]

# File lib/em-monitor.rb, line 189
def create_timer(interval)
  @timings = []
  time = Time.now
  EM::PeriodicTimer.new(interval) do
    # Set last_time to *before* we call the monitor.
    # This is because the duration of the monitor will be included in the next set of
    # timings.
    last_time, time = time, Time.now
    timings = @timings.slice!(0..-1)
    @monitor.call timings, last_time, time
  end
end