12 #ifndef SST_CORE_INTERPROCESS_TUNNEL_DEF_H 13 #define SST_CORE_INTERPROCESS_TUNNEL_DEF_H 23 #include "sst/core/interprocess/circularBuffer.h" 31 namespace Interprocess {
33 extern uint32_t globalMMAPIPCCount;
38 volatile uint32_t expectedChildren;
55 template <
typename ShareDataType,
typename MsgType>
68 TunnelDef(
size_t numBuffers,
size_t bufferSize, uint32_t expectedChildren) : master(true), shmPtr(NULL)
71 numBuffs = numBuffers;
72 buffSize = bufferSize;
73 children = expectedChildren;
74 shmSize = calculateShmemSize(numBuffers, bufferSize);
84 shmSize = isd->shmSegSize;
97 nextAllocPtr = (uint8_t*)shmPtr;
101 std::pair<size_t, InternalSharedData*> aResult =
102 reserveSpace<InternalSharedData>((1 + numBuffs) *
sizeof(
size_t));
103 isd = aResult.second;
104 isd->expectedChildren = children;
105 isd->shmSegSize = shmSize;
106 isd->numBuffers = numBuffs;
109 std::pair<size_t, ShareDataType*> bResult = reserveSpace<ShareDataType>(0);
110 isd->offsets[0] = bResult.first;
114 const size_t cbSize =
sizeof(MsgType) * buffSize;
115 for (
size_t c = 0; c < isd->numBuffers; c++ ) {
118 std::pair<size_t, CircBuff_t*> cResult = reserveSpace<CircBuff_t>(cbSize);
119 isd->offsets[1 + c] = cResult.first;
120 cPtr = cResult.second;
121 if ( !cPtr->setBufferSize(buffSize) ) exit(1);
122 circBuffs.push_back(cPtr);
124 return isd->expectedChildren;
128 shmSize = isd->shmSegSize;
130 sharedData = (ShareDataType*)((uint8_t*)shmPtr + isd->offsets[0]);
132 for (
size_t c = 0; c < isd->numBuffers; c++ ) {
133 circBuffs.push_back((
CircBuff_t*)((uint8_t*)shmPtr + isd->offsets[c + 1]));
135 numBuffs = isd->numBuffers;
137 return --(isd->expectedChildren);
148 for (
size_t i = 0; i < circBuffs.size(); i++ ) {
149 circBuffs[i]->~CircBuff_t();
171 void writeMessage(
size_t buffer,
const MsgType& command) { circBuffs[buffer]->write(command); }
177 MsgType
readMessage(
size_t buffer) {
return circBuffs[buffer]->read(); }
184 bool readMessageNB(
size_t buffer, MsgType* result) {
return circBuffs[buffer]->readNB(result); }
189 void clearBuffer(
size_t buffer) { circBuffs[buffer]->clearBuffer(); }
200 template <
typename T>
201 std::pair<size_t, T*> reserveSpace(
size_t extraSpace = 0)
203 size_t space =
sizeof(T) + extraSpace;
204 if ( (
size_t)((nextAllocPtr + space) - (uint8_t*)shmPtr) > shmSize )
return std::make_pair<size_t, T*>(0, NULL);
205 T* ptr = (T*)nextAllocPtr;
206 nextAllocPtr += space;
208 return std::make_pair((uint8_t*)ptr - (uint8_t*)shmPtr, ptr);
212 static size_t calculateShmemSize(
size_t numBuffers,
size_t bufferSize)
214 long pagesize = sysconf(_SC_PAGESIZE);
216 size_t isd = 1 + ((
sizeof(InternalSharedData) + (1 + numBuffers) *
sizeof(size_t)) / pagesize);
217 size_t buffer = 1 + ((
sizeof(CircularBuffer<MsgType>) + bufferSize *
sizeof(MsgType)) / pagesize);
218 size_t shdata = 1 + ((
sizeof(ShareDataType) +
sizeof(InternalSharedData)) / pagesize);
221 return (2 + isd + shdata + numBuffers * buffer) * pagesize;
235 uint8_t* nextAllocPtr;
245 std::vector<CircBuff_t*> circBuffs;
252 #endif // SST_CORE_INTERPROCESS_TUNNEL_DEF_H ShareDataType * getSharedData()
return a pointer to the ShareDataType region
Definition: tunneldef.h:165
void clearBuffer(size_t buffer)
Empty the messages in a buffer.
Definition: tunneldef.h:189
size_t getTunnelSize()
return size of tunnel
Definition: tunneldef.h:162
ShareDataType * sharedData
Pointer to the Shared Data Region.
Definition: tunneldef.h:226
TunnelDef(void *sPtr)
Access an existing tunnel Child creates the TunnelDef, reads the shmSize, and then resizes its map ac...
Definition: tunneldef.h:81
size_t getNumBuffers()
Return the number of buffers.
Definition: tunneldef.h:229
uint32_t initialize(void *sPtr)
Finish setting up a tunnel once the manager knows the correct size of the tunnel and has mmap'd a lar...
Definition: tunneldef.h:92
Definition: circularBuffer.h:22
void shutdown()
Clean up a region.
Definition: tunneldef.h:145
This class defines a shared-memory region between a master process and one or more child processes Re...
Definition: tunneldef.h:56
virtual ~TunnelDef()
Destructor.
Definition: tunneldef.h:142
bool readMessageNB(size_t buffer, MsgType *result)
Read data from buffer, non-blocking.
Definition: tunneldef.h:184
Definition: tunneldef.h:36
void writeMessage(size_t buffer, const MsgType &command)
Write data to buffer, blocks until space is available.
Definition: tunneldef.h:171
MsgType readMessage(size_t buffer)
Read data from buffer, blocks until message received.
Definition: tunneldef.h:177
TunnelDef(size_t numBuffers, size_t bufferSize, uint32_t expectedChildren)
Create a new tunnel.
Definition: tunneldef.h:68
bool isMaster()
return whether this is a master-side tunnel or a child
Definition: tunneldef.h:192