Files
leonarski_f aecd48b9d3
Build Packages / Build documentation (push) Successful in 48s
Build Packages / build:portable:linux-x86_64:nocuda (push) Successful in 15m47s
Build Packages / build:portable:linux-x86_64:cuda (push) Successful in 23m19s
Build Packages / build:portable:windows-x86_64:nocuda (push) Successful in 18m47s
Build Packages / build:portable:macos-arm64:nocuda (push) Successful in 5m54s
Build Packages / build:rugnux:linux-aarch64:cuda (push) Successful in 11m27s
Build Packages / build:jfjoch:ubuntu2204:cuda (push) Successful in 26m59s
Build Packages / Unit tests (push) Successful in 1h50m25s
Build Packages / Validation Battery (push) Successful in 46m38s
Build Packages / build:jfjoch:rocky8:nocuda (push) Successful in 32m52s
Build Packages / build:jfjoch:rocky9:nocuda (push) Successful in 32m32s
Build Packages / build:jfjoch:ubuntu2204:nocuda (push) Successful in 28m34s
Build Packages / build:jfjoch:ubuntu2404:nocuda (push) Successful in 29m19s
Build Packages / build:jfjoch:rocky8:cuda-sls9 (push) Successful in 30m11s
Build Packages / build:jfjoch:rocky9:cuda-sls9 (push) Successful in 33m58s
Build Packages / build:jfjoch:rocky8:cuda (push) Successful in 31m26s
Build Packages / build:jfjoch:rocky9:cuda (push) Successful in 34m4s
Build Packages / HDF5 consumer tests (DIALS, XDS) (push) Successful in 32m17s
Build Packages / Generate python client (push) Successful in 32s
Build Packages / Create release (push) Successful in 9s
Build Packages / build:jfjoch:ubuntu2404:cuda (push) Successful in 31m50s
Build Packages / build:portable:windows-x86_64:cuda (push) Successful in 19m38s
v1.0.0-rc.186 (#97)
* jfjoch_broker (experimental): Live Rugnux covers serial collections from DECTRIS detectors; a rotation collection too large for the image buffer falls back to standard per-image processing with a warning, and a stream that ends without its end message finishes the collection instead of hanging it.
* jfjoch_broker (experimental): Live Rugnux settings to build the solvent during refinement and to process a rotation collection as stills, a Clear action for the last result, a `_report.txt` for live rotation runs, R-free reported for the refined model, and ice-ring detection on by default for rotation as in the command line.
* jfjoch_broker (experimental): More complete and reproducible live serial merges, analysed for twinning, pseudo-translation and anisotropy, with merging statistics by resolution shell in the live status.
* jfjoch_writer: Live results with large models are written reliably; update jfjoch_writer together with jfjoch_broker.
* Rugnux: Several masters can be processed together with `--force-still`, including grid scans; more robust and faster merging of several sweeps; improved twinning, pseudo-translation, anisotropy, anomalous-signal and space-group decisions.
* Rugnux: Molecular replacement places the copies of a multi-copy solution together; the pseudo-translation warning quotes the Patterson peak and is not raised once a model is placed; the report advises on extending the rotation or reorienting the crystal where the coverage would gain from it.
* Web frontend: Light and dark modes, a phone layout, a hideable page list, a redesigned Rugnux live page with step-through of the Fo-Fc peaks in the structure view.
* jfjoch_viewer: A broker URL given on the command line follows the running collection.

Reviewed-on: #97
Co-authored-by: Filip Leonarski <filip.leonarski@psi.ch>
2026-11-13 20:13:43 +01:00

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;
}