Class: Raptor::Stats

Inherits:
Object
  • Object
show all
Defined in:
lib/raptor/stats.rb

Overview

Shared memory store for per-worker process statistics.

Stats uses an anonymous mmap (MAP_ANON | MAP_SHARED) created before forking so that worker processes can write their stats and the master process can read them without any pipes or signals. Each worker is assigned a fixed-size slot in the shared region.

Binary layout per slot (native byte order):

pid               uint32    4 bytes
index             uint32    4 bytes
phase             uint32    4 bytes
requests          uint64    8 bytes
backlog           uint32    4 bytes
busy_threads      uint32    4 bytes
thread_capacity   uint32    4 bytes
started_at        float64   8 bytes
last_checkin      float64   8 bytes
booted            uint8     1 byte
                           49 bytes total

Constant Summary collapse

SLOT_FORMAT =
"LLLQLLLddC"
SLOT_SIZE =
[0, 0, 0, 0, 0, 0, 0, 0.0, 0.0, 0].pack(SLOT_FORMAT).bytesize

Instance Method Summary collapse

Constructor Details

#initialize(num_workers) ⇒ void

Allocates the shared mmap region. Must be called before forking workers so the mapping is inherited by every child process.

Parameters:

  • num_workers (Integer)

    number of worker slots to allocate



41
42
43
44
# File 'lib/raptor/stats.rb', line 41

def initialize(num_workers)
  @num_workers = num_workers
  @mmap = Mmap.new(nil, length: num_workers * SLOT_SIZE, initialize: "\0")
end

Instance Method Details

#allArray<Hash>

Returns stats for all worker slots.

Returns:

  • (Array<Hash>)

    per-worker stat hashes with :pid, :index, :phase, :requests, :backlog, :busy_threads, :thread_capacity, :started_at, :last_checkin, and :booted



71
72
73
# File 'lib/raptor/stats.rb', line 71

def all
  (0...@num_workers).map { |index| read(index) }
end

#unmapvoid

This method returns an undefined value.

Releases the shared memory mapping.



80
81
82
# File 'lib/raptor/stats.rb', line 80

def unmap
  @mmap.unmap
end

#write(index, pid:, phase:, requests:, backlog:, busy_threads:, thread_capacity:, started_at:, last_checkin:, booted:) ⇒ void

This method returns an undefined value.

Writes stats for a worker slot into shared memory.

Parameters:

  • index (Integer)

    slot index to write into; also written into the slot itself

  • pid (Integer)

    worker process ID

  • phase (Integer)

    cluster phase this worker was forked at

  • requests (Integer)

    total requests handled by this worker

  • backlog (Integer)

    current queue depth

  • busy_threads (Integer)

    worker threads currently processing requests

  • thread_capacity (Integer)

    worker threads configured for this worker

  • started_at (Float)

    process start time as a Unix timestamp

  • last_checkin (Float)

    time of last stats write as a Unix timestamp

  • booted (Boolean)

    whether the worker has finished starting



61
62
63
64
# File 'lib/raptor/stats.rb', line 61

def write(index, pid:, phase:, requests:, backlog:, busy_threads:, thread_capacity:, started_at:, last_checkin:, booted:)
  data = [pid, index, phase, requests, backlog, busy_threads, thread_capacity, started_at, last_checkin, booted ? 1 : 0].pack(SLOT_FORMAT)
  @mmap.semlock { @mmap[index * SLOT_SIZE, SLOT_SIZE] = data }
end