vllm.distributed.device_communicators.shm_object_storage
¶
Classes:
-
ObjectSerde– -
SingleWriterShmObjectStorage–A single-writer, multiple-reader object storage system built on top of a
-
SingleWriterShmRingBuffer–A single-writer, multiple-reader ring buffer implementation using shared
ObjectSerde
¶
Bases: ABC
Methods:
-
deserialize–Deserialize bytes back to an object.
-
serialize–Serialize an object to bytes.
SingleWriterShmObjectStorage
¶
A single-writer, multiple-reader object storage system built on top of a shared memory ring buffer. Provides key-value storage with automatic memory management and cross-process serialization support.
This storage system follows a FIFO (First-In-First-Out) eviction policy where the oldest objects are automatically freed when memory runs low. Memory is reclaimed based on reader reference counting - objects are only freed when all readers have finished accessing them.
Architecture: - Single writer process can put(key, value) objects - Multiple reader processes can get(address, monotonic_id, signature, key) objects - Built on SingleWriterShmRingBuffer for efficient shared memory management - Thread-safe operations with reader synchronization via locks
Key Features: - FIFO Eviction: Oldest objects are evicted first when memory is full - Reference Counting: Objects are only freed when no readers are accessing them - Duplicate Key Handling: Existing keys are not overwritten, just re-referenced - Customized Serialization: By default uses Msgpack for efficient serialization of Python objects, but can be extended for custom types - Cross-Process Safety: Uses shared memory with proper synchronization - Automatic Cleanup: Garbage collection happens transparently during allocation
Handle Signatures: - Handles are signed with an HMAC of (key, address, monotonic_id), keyed by a random secret stored at the start of the shared memory - The writer signs the handles it issues and rejects stale or forged handles supplied by requests - Readers verify the signature in get/touch before reading shared memory
Memory Layout per Object:
[4-byte reference_count][metadata_size][serialized_object_data]
Thread Safety: - Writer operations (put, clear) are single-threaded by design - Reader operations (get) are thread-safe with lock-based reference counting - Memory reclamation is handled exclusively by the writer process
Methods:
-
__init__–Initialize the object storage.
-
clear–Clear the object storage.
-
close–Close the shared memory.
-
default_is_free_check–Default is_free function that checks if the first 4 bytes are zero.
-
free_unused–Free unused buffers in the ring buffer.
-
get_cached–Get the cached object by key if it exists.
-
get_signature–Sign the handle of a cached object so readers can verify it.
-
handle–Get handle for sharing across processes.
-
increment_reader_flag–Set the in-use flag for the reader.
-
increment_writer_flag–Set the in-use flag for the writer.
-
is_cached–Check if the object with the given key is cached.
-
put–Store a key-value pair in the object storage.
-
release_touches–Stop protecting touched items that were not looked up.
-
touch–Touch an existing cached item.
-
verify_signature–Verify that a handle was issued for the given key.
__init__(max_object_size, n_readers, ring_buffer, serde_class=MsgpackSerde, reader_lock=None)
¶
Initialize the object storage.
Parameters:
-
(max_object_size¶int) –Maximum size for a single object in bytes.
-
(n_readers¶int) –Number of reader processes that can access the storage.
-
(ring_buffer¶SingleWriterShmRingBuffer) –The shared memory ring buffer for storing objects.
-
(serde_class¶type[ObjectSerde], default:MsgpackSerde) –Serializer/deserializer for objects.
-
(reader_lock¶Lock | None, default:None) –Optional lock for synchronizing reader access.
Raises:
-
ValueError–If reader_lock is None for readers.
clear()
¶
Clear the object storage.
close()
¶
Close the shared memory.
default_is_free_check(id, buf)
¶
Default is_free function that checks if the first 4 bytes are zero. This indicates that the buffer is free.
free_unused()
¶
Free unused buffers in the ring buffer.
get_cached(key)
¶
Get the cached object by key if it exists.
get_signature(key)
¶
Sign the handle of a cached object so readers can verify it.
handle()
¶
Get handle for sharing across processes.
increment_reader_flag(data_view)
¶
Set the in-use flag for the reader.
increment_writer_flag(id)
¶
Set the in-use flag for the writer.
is_cached(key)
¶
Check if the object with the given key is cached.
put(key, value)
¶
Store a key-value pair in the object storage. Attempts to free max_object_size bytes using FIFO order when the ring buffer runs out of space during a put() operation.
Parameters:
Raises:
-
MemoryError–If there's not enough space in the buffer
-
ValueError–If the serialized object is too large
-
ValueError–If the key already exists in the storage
release_touches()
¶
Stop protecting touched items that were not looked up.
touch(key, address=0, monotonic_id=0, signature=None)
¶
Touch an existing cached item.
For writers (ShmObjectStoreSenderCache): Protect the item from eviction until get_cached or release_touches For readers (ShmObjectStoreReceiverCache): Validate the handle
Parameters:
-
(key¶str) –String key of the object to touch
-
(address¶int, default:0) –Address of the object (only for readers)
-
(monotonic_id¶int, default:0) –Monotonic ID of the object (only for readers)
-
(signature¶list[int] | None, default:None) –Server-issued handle signature (only for readers)
verify_signature(key, address, monotonic_id, signature)
¶
Verify that a handle was issued for the given key.
For writers: the handle must also be the key's current entry For readers: only the signature is checked
Parameters:
SingleWriterShmRingBuffer
¶
A single-writer, multiple-reader ring buffer implementation using shared memory. This class provides a thread-safe ring buffer where one process can write data while multiple processes/threads can read from it.
Architecture:
- Uses shared memory for cross-process communication
- Maintains metadata for each allocated buffer chunk in the writer process
- Supports custom "is_free_fn" functions to determine when buffers can be
reused
- Each buffer chunk contains: [4-byte id][4-byte size][actual_data]
Key Concepts: - monotonic_id_start/end: Track the range of active buffer IDs - data_buffer_start/end: Track the physical memory range in use - Automatic wraparound when reaching buffer end - Lazy garbage collection based on is_free_fn checks
Example Usage Scenarios:
Scenario 1: Simple Linear Allocation
Buffer size: 100 bytes
Initial state: [................................................. ]
^start=end(0)
After allocating 20 bytes (id=0):
[id:0|size:20|data........][...................................]
^start(0) ^end(28)
After allocating 30 bytes (id=1):
[id:0|size:20|data........][id:1|size:30|data..............][..]
^start(0) ^end(66)
Scenario 2: Memory Reclamation
Before freeing (both buffers still in use):
[id:0|size:20|data........][id:1|size:30|data..............][..]
^start(0) ^end(66)
After id:0 is marked free by readers:
[FREED.................... ][id:1|size:30|data..............][..]
^start(28) ^end(66)
After both are freed:
[FREED..............................................][..]
^start=end(66)
Scenario 3: Wraparound Allocation (continuing from Scenario 2)
Starting from after memory reclamation in Scenario 2:
[FREED..............................................][..]
^start=end(66)
Allocate 40 bytes (id=2) - only 34 bytes available at end, so wraparound:
[id:2|size:40|data........................][FREED.............][..]
^end(148) ^start(66)
Scenario 4: Error Handling - Out of Space
Starting from after wraparound allocation in Scenario 3:
[id:2|size:40|data........................][FREED.............][..]
^end(148) ^start(66)
Trying to allocate 20 more bytes:
occupied_size_new = end + size - start = 148 + 28 - 66 > buffer_size(100)
-> Raises MemoryError: "Not enough space in the data buffer"
Thread Safety: - Single writer: Only one process/thread should write (allocate_buf) - Multiple readers: Multiple processes/threads can read (access_buf) - Reader synchronization handled by is_free_fn callback - Writer handles garbage collection (free_buf) based on reader feedback
Shared Memory Layout:
[32-byte handle-signing secret][ring buffer chunks...]
Memory Layout per Buffer Chunk:
[4-byte monotonic_id][4-byte chunk_size][actual_data...]
^metadata_start ^data_start
The monotonic_id ensures data integrity - readers can verify they're accessing the correct data even after buffer wraparound or reuse.
Methods:
-
allocate_buf–Allocate a buffer
MD_SIZE+sizebytes in the shared memory. -
byte2int–Convert bytes back to an integer.
-
clear–Clear the ring buffer.
-
close–Close the shared memory.
-
free_buf–Free a buffer of the given size. This is a no-op in shared memory,
-
get_auth_secret–Return the current handle-signing secret.
-
int2byte–Convert an integer to bytes.
allocate_buf(size)
¶
Allocate a buffer MD_SIZE + size bytes in the shared memory.
Memory layout:
[4-byte monotonic_id][4-byte size][buffer data...]
byte2int(byte_data)
¶
Convert bytes back to an integer.
clear()
¶
Clear the ring buffer.
close()
¶
Close the shared memory.
free_buf(is_free_fn, nbytes=None)
¶
Free a buffer of the given size. This is a no-op in shared memory, but we need to keep track of the metadata.
If freed memory spreads across the end and start of the ring buffer,
the actual freed memory will be in two segments. In this case there
still might not be a contiguous space of nbytes available.
Parameters:
get_auth_secret()
¶
Return the current handle-signing secret.
int2byte(integer)
¶
Convert an integer to bytes.