Class: Raptor::Stats
- Inherits:
-
Object
- Object
- Raptor::Stats
- Defined in:
- lib/raptor/stats.rb,
sig/generated/raptor/stats.rbs
Overview
Shared-memory store for per-worker process statistics. Each worker holds a fixed-size slot in an anonymous mmap 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
-
#all ⇒ Array<Hash>
Returns stats for all worker slots.
-
#initialize(num_workers) ⇒ Stats
constructor
Creates a new Stats and allocates the shared mmap region for
num_workersslots. -
#read(slot) ⇒ Hash
Reads stats for a worker slot from shared memory.
-
#unmap ⇒ void
Releases the shared memory mapping.
-
#write(index, pid:, phase:, requests:, backlog:, busy_threads:, thread_capacity:, started_at:, last_checkin:, booted:) ⇒ void
Writes stats for a worker slot into shared memory.
Constructor Details
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 |
#read(slot) ⇒ Hash
Reads stats for a worker slot from shared memory.
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/raptor/stats.rb', line 88 def read(slot) data = @mmap[slot * SLOT_SIZE, SLOT_SIZE] pid, index, phase, requests, backlog, busy_threads, thread_capacity, started_at, last_checkin, booted = data.unpack(SLOT_FORMAT) { pid: pid, index: index, phase: phase, requests: requests, backlog: backlog, busy_threads: busy_threads, thread_capacity: thread_capacity, started_at: started_at, last_checkin: last_checkin, booted: booted == 1 } 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:, 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.
57 58 59 60 |
# File 'lib/raptor/stats.rb', line 57 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[index * SLOT_SIZE, SLOT_SIZE] = data end |