A cold run on a spinning disk waited on the disk twice over. The CBF header
scan read 256 kB from every frame on eight threads that each strode through
their own share of the sweep, so they drifted apart and the scan became a
seek storm (34 s for 2400 frames here); and after it, the pre-scan and the
first-pass indexing touch a few hundred frames and leave the disk idle until
the first image loop reads everything at seek-bound rates.
- ReadAhead (reader/): once the dataset is open, rugnux starts eight threads
that read the data files - HDF5 data files (legacy, VDS or the integrated
master) or the per-frame CBF/marCCD/SMV files - in 4 MB pieces taken
strictly in order, into a throwaway buffer. One stream reads this disk at
125 MB/s, eight in-order streams at 190 MB/s, 32 at 157 MB/s. It never gets
more than a quarter of MemAvailable (GlobalMemoryStatusEx on Windows, 4 GiB
where there is no figure) ahead of what ReadRawImage has handed out, so a
dataset bigger than the cache does not evict its own start, and it stops
with the reader. Plain ifstream reads: portable, no POSIX calls.
- Header scans (CBF, marCCD, SMV) hand the files out in order from an atomic
counter (sweep::ForEachInOrder) instead of striding: 18 s -> 12 s for 2400
cold CBF headers. The CBF header is first read with a 16 kB probe and again
with the old 256 kB one only when the separator is not in it, so the parsed
header is exactly what it was: 12 s -> 6 s.
Output unchanged: p.hkl, p.mtz and p_unmerged.mtz md5-identical to the
rc173 baseline on 6toc (CBF, 2400 frames, 6.0 GB) and 9q41 (HDF5 VDS, 900
frames, 5.1 GB), and on 6z9g (HDF5, 12.8 GB) to the unmodified branch; myob
(p.hkl p.mtz p_P1.mtz p_unmerged.mtz) md5-identical to the reference.
Measured cold (files evicted with POSIX_FADV_DONTNEED before every run),
same code without this commit vs with it, on a shared box (load 20-70, other
agents reading the same disk, so single runs scatter by +-20 s):
6toc wall 61.7/62.7 -> 49.3/49.8 s (clean pairs); all data resident
after 62/51/50 -> 45/41/42 s
9q41 wall 67.3 -> 57.8 s (clean pair); resident after 58/46/43 -> 48/37/35 s
6z9g resident after 81 -> 69 s
The first image loop can look slower with this in CBF runs: the old 256 kB
header probes pulled ~70% of the data in as kernel readahead, so the old
loop started warm - after a 34 s header scan instead of 10 s.
Warm (myob, NVMe, cached): 19.35/20.00 s without, 19.76-20.16 s with; the
read-ahead then only copies 9.3 GB out of the page cache, 0.44 s wall and
3.4 CPU-s measured standalone.
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01D1G8gJVAy6gp1K5Dz3NE5C
82 lines
3.3 KiB
C++
82 lines
3.3 KiB
C++
// SPDX-FileCopyrightText: 2025 Filip Leonarski, Paul Scherrer Institute <filip.leonarski@psi.ch>
|
|
// SPDX-License-Identifier: GPL-3.0-only
|
|
|
|
#ifndef JFJOCHIMAGEREADER_H
|
|
#define JFJOCHIMAGEREADER_H
|
|
|
|
#include <unordered_set>
|
|
#include <map>
|
|
#include <memory>
|
|
#include <mutex>
|
|
|
|
#include "../common/JFJochMessages.h"
|
|
#include "../common/Plot.h"
|
|
#include "../common/ROIBox.h"
|
|
#include "../common/DiffractionExperiment.h"
|
|
#include "JFJochReaderDataset.h"
|
|
#include "JFJochReaderImage.h"
|
|
#include "JFJochReaderSpots.h"
|
|
#include "ReadAhead.h"
|
|
|
|
class JFJochReader {
|
|
mutable std::mutex m;
|
|
std::shared_ptr<JFJochReaderDataset> dataset;
|
|
|
|
virtual bool LoadImage_i(std::shared_ptr<JFJochReaderDataset> &dataset,
|
|
DataMessage& message,
|
|
std::vector<uint8_t> &buffer,
|
|
int64_t image_number,
|
|
bool update_dataset) = 0;
|
|
|
|
mutable std::mutex summation_mutex;
|
|
void SummationThread(int64_t image0,
|
|
int64_t n_image,
|
|
int64_t image_jump,
|
|
JFJochReaderImage &image);
|
|
|
|
std::unique_ptr<ReadAhead> read_ahead_;
|
|
protected:
|
|
void SetStartMessage(const std::shared_ptr<JFJochReaderDataset> &val);
|
|
|
|
// The files that hold the frame data, in the order the images sit in them: what StartReadAhead()
|
|
// streams. Empty for a reader with no files of its own.
|
|
virtual std::vector<std::string> DataFiles() const { return {}; }
|
|
// Every ReadRawImage() that read an image reports it here, which is what moves the read-ahead on.
|
|
void NoteImageRead() { if (read_ahead_) read_ahead_->ImageRead(); }
|
|
|
|
DiffractionExperiment default_experiment;
|
|
public:
|
|
virtual ~JFJochReader() = default;
|
|
|
|
JFJochReader& Experiment(const DiffractionExperiment& experiment);
|
|
|
|
std::shared_ptr<const JFJochReaderDataset> GetDataset() const;
|
|
[[nodiscard]] virtual uint64_t GetNumberOfImages() const = 0;
|
|
|
|
virtual void Close() = 0;
|
|
|
|
// Start streaming the frame data into the page cache ahead of the image reads (see ReadAhead).
|
|
// For a batch run over the whole dataset; call it once the dataset is open, before any image is
|
|
// read. It changes only when the bytes arrive, never what is read.
|
|
void StartReadAhead();
|
|
|
|
std::shared_ptr<JFJochReaderImage> LoadImage(int64_t image_number, int64_t summation_factor = 1);
|
|
|
|
void UpdateGeomMetadata(const DiffractionExperiment& experiment);
|
|
void UpdateUserMask(const std::vector<uint32_t>& mask);
|
|
|
|
// Read one image into a raw image the caller owns, false where there is nothing to read. A
|
|
// worker that walks a whole sweep hands the same object back on every frame and so keeps its
|
|
// buffer: GetRawImage() allocates a fresh one per call, which on a 4-byte 16 Mpx frame is 72 MB
|
|
// of untouched pages the decoder then faults in one by one, measurably more than the decode.
|
|
virtual bool ReadRawImage(int64_t image_number, JFJochReaderRawImage &image) = 0;
|
|
|
|
// The same image in a freshly allocated raw image.
|
|
std::shared_ptr<JFJochReaderRawImage> GetRawImage(int64_t image_number);
|
|
|
|
virtual std::vector<SpotToSave> ReadSpots(int64_t image) const = 0;
|
|
std::shared_ptr<JFJochReaderSpots> ReadAllSpots(int64_t start_image, int64_t end_image, int64_t stride = 1) const;
|
|
};
|
|
|
|
#endif //JFJOCHIMAGEREADER_H
|