12 #ifndef SST_CORE_INTERPROCESS_TUNNEL_DEF_H
13 #define SST_CORE_INTERPROCESS_TUNNEL_DEF_H 1
27 #include "sst/core/interprocess/circularBuffer.h"
31 namespace Interprocess {
34 extern uint32_t globalMMAPIPCCount;
38 volatile uint32_t expectedChildren;
55 template<
typename ShareDataType,
typename MsgType>
67 TunnelDef(
size_t numBuffers,
size_t bufferSize, uint32_t expectedChildren) : master(true), shmPtr(NULL) {
69 numBuffs = numBuffers;
70 buffSize = bufferSize;
71 children = expectedChildren;
72 shmSize = calculateShmemSize(numBuffers, bufferSize);
79 TunnelDef(
void* sPtr) : master(false), shmPtr(sPtr) {
81 shmSize = isd->shmSegSize;
93 nextAllocPtr = (uint8_t*)shmPtr;
97 std::pair<size_t, InternalSharedData*> aResult = reserveSpace<InternalSharedData>((1 + numBuffs) *
sizeof(
size_t));
99 isd->expectedChildren = children;
100 isd->shmSegSize = shmSize;
101 isd->numBuffers = numBuffs;
104 std::pair<size_t, ShareDataType*> bResult = reserveSpace<ShareDataType>(0);
105 isd->offsets[0] = bResult.first;
109 const size_t cbSize =
sizeof(MsgType) * buffSize;
110 for (
size_t c = 0; c < isd->numBuffers; c++) {
113 std::pair<size_t, CircBuff_t*> cResult = reserveSpace<CircBuff_t>(cbSize);
114 isd->offsets[1+c] = cResult.first;
115 cPtr = cResult.second;
116 if (!cPtr->setBufferSize(buffSize))
118 circBuffs.push_back(cPtr);
120 return isd->expectedChildren;
123 shmSize = isd->shmSegSize;
125 sharedData = (ShareDataType*)((uint8_t*)shmPtr + isd->offsets[0]);
127 for (
size_t c = 0 ; c < isd->numBuffers ; c++ ) {
128 circBuffs.push_back((
CircBuff_t*)((uint8_t*)shmPtr + isd->offsets[c+1]));
130 numBuffs = isd->numBuffers;
132 return --(isd->expectedChildren);
146 for (
size_t i = 0; i < circBuffs.size(); i++) {
147 circBuffs[i]->~CircBuff_t();
170 circBuffs[buffer]->write(command);
178 return circBuffs[buffer]->read();
187 return circBuffs[buffer]->readNB(result);
194 circBuffs[buffer]->clearBuffer();
208 template <
typename T>
209 std::pair<size_t, T*> reserveSpace(
size_t extraSpace = 0)
211 size_t space =
sizeof(T) + extraSpace;
212 if ( (
size_t)((nextAllocPtr + space) - (uint8_t*)shmPtr) > shmSize )
213 return std::make_pair<size_t, T*>(0, NULL);
214 T* ptr = (T*)nextAllocPtr;
215 nextAllocPtr += space;
217 return std::make_pair((uint8_t*)ptr - (uint8_t*)shmPtr, ptr);
221 static size_t calculateShmemSize(
size_t numBuffers,
size_t bufferSize) {
222 long pagesize = sysconf(_SC_PAGESIZE);
224 size_t isd = 1 + ((
sizeof(InternalSharedData) + (1+numBuffers)*
sizeof(size_t)) / pagesize);
225 size_t buffer = 1 + ((
sizeof(CircularBuffer<MsgType>) + bufferSize*
sizeof(MsgType)) / pagesize);
226 size_t shdata = 1 + ((
sizeof(ShareDataType) +
sizeof(InternalSharedData)) / pagesize);
229 return (2 + isd + shdata + numBuffers*buffer) * pagesize;
244 uint8_t *nextAllocPtr;
254 std::vector<CircBuff_t* > circBuffs;
ShareDataType * getSharedData()
return a pointer to the ShareDataType region
Definition: tunneldef.h:163
void clearBuffer(size_t buffer)
Empty the messages in a buffer.
Definition: tunneldef.h:193
size_t getTunnelSize()
return size of tunnel
Definition: tunneldef.h:160
ShareDataType * sharedData
Pointer to the Shared Data Region.
Definition: tunneldef.h:235
TunnelDef(void *sPtr)
Access an existing tunnel Child creates the TunnelDef, reads the shmSize, and then resizes its map ac...
Definition: tunneldef.h:79
size_t getNumBuffers()
Return the number of buffers.
Definition: tunneldef.h:238
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:89
Definition: circularBuffer.h:22
void shutdown()
Clean up a region.
Definition: tunneldef.h:144
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:138
bool readMessageNB(size_t buffer, MsgType *result)
Read data from buffer, non-blocking.
Definition: tunneldef.h:186
Definition: tunneldef.h:37
void writeMessage(size_t buffer, const MsgType &command)
Write data to buffer, blocks until space is available.
Definition: tunneldef.h:169
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:67
bool isMaster()
return whether this is a master-side tunnel or a child
Definition: tunneldef.h:198