Every stage between the socket and the HDF5 write was bounded by a message count and never by bytes: ZeroMQ receive watermark 100, cbor_fifo 200, outside_fifo 1000, repub_fifo 200 - up to some 1500 images in flight per writer process, each a whole compressed image, so 6-12 GB for a JUNGFRAU 4M and 30-60 GB for a 9M, and the writer's memory was a quantity nobody had chosen. ReceiveRingBuffer is a fixed ring of bytes owned by the ImagePuller base class, so the ZeroMQ and TCP pullers share it. A message is received straight into it (TCP reads the payload into the ring; ZeroMQ copies the message in) and travels as a shared_ptr region in ImagePullerOutput; the deserialised image points into that region, republish sends it as is, and the bytes come back when the last holder lets go. Space is reclaimed in arrival order: a region released out of turn - republish finishes with messages in its own order - is marked and waits for the ones in front of it. Because of that, what republish may hold is capped at a quarter of the ring, and an image over the cap is not republished, exactly as one that finds the republish queue full; otherwise a stalled republish consumer would pin the ring and slow the writer to its rate. A message larger than the whole ring (a START or END of a very long run) goes on the heap, so it cannot wedge the receiver. A full ring stalls the receive thread, and what that does depends on the stream. On TCP the pusher waits behind the writer's BUSY heartbeats and gives up only after a minute in which no image at all was written. On ZeroMQ the pusher's send blocks for one second, then drops the image and sends without waiting for the rest of the run - so on ZeroMQ a ring that is smaller than the writer's stalls costs images, where the old 1500-message queues absorbed them. That is the trade: the budget is now a number, and it has to be chosen. jfjoch_writer sets it with -m/--rcv_ring_mb, default 2048 MB per writer process; the ZeroMQ receive watermark default goes from 100 to 10 messages, since it now only counts what sits beyond the ring. The ring's peak use is in the end-of-run log line, and docs/JFJOCH_WRITER.md has a Memory section with the sizing rule. The puller's own default is no ring at all, which keeps every other user as it was: the broker's DECTRIS stream2 puller (JFJochServices, JFJochReceiverService) stays zero-copy with the ZeroMQ message as the buffer, and the broker's direct HDF5 writer (HDF5FilePusher -> FileWriter) never goes through a puller. The puller threads now also catch std::exception, as the writer does since the previous commit: deserialising the END message of a million-image run allocates a vector per per-image array, and a bad_alloc leaving the thread ended the process. Tests: ReceiveRingBufferTest (reuse, wrap-around, full ring, out-of-order release, heap fallback, zero size, stop, a region outliving its ring); ZMQImageCommTest_SmallRingBuffer (64 one-megabyte images through a three-image ring, nothing lost, peak within the bound); one of the two writers in TCPImageCommTest_2Writers_WithAck runs on a three-image ring. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
130 lines
4.1 KiB
C++
130 lines
4.1 KiB
C++
// SPDX-FileCopyrightText: 2026 Filip Leonarski, Paul Scherrer Institute <filip.leonarski@psi.ch>
|
|
// SPDX-License-Identifier: GPL-3.0-only
|
|
|
|
#include <catch2/catch_all.hpp>
|
|
#include <future>
|
|
#include <thread>
|
|
|
|
#include "../image_puller/ReceiveRingBuffer.h"
|
|
|
|
using namespace std::chrono_literals;
|
|
|
|
TEST_CASE("ReceiveRingBuffer_sequential_reuse", "[ImagePuller][Unit]") {
|
|
ReceiveRingBuffer ring(1024);
|
|
// Far more messages than fit at once: each one is released before the next is taken.
|
|
for (int i = 0; i < 100; i++) {
|
|
auto r = ring.Allocate(300);
|
|
REQUIRE(r);
|
|
REQUIRE(r->size == 300);
|
|
r->data[0] = 1;
|
|
r->data[299] = 2;
|
|
}
|
|
CHECK(ring.GetUtilization() == 0);
|
|
CHECK(ring.GetMaxUtilization() == 300);
|
|
CHECK(ring.GetCapacity() == 1024);
|
|
}
|
|
|
|
TEST_CASE("ReceiveRingBuffer_wraps_to_the_start", "[ImagePuller][Unit]") {
|
|
ReceiveRingBuffer ring(1000);
|
|
auto a = ring.Allocate(400);
|
|
auto b = ring.Allocate(400);
|
|
REQUIRE(a);
|
|
REQUIRE(b);
|
|
uint8_t *a_data = a->data;
|
|
|
|
// 200 bytes left at the end and nothing free at the start: a third 400 has to wait.
|
|
auto f = std::async(std::launch::async, [&] { return ring.Allocate(400); });
|
|
CHECK(f.wait_for(50ms) == std::future_status::timeout);
|
|
|
|
a.reset(); // frees [0, 400): the waiter wraps round to the start
|
|
auto c = f.get();
|
|
REQUIRE(c);
|
|
CHECK(c->data == a_data);
|
|
CHECK(ring.GetUtilization() == 800);
|
|
// Never two live regions on the same bytes
|
|
CHECK((b->data + 400 <= c->data || c->data + 400 <= b->data));
|
|
}
|
|
|
|
TEST_CASE("ReceiveRingBuffer_full_ring_blocks", "[ImagePuller][Unit]") {
|
|
ReceiveRingBuffer ring(1000);
|
|
auto a = ring.Allocate(500);
|
|
auto b = ring.Allocate(500); // tail has come round to head
|
|
REQUIRE(a);
|
|
REQUIRE(b);
|
|
uint8_t *a_data = a->data;
|
|
auto f = std::async(std::launch::async, [&] { return ring.Allocate(1); });
|
|
CHECK(f.wait_for(50ms) == std::future_status::timeout);
|
|
a.reset();
|
|
auto c = f.get();
|
|
REQUIRE(c);
|
|
CHECK(c->data == a_data);
|
|
}
|
|
|
|
TEST_CASE("ReceiveRingBuffer_out_of_order_release_reclaims_in_order", "[ImagePuller][Unit]") {
|
|
ReceiveRingBuffer ring(1000);
|
|
auto a = ring.Allocate(500);
|
|
auto b = ring.Allocate(500);
|
|
REQUIRE(a);
|
|
REQUIRE(b);
|
|
|
|
b.reset(); // the newer region goes first - as republish may do
|
|
CHECK(ring.GetUtilization() == 1000); // its bytes are behind a's and cannot be reused yet
|
|
|
|
auto f = std::async(std::launch::async, [&] { return ring.Allocate(600); });
|
|
CHECK(f.wait_for(50ms) == std::future_status::timeout);
|
|
|
|
a.reset(); // now both are reclaimed
|
|
auto c = f.get();
|
|
REQUIRE(c);
|
|
CHECK(ring.GetUtilization() == 600);
|
|
}
|
|
|
|
TEST_CASE("ReceiveRingBuffer_oversized_message_goes_to_heap", "[ImagePuller][Unit]") {
|
|
ReceiveRingBuffer ring(100);
|
|
auto small = ring.Allocate(100);
|
|
REQUIRE(small);
|
|
// Does not wait for the full ring - it never fits in it
|
|
auto big = ring.Allocate(1000);
|
|
REQUIRE(big);
|
|
REQUIRE(big->size == 1000);
|
|
big->data[999] = 1;
|
|
CHECK(ring.GetUtilization() == 1100);
|
|
CHECK(ring.GetMaxUtilization() == 1100);
|
|
big.reset();
|
|
CHECK(ring.GetUtilization() == 100);
|
|
}
|
|
|
|
TEST_CASE("ReceiveRingBuffer_zero_size", "[ImagePuller][Unit]") {
|
|
ReceiveRingBuffer ring(100);
|
|
auto a = ring.Allocate(0);
|
|
auto b = ring.Allocate(100);
|
|
REQUIRE(a);
|
|
REQUIRE(b);
|
|
CHECK(a->size == 0);
|
|
a.reset();
|
|
CHECK(ring.GetUtilization() == 100);
|
|
b.reset();
|
|
CHECK(ring.GetUtilization() == 0);
|
|
}
|
|
|
|
TEST_CASE("ReceiveRingBuffer_stop_releases_waiter", "[ImagePuller][Unit]") {
|
|
ReceiveRingBuffer ring(100);
|
|
auto a = ring.Allocate(100);
|
|
REQUIRE(a);
|
|
auto f = std::async(std::launch::async, [&] { return ring.Allocate(50); });
|
|
CHECK(f.wait_for(50ms) == std::future_status::timeout);
|
|
ring.Stop();
|
|
CHECK(f.get() == nullptr);
|
|
}
|
|
|
|
TEST_CASE("ReceiveRingBuffer_region_outlives_ring", "[ImagePuller][Unit]") {
|
|
std::shared_ptr<ReceiveBufferRegion> r;
|
|
{
|
|
ReceiveRingBuffer ring(100);
|
|
r = ring.Allocate(50);
|
|
}
|
|
REQUIRE(r);
|
|
r->data[49] = 1;
|
|
r.reset();
|
|
}
|