Class: RbBCC::RingBuf

Inherits:
TableBase show all
Includes:
EventTypeSupported
Defined in:
lib/rbbcc/table.rb

Instance Attribute Summary

Attributes inherited from TableBase

#bpf, #flags, #keysize, #keytype, #leafsize, #leaftype, #map_fd, #map_id, #name, #ttype

Instance Method Summary collapse

Methods included from EventTypeSupported

#get_event_class

Methods inherited from TableBase

#[], #[]=, #clear, #delete, #each_key, #each_pair, #each_value, #fetch, #items, #next, #print_linear_hist, #print_log2_hist, #structured_key?, #values

Methods included from CPUHelper

_read_cpu_range, get_online_cpus, get_possible_cpus

Constructor Details

#initialize(bpf, map_id, map_fd, keytype, leaftype, name: nil) ⇒ RingBuf

Returns a new instance of RingBuf.



404
405
406
407
408
409
# File 'lib/rbbcc/table.rb', line 404

def initialize(bpf, map_id, map_fd, keytype, leaftype, name: nil)
  super
  @_ringbuf = nil
  @_ringbuf_manager = nil
  @event_class = nil
end

Instance Method Details

#event(data) ⇒ Object



411
412
413
414
415
416
# File 'lib/rbbcc/table.rb', line 411

def event(data)
  @event_class ||= get_event_class
  ev = @event_class.malloc
  Fiddle::Pointer.new(ev.to_ptr)[0, @event_class.size] = data[0, @event_class.size]
  return ev
end

#open_ring_buffer(ctx = nil, &callback) ⇒ Object



418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
# File 'lib/rbbcc/table.rb', line 418

def open_ring_buffer(ctx=nil, &callback)
  # bind("int ring_buffer_sample_fn(void *, void *, int)")
  fn = Fiddle::Closure::BlockCaller.new(
    Fiddle::TYPE_INT,
    [Fiddle::TYPE_VOIDP, Fiddle::TYPE_VOIDP, Fiddle::TYPE_INT]
  ) do |_dummy, data, size|
    begin
      _ret = callback.call(ctx, data, size)
      ret = _ret.to_i
      ret
    rescue NoMethodError
      # Callback for ringbufs should _always_ return an integer.
      # simply fall back to returning 0 when failed
      0
    rescue => e
      if Fiddle.last_error == 32 # EPIPE
        exit
      else
        raise e
      end
    end
  end

  @bpf._open_ring_buffer(@map_fd, fn, ctx)
  nil
end