mirror of
https://github.com/paulscherrerinstitute/sf_daq_buffer.git
synced 2026-05-01 18:12:23 +02:00
Finish BinaryWriter for Images in buffer
This commit is contained in:
@@ -1,15 +1,16 @@
|
||||
#ifndef BINARYWRITER_HPP
|
||||
#define BINARYWRITER_HPP
|
||||
#ifndef IMAGEBINARYWRITER_HPP
|
||||
#define IMAGEBINARYWRITER_HPP
|
||||
|
||||
#include <string>
|
||||
|
||||
#include "formats.hpp"
|
||||
|
||||
|
||||
|
||||
class ImageBinaryWriter {
|
||||
|
||||
const size_t MAX_FILE_BYTES =
|
||||
buffer_config::FILE_MOD * sizeof(BufferBinaryFormat);
|
||||
|
||||
const size_t IMAGE_BYTES;
|
||||
const size_t IMAGE_SLOT_BYTES;
|
||||
const size_t MAX_FILE_BYTES;
|
||||
const std::string detector_folder_;
|
||||
std::string latest_filename_;
|
||||
|
||||
@@ -21,13 +22,15 @@ class ImageBinaryWriter {
|
||||
|
||||
|
||||
public:
|
||||
ImageBinaryWriter(const std::string& detector_folder);
|
||||
ImageBinaryWriter(
|
||||
const std::string& detector_folder,
|
||||
const uint64_t image_n_bytes);
|
||||
|
||||
virtual ~ImageBinaryWriter();
|
||||
|
||||
void write(const uint64_t pulse_id, const BufferBinaryFormat* buffer);
|
||||
void write(const ImageMetadata meta, const char* data);
|
||||
|
||||
};
|
||||
|
||||
|
||||
#endif //BINARYWRITER_HPP
|
||||
#endif //IMAGEBINARYWRITER_HPP
|
||||
|
||||
@@ -11,9 +11,14 @@
|
||||
#include "BufferUtils.hpp"
|
||||
|
||||
using namespace std;
|
||||
using namespace buffer_config;
|
||||
|
||||
ImageBinaryWriter::ImageBinaryWriter(
|
||||
const string& detector_folder):
|
||||
const string& detector_folder,
|
||||
const size_t image_n_bytes):
|
||||
IMAGE_BYTES(image_n_bytes),
|
||||
IMAGE_SLOT_BYTES(IMAGE_BYTES + sizeof(ImageMetadata)),
|
||||
MAX_FILE_BYTES(IMAGE_SLOT_BYTES * FILE_MOD),
|
||||
detector_folder_(detector_folder),
|
||||
latest_filename_(detector_folder + "/LATEST"),
|
||||
current_output_filename_(""),
|
||||
@@ -26,20 +31,17 @@ ImageBinaryWriter::~ImageBinaryWriter()
|
||||
close_current_file();
|
||||
}
|
||||
|
||||
void ImageBinaryWriter::write(
|
||||
const uint64_t pulse_id,
|
||||
const BufferBinaryFormat* buffer)
|
||||
void ImageBinaryWriter::write(const ImageMetadata meta, const char* data)
|
||||
{
|
||||
auto current_frame_file =
|
||||
BufferUtils::get_filename(detector_folder_, module_name_, pulse_id);
|
||||
BufferUtils::get_image_filename(detector_folder_, meta.pulse_id);
|
||||
|
||||
if (current_frame_file != current_output_filename_) {
|
||||
open_file(current_frame_file);
|
||||
}
|
||||
|
||||
size_t n_bytes_offset =
|
||||
BufferUtils::get_file_frame_index(pulse_id) *
|
||||
sizeof(BufferBinaryFormat);
|
||||
BufferUtils::get_file_frame_index(meta.pulse_id) * IMAGE_SLOT_BYTES;
|
||||
|
||||
auto lseek_result = lseek(output_file_fd_, n_bytes_offset, SEEK_SET);
|
||||
if (lseek_result < 0) {
|
||||
@@ -48,7 +50,7 @@ void ImageBinaryWriter::write(
|
||||
using namespace date;
|
||||
using namespace chrono;
|
||||
err_msg << "[" << system_clock::now() << "]";
|
||||
err_msg << "[BufferBinaryWriter::write]";
|
||||
err_msg << "[ImageBinaryWriter::write]";
|
||||
err_msg << " Error while lseek on file ";
|
||||
err_msg << current_output_filename_;
|
||||
err_msg << " for n_bytes_offset ";
|
||||
@@ -58,8 +60,23 @@ void ImageBinaryWriter::write(
|
||||
throw runtime_error(err_msg.str());
|
||||
}
|
||||
|
||||
auto n_bytes = ::write(output_file_fd_, buffer, sizeof(BufferBinaryFormat));
|
||||
if (n_bytes < sizeof(BufferBinaryFormat)) {
|
||||
auto n_bytes_meta = ::write(output_file_fd_, &meta, sizeof(ImageMetadata));
|
||||
if (n_bytes_meta < sizeof(ImageMetadata)) {
|
||||
stringstream err_msg;
|
||||
|
||||
using namespace date;
|
||||
using namespace chrono;
|
||||
err_msg << "[" << system_clock::now() << "]";
|
||||
err_msg << "[BufferBinaryWriter::write]";
|
||||
err_msg << " Error while writing to file ";
|
||||
err_msg << current_output_filename_ << ": ";
|
||||
err_msg << strerror(errno) << endl;
|
||||
|
||||
throw runtime_error(err_msg.str());
|
||||
}
|
||||
|
||||
auto n_bytes_data = ::write(output_file_fd_, data, IMAGE_BYTES);
|
||||
if (n_bytes_data < sizeof(IMAGE_BYTES)) {
|
||||
stringstream err_msg;
|
||||
|
||||
using namespace date;
|
||||
@@ -88,7 +105,7 @@ void ImageBinaryWriter::open_file(const std::string& filename)
|
||||
using namespace date;
|
||||
using namespace chrono;
|
||||
err_msg << "[" << system_clock::now() << "]";
|
||||
err_msg << "[BinaryWriter::open_file]";
|
||||
err_msg << "[ImageBinaryWriter::open_file]";
|
||||
err_msg << " Cannot create file ";
|
||||
err_msg << filename << ": ";
|
||||
err_msg << strerror(errno) << endl;
|
||||
@@ -96,45 +113,6 @@ void ImageBinaryWriter::open_file(const std::string& filename)
|
||||
throw runtime_error(err_msg.str());
|
||||
}
|
||||
|
||||
// TODO: Remove context if test successful.
|
||||
|
||||
/** Setting the buffer file size in advance to try to lower the number of
|
||||
metadata updates on GPFS. */
|
||||
{
|
||||
// TODO: Try instead to use fallocate.
|
||||
if (lseek(output_file_fd_, MAX_FILE_BYTES, SEEK_SET) < 0) {
|
||||
stringstream err_msg;
|
||||
|
||||
using namespace date;
|
||||
using namespace chrono;
|
||||
err_msg << "[" << system_clock::now() << "]";
|
||||
err_msg << "[BufferBinaryWriter::open_file]";
|
||||
err_msg << " Error while lseek on end of file ";
|
||||
err_msg << current_output_filename_;
|
||||
err_msg << " for MAX_FILE_BYTES ";
|
||||
err_msg << MAX_FILE_BYTES << ": ";
|
||||
err_msg << strerror(errno) << endl;
|
||||
|
||||
throw runtime_error(err_msg.str());
|
||||
}
|
||||
|
||||
const uint8_t mark = 255;
|
||||
if(::write(output_file_fd_, &mark, sizeof(mark)) != sizeof(mark)) {
|
||||
stringstream err_msg;
|
||||
|
||||
using namespace date;
|
||||
using namespace chrono;
|
||||
err_msg << "[" << system_clock::now() << "]";
|
||||
err_msg << "[BufferBinaryWriter::open_file]";
|
||||
err_msg << " Error while writing to file ";
|
||||
err_msg << current_output_filename_ << ": ";
|
||||
err_msg << strerror(errno) << endl;
|
||||
|
||||
throw runtime_error(err_msg.str());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
current_output_filename_ = filename;
|
||||
}
|
||||
|
||||
@@ -147,7 +125,7 @@ void ImageBinaryWriter::close_current_file()
|
||||
using namespace date;
|
||||
using namespace chrono;
|
||||
err_msg << "[" << system_clock::now() << "]";
|
||||
err_msg << "[BufferBinaryWriter::close_current_file]";
|
||||
err_msg << "[ImageBinaryWriter::close_current_file]";
|
||||
err_msg << " Error while closing file ";
|
||||
err_msg << current_output_filename_ << ": ";
|
||||
err_msg << strerror(errno) << endl;
|
||||
|
||||
Reference in New Issue
Block a user