Project
Loading...
Searching...
No Matches
ShmManager.h
Go to the documentation of this file.
1// Copyright 2019-2020 CERN and copyright holders of ALICE O2.
2// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
3// All rights not expressly granted are reserved.
4//
5// This software is distributed under the terms of the GNU General Public
6// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
7//
8// In applying this license CERN does not waive the privileges and immunities
9// granted to it by virtue of its status as an Intergovernmental Organization
10// or submit itself to any jurisdiction.
11
12/*
13 * ShmManager.h
14 *
15 * Created on: Jun 17, 2018
16 * Author: swenzel
17 */
18
19#ifndef COMMON_UTILS_INCLUDE_COMMONUTILS_SHMMANAGER_H_
20#define COMMON_UTILS_INCLUDE_COMMONUTILS_SHMMANAGER_H_
21
22#include <list>
23#include <cstddef>
24#include <atomic>
25#include <mutex>
26
27#if !defined(__CINT__) && !defined(__MAKECINT__) && !defined(__ROOTCLING__) && !defined(__CLING__)
28#include <boost/interprocess/managed_external_buffer.hpp>
29#include <boost/interprocess/allocators/allocator.hpp>
30#endif
31
32#if !defined(__CINT__) && !defined(__MAKECINT__) && !defined(__ROOTCLING__) && !defined(__CLING__) && !defined(__APPLE__)
33// this shared mem mode is meant for compiled stuff in o2-sim; not for ROOT sessions
34#define USESHM 1
35#endif
36
37namespace o2
38{
39namespace utils
40{
41
42// the size dedicated to each attached worker/process
43constexpr size_t SHMPOOLSIZE = 1024 * 1024 * 1024; // 1 GB
44
45// some meta info stored at the beginning of the global shared mem segment
47 unsigned long long allocedbytes = 0;
48 std::atomic<int> counter = 0; // atomic counter .. counter number of attached processes
49 // and used to assign a subregion to the attached processes
50 std::atomic<int> failures = 0;
51};
52
53// Class creating -- or attaching to -- a shared memory pool
54// and manages allocations within the pool
55// This is used in the parallel simulation in order
56// to put hits directly in shared mem; I hope this can be replaced/refactored
57// to use directly functionality by FairMQ some day.
58// For the moment a wrapper around boost allocators ... enhancing them with some state.
60{
61 public:
63 {
64 static ShmManager instance;
65 return instance;
66 }
67
68 // creates a global shared mem region
69 // to be used by "nsubsegments" simulation processes
70 bool createGlobalSegment(int nsubsegments = 1);
71
72 // create the local segment
73 // this will occupy a subregion of an already created global shared mem segment
74 // (all of it for the single process of multithreaded Geant4, whose threads share it)
75 void occupySegment(bool wholeRegion = false);
76
77 // simply attaches to the global segment
79
80 // the equivalent of malloc
81 void* getmemblock(size_t size);
82 // the equivalent of free
83 void freememblock(void*, std::size_t = 1);
84
85 void release();
86 int getShmID() const { return mShmID; }
87 bool hasSegment() const { return mShmID != -1; }
88 bool readyToAllocate() const { return mShmID != -1 && mBufferPtr; }
89
90 // returns if pointer is part of the shm region under control of this manager
91 bool isPointerOk(void* ptr) const
92 {
93 return mBufferPtr && getPointerOffset(ptr) < mPoolSize;
94 }
95
96 // returns if shared mem setup is correctly setup/operational
97 // used to decide whether to communicate via shared mem at runtime or via
98 // TMessages /etc/
99 bool isOperational() const { return mSegInfoPtr && mSegInfoPtr->failures == 0; /* mIsOperational; */ }
100
101 void disable()
102 {
103 if (mSegInfoPtr) {
104 mSegInfoPtr->failures.fetch_add(1);
105 };
106 }
107
108 void printSegInfo() const;
109
110 private:
111 ShmManager();
112 ~ShmManager();
113 int mShmID = -1; // id of shared mem created or used
114 void* mBufferPtr = nullptr; // the mapped/start ptr of the buffer to use
115 void* mSegPtr = nullptr; // address of the segment start
116 ShmMetaInfo* mSegInfoPtr = nullptr; // pointing to the meta information object
117 bool mIsMaster = false; // true if the manager who allocated the region
118 bool mIsOperational = false;
119 size_t mPoolSize = SHMPOOLSIZE; // size of the region occupied by this process
120 std::mutex mAllocMutex; // the boost buffer has no locking of its own; threads of multithreaded Geant4 share it
121 // helper function
122 void* tryAttach(bool& success);
123 size_t getPointerOffset(void* ptr) const { return (size_t)((char*)ptr - (char*)mBufferPtr); }
124
125#if !defined(__CINT__) && !defined(__MAKECINT__) && !defined(__ROOTCLING__) && !defined(__CLING__)
126 boost::interprocess::wmanaged_external_buffer* boostmanagedbuffer;
127 boost::interprocess::allocator<char, boost::interprocess::wmanaged_external_buffer::segment_manager>* boostallocator;
128#endif
129};
130
131} // namespace utils
132} // namespace o2
133
134#endif /* COMMON_UTILS_INCLUDE_COMMONUTILS_SHMMANAGER_H_ */
TBranch * ptr
int getShmID() const
Definition ShmManager.h:86
static ShmManager & Instance()
Definition ShmManager.h:62
void * getmemblock(size_t size)
bool isOperational() const
Definition ShmManager.h:99
void printSegInfo() const
bool readyToAllocate() const
Definition ShmManager.h:88
bool isPointerOk(void *ptr) const
Definition ShmManager.h:91
void freememblock(void *, std::size_t=1)
bool hasSegment() const
Definition ShmManager.h:87
bool createGlobalSegment(int nsubsegments=1)
void occupySegment(bool wholeRegion=false)
GLsizeiptr size
Definition glcorearb.h:659
constexpr size_t SHMPOOLSIZE
Definition ShmManager.h:43
a couple of static helper functions to create timestamp values for CCDB queries or override obsolete ...
Common utility functions.
std::atomic< int > failures
Definition ShmManager.h:50
unsigned long long allocedbytes
Definition ShmManager.h:47