From 4c9cc48382bad97db2e05bac5197db6ff19de1e6 Mon Sep 17 00:00:00 2001 From: Filip Leonarski Date: Wed, 18 Nov 2026 17:10:32 +0100 Subject: [PATCH] Live stills hold: frames in a slot of their own in a quarter of the image buffer, seen while they wait 38137e16a packed every waiting frame into the hold, so a frame was no longer in the image buffer by its number until analysed (JFJochReceiverRugnux_Stills_ ImagesVisibleDuringPrePass failed). The waiting half of the image buffer is now split: up to a quarter of the slots hold a frame each, serialised there as before (the image routes find it), the rest of the half packs frames as their compressed bytes (HoldArena). The bound is unchanged: at most half of the image buffer, nothing beside it. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01UqXYdHHZ4cogS1mrGUtfN7 --- docs/JFJOCH_BROKER.md | 7 +-- receiver/JFJochReceiverRugnuxStills.cpp | 67 ++++++++++++++++++++----- receiver/JFJochReceiverRugnuxStills.h | 9 ++-- 3 files changed, 65 insertions(+), 18 deletions(-) diff --git a/docs/JFJOCH_BROKER.md b/docs/JFJOCH_BROKER.md index b1c77b14d..43e3b31f4 100644 --- a/docs/JFJOCH_BROKER.md +++ b/docs/JFJOCH_BROKER.md @@ -373,9 +373,10 @@ of its frames indexed, as with a small image buffer, a low hit rate or a chip wh cell given), the distance in the header is used for the whole run: this is a warning in the log and a note of the live status, and a wrong header distance then costs resolution and indexed frames. -Frames arriving during the pre-pass, or faster than the analysis, wait in the image buffer as their compressed bytes, -packed into at most half of its slots (several frames to a slot); the other half is for the analysed frames, which go -to the writer from a slot each, with one slot kept per analysis thread. The receiver's image memory is the image +Frames arriving during the pre-pass, or faster than the analysis, wait in at most half of the image buffer: a quarter +of its slots hold a frame each (it can be seen through the image routes while it waits), the rest of that half +holds frames as their compressed bytes, several to a slot; the other half is for the analysed frames, which go to +the writer from a slot each, with one slot kept per analysis thread. The receiver's image memory is the image buffer (`image_buffer_MiB`, `-B` of `jfjoch_fpga_stills_test`) and nothing beside it. A frame that finds the waiting half full is dropped and counted as for a full image buffer, so analysis slower than the frame rate costs frames, not memory. Where the writer lags behind, the analysis diff --git a/receiver/JFJochReceiverRugnuxStills.cpp b/receiver/JFJochReceiverRugnuxStills.cpp index 82dc8c559..809e0b64d 100644 --- a/receiver/JFJochReceiverRugnuxStills.cpp +++ b/receiver/JFJochReceiverRugnuxStills.cpp @@ -54,8 +54,10 @@ void JFJochReceiverRugnuxStills::OnStart(std::chrono::steady_clock::time_point r const int64_t slots = image_buffer.GetStatus().total_slots; // The frames that wait take at most half of the image buffer; the other half is for those analysed, sent from // a slot each, and a slot is kept free for each analysis thread to send with - const int64_t keep_free = std::min(stills_nthreads, slots / 2); - hold_arena = std::make_unique(image_buffer, slots / 2, keep_free, experiment.GetImageBufferLocationSize()); + stills_keep_free = std::min(stills_nthreads, slots / 2); + visible_max = slots / 4; + hold_arena = std::make_unique(image_buffer, slots / 2 - visible_max, stills_keep_free, + 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. @@ -90,26 +92,67 @@ void JFJochReceiverRugnuxStills::OnImage(uint32_t id, DataMessage &message, std: CountImage(message); const size_t size = message.image.GetCompressedSize(); - // Waits for the analysis as its compressed bytes, packed into slots of the image buffer (HoldArena) + // Waits for the analysis in half of the image buffer at most: in a slot of its own, where it can be seen while it + // waits (it is not sent from there), in up to half of those slots; else as its compressed bytes, packed with + // others into the rest (HoldArena) auto held = std::make_shared(); - HoldArena::Region region = hold_arena->Allocate(size); - while (!region.data && image_puller.IsBlocking() && !cancelled) { - std::this_thread::sleep_for(std::chrono::milliseconds(1)); + HoldArena::Region region; + const auto hold = [&] { + if (visible_held.fetch_add(1) < visible_max) { + held->slot = image_buffer.GetImageSlot(stills_keep_free); + if (held->slot) { + held->release = [this] { --visible_held; }; + return true; + } + } + --visible_held; region = hold_arena->Allocate(size); + return region.data != 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 (!region.data) { + if (!held_ok) { writer_queue_full = true; return; } - memcpy(region.data, message.image.GetCompressed(), size); - held->release = std::move(region.release); // Not chosen (stills_downsample): written as it came, without its analysis held->analyse = StillsImageSelected(message.number, downsample, StillsGPUAnalysis::PREPASS_MAX_IMAGES); held->message = message; StampImage(held->message, image_puller.GetCurrentFifoUtilization()); - held->message.image = CompressedImage(region.data, size, message.image.GetWidth(), message.image.GetHeight(), - message.image.GetMode(), message.image.GetCompressionAlgorithm(), - message.image.GetChannel()); + + 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 { + memcpy(region.data, message.image.GetCompressed(), size); + held->release = std::move(region.release); + held->message.image = CompressedImage(region.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 7227e2fe4..57d5c09ee 100644 --- a/receiver/JFJochReceiverRugnuxStills.h +++ b/receiver/JFJochReceiverRugnuxStills.h @@ -22,9 +22,12 @@ class JFJochReceiverRugnuxStills : public JFJochReceiverStream, public StillsSin IndexerThreadPool *const indexing_thread_pool; const int64_t stills_nthreads; - // The frames waiting for the analysis, packed as their compressed bytes into at most half of the image - // buffer's slots: a frame is a fraction of a slot (1.5 MB of a 24 MB slot on a 9M detector), and the pre-pass - // holds every frame that arrives before it is over + // The frames waiting for the analysis take at most half of the image buffer's slots: a quarter as a slot each, + // the rest packed as their compressed bytes (a frame is a fraction of a slot: 1.5 MB of a 24 MB slot on a 9M + // detector, and the pre-pass holds every frame that arrives before it is over) + int64_t stills_keep_free = 0; // slots frames do not take, for the analysis threads to send with + int64_t visible_max = 0; + std::atomic visible_held = 0; std::unique_ptr hold_arena; std::mutex stills_mutex;