[zeromq-dev] how to receive a message into a static buffer?
Martin Sustrik
sustrik at fastmq.com
Tue May 5 10:48:53 CEST 2009
Hi,
> I see the problem. What if the user supplies a fixed-length buffer at
> the time of queue creation and 0MQ refuses to asynchronously retrieve
> data from the network until buffer space becomes available? The user
> would have to indicate when it done with each message so that 0MQ knows
> which buffers are available.
>
> The reason I'm so concerned about this is that I'm using 0MQ for
> process-scope messaging between threads ... So 0MQ does one copy to move
> the message between the threads, but then I must do a second copy to
> place the data in my static buffer ... I may be wrong, but that sounds
> like one too many memcopies for just communicating between two threads.
Actually, when message is sent between threads, there's no copy
involved. Instead, pointer is passed between the threads. (There's an
optimisation for message under 30 bytes of length that does copy the
message as copying 30 bytes is far more efficient than allocating the
storage on the heap.)
> In my case, my buffer need to be very small due to hardware
> restrictions. It has space for something like 4 to 8 messages. As soon
> as the data arrives in the buffer, a DMA transfer occurs to copy the
> buffer into some specialized hardware for processing. Once the
> processing is complete, the buffer is ready for the next batch of
messages.
Understood. You have a pinned buffer and notify the hardware that it can
access the buffer once the data are in-place.
Still, I believe the current solution (1 copy) is the best possible way
to handle this use case. If there was no copy involved the whole system
would work in a lock-step fashion which defeats the whole point of using
a message queue. Simply writing the data into DMA buffer and
synchronously waiting while it's processed before filling in new data
would be the preferred solution then.
> Well the application I'm describing requires that the buffers be aligned
> on a 128-byte boundary in memory. If the 0MQ allocated buffers can
> somehow be coerced into this behavior then stealing those buffers could
> work.
Ah, so you are able to pin the buffer after you've received the message.
That can possibly work. The easiest way to achieve it is to supply
buffers of your own on the sender side (we're talking about in-process
messaging, right?)
Allocate your 128-byte alligned buffer on the sender side, fill it with
data and create a message this way:
message_t msg (data, data_size, free_function);
The message will be transferred to the other thread. On the receiver
size msg.data() and msg.size() would return exactly the same buffer
you've used on the sender side. When you destroy the message on the
receiver side, free_function supplied on the sender side will be called
to deallocate the buffer.
(Note that there's no copy involved altogether here.)
Would that work for you?
Martin
More information about the zeromq-dev
mailing list