Live stills: a frame with no free slot during the pre-pass waits as its compressed bytes

Every frame that arrives before the pre-pass is over waits for the analysis, and each took a slot
of the image buffer, sized for an uncompressed frame plus metadata (24 MB on a 9M detector for a
frame of ~1.5 MB compressed). Not blocking, the frames past the buffer were dropped: on the mock,
9M, 2 cards, 1500 frames at ~140 Hz, a 13-17 s pre-pass, 683 of 1186-1400 collected were sent.

A frame that finds no slot is now held as a copy of its compressed bytes, up to the image buffer's
size again; a slot still comes first (the frame can be seen there while it waits), and the frame
is sent from a fresh slot after its analysis as before. Same mock run: 1462-1489 of 1462-1489
collected sent (the rest are the mock puller's own module drops), peak RSS 27.1 -> 27.7 GB.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01UqXYdHHZ4cogS1mrGUtfN7
This commit is contained in:
leonarski_fandClaude Opus 5.5 committed 2026-11-18 11:49:09 +01:00
1 parent 466edb7415
commit 702b8d4a68
4 files changed
+60 -31

No files matched your search

+45 -28
View File
@@ -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<HeldImage>();
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<int64_t>(size)) + static_cast<int64_t>(size) <= copy_budget) {
held->copy.assign(message.image.GetCompressed(), message.image.GetCompressed() + size);
held->copy_bytes = &copy_bytes;
} else
copy_bytes -= static_cast<int64_t>(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<uint8_t *>(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<size_t>(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<uint8_t *>(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<size_t>(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);
}
+5
View File
@@ -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<int64_t> copy_bytes = 0;
std::mutex stills_mutex;
bool cut = false;
+7 -1
View File
@@ -3,6 +3,7 @@
#pragma once
#include <atomic>
#include <map>
#include <memory>
#include <vector>
@@ -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<uint8_t> copy;
std::atomic<int64_t> *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<int64_t>(copy.size());
}
};
+3 -2
View File
@@ -663,10 +663,11 @@ TEST_CASE("JFJochReceiverRugnux_Stills_BufferFullDuringPrePass", "[JFJochReceive
CHECK(!out.status.cancelled);
CHECK(pusher.Events() == std::vector<std::string>{"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);