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