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
26#if !defined(__CINT__) && !defined(__MAKECINT__) && !defined(__ROOTCLING__) && !defined(__CLING__)
27#include <boost/interprocess/managed_external_buffer.hpp>
28#include <boost/interprocess/allocators/allocator.hpp>
29#endif
30
31#if !defined(__CINT__) && !defined(__MAKECINT__) && !defined(__ROOTCLING__) && !defined(__CLING__) && !defined(__APPLE__)
32// this shared mem mode is meant for compiled stuff in o2-sim; not for ROOT sessions
33#define USESHM 1
34#endif
35
36namespace o2
37{
38namespace utils
39{
40
41// the size dedicated to each attached worker/process
42constexpr size_t SHMPOOLSIZE = 1024 * 1024 * 1024; // 1 GB
43
44// some meta info stored at the beginning of the global shared mem segment
46 unsigned long long allocedbytes = 0;
47 std::atomic<int> counter = 0; // atomic counter .. counter number of attached processes
48 // and used to assign a subregion to the attached processes
49 std::atomic<int> failures = 0;
50};
51
52// Class creating -- or attaching to -- a shared memory pool
53// and manages allocations within the pool
54// This is used in the parallel simulation in order
55// to put hits directly in shared mem; I hope this can be replaced/refactored
56// to use directly functionality by FairMQ some day.
57// For the moment a wrapper around boost allocators ... enhancing them with some state.
59{
60 public:
62 {
63 static ShmManager instance;
64 return instance;
65 }
66
67 // creates a global shared mem region
68 // to be used by "nsubsegments" simulation processes
69 bool createGlobalSegment(int nsubsegments = 1);
70
71 // create the local segment
72 // this will occupy a subregion of an already created global shared mem segment
73 void occupySegment();
74
75 // simply attaches to the global segment
77
78 // the equivalent of malloc
79 void* getmemblock(size_t size);
80 // the equivalent of free
81 void freememblock(void*, std::size_t = 1);
82
83 void release();
84 int getShmID() const { return mShmID; }
85 bool hasSegment() const { return mShmID != -1; }
86 bool readyToAllocate() const { return mShmID != -1 && mBufferPtr; }
87
88 // returns if pointer is part of the shm region under control of this manager
89 bool isPointerOk(void* ptr) const
90 {
91 return mBufferPtr && getPointerOffset(ptr) < SHMPOOLSIZE;
92 }
93
94 // returns if shared mem setup is correctly setup/operational
95 // used to decide whether to communicate via shared mem at runtime or via
96 // TMessages /etc/
97 bool isOperational() const { return mSegInfoPtr && mSegInfoPtr->failures == 0; /* mIsOperational; */ }
98
99 void disable()
100 {
101 if (mSegInfoPtr) {
102 mSegInfoPtr->failures.fetch_add(1);
103 };
104 }
105
106 void printSegInfo() const;
107
108 private:
109 ShmManager();
110 ~ShmManager();
111 int mShmID = -1; // id of shared mem created or used
112 void* mBufferPtr = nullptr; // the mapped/start ptr of the buffer to use
113 void* mSegPtr = nullptr; // address of the segment start
114 ShmMetaInfo* mSegInfoPtr = nullptr; // pointing to the meta information object
115 bool mIsMaster = false; // true if the manager who allocated the region
116 bool mIsOperational = false;
117 // helper function
118 void* tryAttach(bool& success);
119 size_t getPointerOffset(void* ptr) const { return (size_t)((char*)ptr - (char*)mBufferPtr); }
120
121#if !defined(__CINT__) && !defined(__MAKECINT__) && !defined(__ROOTCLING__) && !defined(__CLING__)
122 boost::interprocess::wmanaged_external_buffer* boostmanagedbuffer;
123 boost::interprocess::allocator<char, boost::interprocess::wmanaged_external_buffer::segment_manager>* boostallocator;
124#endif
125};
126
127} // namespace utils
128} // namespace o2
129
130#endif /* COMMON_UTILS_INCLUDE_COMMONUTILS_SHMMANAGER_H_ */
TBranch * ptr
int getShmID() const
Definition ShmManager.h:84
static ShmManager & Instance()
Definition ShmManager.h:61
void * getmemblock(size_t size)
bool isOperational() const
Definition ShmManager.h:97
void printSegInfo() const
bool readyToAllocate() const
Definition ShmManager.h:86
bool isPointerOk(void *ptr) const
Definition ShmManager.h:89
void freememblock(void *, std::size_t=1)
bool hasSegment() const
Definition ShmManager.h:85
bool createGlobalSegment(int nsubsegments=1)
GLsizeiptr size
Definition glcorearb.h:659
GLuint counter
Definition glcorearb.h:3987
constexpr size_t SHMPOOLSIZE
Definition ShmManager.h:42
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:49
unsigned long long allocedbytes
Definition ShmManager.h:46