Build Packages / Build documentation (push) Successful in 45s
Build Packages / build:portable:windows-x86_64:cuda (push) Successful in 28m34s
Build Packages / Create release (push) Successful in 8s
Build Packages / build:portable:macos-arm64:nocuda (push) Successful in 8m55s
Build Packages / build:rugnux:linux-aarch64:cuda (push) Successful in 11m11s
Build Packages / build:portable:linux-x86_64:nocuda (push) Successful in 16m51s
Build Packages / build:portable:linux-x86_64:cuda (push) Successful in 22m42s
Build Packages / build:portable:windows-x86_64:nocuda (push) Successful in 24m46s
Build Packages / build:jfjoch:ubuntu2204:nocuda (push) Successful in 26m18s
Build Packages / HDF5 consumer tests (DIALS, XDS) (push) Successful in 31m0s
Build Packages / build:jfjoch:rocky8:nocuda (push) Successful in 31m16s
Build Packages / build:jfjoch:rocky9:nocuda (push) Successful in 32m15s
Build Packages / build:jfjoch:ubuntu2404:nocuda (push) Successful in 27m12s
Build Packages / Generate python client (push) Successful in 30s
Build Packages / build:jfjoch:rocky8:cuda-sls9 (push) Successful in 30m40s
Build Packages / build:jfjoch:rocky9:cuda-sls9 (push) Successful in 33m20s
Build Packages / build:jfjoch:rocky8:cuda (push) Successful in 31m3s
Build Packages / build:jfjoch:ubuntu2204:cuda (push) Successful in 27m5s
Build Packages / build:jfjoch:ubuntu2404:cuda (push) Successful in 30m24s
Build Packages / build:jfjoch:rocky9:cuda (push) Successful in 32m54s
Build Packages / Unit tests (push) Failing after 1h53m52s
Build Packages / Validation Battery (push) Successful in 46m47s
Comments say what the next line does or why; names follow the rest of the receiver: <thing>_mutex, thread bodies <What>Thread (FPGAImagePuller: AcquireThread, ModuleThread, CompressThread, FinalizeMeasurement), no single letters for objects. Code does not change. JFJochReceiverFPGA and what the style review's "leave alone" list names (deleters, exception_ptr, the condition variables, LiveModel) stay as they were. Moved here from the comments: Pre-pass size: of 100 frames of a collection indexed at 18%, 7 indexed at a distance 2.6% off and the geometry refinement did not run; of 500, 34 did and refined it to 0.4%. With the broker's default fixed threshold 3 points fewer frames index than with the adaptive one the analysis takes. Preflight sends FillMessage alone, measured on a JUNGFRAU 9M at 1.4 kB against 543 kB for the start message, and the whole of it rather than the fields the check reads today, so the writer sees the description it will be given at start. FFT indexers and cuFFT plans are kept for the life of the broker because every pass of every collection builds the same pools, and making a plan takes a process-wide lock and took over a second on a card busy with the online analysis. The host memory budget subtracts the image buffer, which has to keep the whole collection and so is most of the machine; at worst what could run side by side runs one part after the other. The receiver is released before the indexer pool is rebuilt because a fresh GPU indexer next to a stale receiver (after a failed start the receiver is stopped but not destroyed until the next Start) can fail to initialise. ReceiveRingBuffer: one image of a large detector is tens of megabytes, so a queue of a thousand of them is a quantity of memory nobody chose. Regions are handed out and reclaimed in arrival order; a region released early is only remembered, and republish holds at most its queue's worth, so that is as far as the reclaim can lag. A message larger than the whole ring goes on the heap, so an oversized START or END cannot wedge the receiver. Republish is capped at a share of the ring because it keeps the oldest unsent images and would otherwise pin everything received after them, running the writer at the republish rate. ZMQImagePuller: with a full ring the broker's pusher waits one second, then drops the image and sends without waiting for the rest of the run, so a ring too small for the writer's stalls costs images. TCPImagePuller: a full ring blocks the pusher's send; its BUSY heartbeats tell it apart from a dead writer, and it gives up only after a minute in which no image was written. Keep-alive detection is about 60 s, both sides. Stills model step: the step on a provisional merge still running when the next merge is made is cut, one not started is replaced; a molecular replacement or a refinement not started is left out at the deadline, a refinement running is cut short; each step starts from the last placement. Without a space group stated the frames are merged in the model's where its cell is the run's consensus, otherwise the model is left out. Rotation, stills and stills with a model end to end against the build before this work: MTZ data columns, both maps, placed and refined models and blobs are byte for byte the same; only the MTZ history's build stamp differs. Scan results are the same with times stripped. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01HVeDQsU5a8AJUdJPgWevAK
142 lines
4.4 KiB
C++
142 lines
4.4 KiB
C++
// SPDX-FileCopyrightText: 2026 Filip Leonarski, Paul Scherrer Institute <filip.leonarski@psi.ch>
|
|
// SPDX-License-Identifier: GPL-3.0-only
|
|
|
|
#include "ReceiveRingBuffer.h"
|
|
|
|
#include <algorithm>
|
|
#include <optional>
|
|
|
|
struct ReceiveRingBuffer::State {
|
|
// Uninitialised: a ring that is never filled should not cost its capacity in resident memory
|
|
std::unique_ptr<uint8_t[]> memory;
|
|
const size_t capacity;
|
|
|
|
std::mutex state_mutex;
|
|
std::condition_variable cv;
|
|
|
|
// Live regions in arrival order; only the oldest can give back space
|
|
struct Slot {
|
|
size_t offset;
|
|
size_t size;
|
|
bool released = false;
|
|
};
|
|
std::deque<Slot> slots;
|
|
// Next byte to hand out; the oldest live byte is slots.front().offset
|
|
size_t tail = 0;
|
|
|
|
size_t utilization = 0;
|
|
size_t max_utilization = 0;
|
|
bool stopped = false;
|
|
|
|
explicit State(size_t in_capacity)
|
|
: memory(in_capacity > 0 ? std::make_unique_for_overwrite<uint8_t[]>(in_capacity) : nullptr),
|
|
capacity(in_capacity) {}
|
|
|
|
// Where a region of `size` bytes would start, nothing if it does not fit now; under the mutex
|
|
std::optional<size_t> FindSpace(size_t size) const {
|
|
if (slots.empty())
|
|
return 0;
|
|
const size_t head = slots.front().offset;
|
|
// tail has come round to head: treated as full
|
|
if (tail == head)
|
|
return std::nullopt;
|
|
if (tail > head) {
|
|
// Live data is [head, tail): free space is behind it to the end, or before it at the start.
|
|
if (capacity - tail >= size)
|
|
return tail;
|
|
if (head >= size)
|
|
return 0;
|
|
return std::nullopt;
|
|
}
|
|
// Wrapped: live data is [head, capacity) + [0, tail), free space is [tail, head).
|
|
if (head - tail >= size)
|
|
return tail;
|
|
return std::nullopt;
|
|
}
|
|
|
|
void Release(size_t offset) {
|
|
std::unique_lock ul(state_mutex);
|
|
for (auto &s: slots) {
|
|
if (s.offset == offset) {
|
|
s.released = true;
|
|
break;
|
|
}
|
|
}
|
|
while (!slots.empty() && slots.front().released) {
|
|
utilization -= slots.front().size;
|
|
slots.pop_front();
|
|
}
|
|
if (slots.empty())
|
|
tail = 0;
|
|
cv.notify_all();
|
|
}
|
|
|
|
void ReleaseHeap(size_t size) {
|
|
std::unique_lock ul(state_mutex);
|
|
utilization -= size;
|
|
}
|
|
};
|
|
|
|
ReceiveRingBuffer::ReceiveRingBuffer(size_t capacity) : state(std::make_shared<State>(capacity)) {}
|
|
|
|
std::shared_ptr<ReceiveBufferRegion> ReceiveRingBuffer::Allocate(size_t size) {
|
|
// Nothing to hold, and a zero-length slot would share its offset with the next real one.
|
|
if (size == 0)
|
|
return std::make_shared<ReceiveBufferRegion>();
|
|
|
|
std::unique_lock ul(state->state_mutex);
|
|
|
|
if (size > state->capacity) {
|
|
// Heap fallback: counted, not bounded.
|
|
state->utilization += size;
|
|
state->max_utilization = std::max(state->max_utilization, state->utilization);
|
|
auto *region = new ReceiveBufferRegion{new uint8_t[size], size};
|
|
return std::shared_ptr<ReceiveBufferRegion>(region, [s = state](ReceiveBufferRegion *r) {
|
|
delete[] r->data;
|
|
s->ReleaseHeap(r->size);
|
|
delete r;
|
|
});
|
|
}
|
|
|
|
std::optional<size_t> offset;
|
|
state->cv.wait(ul, [&] {
|
|
if (state->stopped)
|
|
return true;
|
|
offset = state->FindSpace(size);
|
|
return offset.has_value();
|
|
});
|
|
if (!offset)
|
|
return nullptr;
|
|
|
|
state->slots.push_back({*offset, size});
|
|
state->tail = *offset + size;
|
|
state->utilization += size;
|
|
state->max_utilization = std::max(state->max_utilization, state->utilization);
|
|
|
|
auto *region = new ReceiveBufferRegion{state->memory.get() + *offset, size};
|
|
return std::shared_ptr<ReceiveBufferRegion>(region, [s = state, off = *offset](ReceiveBufferRegion *r) {
|
|
s->Release(off);
|
|
delete r;
|
|
});
|
|
}
|
|
|
|
void ReceiveRingBuffer::Stop() {
|
|
std::unique_lock ul(state->state_mutex);
|
|
state->stopped = true;
|
|
state->cv.notify_all();
|
|
}
|
|
|
|
size_t ReceiveRingBuffer::GetCapacity() const {
|
|
return state->capacity;
|
|
}
|
|
|
|
size_t ReceiveRingBuffer::GetUtilization() const {
|
|
std::unique_lock ul(state->state_mutex);
|
|
return state->utilization;
|
|
}
|
|
|
|
size_t ReceiveRingBuffer::GetMaxUtilization() const {
|
|
std::unique_lock ul(state->state_mutex);
|
|
return state->max_utilization;
|
|
}
|