Files
Jungfraujoch/reader/JFJochReaderDataset.h
T
leonarski_fandClaude Fable 5 dc8a80f3e0 viewer: fix HTTP live-follow OOM via datasetLoaded backpressure + shared pixel mask
The viewer could grow to ~100 GB RAM when live-following an HTTP broker. The
rc.153 images_in_flight backpressure only throttled imageLoaded; the heavy
per-frame payload rides datasetLoaded, fanned out over ~10 queued cross-thread
connections with no cap. In HTTPSyncDataset follow mode (entered when an operator
clicks an image while following live) RefreshDatasetOnly_i emits a fresh full
dataset every autoload tick with no imageLoaded, so the gate never engaged and
the queued events - each pinning a full JFJochReaderDataset (full-detector
PixelMask + per-image plots) - accumulated without bound.

Backpressure datasetLoaded the same way as imageLoaded: a datasets_in_flight
counter (cap 2), all emits routed through EmitDatasetLoaded_i, and
AutoLoadTimerExpired gated on it (covers HTTPSyncDataset). The window routes the
worker's datasetLoaded through a single OnDatasetReady sink that fans out
synchronously via datasetReady and acks with datasetConsumed. Under load stale
datasets are dropped; the next tick sends the latest.

Share the pixel mask instead of deep-copying it: JFJochReaderDataset::pixel_mask
is now shared_ptr<const PixelMask>, so per-frame dataset copies share the ~72 MB
mask. UpdateUserMask does copy-on-write; JFJochHttpReader caches the mask by
arm_date so a live refresh reuses one shared mask per acquisition.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-09 10:58:10 +02:00

84 lines
3.0 KiB
C++

// SPDX-FileCopyrightText: 2025 Filip Leonarski, Paul Scherrer Institute <filip.leonarski@psi.ch>
// SPDX-License-Identifier: GPL-3.0-only
#pragma once
#include <string>
#include <optional>
#include <vector>
#include <map>
#include <memory>
#include "../common/DiffractionGeometry.h"
#include "../common/DiffractionExperiment.h"
#include "../common/PixelMask.h"
#include "../common/AzimuthalIntegrationMapping.h"
struct JFJochReaderDataset {
std::string arm_date;
DiffractionExperiment experiment;
// Shared, not copied, across dataset snapshots: the mask is constant for a run, so a per-frame
// dataset refresh / mutable copy must not clone the full-detector (~tens of MB) mask. Held as
// shared_ptr<const>; the rare edit (user mask) builds a fresh mask (copy-on-write). Never null.
std::shared_ptr<const PixelMask> pixel_mask = std::make_shared<const PixelMask>();
std::optional<int64_t> error_value;
std::string jfjoch_release;
std::vector<float> az_int_bin_to_q;
std::vector<float> az_int_bin_to_phi;
size_t azimuthal_bins = 0;
size_t q_bins = 0;
std::vector<float> spot_count;
std::vector<float> spot_count_indexed;
std::vector<float> spot_count_low_res;
std::vector<float> spot_count_ice_rings;
std::vector<float> indexing_result;
std::vector<float> indexing_lattice_count;
std::vector<float> bkg_estimate;
std::vector<float> ice_ring_score;
std::vector<float> resolution_estimate;
std::vector<float> efficiency;
std::vector<float> profile_radius;
std::vector<float> mosaicity_deg;
std::vector<float> b_factor;
std::vector<float> integrated_reflections;
std::vector<float> image_scale_factor;
std::vector<float> image_scale_cc;
std::vector<float> image_scale_b;
std::vector<int64_t> max_value;
// Maps this dataset's image index -> the original/collected image number it came from.
// Empty means identity (image i == original image i). Lets a dataset be a subset (or strided
// selection) of the truly collected images: reprocessing snapshots over a sub-range, and (in
// future) a main dataset that was filtered on-the-fly during collection.
std::vector<int> source_image_number;
std::vector<std::string> roi;
std::vector<std::vector<int64_t>> roi_sum;
std::vector<std::vector<int64_t>> roi_sum_sq;
std::vector<std::vector<int64_t>> roi_max;
std::vector<std::vector<int64_t>> roi_npixel;
std::vector<std::vector<float>> roi_x;
std::vector<std::vector<float>> roi_y;
// ROI definitions stored in the master file. The logical definitions populate
// experiment.ROI() (they re-derive with the current geometry); roi_map is the
// per-pixel bitmask as written (constant footprint), with roi_bit_index mapping
// each ROI name to its bit.
std::vector<uint16_t> roi_map;
std::map<std::string, uint16_t> roi_bit_index;
std::vector<std::string> calibration_data;
JFJochReaderDataset() = default;
JFJochReaderDataset(const JFJochReaderDataset &other) = default;
};