Implement common class for RamBuffer

This commit is contained in:
2020-09-02 15:16:02 +02:00
parent 20f5557cc1
commit 28d407fb0e
4 changed files with 98 additions and 72 deletions
+32
View File
@@ -0,0 +1,32 @@
#ifndef SF_DAQ_BUFFER_RAMBUFFER_HPP
#define SF_DAQ_BUFFER_RAMBUFFER_HPP
#include <string>
#include "formats.hpp"
class RamBuffer {
const std::string detector_name_;
const int module_n_;
const size_t n_slots_;
const size_t image_size_;
const size_t buffer_size_;
int shm_fd_;
void* buffer_;
ModuleFrame* meta_buffer_;
char* image_buffer_;
public:
RamBuffer(const std::string& detector_name,
const size_t n_modules,
const int module_n,
const size_t n_slots);
~RamBuffer();
void write_frame(const ModuleFrame& metadata, const char* data);
};
#endif //SF_DAQ_BUFFER_RAMBUFFER_HPP
-24
View File
@@ -1,24 +0,0 @@
#ifndef SF_DAQ_BUFFER_RAMBUFFERWRITER_HPP
#define SF_DAQ_BUFFER_RAMBUFFERWRITER_HPP
#include <string>
class RamBufferWriter {
const int module_n_;
const std::string detector_name_;
int shm_fd_;
size_t buffer_size_;
void* buffer_;
public:
RamBufferWriter(const std::string& detector_name,
const int module_n,
const size_t n_modules,
const size_t n_slots);
~RamBufferWriter();
};
#endif //SF_DAQ_BUFFER_RAMBUFFERWRITER_HPP
+66
View File
@@ -0,0 +1,66 @@
#include <sys/mman.h>
#include <bits/fcntl-linux.h>
#include <cstring>
#include <stdexcept>
#include <unistd.h>
#include "RamBuffer.hpp"
#include "buffer_config.hpp"
using namespace std;
using namespace buffer_config;
RamBuffer::RamBuffer(
const string &detector_name,
const size_t n_modules,
const int module_n,
const size_t n_slots) :
detector_name_(detector_name),
module_n_(module_n),
n_slots_(n_slots),
image_size_(MODULE_N_BYTES * n_modules),
buffer_size_((sizeof(ModuleFrame) + image_size_) * n_slots)
{
shm_fd_ = shm_open(detector_name_.c_str(), O_RDWR | O_CREAT, 0777);
if (shm_fd_ < 0) {
throw runtime_error(strerror(errno));
}
if ((ftruncate(shm_fd_, buffer_size_)) == -1) {
throw runtime_error(strerror(errno));
}
// TODO: Test with MAP_HUGETLB
buffer_ = mmap(NULL, buffer_size_, PROT_WRITE, MAP_SHARED, shm_fd_, 0);
if (buffer_ == MAP_FAILED) {
throw runtime_error(strerror(errno));
}
meta_buffer_ = (ModuleFrame *) buffer_;
// Image buffer start right after metadata buffer.
image_buffer_ = (char *) (meta_buffer_ + n_slots);
}
RamBuffer::~RamBuffer()
{
munmap(buffer_, buffer_size_);
close(shm_fd_);
shm_unlink(detector_name_.c_str());
}
void RamBuffer::write_frame(
const ModuleFrame &metadata,
const char *data)
{
size_t slot_n = metadata.pulse_id % n_slots_;
ModuleFrame *meta = meta_buffer_ + slot_n;
*meta = metadata;
char *frame = image_buffer_ +
(image_size_ * slot_n) +
(MODULE_N_BYTES * module_n_);
memcpy(frame, data, MODULE_N_BYTES);
}
-48
View File
@@ -1,48 +0,0 @@
#include <sys/mman.h>
#include <bits/fcntl-linux.h>
#include <cstring>
#include <stdexcept>
#include <formats.hpp>
#include <unistd.h>
#include "RamBufferWriter.hpp"
#include "buffer_config.hpp"
using namespace std;
using namespace buffer_config;
RamBufferWriter::RamBufferWriter(
const string &detector_name,
const int module_n,
const size_t n_modules,
const size_t n_slots) :
detector_name_(detector_name),
module_n_(module_n)
{
shm_fd_ = shm_open(detector_name_.c_str(), O_RDWR | O_CREAT, 0777);
if (shm_fd_ < 0) {
throw runtime_error(strerror(errno));
}
size_t slot_size = MODULE_N_BYTES + sizeof(ModuleFrame);
buffer_size_ = slot_size * n_modules * n_slots;
if ((ftruncate(shm_fd_, buffer_size_)) == -1) {
throw runtime_error(strerror(errno));
}
// TODO: Test with MAP_HUGETLB
void* ptr = mmap(NULL, buffer_size_, PROT_WRITE, MAP_SHARED, shm_fd_, 0);
if (ptr == MAP_FAILED) {
throw runtime_error(strerror(errno));
}
buffer_ = ptr;
}
RamBufferWriter::~RamBufferWriter()
{
munmap(buffer_, buffer_size_);
shm_unlink(detector_name_.c_str());
close(shm_fd_);
}