Module: Vivarium

Defined in:
lib/vivarium.rb,
lib/vivarium/version.rb

Defined Under Namespace

Classes: Daemon, Error, Event, MapStore

Constant Summary collapse

PIN_DIR =
ENV.fetch("VIVARIUM_BPF_PIN_DIR", "/sys/fs/bpf/vivarium")
CONFIG_TARGETS_PIN =
File.join(PIN_DIR, "config_targets")
EVENT_INVOKED_PIN =
File.join(PIN_DIR, "event_invoked")
EVENT_WRITE_POS_PIN =
File.join(PIN_DIR, "event_write_pos")
EVENT_NAME_SIZE =
16
EVENT_PAYLOAD_SIZE =
64
EVENT_STRUCT_SIZE =
4 + EVENT_NAME_SIZE + EVENT_PAYLOAD_SIZE
EVENT_CAPACITY =
64
VERSION =
"0.0.1"

Class Method Summary collapse

Class Method Details

.observe(pin_dir: PIN_DIR, out: $stdout) ⇒ Object



282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
# File 'lib/vivarium.rb', line 282

def self.observe(pin_dir: PIN_DIR, out: $stdout)
  raise ArgumentError, "block is required" unless block_given?

  store = MapStore.new(pin_dir: pin_dir)
  pid = Process.pid
  store.register_pid(pid)

  tracer = TracePoint.new(:return, :c_return) do |tp|
    events = store.drain_events
    next if events.empty?

    out.puts "[vivarium] #{events.size} event(s) at #{tp.defined_class}##{tp.method_id} (#{tp.event})"
    events.each do |event|
      out.puts "  pid=#{event.pid} #{event.event_name} payload=#{event.payload.inspect}"
    end
    out.puts "  stack:"
    caller_locations(0, 12).each do |loc|
      out.puts "    #{loc.path}:#{loc.lineno}:in #{loc.base_label}"
    end
  end

  tracer.enable
  yield
ensure
  tracer&.disable
  store&.unregister_pid(pid)
end

.run_daemon!(argv = ARGV) ⇒ Object



310
311
312
313
314
315
316
317
318
# File 'lib/vivarium.rb', line 310

def self.run_daemon!(argv = ARGV)
  options = { pin_dir: PIN_DIR }
  OptionParser.new do |opts|
    opts.banner = "Usage: vivariumd [--pin-dir PATH]"
    opts.on("--pin-dir PATH", "Pinned map directory") { |v| options[:pin_dir] = v }
  end.parse!(argv)

  Daemon.new(pin_dir: options[:pin_dir]).run
end