FairMQ  1.4.33
C++ Message Queuing Library and Framework
Common.h
1 /********************************************************************************
2  * Copyright (C) 2014 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH *
3  * *
4  * This software is distributed under the terms of the *
5  * GNU Lesser General Public Licence (LGPL) version 3, *
6  * copied verbatim in the file "LICENSE" *
7  ********************************************************************************/
8 #ifndef FAIR_MQ_SHMEM_COMMON_H_
9 #define FAIR_MQ_SHMEM_COMMON_H_
10 
11 #include <picosha2.h>
12 
13 #include <atomic>
14 #include <sstream>
15 #include <string>
16 #include <functional> // std::equal_to
17 
18 #include <boost/functional/hash.hpp>
19 #include <boost/interprocess/allocators/allocator.hpp>
20 #include <boost/interprocess/containers/map.hpp>
21 #include <boost/interprocess/containers/string.hpp>
22 #include <boost/interprocess/containers/vector.hpp>
23 #include <boost/interprocess/indexes/null_index.hpp>
24 #include <boost/interprocess/managed_shared_memory.hpp>
25 #include <boost/interprocess/mem_algo/simple_seq_fit.hpp>
26 #include <boost/unordered_map.hpp>
27 #include <boost/variant.hpp>
28 
29 #include <unistd.h>
30 #include <sys/types.h>
31 
32 namespace fair::mq::shmem
33 {
34 
35 struct SharedMemoryError : std::runtime_error { using std::runtime_error::runtime_error; };
36 
37 using SimpleSeqFitSegment = boost::interprocess::basic_managed_shared_memory<char,
38  boost::interprocess::simple_seq_fit<boost::interprocess::mutex_family>,
39  boost::interprocess::null_index>;
40  // boost::interprocess::iset_index>;
41 using RBTreeBestFitSegment = boost::interprocess::basic_managed_shared_memory<char,
42  boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family>,
43  boost::interprocess::null_index>;
44  // boost::interprocess::iset_index>;
45 
46 using SegmentManager = boost::interprocess::managed_shared_memory::segment_manager;
47 using VoidAlloc = boost::interprocess::allocator<void, SegmentManager>;
48 using CharAlloc = boost::interprocess::allocator<char, SegmentManager>;
49 using Str = boost::interprocess::basic_string<char, std::char_traits<char>, CharAlloc>;
50 using StrAlloc = boost::interprocess::allocator<Str, SegmentManager>;
51 using StrVector = boost::interprocess::vector<Str, StrAlloc>;
52 
53 enum class AllocationAlgorithm : int
54 {
55  rbtree_best_fit,
56  simple_seq_fit
57 };
58 
59 struct RegionInfo
60 {
61  RegionInfo(const VoidAlloc& alloc)
62  : fPath("", alloc)
63  , fFlags(0)
64  , fUserFlags(0)
65  , fDestroyed(false)
66  {}
67 
68  RegionInfo(const char* path, const int flags, const uint64_t userFlags, const VoidAlloc& alloc)
69  : fPath(path, alloc)
70  , fFlags(flags)
71  , fUserFlags(userFlags)
72  , fDestroyed(false)
73  {}
74 
75  Str fPath;
76  int fFlags;
77  uint64_t fUserFlags;
78  bool fDestroyed;
79 };
80 
81 using Uint16RegionInfoPairAlloc = boost::interprocess::allocator<std::pair<const uint16_t, RegionInfo>, SegmentManager>;
82 using Uint16RegionInfoMap = boost::interprocess::map<uint16_t, RegionInfo, std::less<uint16_t>, Uint16RegionInfoPairAlloc>;
83 using Uint16RegionInfoHashMap = boost::unordered_map<uint16_t, RegionInfo, boost::hash<uint16_t>, std::equal_to<uint16_t>, Uint16RegionInfoPairAlloc>;
84 
85 struct SegmentInfo
86 {
87  SegmentInfo(AllocationAlgorithm aa)
88  : fAllocationAlgorithm(aa)
89  {}
90 
91  AllocationAlgorithm fAllocationAlgorithm;
92 };
93 
94 using Uint16SegmentInfoPairAlloc = boost::interprocess::allocator<std::pair<const uint16_t, SegmentInfo>, SegmentManager>;
95 using Uint16SegmentInfoHashMap = boost::unordered_map<uint16_t, SegmentInfo, boost::hash<uint16_t>, std::equal_to<uint16_t>, Uint16SegmentInfoPairAlloc>;
96 // using Uint16SegmentInfoMap = boost::interprocess::map<uint16_t, SegmentInfo, std::less<uint16_t>, Uint16SegmentInfoPairAlloc>;
97 
98 struct DeviceCounter
99 {
100  DeviceCounter(unsigned int c)
101  : fCount(c)
102  {}
103 
104  std::atomic<unsigned int> fCount;
105 };
106 
107 struct EventCounter
108 {
109  EventCounter(uint64_t c)
110  : fCount(c)
111  {}
112 
113  std::atomic<uint64_t> fCount;
114 };
115 
116 struct RegionCounter
117 {
118  RegionCounter(uint16_t c)
119  : fCount(c)
120  {}
121 
122  std::atomic<uint16_t> fCount;
123 };
124 
125 struct MetaHeader
126 {
127  size_t fSize;
128  size_t fHint;
129  uint16_t fRegionId;
130  uint16_t fSegmentId;
131  boost::interprocess::managed_shared_memory::handle_t fHandle;
132 };
133 
134 #ifdef FAIRMQ_DEBUG_MODE
135 struct MsgCounter
136 {
137  MsgCounter()
138  : fCount(0)
139  {}
140 
141  MsgCounter(unsigned int c)
142  : fCount(c)
143  {}
144 
145  std::atomic<unsigned int> fCount;
146 };
147 
148 using Uint16MsgCounterPairAlloc = boost::interprocess::allocator<std::pair<const uint16_t, MsgCounter>, SegmentManager>;
149 using Uint16MsgCounterHashMap = boost::unordered_map<uint16_t, MsgCounter, boost::hash<uint16_t>, std::equal_to<uint16_t>, Uint16MsgCounterPairAlloc>;
150 
151 struct MsgDebug
152 {
153  MsgDebug()
154  : fPid(0)
155  , fSize(0)
156  , fCreationTime(0)
157  {}
158 
159  MsgDebug(pid_t pid, size_t size, const uint64_t creationTime)
160  : fPid(pid)
161  , fSize(size)
162  , fCreationTime(creationTime)
163  {}
164 
165  pid_t fPid;
166  size_t fSize;
167  uint64_t fCreationTime;
168 };
169 
170 using SizetMsgDebugPairAlloc = boost::interprocess::allocator<std::pair<const size_t, MsgDebug>, SegmentManager>;
171 // using SizetMsgDebugHashMap = boost::unordered_map<size_t, MsgDebug, boost::hash<size_t>, std::equal_to<size_t>, SizetMsgDebugPairAlloc>;
172 using SizetMsgDebugMap = boost::interprocess::map<size_t, MsgDebug, std::less<size_t>, SizetMsgDebugPairAlloc>;
173 using Uint16MsgDebugMapPairAlloc = boost::interprocess::allocator<std::pair<const uint16_t, SizetMsgDebugMap>, SegmentManager>;
174 using Uint16MsgDebugMapHashMap = boost::unordered_map<uint16_t, SizetMsgDebugMap, boost::hash<uint16_t>, std::equal_to<uint16_t>, Uint16MsgDebugMapPairAlloc>;
175 #endif
176 
177 struct RegionBlock
178 {
179  RegionBlock()
180  : fHandle()
181  , fSize(0)
182  , fHint(0)
183  {}
184 
185  RegionBlock(boost::interprocess::managed_shared_memory::handle_t handle, size_t size, size_t hint)
186  : fHandle(handle)
187  , fSize(size)
188  , fHint(hint)
189  {}
190 
191  boost::interprocess::managed_shared_memory::handle_t fHandle;
192  size_t fSize;
193  size_t fHint;
194 };
195 
196 // find id for unique shmem name:
197 // a hash of user id + session id, truncated to 8 characters (to accommodate for name size limit on some systems (MacOS)).
198 inline std::string makeShmIdStr(const std::string& sessionId)
199 {
200  std::string seed((std::to_string(geteuid()) + sessionId));
201  // generate a 8-digit hex value out of sha256 hash
202  std::vector<unsigned char> hash(4);
203  picosha2::hash256(seed.begin(), seed.end(), hash.begin(), hash.end());
204 
205  return picosha2::bytes_to_hex_string(hash.begin(), hash.end());
206 }
207 
208 inline uint64_t makeShmIdUint64(const std::string& sessionId)
209 {
210  std::string shmId = makeShmIdStr(sessionId);
211  uint64_t id = 0;
212  std::stringstream ss;
213  ss << std::hex << shmId;
214  ss >> id;
215 
216  return id;
217 }
218 
219 struct SegmentSize : public boost::static_visitor<size_t>
220 {
221  template<typename S>
222  size_t operator()(S& s) const { return s.get_size(); }
223 };
224 
225 struct SegmentAddress : public boost::static_visitor<void*>
226 {
227  template<typename S>
228  void* operator()(S& s) const { return s.get_address(); }
229 };
230 
231 struct SegmentMemoryZeroer : public boost::static_visitor<>
232 {
233  template<typename S>
234  void operator()(S& s) const { s.zero_free_memory(); }
235 };
236 
237 struct SegmentFreeMemory : public boost::static_visitor<size_t>
238 {
239  template<typename S>
240  size_t operator()(S& s) const { return s.get_free_memory(); }
241 };
242 
243 struct SegmentHandleFromAddress : public boost::static_visitor<boost::interprocess::managed_shared_memory::handle_t>
244 {
245  SegmentHandleFromAddress(const void* _ptr) : ptr(_ptr) {}
246 
247  template<typename S>
248  boost::interprocess::managed_shared_memory::handle_t operator()(S& s) const { return s.get_handle_from_address(ptr); }
249 
250  const void* ptr;
251 };
252 
253 struct SegmentAddressFromHandle : public boost::static_visitor<void*>
254 {
255  SegmentAddressFromHandle(const boost::interprocess::managed_shared_memory::handle_t _handle) : handle(_handle) {}
256 
257  template<typename S>
258  void* operator()(S& s) const { return s.get_address_from_handle(handle); }
259 
260  const boost::interprocess::managed_shared_memory::handle_t handle;
261 };
262 
263 struct SegmentAllocate : public boost::static_visitor<void*>
264 {
265  SegmentAllocate(const size_t _size) : size(_size) {}
266 
267  template<typename S>
268  void* operator()(S& s) const { return s.allocate(size); }
269 
270  const size_t size;
271 };
272 
273 struct SegmentAllocateAligned : public boost::static_visitor<void*>
274 {
275  SegmentAllocateAligned(const size_t _size, const size_t _alignment) : size(_size), alignment(_alignment) {}
276 
277  template<typename S>
278  void* operator()(S& s) const { return s.allocate_aligned(size, alignment); }
279 
280  const size_t size;
281  const size_t alignment;
282 };
283 
284 struct SegmentBufferShrink : public boost::static_visitor<char*>
285 {
286  SegmentBufferShrink(const size_t _new_size, char* _local_ptr)
287  : new_size(_new_size)
288  , local_ptr(_local_ptr)
289  {}
290 
291  template<typename S>
292  char* operator()(S& s) const
293  {
294  boost::interprocess::managed_shared_memory::size_type shrunk_size = new_size;
295  return s.template allocation_command<char>(boost::interprocess::shrink_in_place, new_size + 128, shrunk_size, local_ptr);
296  }
297 
298  const size_t new_size;
299  mutable char* local_ptr;
300 };
301 
302 struct SegmentDeallocate : public boost::static_visitor<>
303 {
304  SegmentDeallocate(void* _ptr) : ptr(_ptr) {}
305 
306  template<typename S>
307  void operator()(S& s) const { return s.deallocate(ptr); }
308 
309  void* ptr;
310 };
311 
312 } // namespace fair::mq::shmem
313 
314 #endif /* FAIR_MQ_SHMEM_COMMON_H_ */
fair::mq::shmem::DeviceCounter
Definition: Common.h:105
fair::mq::shmem::EventCounter
Definition: Common.h:114
fair::mq::shmem::SegmentAllocate
Definition: Common.h:270
fair::mq::shmem::RegionBlock
Definition: Common.h:184
fair::mq::shmem::SegmentHandleFromAddress
Definition: Common.h:250
fair::mq::shmem::SegmentMemoryZeroer
Definition: Common.h:238
fair::mq::shmem::RegionCounter
Definition: Common.h:123
fair::mq::shmem::SegmentFreeMemory
Definition: Common.h:244
fair::mq::shmem::SegmentAllocateAligned
Definition: Common.h:280
fair::mq::shmem::SegmentAddressFromHandle
Definition: Common.h:260
fair::mq::shmem::RegionInfo
Definition: Common.h:66
fair::mq::shmem::SegmentAddress
Definition: Common.h:232
fair::mq::shmem::MetaHeader
Definition: Common.h:132
fair::mq::shmem::SharedMemoryError
Definition: Common.h:41
fair::mq::shmem
Definition: Common.h:33
fair::mq::shmem::SegmentBufferShrink
Definition: Common.h:291

privacy