SST 15.0
Structural Simulation Toolkit
tunneldef.h
1// Copyright 2009-2025 NTESS. Under the terms
2// of Contract DE-NA0003525 with NTESS, the U.S.
3// Government retains certain rights in this software.
4//
5// Copyright (c) 2009-2025, NTESS
6// All rights reserved.
7//
8// This file is part of the SST software package. For license
9// information, see the LICENSE file in the top level directory of the
10// distribution.
11
12#ifndef SST_CORE_INTERPROCESS_TUNNEL_DEF_H
13#define SST_CORE_INTERPROCESS_TUNNEL_DEF_H
14
15/*
16 * This may be compiled into both SST and an Intel Pin3 tool
17 * Therefore it has the following restrictions:
18 * - Nothing that uses runtime type info (e.g., dynamic cast)
19 * - No c++11 (auto, nullptr, etc.)
20 * - Includes must all be PinCRT-enabled but not require PinCRT
21 */
22
23#include "sst/core/interprocess/circularBuffer.h"
24
25#include <inttypes.h>
26#include <unistd.h>
27#include <vector>
28
29namespace SST::Core::Interprocess {
30
31extern uint32_t globalMMAPIPCCount;
32
33/* Internal bookkeeping */
35{
36 volatile uint32_t expectedChildren;
37 size_t shmSegSize;
38 size_t numBuffers;
39 size_t offsets[0]; // offset[0] points to user region, offset[1]... points to circular buffers
40};
41
42/**
43 * This class defines a shared-memory region between a master process and
44 * one or more child processes
45 * Region has three data structures:
46 * - internal bookkeeping (InternalSharedData),
47 * - user defined shared data (ShareDataType)
48 * - multiple circular-buffer queues with entries of type MsgType
49 *
50 * @tparam ShareDataType Type to put in the shared data region
51 * @tparam MsgType Type of messages being sent in the circular buffers
52 */
53template <typename ShareDataType, typename MsgType>
55{
56
58
59public:
60 /** Create a new tunnel
61 *
62 * @param numBuffers Number of buffers for which we should tunnel
63 * @param bufferSize How large each buffer should be
64 * @param expectedChildren Number of child processes that will connect to this tunnel
65 */
66 TunnelDef(size_t numBuffers, size_t bufferSize, uint32_t expectedChildren) :
67 master(true),
68 shmPtr(NULL)
69 {
70 // Locally buffer info
71 numBuffs = numBuffers;
72 buffSize = bufferSize;
73 children = expectedChildren;
74 shmSize = calculateShmemSize(numBuffers, bufferSize);
75 }
76
77 /** Access an existing tunnel
78 * Child creates the TunnelDef, reads the shmSize, and then resizes its map accordingly
79 * @param sPtr Location of shared memory region
80 */
81 explicit TunnelDef(void* sPtr) :
82 master(false),
83 shmPtr(sPtr)
84 {
85 isd = (InternalSharedData*)shmPtr;
86 shmSize = isd->shmSegSize;
87 }
88
89 /** Finish setting up a tunnel once the manager knows the correct size of the tunnel
90 * and has mmap'd a large enough region for it
91 * @param sPtr Location of shared memory region
92 * returns number of children left to attach
93 */
94 uint32_t initialize(void* sPtr)
95 {
96 shmPtr = sPtr;
97
98 if ( master ) {
99 nextAllocPtr = (uint8_t*)shmPtr;
100
101 // Reserve space for InternalSharedData
102 // Including an offset array entry for each buffer & sharedData structure
103 std::pair<size_t, InternalSharedData*> aResult =
104 reserveSpace<InternalSharedData>((1 + numBuffs) * sizeof(size_t));
105 isd = aResult.second;
106 isd->expectedChildren = children;
107 isd->shmSegSize = shmSize;
108 isd->numBuffers = numBuffs;
109
110 // Reserve space for ShareDataType structure
111 std::pair<size_t, ShareDataType*> bResult = reserveSpace<ShareDataType>(0);
112 isd->offsets[0] = bResult.first;
113 sharedData = bResult.second;
114
115 // Reserve space for circular buffers
116 const size_t cbSize = sizeof(MsgType) * buffSize;
117 for ( size_t c = 0; c < isd->numBuffers; c++ ) {
118 CircBuff_t* cPtr = NULL;
119
120 std::pair<size_t, CircBuff_t*> cResult = reserveSpace<CircBuff_t>(cbSize);
121 isd->offsets[1 + c] = cResult.first;
122 cPtr = cResult.second;
123 if ( !cPtr->setBufferSize(buffSize) ) exit(1); // function prints error message
124 circBuffs.push_back(cPtr);
125 }
126 return isd->expectedChildren;
127 }
128 else {
129 isd = (InternalSharedData*)shmPtr;
130 shmSize = isd->shmSegSize;
131
132 sharedData = (ShareDataType*)((uint8_t*)shmPtr + isd->offsets[0]);
133
134 for ( size_t c = 0; c < isd->numBuffers; c++ ) {
135 circBuffs.push_back((CircBuff_t*)((uint8_t*)shmPtr + isd->offsets[c + 1]));
136 }
137 numBuffs = isd->numBuffers;
138
139 return --(isd->expectedChildren);
140 }
141 }
142
143 /** Destructor */
144 virtual ~TunnelDef() { shutdown(); }
145
146 /** Clean up a region */
147 void shutdown()
148 {
149 if ( master ) {
150 for ( size_t i = 0; i < circBuffs.size(); i++ ) {
151 circBuffs[i]->~CircBuff_t();
152 }
153 }
154
155 if ( shmPtr ) {
156 shmPtr = NULL;
157 isd = NULL;
158 sharedData = NULL;
159 shmSize = 0;
160 }
161 }
162
163 /** return size of tunnel */
164 size_t getTunnelSize() { return shmSize; }
165
166 /** return a pointer to the ShareDataType region */
167 ShareDataType* getSharedData() { return sharedData; }
168
169 /** Write data to buffer, blocks until space is available
170 * @param buffer which buffer index to write to
171 * @param command message to write to buffer
172 */
173 void writeMessage(size_t buffer, const MsgType& command) { circBuffs[buffer]->write(command); }
174
175 /** Read data from buffer, blocks until message received
176 * @param buffer which buffer to read from
177 * return the message
178 */
179 MsgType readMessage(size_t buffer) { return circBuffs[buffer]->read(); }
180
181 /** Read data from buffer, non-blocking
182 * @param buffer which buffer to read from
183 * @param result pointer to return read message at
184 * return whether a message was read
185 */
186 bool readMessageNB(size_t buffer, MsgType* result) { return circBuffs[buffer]->readNB(result); }
187
188 /** Empty the messages in a buffer
189 * @param buffer which buffer to empty
190 */
191 void clearBuffer(size_t buffer) { circBuffs[buffer]->clearBuffer(); }
192
193 /** return whether this is a master-side tunnel or a child*/
194 bool isMaster() { return master; }
195
196private:
197 /** Allocate space for a data structure in the shared region
198 * @tparam T data structure type to allocate space for
199 * @param extraSpace how many extra bytes to reserve with this allocation
200 * return offset from shmPtr where structure was allocated and pointer to structure
201 */
202 template <typename T>
203 std::pair<size_t, T*> reserveSpace(size_t extraSpace = 0)
204 {
205 size_t space = sizeof(T) + extraSpace;
206 if ( (size_t)((nextAllocPtr + space) - (uint8_t*)shmPtr) > shmSize ) return std::make_pair<size_t, T*>(0, NULL);
207 T* ptr = (T*)nextAllocPtr;
208 nextAllocPtr += space;
209 new (ptr) T(); // Call constructor if need be
210 return std::make_pair((uint8_t*)ptr - (uint8_t*)shmPtr, ptr);
211 }
212
213 /** Calculate the size of the tunnel */
214 static size_t calculateShmemSize(size_t numBuffers, size_t bufferSize)
215 {
216 long pagesize = sysconf(_SC_PAGESIZE);
217 /* Count how many pages are needed, at minimum */
218 size_t isd = 1 + ((sizeof(InternalSharedData) + (1 + numBuffers) * sizeof(size_t)) / pagesize);
219 size_t buffer = 1 + ((sizeof(CircularBuffer<MsgType>) + bufferSize * sizeof(MsgType)) / pagesize);
220 size_t shdata = 1 + ((sizeof(ShareDataType) + sizeof(InternalSharedData)) / pagesize);
221
222 /* Alloc 2 extra pages just in case */
223 return (2 + isd + shdata + numBuffers * buffer) * pagesize;
224 }
225
226protected:
227 /** Pointer to the Shared Data Region */
228 ShareDataType* sharedData;
229
230 /** Return the number of buffers */
231 size_t getNumBuffers() { return numBuffs; }
232
233private:
234 bool master;
235 void* shmPtr;
236
237 uint8_t* nextAllocPtr;
238 size_t shmSize;
239
240 // Local data
241 size_t numBuffs;
242 size_t buffSize;
243 uint32_t children;
244
245 // Shared objects
247 std::vector<CircBuff_t*> circBuffs;
248};
249
250} // namespace SST::Core::Interprocess
251
252#endif // SST_CORE_INTERPROCESS_TUNNEL_DEF_H
Definition circularBuffer.h:24
virtual ~TunnelDef()
Destructor.
Definition tunneldef.h:144
void writeMessage(size_t buffer, const MsgType &command)
Write data to buffer, blocks until space is available.
Definition tunneldef.h:173
MsgType readMessage(size_t buffer)
Read data from buffer, blocks until message received.
Definition tunneldef.h:179
bool isMaster()
return whether this is a master-side tunnel or a child
Definition tunneldef.h:194
TunnelDef(void *sPtr)
Access an existing tunnel Child creates the TunnelDef, reads the shmSize, and then resizes its map ac...
Definition tunneldef.h:81
TunnelDef(size_t numBuffers, size_t bufferSize, uint32_t expectedChildren)
Create a new tunnel.
Definition tunneldef.h:66
bool readMessageNB(size_t buffer, MsgType *result)
Read data from buffer, non-blocking.
Definition tunneldef.h:186
void shutdown()
Clean up a region.
Definition tunneldef.h:147
void clearBuffer(size_t buffer)
Empty the messages in a buffer.
Definition tunneldef.h:191
ShareDataType * getSharedData()
return a pointer to the ShareDataType region
Definition tunneldef.h:167
size_t getTunnelSize()
return size of tunnel
Definition tunneldef.h:164
int * sharedData
Definition tunneldef.h:228
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:94
size_t getNumBuffers()
Return the number of buffers.
Definition tunneldef.h:231