Class: Raptor::Stats
- Inherits:
-
Object
- Object
- Raptor::Stats
- 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
requests uint64 8 bytes
backlog uint32 4 bytes
started_at float64 8 bytes
last_checkin float64 8 bytes
booted uint8 1 byte
33 bytes total
Constant Summary collapse
- SLOT_FORMAT =
"LQLddC"- SLOT_SIZE =
[0, 0, 0, 0.0, 0.0, 0].pack(SLOT_FORMAT).bytesize
Instance Method Summary collapse
-
#all ⇒ Array<Hash>
Returns stats for all worker slots.
-
#initialize(num_workers) ⇒ void
constructor
Creates a new Stats instance backed by anonymous shared memory.
-
#unmap ⇒ void
Releases the shared memory mapping.
-
#write(index, pid:, requests:, backlog:, started_at:, last_checkin:, booted:) ⇒ void
Writes stats for a worker slot into shared memory.
Constructor Details
#initialize(num_workers) ⇒ void
Creates a new Stats instance backed by anonymous shared memory.
Allocates a MAP_ANON | MAP_SHARED mmap region large enough for num_workers slots. Must be called before forking so that all worker processes share the same backing memory.
40 41 42 43 |
# File 'lib/raptor/stats.rb', line 40 def initialize(num_workers) @num_workers = num_workers @mmap = Mmap.new(nil, length: num_workers * SLOT_SIZE, initialize: "\0") end |
Instance Method Details
#all ⇒ Array<Hash>
Returns stats for all worker slots.
67 68 69 |
# File 'lib/raptor/stats.rb', line 67 def all (0...@num_workers).map { |index| read(index) } end |
#unmap ⇒ void
This method returns an undefined value.
Releases the shared memory mapping.
76 77 78 |
# File 'lib/raptor/stats.rb', line 76 def unmap @mmap.unmap end |
#write(index, pid:, requests:, backlog:, started_at:, last_checkin:, booted:) ⇒ void
This method returns an undefined value.
Writes stats for a worker slot into shared memory.
57 58 59 60 |
# File 'lib/raptor/stats.rb', line 57 def write(index, pid:, requests:, backlog:, started_at:, last_checkin:, booted:) data = [pid, requests, backlog, started_at, last_checkin, booted ? 1 : 0].pack(SLOT_FORMAT) @mmap.semlock { @mmap[index * SLOT_SIZE, SLOT_SIZE] = data } end |