ddt  0.1
ddtMemoryManager.hpp
Go to the documentation of this file.
1 // @copyright
2 // (c) Copyright ESO 2020
3 // All Rights Reserved
4 // ESO (eso.org) is an Intergovernmental Organization, and therefore special
5 // legal conditions apply.
6 //
7 // @file ddtMemoryManager.hpp
8 // @brief Manager for shared memories.
9 //
10 // This class manages the handling of shared memories.
11 //
12 // @author Matthias Grimm, CGI
13 // @since 2020/01/16
14 //
15 
16 #ifndef DDTMEMORYMANAGER_HPP_
17 #define DDTMEMORYMANAGER_HPP_
18 
19 #include <boost/circular_buffer.hpp>
20 #include <boost/interprocess/containers/string.hpp>
21 #include <boost/interprocess/managed_shared_memory.hpp>
22 #include <boost/tokenizer.hpp>
23 #include <csignal>
24 #include <fstream>
25 #include <iostream>
26 
27 #include "ddt/ddtConstants.hpp"
28 #include "ddt/ddtErrorCodes.hpp"
29 #include "ddt/ddtLogger.hpp"
31 
32 namespace ip = boost::interprocess;
33 
34 namespace ddt {
35 
40 struct Shm {
41  ip::managed_shared_memory *managed_shm;
45 };
46 
51  public:
58  DdtMemoryManager(DdtLogger *logger, const std::string broker_port);
59 
63  virtual ~DdtMemoryManager();
64 
75  int32_t CreateSharedMemory(const std::string data_stream_identifier,
76  const int32_t max_data_sample_size,
77  const int32_t number_of_samples);
78 
85  bool DeleteSharedMemory(const std::string data_stream_identifier);
86 
93  bool CheckIfShmExists(const std::string data_stream_identifier);
94 
100  int32_t get_max_data_sample_size(const std::string data_stream_identifier);
101 
107  int32_t get_number_of_samples(const std::string data_stream_identifier);
108 
115  const std::string data_stream_identifier);
116 
123  std::string get_shm_id(const std::string data_stream_identifier) const;
124 
130  int32_t GetMaxPossibleBufferSize(const int32_t max_data_sample_size);
131 
132  protected:
139  bool CheckSpaceAvailability(const int32_t max_data_sample_size,
140  const int buffer_size);
141 
146  const uint64_t GetAvailableSpace();
147 
148  private:
155  void Init(DdtLogger *ddt_logger, const std::string broker_port);
156 
160  int32_t CreateNewSharedMemory(const std::string data_stream_identifier,
161  const int32_t max_data_sample_size,
162  const int32_t number_of_samples,
163  const std::string shm_id);
164 
168  int32_t CreateCircularBuffer(const std::string data_stream_identifier,
169  const int32_t max_data_sample_size,
170  const int32_t number_of_samples);
171 
176  std::map<std::string, Shm> shm_map;
177 
178  std::mutex shm_mutex;
179  std::string port;
180  DdtLogger *logger;
181 
182  const std::string SHM_PATH = "/dev/shm";
183  const int AVAILABLE_POSITION = 3;
184  const int BYTES_PER_KBYTE = 1024;
185  static const int CHAR_LEN = 128;
186  const int PERM_TYPE = 0664;
187  const int NO_SUCH_SHM = 7;
188 };
189 
190 } // namespace ddt
191 
192 #endif /* DDTMEMORYMANAGER_HPP_ */
ddt::DdtMemoryManager::get_number_of_samples
int32_t get_number_of_samples(const std::string data_stream_identifier)
Definition: ddtMemoryManager.cpp:307
ddtErrorCodes.hpp
ddt::DdtMemoryManager::CheckSpaceAvailability
bool CheckSpaceAvailability(const int32_t max_data_sample_size, const int buffer_size)
Definition: ddtMemoryManager.cpp:384
ddt::DdtLogger
Definition: ddtLogger.hpp:48
ddt::DdtMemoryAccessor
Definition: ddtMemoryAccessor.hpp:257
ddtConstants.hpp
ddt
Definition: ddtClient.hpp:36
ddt::DdtMemoryManager::GetMaxPossibleBufferSize
int32_t GetMaxPossibleBufferSize(const int32_t max_data_sample_size)
Definition: ddtMemoryManager.cpp:372
ddt::DdtMemoryManager::GetAvailableSpace
const uint64_t GetAvailableSpace()
Definition: ddtMemoryManager.cpp:333
ddt::DdtMemoryManager
Definition: ddtMemoryManager.hpp:50
ddt::DdtMemoryManager::CheckIfShmExists
bool CheckIfShmExists(const std::string data_stream_identifier)
Definition: ddtMemoryManager.cpp:43
ddt::DdtMemoryManager::get_max_data_sample_size
int32_t get_max_data_sample_size(const std::string data_stream_identifier)
Definition: ddtMemoryManager.cpp:294
ddt::DdtMemoryManager::get_shm_id
std::string get_shm_id(const std::string data_stream_identifier) const
Definition: ddtMemoryManager.cpp:33
ddt::DdtMemoryManager::get_memory_accessor
DdtMemoryAccessor * get_memory_accessor(const std::string data_stream_identifier)
Definition: ddtMemoryManager.cpp:320
ddt::Shm::managed_shm
ip::managed_shared_memory * managed_shm
Definition: ddtMemoryManager.hpp:41
ddt::DdtMemoryManager::DeleteSharedMemory
bool DeleteSharedMemory(const std::string data_stream_identifier)
Definition: ddtMemoryManager.cpp:266
ddt::Shm
Definition: ddtMemoryManager.hpp:40
ddt::Shm::number_of_samples
int number_of_samples
Definition: ddtMemoryManager.hpp:43
ddt::DdtMemoryManager::CreateSharedMemory
int32_t CreateSharedMemory(const std::string data_stream_identifier, const int32_t max_data_sample_size, const int32_t number_of_samples)
Definition: ddtMemoryManager.cpp:56
ddt::Shm::max_data_sample_size
int max_data_sample_size
Definition: ddtMemoryManager.hpp:42
ddt::Shm::memory_accessor
DdtMemoryAccessor * memory_accessor
Definition: ddtMemoryManager.hpp:44
ddt::DdtMemoryManager::DdtMemoryManager
DdtMemoryManager(DdtLogger *logger, const std::string broker_port)
Definition: ddtMemoryManager.cpp:20
ddt::DdtMemoryManager::~DdtMemoryManager
virtual ~DdtMemoryManager()
ddtLogger.hpp
ddtMemoryAccessor.hpp