diff --git a/receiver/JFJochReceiverRugnuxStills.cpp b/receiver/JFJochReceiverRugnuxStills.cpp index 3f237f97c..b80066f07 100644 --- a/receiver/JFJochReceiverRugnuxStills.cpp +++ b/receiver/JFJochReceiverRugnuxStills.cpp @@ -48,6 +48,7 @@ JFJochReceiverRugnuxStills::~JFJochReceiverRugnuxStills() { void JFJochReceiverRugnuxStills::OnStart(std::chrono::steady_clock::time_point received) { const int64_t slots = image_buffer.GetStatus().total_slots; stills_keep_free = std::min(stills_nthreads, slots / 2); + copy_budget = slots * experiment.GetImageBufferLocationSize(); const StartMessage start = MakeStartMessage(experiment, pixel_mask, *az_int_mapping); // The frames are shown from their arrival, on the stated geometry until the pre-pass has decided its own. // To the image buffer only: the writer and the ZeroMQ streams get the one start message, after the pre-pass. @@ -82,43 +83,59 @@ void JFJochReceiverRugnuxStills::OnImage(uint32_t id, DataMessage &message, std: CountImage(message); const size_t size = message.image.GetCompressedSize(); - // Waits for the analysis in a slot of the image buffer; a slot is kept free for each analysis thread to send with + // Waits for the analysis in a slot of the image buffer, where it can be seen while it waits (it is not sent from + // there); a slot is kept free for each analysis thread to send with. Without a slot, in a copy of its own. auto held = std::make_shared(); - held->slot = image_buffer.GetImageSlot(stills_keep_free); - while ((held->slot == nullptr) && image_puller.IsBlocking() && !cancelled) { - std::this_thread::sleep_for(std::chrono::milliseconds(1)); + const auto hold = [&] { held->slot = image_buffer.GetImageSlot(stills_keep_free); + if (held->slot == nullptr) { + if (copy_bytes.fetch_add(static_cast(size)) + static_cast(size) <= copy_budget) { + held->copy.assign(message.image.GetCompressed(), message.image.GetCompressed() + size); + held->copy_bytes = ©_bytes; + } else + copy_bytes -= static_cast(size); + } + return held->slot != nullptr || held->copy_bytes != nullptr; + }; + bool held_ok = hold(); + while (!held_ok && image_puller.IsBlocking() && !cancelled) { + std::this_thread::sleep_for(std::chrono::milliseconds(1)); + held_ok = hold(); } - if (held->slot == nullptr) { + if (!held_ok) { writer_queue_full = true; return; } held->message = message; StampImage(held->message, image_puller.GetCurrentFifoUtilization()); - // In the slot as an image without analysis, so that it can be seen while it waits; it is not sent. The - // analysis reads the image where it is in the slot. - auto slot_buffer = static_cast(held->slot->GetImage()); - CBORStream2Serializer serializer(slot_buffer, experiment.GetImageBufferLocationSize()); - held->message.image = CompressedImage(nullptr, 0, message.image.GetWidth(), message.image.GetHeight(), - message.image.GetMode(), message.image.GetCompressionAlgorithm(), - message.image.GetChannel()); - serializer.SerializeImage(held->message); - const size_t image_offset = serializer.GetImageAppendOffset(); - if (image_offset + size + 32 >= static_cast(experiment.GetImageBufferLocationSize())) { - logger.Error("Dropping image {} - metadata {} B + image {} B do not fit in buffer slot {} B", message.number, - image_offset, size, experiment.GetImageBufferLocationSize()); - return; - } - memcpy(slot_buffer + image_offset, message.image.GetCompressed(), size); - serializer.AppendImage(size); - held->message.image = CompressedImage(slot_buffer + image_offset, size, message.image.GetWidth(), - message.image.GetHeight(), message.image.GetMode(), - message.image.GetCompressionAlgorithm(), message.image.GetChannel()); - held->slot->SetImageNumber(message.number); - held->slot->SetImageSize(serializer.GetBufferSize()); - held->slot->SetIndexed(false); - held->slot->ReadyToSend(); + if (held->slot) { + // In the slot as an image without analysis; the analysis reads the image where it is in the slot. + auto slot_buffer = static_cast(held->slot->GetImage()); + CBORStream2Serializer serializer(slot_buffer, experiment.GetImageBufferLocationSize()); + held->message.image = CompressedImage(nullptr, 0, message.image.GetWidth(), message.image.GetHeight(), + message.image.GetMode(), message.image.GetCompressionAlgorithm(), + message.image.GetChannel()); + serializer.SerializeImage(held->message); + const size_t image_offset = serializer.GetImageAppendOffset(); + if (image_offset + size + 32 >= static_cast(experiment.GetImageBufferLocationSize())) { + logger.Error("Dropping image {} - metadata {} B + image {} B do not fit in buffer slot {} B", message.number, + image_offset, size, experiment.GetImageBufferLocationSize()); + return; + } + memcpy(slot_buffer + image_offset, message.image.GetCompressed(), size); + serializer.AppendImage(size); + held->message.image = CompressedImage(slot_buffer + image_offset, size, message.image.GetWidth(), + message.image.GetHeight(), message.image.GetMode(), + message.image.GetCompressionAlgorithm(), message.image.GetChannel()); + held->slot->SetImageNumber(message.number); + held->slot->SetImageSize(serializer.GetBufferSize()); + held->slot->SetIndexed(false); + held->slot->ReadyToSend(); + } else + held->message.image = CompressedImage(held->copy.data(), size, message.image.GetWidth(), + message.image.GetHeight(), message.image.GetMode(), + message.image.GetCompressionAlgorithm(), message.image.GetChannel()); stills->Hold(held); } diff --git a/receiver/JFJochReceiverRugnuxStills.h b/receiver/JFJochReceiverRugnuxStills.h index 1a4ab1b94..3f4d8ba4e 100644 --- a/receiver/JFJochReceiverRugnuxStills.h +++ b/receiver/JFJochReceiverRugnuxStills.h @@ -21,6 +21,11 @@ class JFJochReceiverRugnuxStills : public JFJochReceiverStream, public StillsSin IndexerThreadPool *const indexing_thread_pool; const int64_t stills_nthreads; int64_t stills_keep_free = 0; // slots frames do not take, for the analysis threads to send with + // Frames that find no free slot while they wait for the analysis are held as their compressed bytes, up to the + // image buffer's size again: the pre-pass holds every frame that arrives before it is over, and a frame is a + // fraction of a slot (1.5 MB of a 24 MB slot on a 9M detector) + int64_t copy_budget = 0; + std::atomic copy_bytes = 0; std::mutex stills_mutex; bool cut = false; diff --git a/receiver/StillsPrePass.h b/receiver/StillsPrePass.h index 33b564eb7..2a3765664 100644 --- a/receiver/StillsPrePass.h +++ b/receiver/StillsPrePass.h @@ -3,6 +3,7 @@ #pragma once +#include #include #include #include @@ -15,10 +16,13 @@ #include "../image_analysis/IndexAndRefine.h" // StillsWindow // An image held until analysed, pointing into `slot`, which goes back unsent with the last copy. The slot is -// forgotten then, so that the copy sent with the analysis is the only one the image buffer finds. +// forgotten then, so that the copy sent with the analysis is the only one the image buffer finds. Where no slot +// was free, the image points into `copy` instead, its compressed bytes, counted in `copy_bytes` while held. struct HeldImage { DataMessage message; ZeroCopyReturnValue *slot = nullptr; + std::vector copy; + std::atomic *copy_bytes = nullptr; HeldImage() = default; HeldImage(const HeldImage &) = delete; @@ -26,6 +30,8 @@ struct HeldImage { ~HeldImage() { if (slot) slot->discard(); + if (copy_bytes) + *copy_bytes -= static_cast(copy.size()); } }; diff --git a/tests/JFJochReceiverRugnuxTest.cpp b/tests/JFJochReceiverRugnuxTest.cpp index 18789f6c5..167167439 100644 --- a/tests/JFJochReceiverRugnuxTest.cpp +++ b/tests/JFJochReceiverRugnuxTest.cpp @@ -663,10 +663,11 @@ TEST_CASE("JFJochReceiverRugnux_Stills_BufferFullDuringPrePass", "[JFJochReceive CHECK(!out.status.cancelled); CHECK(pusher.Events() == std::vector{"start", "image", "end"}); - // A frame that finds no free slot is not written, and the collection says so. + // A frame that finds no free slot waits in a copy of its own, and is written. CHECK(out.status.images_collected == images); CHECK(out.efficiency == 1.0f); - CHECK(out.writer_queue_full_warning == (out.status.images_sent < images)); + CHECK(out.status.images_sent == images); + CHECK(!out.writer_queue_full_warning); CHECK(out.scan_result.images.size() == out.status.images_sent); REQUIRE(pusher.End()); CHECK(pusher.End()->images_collected_count == images);