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.
Source code in vllm/distributed/device_communicators/shm_object_storage.py
deserialize(data)
abstractmethod
¶
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.
Source code in vllm/distributed/device_communicators/shm_object_storage.py
447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 | |
__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.
Source code in vllm/distributed/device_communicators/shm_object_storage.py
clear()
¶
Clear the object storage.
Source code in vllm/distributed/device_communicators/shm_object_storage.py
close()
¶
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.
Source code in vllm/distributed/device_communicators/shm_object_storage.py
free_unused()
¶
Free unused buffers in the ring buffer.
Source code in vllm/distributed/device_communicators/shm_object_storage.py
get_cached(key)
¶
Get the cached object by key if it exists.
Source code in vllm/distributed/device_communicators/shm_object_storage.py
get_signature(key)
¶
Sign the handle of a cached object so readers can verify it.
Source code in vllm/distributed/device_communicators/shm_object_storage.py
handle()
¶
Get handle for sharing across processes.
Source code in vllm/distributed/device_communicators/shm_object_storage.py
increment_reader_flag(data_view)
¶
Set the in-use flag for the reader.
Source code in vllm/distributed/device_communicators/shm_object_storage.py
increment_writer_flag(id)
¶
is_cached(key)
¶
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
Source code in vllm/distributed/device_communicators/shm_object_storage.py
release_touches()
¶
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)
Source code in vllm/distributed/device_communicators/shm_object_storage.py
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:
-
(key¶str | None) –String key the handle belongs to
-
(address¶int) –Address of the object
-
(monotonic_id¶int) –Monotonic ID of the object
-
(signature¶list[int] | None) –Signature issued with the handle
Source code in vllm/distributed/device_communicators/shm_object_storage.py
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.
Source code in vllm/distributed/device_communicators/shm_object_storage.py
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 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 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 | |
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...]
Source code in vllm/distributed/device_communicators/shm_object_storage.py
byte2int(byte_data)
¶
clear()
¶
Clear the ring buffer.
Source code in vllm/distributed/device_communicators/shm_object_storage.py
close()
¶
Close the shared memory.
Source code in vllm/distributed/device_communicators/shm_object_storage.py
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:
-
(is_free_fn¶Callable[[int, memoryview], bool]) –Predicate called with a monotonic id and the buffer, returning True when that buffer can be reclaimed.
-
(nbytes¶int, default:None) –The size of the buffer to free. If None, frees the maximum size of the ring buffer.
Source code in vllm/distributed/device_communicators/shm_object_storage.py
289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 | |
get_auth_secret()
¶
Return the current handle-signing secret.