From 3f9dadb5dbbd48933c3f05d6082d9477e68c99bb Mon Sep 17 00:00:00 2001 From: Giovanna Lazzari Miotto <giovanna.lazzari.miotto@cern.ch> Date: Fri, 12 Apr 2024 15:22:43 +0200 Subject: [PATCH] Bounded input --- src/FileDmaInputFilter.cc | 30 ++++++++++++++++++++---------- src/FileDmaInputFilter.h | 4 ++++ 2 files changed, 24 insertions(+), 10 deletions(-) diff --git a/src/FileDmaInputFilter.cc b/src/FileDmaInputFilter.cc index cb94aa01..f199a14a 100644 --- a/src/FileDmaInputFilter.cc +++ b/src/FileDmaInputFilter.cc @@ -6,6 +6,7 @@ #include <cassert> #include <cerrno> +#include <cstdint> #include <sstream> #include <system_error> @@ -19,6 +20,9 @@ FileDmaInputFilter::FileDmaInputFilter(const std::string &filename, size_t packe throw std::invalid_argument("Invalid input file name: " + filename); } LOG(TRACE) << "Created file input filter"; + fseek(inputFile, 0, SEEK_END); // seek to end of file + file_size = ftell(inputFile); // get current file pointer + fseek(inputFile, 0, SEEK_SET); // seek back to beginning of file } FileDmaInputFilter::~FileDmaInputFilter() { @@ -32,25 +36,31 @@ FileDmaInputFilter::~FileDmaInputFilter() { * * TODO: Better would be mmap directly the file */ -static inline ssize_t read_dma_packet_from_file(FILE *inputFile, char *buffer, ssize_t size, - uint64_t nbReads) { +ssize_t FileDmaInputFilter::read_dma_packet_from_file(FILE *input_file, char *buffer, ssize_t size, + uint64_t nbReads) { static constexpr uint64_t deadbeef = 0xdeadbeefdeadbeefL; ssize_t bytesRead = 0; size_t rc; - while (!feof(inputFile) && bytesRead < size) { + while (!feof(input_file) && bytesRead < size) { // Expecting 32 byte alignment - rc = fread(buffer, 1, 32, inputFile); + rc = fread(buffer, 1, 32, input_file); - if (ferror(inputFile)) { + if (ferror(input_file)) { throw std::system_error(errno, std::system_category(), "File error"); } if (rc != 32) { - if (feof(inputFile) && rc == 0 && bytesRead == 0) { - // We have reached the perfect end of the file, let's start again - fseek(inputFile, 0, SEEK_SET); - continue; + if (feof(input_file) && rc == 0 && bytesRead == 0) { + stats.num_bytes += file_size; + if ((stats.num_bytes / file_size) != 100) { + // We have reached the perfect end of the file, let's start again + fseek(inputFile, 0, SEEK_SET); // seek back to beginning of file + continue; + } else { + // We stop at 100 * file size + return 0; + } } // Misaligned data @@ -74,7 +84,7 @@ static inline ssize_t read_dma_packet_from_file(FILE *inputFile, char *buffer, s // or the packet cannot fit into the buffer // We can deal with both conditions but for the moment we throw an error - if (feof(inputFile)) { + if (feof(input_file)) { throw std::runtime_error("EOF reached but no end of the packet found."); } diff --git a/src/FileDmaInputFilter.h b/src/FileDmaInputFilter.h index 20c7df1a..9ea947f5 100644 --- a/src/FileDmaInputFilter.h +++ b/src/FileDmaInputFilter.h @@ -15,6 +15,8 @@ class FileDmaInputFilter : public InputFilter { ctrl &control); ~FileDmaInputFilter() override; + ssize_t read_dma_packet_from_file(FILE *input_file, char *buffer, ssize_t size, uint64_t nbReads); + protected: ssize_t readInput(char **buffer, size_t bufferSize) override; void print(std::ostream &out) const override; @@ -24,9 +26,11 @@ class FileDmaInputFilter : public InputFilter { private: FILE *inputFile; + size_t file_size{}; struct Statistics { uint64_t nbOversizedPackets = 0; + uint64_t num_bytes = 0; } stats; }; -- GitLab