[zeromq-dev] Memory management
Martin Sustrik
sustrik at fastmq.com
Thu May 14 10:24:19 CEST 2009
Hi Steven,
Comments from the point of view of 0MQ inlined:
> OpenPGM currently uses a trash stack sitting above a slab allocator
> for memory management. Packets and data buffers are separately managed
> entities with their own registered size with the slab. Allocations are
> only ever returned to the slab when the transport is destroyed.
>
> Incoming packet payloads are copied once to a buffer popped from the
> trash stack. This raises a known question: is it faster to copy just
> the payload to a PGM buffer, or read in the entire packet, possibly
> including IPv4 header, into a PGM buffer and deal with the memory overhead?
We are trying to fill the packets as much as possible. Thus in
high-volume scenarios the payload size is much higher then
ipv4-header-size. Copying the payload may slow the thing down
(especially as there can be cache missed involved) while the additional
memory usage for ipv4 headers is almost negligible.
> When an application reads a message from PGM the buffers are marked
> "commit". A subsequent call to read data updates all "commit" buffers
> to a "parity" state. The parity state is required for FEC enabled
> transports when the data needs to be held until the current transmission
> block has been completed as the packet can be used for erasure
> calculation. After the transmission block has completed the buffer is
> returned to the trash stack.
>
> trash stack.png
>
> A trash stack is used to keep buffers that are in the page cache,
> testing has shown this to be faster than regular direct slab usage.
> However this prefers that the transport be local to one processor core
> to avoid cache flushes. Direct slab usage is preferred when using
> multiple cores as the slab allocator will keep a cache of buffers local
> to each core.
Access to the transport from a single thread is what we do. Using SLAB
or even optmised version like tcmalloc would be detrimental in our use case.
> Ring Buffer.
>
> Data buffers are managed as separate objects each pulled separately out
> of the slab allocator. There are two disadvantages to this
> architecture; there is an overhead necessary to manage each buffer as a
> separate object; buffers are stored as maximum TPDU size, e.g. 1,500
> bytes even if packets are only 100 bytes long.
>
> Low level network buffers are often stored as a contiguous block which
> can be used as a ring buffer. This can reduce the overhead of counting
> each separate buffer entry and allows the optional use of smaller buffer
> sizes.
This may be interesting. CPU caches today are quite sophisticated and
are often able to preload cacheline N+1 in the background after
cacheline N was touched. Thus rx/tx window in the form of contiguous
cyclical buffer may yield an interesting performance improvement.
It would require slight change to the API. User apps won't be able to
return buffers out of order (which is possible today IIRC). However, we
would be perfectly happy to return buffers to OpenPGM in order.
> Per Peer vs. Per Transport.
>
> With a ring buffer we are permanently tying the memory to the transport
> and we impose a ceiling on the number of packets that can be used.
> Buffers are currently pulled from the slab or trash stack for each
> receive window on demand. If a ring buffer is allocated per peer we may
> see a significant problem with peers bouncing up and down due to long
> expiration periods and no supported OPT_FIN notifications. This
> suggests a fixed buffer for the entire transport to share between all
> peers, the implication being variable window sizes for each peer and
> additional complexity of handling windows that must be shrunk. Should
> the buffer be equally split among peers, on a first come first serve
> basis, etc?
Peer = multicast group, right? If so, we don't really care as with 0MQ
there no way to use one transport with multiple multicast groups.
> Page Cache Friendly.
>
> The slab allocates blocks of memory aligned as preferred by the
> underlying architecture. Using a ring buffer and arbritarily slicing up
> blocks can make paging worse as multiple requests might be needed for
> one buffer. One solution is to implement a hybrid design that allocates
> page size and alignment friendly blocks of memory.
>
> wide stack.png
>
> This could still be implemented in a stack or a ring buffer, reference
> counting can be used for individual slices of a chunk.
Sorry, I'm lost here. What do you mean by "multiple requests might be
needed for one buffer"?
Martin
More information about the zeromq-dev
mailing list