SST  11.1.0
StructuralSimulationToolkit
shmparent.h
1 // Copyright 2009-2021 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-2021, 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_SHM_PARENT_H
13 #define SST_CORE_INTERPROCESS_TUNNEL_SHM_PARENT_H
14 
15 #include <cstdio>
16 #include <cstdlib>
17 #include <cstring>
18 #include <errno.h>
19 #include <fcntl.h>
20 #include <inttypes.h>
21 #include <string>
22 #include <sys/mman.h>
23 #include <sys/stat.h>
24 #include <unistd.h>
25 
26 namespace SST {
27 namespace Core {
28 namespace Interprocess {
29 
30 /** Class supports an IPC tunnel between two or more processes via posix shared memory
31  * This class creates the tunnel for the parent/master process
32  *
33  * @tparam TunnelType Tunnel definition
34  */
35 template <typename TunnelType>
36 class SHMParent
37 {
38 
39 public:
40  /** Parent/master manager for an IPC tunnel
41  * Creates a shared memory region and initializes a
42  * TunnelType data strucgture in the region
43  *
44  * @param comp_id Component ID of owner
45  * @param numBuffers Number of buffers for which we should tunnel
46  * @param bufferSize How large each core's buffer should be
47  * @param expectedChildren How many child processes will connect to the tunnel
48  */
49  SHMParent(uint32_t comp_id, size_t numBuffers, size_t bufferSize, uint32_t expectedChildren = 1) :
50  shmPtr(nullptr),
51  fd(-1)
52  {
53  char key[256];
54  memset(key, '\0', sizeof(key));
55  do {
56  snprintf(key, sizeof(key), "/sst_shmem_%u-%" PRIu32 "-%d", getpid(), comp_id, rand());
57  filename = key;
58 
59  fd = shm_open(filename.c_str(), O_RDWR | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR);
60  /* There's a rare chance that a file we are looking to use exists.
61  * It's unlikely, but perhaps a previous run (with the same PID
62  * and random number) crashed before the * clients all connected.
63  *
64  * So, if we get an error, and the error is EEXIST, try again with
65  * a different random number.
66  */
67  } while ( (fd < 0) && (errno == EEXIST) );
68  if ( fd < 0 ) {
69  // Not using Output because IPC means Output might not be available
70  fprintf(stderr, "Failed to create IPC region '%s': %s\n", filename.c_str(), strerror(errno));
71  exit(1);
72  }
73 
74  tunnel = new TunnelType(numBuffers, bufferSize, expectedChildren);
75  shmSize = tunnel->getTunnelSize();
76 
77  if ( ftruncate(fd, shmSize) ) {
78  // Not using Output because IPC means Output might not be available
79  fprintf(stderr, "Resizing shared file '%s' failed: %s\n", filename.c_str(), strerror(errno));
80  exit(1);
81  }
82 
83  shmPtr = mmap(nullptr, shmSize, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
84  if ( shmPtr == MAP_FAILED ) {
85  // Not using Output because IPC means Output might not be available
86  fprintf(stderr, "mmap failed: %s\n", strerror(errno));
87  exit(1);
88  }
89  memset(shmPtr, '\0', shmSize);
90  tunnel->initialize(shmPtr);
91  }
92 
93  /** Destructor */
94  virtual ~SHMParent()
95  {
96  delete tunnel;
97  if ( shmPtr ) {
98  munmap(shmPtr, shmSize);
99  shmPtr = nullptr;
100  shmSize = 0;
101  }
102  if ( fd >= 0 ) {
103  close(fd);
104  fd = -1;
105  }
106  }
107 
108  /** returns name of the mmap'd region */
109  const std::string& getRegionName(void) const { return filename; }
110 
111  /** return the created tunnel pointer */
112  TunnelType* getTunnel() { return tunnel; }
113 
114 private:
115  void* shmPtr;
116  int fd;
117 
118  std::string filename;
119  size_t shmSize;
120 
121  TunnelType* tunnel;
122 };
123 
124 } // namespace Interprocess
125 } // namespace Core
126 } // namespace SST
127 
128 #endif // SST_CORE_INTERPROCESS_TUNNEL_SHM_PARENT_H
virtual ~SHMParent()
Destructor.
Definition: shmparent.h:94
SHMParent(uint32_t comp_id, size_t numBuffers, size_t bufferSize, uint32_t expectedChildren=1)
Parent/master manager for an IPC tunnel Creates a shared memory region and initializes a TunnelType d...
Definition: shmparent.h:49
Class supports an IPC tunnel between two or more processes via posix shared memory This class creates...
Definition: shmparent.h:36
const std::string & getRegionName(void) const
returns name of the mmap&#39;d region
Definition: shmparent.h:109
TunnelType * getTunnel()
return the created tunnel pointer
Definition: shmparent.h:112