DUNE-DAQ
DUNE Trigger and Data Acquisition software
Loading...
Searching...
No Matches
dunedaq::datahandlinglibs::BufferedFileWriter< Alignment > Class Template Reference

#include <BufferedFileWriter.hpp>

Public Member Functions

 BufferedFileWriter (std::string filename, size_t buffer_size, std::string compression_algorithm="None", bool use_o_direct=true)
 
 BufferedFileWriter ()
 
 ~BufferedFileWriter ()
 
 BufferedFileWriter (const BufferedFileWriter &)=delete
 BufferedFileWriter is not copy-constructible.
 
BufferedFileWriteroperator= (const BufferedFileWriter &)=delete
 BufferedFileWriter is not copy-assginable.
 
 BufferedFileWriter (BufferedFileWriter &&)=delete
 BufferedFileWriter is not move-constructible.
 
BufferedFileWriteroperator= (BufferedFileWriter &&)=delete
 BufferedFileWriter is not move-assignable.
 
void open (std::string filename, size_t buffer_size, std::string compression_algorithm="None", bool use_o_direct=true)
 
bool is_open () const
 
bool write (const char *memory, const size_t size)
 
void close ()
 
void flush ()
 

Private Types

using io_sink_t = boost::iostreams::file_descriptor_sink
 
using aligned_allocator_t = boost::alignment::aligned_allocator<io_sink_t::char_type, Alignment>
 
using filtering_ostream_t
 

Private Attributes

std::string m_filename
 
size_t m_buffer_size
 
std::string m_compression_algorithm
 
int m_fd
 
io_sink_t m_sink
 
filtering_ostream_t m_output_stream
 
bool m_is_open = false
 
bool m_use_o_direct = true
 

Detailed Description

template<size_t Alignment = 4096>
class dunedaq::datahandlinglibs::BufferedFileWriter< Alignment >

Class to buffer and write data of a specified type to a file using O_DIRECT. In addition, data can be compressed. before being written.

Template Parameters
ReadoutTypeType of the data that will be written to file
AlignmentThe alignment used for allocations of buffers. It has to fulfil certain system specific requirements such that file writing with O_DIRECT can work.

Definition at line 47 of file BufferedFileWriter.hpp.

Member Typedef Documentation

◆ aligned_allocator_t

template<size_t Alignment = 4096>
using dunedaq::datahandlinglibs::BufferedFileWriter< Alignment >::aligned_allocator_t = boost::alignment::aligned_allocator<io_sink_t::char_type, Alignment>
private

Definition at line 50 of file BufferedFileWriter.hpp.

◆ filtering_ostream_t

template<size_t Alignment = 4096>
using dunedaq::datahandlinglibs::BufferedFileWriter< Alignment >::filtering_ostream_t
private
Initial value:
boost::iostreams::filtering_stream<boost::iostreams::output, char, std::char_traits<char>, aligned_allocator_t>
boost::alignment::aligned_allocator< io_sink_t::char_type, Alignment > aligned_allocator_t

Definition at line 51 of file BufferedFileWriter.hpp.

◆ io_sink_t

template<size_t Alignment = 4096>
using dunedaq::datahandlinglibs::BufferedFileWriter< Alignment >::io_sink_t = boost::iostreams::file_descriptor_sink
private

Definition at line 49 of file BufferedFileWriter.hpp.

Constructor & Destructor Documentation

◆ BufferedFileWriter() [1/4]

template<size_t Alignment = 4096>
dunedaq::datahandlinglibs::BufferedFileWriter< Alignment >::BufferedFileWriter ( std::string filename,
size_t buffer_size,
std::string compression_algorithm = "None",
bool use_o_direct = true )
inline

Constructor to construct and initialize an instance. The file will be open after initialization.

Parameters
filenameThe file to be used. Existing data will be overwritten, if the file does not exist it will be created.
buffer_sizeThe size of the buffer that is used before data is written to the file. Make sure that this size fulfils size requirements of O_DIRECT, otherwise writes will fail.
compression_algorithmThe compression algorithm to use. Can be one of: None, zstd, lzma or zlib
use_o_directfile descriptors : avoid excessive resource footprint. It also avoids the intermediate aligned buffer, requires the source latency buffer to be memory aligned.
Exceptions
CannotOpenFileIf the file can not be opened.
ConfigurationErrorIf the compression algorithm parameter is not recognized.

Definition at line 66 of file BufferedFileWriter.hpp.

67 {
68 open(filename, buffer_size, compression_algorithm, use_o_direct);
69 }
void open(std::string filename, size_t buffer_size, std::string compression_algorithm="None", bool use_o_direct=true)

◆ BufferedFileWriter() [2/4]

template<size_t Alignment = 4096>
dunedaq::datahandlinglibs::BufferedFileWriter< Alignment >::BufferedFileWriter ( )
inline

Constructor to construct an instance without opening a file.

Definition at line 74 of file BufferedFileWriter.hpp.

74{}

◆ ~BufferedFileWriter()

template<size_t Alignment = 4096>
dunedaq::datahandlinglibs::BufferedFileWriter< Alignment >::~BufferedFileWriter ( )
inline

Destructor that closes the file before destruction to make sure all data in any buffers is written to file.

Definition at line 79 of file BufferedFileWriter.hpp.

◆ BufferedFileWriter() [3/4]

template<size_t Alignment = 4096>
dunedaq::datahandlinglibs::BufferedFileWriter< Alignment >::BufferedFileWriter ( const BufferedFileWriter< Alignment > & )
delete

BufferedFileWriter is not copy-constructible.

◆ BufferedFileWriter() [4/4]

template<size_t Alignment = 4096>
dunedaq::datahandlinglibs::BufferedFileWriter< Alignment >::BufferedFileWriter ( BufferedFileWriter< Alignment > && )
delete

BufferedFileWriter is not move-constructible.

Member Function Documentation

◆ close()

template<size_t Alignment = 4096>
void dunedaq::datahandlinglibs::BufferedFileWriter< Alignment >::close ( )
inline

Close the writer. All data from any buffers will be written to file.

Definition at line 167 of file BufferedFileWriter.hpp.

168 {
169 // Set the file descriptor to not use O_DIRECT. This is necessary because the write size has to be aligned for
170 // O_DIRECT to succeed. This is not guaranteed for the data that remains in the buffer.
171 fcntl(m_fd, F_SETFL, O_CREAT | O_WRONLY);
172 m_output_stream.reset();
173 m_is_open = false;
174 }

◆ flush()

template<size_t Alignment = 4096>
void dunedaq::datahandlinglibs::BufferedFileWriter< Alignment >::flush ( )
inline

If no compression is used, this writes all data from buffers to the file. In case that compression is used, this is not guaranteed.

Definition at line 180 of file BufferedFileWriter.hpp.

181 {
182 // Set the file descriptor to not use O_DIRECT. This is necessary because the write size has to be aligned for
183 // O_DIRECT to succeed. This is not guaranteed for the data that remains in the buffer.
184 fcntl(m_fd, F_SETFL, O_CREAT | O_WRONLY);
185 // This does not flush the compressor as it is not flushable
186 m_output_stream.flush();
187 // Activate O_DIRECT again
188 auto oflag = O_CREAT | O_WRONLY;
189 if (m_use_o_direct) {
190 oflag = oflag | O_DIRECT;
191 }
192 fcntl(m_fd, F_SETFL, oflag);
193 }

◆ is_open()

template<size_t Alignment = 4096>
bool dunedaq::datahandlinglibs::BufferedFileWriter< Alignment >::is_open ( ) const
inline

Check if the file is open.

Returns
true if the file is open, false otherwise.

Definition at line 149 of file BufferedFileWriter.hpp.

149{ return m_is_open; }

◆ open()

template<size_t Alignment = 4096>
void dunedaq::datahandlinglibs::BufferedFileWriter< Alignment >::open ( std::string filename,
size_t buffer_size,
std::string compression_algorithm = "None",
bool use_o_direct = true )
inline

Open a file.

Parameters
filenameThe file to be used. Existing data will be overwritten, if the file does not exist it will be created.
buffer_sizeThe size of the buffer that is used before data is written to the file. Make sure that this size fulfils size requirements of O_DIRECT, otherwise writes will fail.
compression_algorithmThe compression algorithm to use. Can be one of: None, zstd, lzma or zlib
use_o_directfile descriptors : avoid excessive resource footprint. It also avoids the intermediate aligned buffer, requires the source latency buffer to be memory aligned.
Exceptions
CannotOpenFileIf the file can not be opened.
ConfigurationErrorIf the compression algorithm parameter is not recognized.

Definition at line 101 of file BufferedFileWriter.hpp.

105 {
106 m_use_o_direct = use_o_direct;
107 if (m_is_open) {
108 close();
109 }
110
112 m_buffer_size = buffer_size;
113 m_compression_algorithm = compression_algorithm;
114 auto oflag = O_CREAT | O_WRONLY;
115 if (m_use_o_direct) {
116 oflag = oflag | O_DIRECT;
117 }
118
119 m_fd = ::open(m_filename.c_str(), oflag, 0644);
120 if (m_fd == -1) {
121 throw BufferedReaderWriterCannotOpenFile(ERS_HERE, m_filename);
122 }
123
124 m_sink = io_sink_t(m_fd, boost::iostreams::file_descriptor_flags::close_handle);
125 if (m_compression_algorithm == "zstd") {
126 TLOG_DEBUG(TLVL_WORK_STEPS) << "Using zstd compression" << std::endl;
127 m_output_stream.push(boost::iostreams::zstd_compressor(boost::iostreams::zstd::best_speed));
128 } else if (m_compression_algorithm == "lzma") {
129 TLOG_DEBUG(TLVL_WORK_STEPS) << "Using lzma compression" << std::endl;
130 m_output_stream.push(boost::iostreams::lzma_compressor(boost::iostreams::lzma::best_speed));
131 } else if (m_compression_algorithm == "zlib") {
132 TLOG_DEBUG(TLVL_WORK_STEPS) << "Using zlib compression" << std::endl;
133 m_output_stream.push(boost::iostreams::zlib_compressor(boost::iostreams::zlib::best_speed));
134 } else if (m_compression_algorithm == "None") {
135 TLOG_DEBUG(TLVL_WORK_STEPS) << "Running without compression" << std::endl;
136 } else {
138 "Non-recognized compression algorithm: " + m_compression_algorithm);
139 }
140
142 m_is_open = true;
143 }
#define ERS_HERE
boost::iostreams::file_descriptor_sink io_sink_t
#define TLOG_DEBUG(lvl,...)
Definition Logging.hpp:112
SourceID[" << sourceid << "] Command daqdataformats::SourceID Readout Initialization std::string initerror BufferedReaderWriterConfigurationError

◆ operator=() [1/2]

template<size_t Alignment = 4096>
BufferedFileWriter & dunedaq::datahandlinglibs::BufferedFileWriter< Alignment >::operator= ( BufferedFileWriter< Alignment > && )
delete

BufferedFileWriter is not move-assignable.

◆ operator=() [2/2]

template<size_t Alignment = 4096>
BufferedFileWriter & dunedaq::datahandlinglibs::BufferedFileWriter< Alignment >::operator= ( const BufferedFileWriter< Alignment > & )
delete

BufferedFileWriter is not copy-assginable.

◆ write()

template<size_t Alignment = 4096>
bool dunedaq::datahandlinglibs::BufferedFileWriter< Alignment >::write ( const char * memory,
const size_t size )
inline

Write something to the buffer. If the buffer is full, all data from it will be written to file.

Parameters
elementThe element to write.
Returns
true if the write was successful, false if the writer is not open or the write was not successful.

Definition at line 156 of file BufferedFileWriter.hpp.

157 {
158 if (!m_is_open)
159 return false;
160 m_output_stream.write(memory, size); // NOLINT
161 return !m_output_stream.bad();
162 }
FELIX Initialization std::string initerror FELIX queue timed std::string queuename Unexpected chunk size

Member Data Documentation

◆ m_buffer_size

template<size_t Alignment = 4096>
size_t dunedaq::datahandlinglibs::BufferedFileWriter< Alignment >::m_buffer_size
private

Definition at line 198 of file BufferedFileWriter.hpp.

◆ m_compression_algorithm

template<size_t Alignment = 4096>
std::string dunedaq::datahandlinglibs::BufferedFileWriter< Alignment >::m_compression_algorithm
private

Definition at line 199 of file BufferedFileWriter.hpp.

◆ m_fd

template<size_t Alignment = 4096>
int dunedaq::datahandlinglibs::BufferedFileWriter< Alignment >::m_fd
private

Definition at line 202 of file BufferedFileWriter.hpp.

◆ m_filename

template<size_t Alignment = 4096>
std::string dunedaq::datahandlinglibs::BufferedFileWriter< Alignment >::m_filename
private

Definition at line 197 of file BufferedFileWriter.hpp.

◆ m_is_open

template<size_t Alignment = 4096>
bool dunedaq::datahandlinglibs::BufferedFileWriter< Alignment >::m_is_open = false
private

Definition at line 205 of file BufferedFileWriter.hpp.

◆ m_output_stream

template<size_t Alignment = 4096>
filtering_ostream_t dunedaq::datahandlinglibs::BufferedFileWriter< Alignment >::m_output_stream
private

Definition at line 204 of file BufferedFileWriter.hpp.

◆ m_sink

template<size_t Alignment = 4096>
io_sink_t dunedaq::datahandlinglibs::BufferedFileWriter< Alignment >::m_sink
private

Definition at line 203 of file BufferedFileWriter.hpp.

◆ m_use_o_direct

template<size_t Alignment = 4096>
bool dunedaq::datahandlinglibs::BufferedFileWriter< Alignment >::m_use_o_direct = true
private

Definition at line 206 of file BufferedFileWriter.hpp.


The documentation for this class was generated from the following file: