SST  10.1.0
StructuralSimulationToolkit
mmapparent.h
1 // Copyright 2009-2020 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-2020, 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_MMAP_PARENT_H
13 #define SST_CORE_INTERPROCESS_TUNNEL_MMAP_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 
32 /** Class supports an IPC tunnel between two or more processes, via an mmap'd file.
33  * This class creates the tunnel for the parent/master process
34  *
35  * @tparam TunnelType Tunnel definition
36  */
37 template<typename TunnelType>
38 class MMAPParent {
39 
40 public:
41  /** Parent/master manager for an IPC Tunnel
42  * Creates a memory-mapped file and initializes a
43  * TunnelType data structure in the mmap'd region
44  *
45  * @param comp_id Component ID of owner
46  * @param numBuffers Number of buffers for which we should tunnel
47  * @param bufferSize How large each core's buffer should be
48  * @expectedChildren How many child processes will connect to the tunnel
49  */
50  MMAPParent(uint32_t comp_id, size_t numBuffers, size_t bufferSize, uint32_t expectedChildren = 1) : shmPtr(nullptr), fd(-1)
51  {
52  char key[256];
53  memset(key, '\0', sizeof(key));
54  do {
55  snprintf(key, sizeof(key), "/tmp/sst_shmem_%u-%" PRIu32 "-%d", getpid(), comp_id, rand());
56  filename = key;
57 
58  fd = open(filename.c_str(), O_RDWR|O_CREAT|O_EXCL, S_IRUSR|S_IWUSR);
59  /* There's a rare chance that a file we are looking to use exists.
60  * It's unlikely, but perhaps a previous run (with the same PID
61  * and random number) crashed before the * clients all connected.
62  *
63  * So, if we get an error, and the error is EEXIST, try again with
64  * a different random number.
65  */
66  } while ( (fd < 0) && (errno == EEXIST) );
67 
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  close(fd);
90 
91  memset(shmPtr, '\0', shmSize);
92  tunnel->initialize(shmPtr);
93  }
94 
95  /** Destructor */
96  virtual ~MMAPParent()
97  {
98  delete tunnel;
99 
100  munmap(shmPtr, shmSize);
101  if (remove(filename.c_str()) != 0) {
102  fprintf(stderr, "Error deleting tunnel file: %s\n", filename.c_str());
103  }
104  }
105 
106  /** returns name of the mmap'd file */
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 ~MMAPParent()
Destructor.
Definition: mmapparent.h:96
MMAPParent(uint32_t comp_id, size_t numBuffers, size_t bufferSize, uint32_t expectedChildren=1)
Parent/master manager for an IPC Tunnel Creates a memory-mapped file and initializes a TunnelType dat...
Definition: mmapparent.h:50
TunnelType * getTunnel()
return the created tunnel pointer
Definition: mmapparent.h:110
const std::string & getRegionName(void) const
returns name of the mmap&#39;d file
Definition: mmapparent.h:107
Class supports an IPC tunnel between two or more processes, via an mmap&#39;d file.
Definition: mmapparent.h:38