Live stills: the frames waiting for the analysis inside the image buffer, at most half of it
Lane l's hold (702b8d4a6) kept a frame that found no free slot as a heap copy
of its compressed bytes, up to the image buffer's size again - up to twice the
configured image memory. Now every waiting frame is held as its compressed bytes
packed into slots of the image buffer (HoldArena): several frames to a slot, at
most half of the slots, the slots kept for the analysis threads to send with
left alone; a slot is reused once its frames are gone, and the slots go back
before the image buffer is checked at the end. A frame that finds the waiting
half full is dropped and counted as before (blocking: waits). The receiver's
image memory is the image buffer (image_buffer_MiB of the broker, -B of the
stills test) and nothing beside it; the broker takes the same receiver.
The frames are no longer serialised into a slot of their own while they wait,
so the broker's image routes show a frame once it is analysed.
HoldArenaTest: half of the slots at most, three frames of a third of a slot
each to a slot, nothing larger than a slot, a freed slot reused, all slots
back after ReleaseSlots. jfjoch_fpga_stills_test prints the process's maximum
resident memory.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01UqXYdHHZ4cogS1mrGUtfN7
This commit is contained in:
1 parent
bb6354252c
commit
38137e16a0
8 files changed
+168
-65
No files matched your search
@@ -373,9 +373,12 @@ 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, in all its slots but
|
||||
one per analysis thread, kept for sending; a frame that finds it 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
|
||||
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
|
||||
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
|
||||
waits for slots to come back. A pre-pass that fails leaves the stated geometry and
|
||||
mask in use. Where the start message cannot be sent after the pre-pass (no writer listening) or the analysis
|
||||
cannot be set up, the collection ends with that error, reported by `/status` and `/wait_till_done`, and the
|
||||
|
||||
@@ -0,0 +1,90 @@
|
||||
// SPDX-FileCopyrightText: 2026 Filip Leonarski, Paul Scherrer Institute <filip.leonarski@psi.ch>
|
||||
// SPDX-License-Identifier: GPL-3.0-only
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
#include <functional>
|
||||
#include <mutex>
|
||||
#include <vector>
|
||||
|
||||
#include "../common/ImageBuffer.h"
|
||||
#include "../common/ZeroCopyReturnValue.h"
|
||||
|
||||
// The frames waiting for the analysis, as their compressed bytes packed into slots of the image buffer: inside its
|
||||
// memory, at most `max_slots` of its slots (half of them in the stills receiver), and the slots kept free for sending
|
||||
// left alone. A slot is reused once the frames in it are all released; with every slot taken and full, Allocate fails.
|
||||
class HoldArena {
|
||||
struct Chunk {
|
||||
ZeroCopyReturnValue *slot;
|
||||
size_t used = 0;
|
||||
int64_t frames = 0;
|
||||
};
|
||||
ImageBuffer &image_buffer;
|
||||
const int64_t max_slots;
|
||||
const int64_t keep_free;
|
||||
const size_t slot_size;
|
||||
std::mutex m;
|
||||
std::vector<Chunk> chunks;
|
||||
size_t current = SIZE_MAX;
|
||||
public:
|
||||
struct Region {
|
||||
uint8_t *data = nullptr;
|
||||
std::function<void()> release; // once the frame is gone
|
||||
};
|
||||
|
||||
HoldArena(ImageBuffer &in_image_buffer, int64_t in_max_slots, int64_t in_keep_free, size_t in_slot_size)
|
||||
: image_buffer(in_image_buffer), max_slots(in_max_slots), keep_free(in_keep_free), slot_size(in_slot_size) {}
|
||||
|
||||
~HoldArena() { ReleaseSlots(); }
|
||||
|
||||
// The slots go back to the image buffer: once no frame is held any more
|
||||
void ReleaseSlots() {
|
||||
std::unique_lock ul(m);
|
||||
for (auto &c: chunks)
|
||||
if (c.slot) {
|
||||
c.slot->discard();
|
||||
c.slot = nullptr;
|
||||
}
|
||||
current = SIZE_MAX;
|
||||
}
|
||||
|
||||
// Empty where there is no room
|
||||
Region Allocate(size_t size) {
|
||||
std::unique_lock ul(m);
|
||||
if (size > slot_size)
|
||||
return {};
|
||||
if (current == SIZE_MAX || chunks[current].used + size > slot_size) {
|
||||
current = SIZE_MAX;
|
||||
for (size_t i = 0; i < chunks.size(); i++)
|
||||
if (chunks[i].slot && chunks[i].frames == 0) {
|
||||
chunks[i].used = 0;
|
||||
current = i;
|
||||
break;
|
||||
}
|
||||
if (current == SIZE_MAX && static_cast<int64_t>(chunks.size()) < max_slots) {
|
||||
if (auto slot = image_buffer.GetImageSlot(keep_free)) {
|
||||
chunks.push_back(Chunk{.slot = slot});
|
||||
current = chunks.size() - 1;
|
||||
}
|
||||
}
|
||||
if (current == SIZE_MAX)
|
||||
return {};
|
||||
}
|
||||
Chunk &c = chunks[current];
|
||||
Region ret{.data = static_cast<uint8_t *>(c.slot->GetImage()) + c.used};
|
||||
c.used += size;
|
||||
c.frames++;
|
||||
const size_t index = current;
|
||||
ret.release = [this, index] {
|
||||
std::unique_lock ul2(m);
|
||||
chunks[index].frames--;
|
||||
};
|
||||
return ret;
|
||||
}
|
||||
|
||||
int64_t GetSlotsUsed() {
|
||||
std::unique_lock ul(m);
|
||||
return static_cast<int64_t>(chunks.size());
|
||||
}
|
||||
};
|
||||
@@ -52,8 +52,10 @@ 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();
|
||||
// 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<HoldArena>(image_buffer, slots / 2, 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.
|
||||
@@ -93,59 +95,24 @@ void JFJochReceiverRugnuxStills::OnImage(uint32_t id, DataMessage &message, std:
|
||||
}
|
||||
const size_t size = message.image.GetCompressedSize();
|
||||
|
||||
// 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.
|
||||
// Waits for the analysis as its compressed bytes, packed into slots of the image buffer (HoldArena)
|
||||
auto held = std::make_shared<HeldImage>();
|
||||
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 = ©_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) {
|
||||
HoldArena::Region region = hold_arena->Allocate(size);
|
||||
while (!region.data && image_puller.IsBlocking() && !cancelled) {
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(1));
|
||||
held_ok = hold();
|
||||
region = hold_arena->Allocate(size);
|
||||
}
|
||||
if (!held_ok) {
|
||||
if (!region.data) {
|
||||
writer_queue_full = true;
|
||||
return;
|
||||
}
|
||||
memcpy(region.data, message.image.GetCompressed(), size);
|
||||
held->release = std::move(region.release);
|
||||
held->message = message;
|
||||
StampImage(held->message, image_puller.GetCurrentFifoUtilization());
|
||||
|
||||
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());
|
||||
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);
|
||||
}
|
||||
|
||||
@@ -159,6 +126,8 @@ void JFJochReceiverRugnuxStills::FinishCollection() {
|
||||
} catch (const std::exception &) {
|
||||
error = std::current_exception();
|
||||
}
|
||||
// Every frame is analysed: the slots the frames waited in go back, before the image buffer is checked
|
||||
hold_arena->ReleaseSlots();
|
||||
|
||||
current_status.SetProgress(1.0);
|
||||
current_status.SetStatus(GetStatus());
|
||||
|
||||
@@ -9,6 +9,7 @@
|
||||
|
||||
#include "JFJochReceiverStream.h"
|
||||
#include "RugnuxLiveStatus.h"
|
||||
#include "HoldArena.h"
|
||||
#include "StillsGPUAnalysis.h"
|
||||
|
||||
// A serial collection analysed frame by frame at the geometry of a pre-pass, which the start message waits for
|
||||
@@ -21,12 +22,10 @@ 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;
|
||||
// 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
|
||||
std::unique_ptr<HoldArena> hold_arena;
|
||||
|
||||
std::mutex stills_mutex;
|
||||
bool cut = false;
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
#pragma once
|
||||
|
||||
#include <atomic>
|
||||
#include <functional>
|
||||
#include <map>
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
@@ -15,14 +16,13 @@
|
||||
#include "../common/ZeroCopyReturnValue.h"
|
||||
#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. Where no slot
|
||||
// was free, the image points into `copy` instead, its compressed bytes, counted in `copy_bytes` while held.
|
||||
// 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), or into bytes
|
||||
// `release` gives back with the last copy (HoldArena).
|
||||
struct HeldImage {
|
||||
DataMessage message;
|
||||
ZeroCopyReturnValue *slot = nullptr;
|
||||
std::vector<uint8_t> copy;
|
||||
std::atomic<int64_t> *copy_bytes = nullptr;
|
||||
std::function<void()> release;
|
||||
|
||||
HeldImage() = default;
|
||||
HeldImage(const HeldImage &) = delete;
|
||||
@@ -30,8 +30,8 @@ struct HeldImage {
|
||||
~HeldImage() {
|
||||
if (slot)
|
||||
slot->discard();
|
||||
if (copy_bytes)
|
||||
*copy_bytes -= static_cast<int64_t>(copy.size());
|
||||
if (release)
|
||||
release();
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -156,6 +156,7 @@ ADD_EXECUTABLE(jfjoch_test
|
||||
JFJochReceiverRugnuxTest.cpp
|
||||
FPGAImagePullerTest.cpp
|
||||
StillsSelectionTest.cpp
|
||||
HoldArenaTest.cpp
|
||||
GridScanSettingsTest.cpp
|
||||
JFJochReceiverPlotsTest.cpp
|
||||
GoniometerAxisTest.cpp
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
// 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 "../receiver/HoldArena.h"
|
||||
|
||||
TEST_CASE("HoldArena_WithinHalfOfTheImageBuffer", "[HoldArena]") {
|
||||
constexpr size_t slot_size = 1024 * 1024;
|
||||
ImageBuffer buffer(64 * slot_size);
|
||||
buffer.StartMeasurement(slot_size);
|
||||
const int64_t total = buffer.GetStatus().total_slots;
|
||||
REQUIRE(total >= 32);
|
||||
|
||||
std::vector<HoldArena::Region> held;
|
||||
{
|
||||
HoldArena arena(buffer, total / 2, 4, slot_size);
|
||||
// Frames of a third of a slot: three to a slot, half of the slots at most
|
||||
while (true) {
|
||||
auto r = arena.Allocate(slot_size / 3);
|
||||
if (!r.data)
|
||||
break;
|
||||
held.push_back(std::move(r));
|
||||
}
|
||||
CHECK(arena.GetSlotsUsed() == total / 2);
|
||||
CHECK(held.size() == static_cast<size_t>(3 * (total / 2)));
|
||||
CHECK(buffer.GetSlotStatus().available_slots == total - total / 2);
|
||||
// Larger than a slot: never held
|
||||
CHECK(!arena.Allocate(slot_size + 1).data);
|
||||
|
||||
// A slot whose frames are all gone is reused, nothing more is taken from the buffer
|
||||
for (int i = 0; i < 3; i++)
|
||||
held[i].release();
|
||||
CHECK(arena.Allocate(slot_size / 3).data != nullptr);
|
||||
CHECK(arena.GetSlotsUsed() == total / 2);
|
||||
|
||||
arena.ReleaseSlots();
|
||||
CHECK(buffer.GetSlotStatus().available_slots == total);
|
||||
}
|
||||
}
|
||||
@@ -258,9 +258,10 @@ bool RunDataset(Logger &logger, AcquisitionDeviceGroup &aq_devices, const std::s
|
||||
output.status.images_dropped, output.efficiency);
|
||||
logger.Info("Delivered {:.1f} Hz (first to last image {:.2f} s), source blocked {:.1f}% of it",
|
||||
delivered_hz, delivery_s, 100.0 * blocked);
|
||||
logger.Info("Sustained {:.1f} Hz (images / wall time {:.2f} s, start to analysis done) CPU {:.1f} s = {:.1f} cores",
|
||||
logger.Info("Sustained {:.1f} Hz (images / wall time {:.2f} s, start to analysis done) CPU {:.1f} s = {:.1f} cores"
|
||||
" max resident memory {:.1f} GiB",
|
||||
static_cast<double>(output.status.images_collected) / wall_time, wall_time, cpu_time,
|
||||
cpu_time / wall_time);
|
||||
cpu_time / wall_time, static_cast<double>(usage_end.ru_maxrss) / (1024.0 * 1024.0));
|
||||
// The card's own count of the cycles (200 MHz) it waited for a host buffer, i.e. was held by the host
|
||||
for (int i = 0; i < x.GetDataStreamsNum(); i++) {
|
||||
const auto status = aq_devices[i].GetDataCollectionStatus();
|
||||
|
||||
Reference in new issue
Block a user