SST  15.1.0
StructuralSimulationToolkit
mmapchild_pin3.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_MMAP_CHILD_PIN3_H
13 #define SST_CORE_INTERPROCESS_TUNNEL_MMAP_CHILD_PIN3_H
14 
15 #include "sst/core/interprocess/tunneldef.h"
16 
17 #include "pin.H"
18 
19 #include <cstddef>
20 #include <cstdio>
21 #include <string>
22 
23 namespace SST::Core::Interprocess {
24 
25 /** Class supports an IPC tunnel between two or more processes, via an mmap'd file
26  * This class attaches to an existing tunnel for a child process using PinCRT
27  *
28  * @tparam TunnelType Tunnel definition
29  */
30 template <typename TunnelType>
32 {
33 
34 public:
35  /** Child-side tunnel manager for an IPC tunnel
36  * Opens an existing file and mmaps it using PinCRT
37  *
38  * @param file_name Name of the shared file to mmap
39  */
40  explicit MMAPChild_Pin3(const std::string& file_name)
41  {
42  filename = file_name;
43  NATIVE_FD fd;
44  OS_RETURN_CODE retval = OS_OpenFD(filename.c_str(), OS_FILE_OPEN_TYPE_READ | OS_FILE_OPEN_TYPE_WRITE, 0, &fd);
45 
46  if ( !OS_RETURN_CODE_IS_SUCCESS(retval) ) {
47  // Not using Output because IPC means Output might not be available
48  fprintf(stderr, "Failed to open file for IPC '%s' (%d): %s\n", filename.c_str(), retval.os_specific_err,
49  strerror(retval.os_specific_err));
50  exit(1);
51  }
52 
53  shmPtr = NULL;
54  retval = OS_MapFileToMemory(NATIVE_PID_CURRENT, OS_PAGE_PROTECTION_TYPE_READ | OS_PAGE_PROTECTION_TYPE_WRITE,
55  sizeof(InternalSharedData), OS_MEMORY_FLAGS_SHARED, fd, 0, &shmPtr);
56 
57  if ( !OS_RETURN_CODE_IS_SUCCESS(retval) ) {
58  // Not using Output because IPC means Output might not be available
59  fprintf(stderr, "mmap failed (%d): %s\n", retval.os_specific_err, strerror(retval.os_specific_err));
60  exit(1);
61  }
62 
63  // Attach to tunnel to discover actual tunnel size
64  tunnel = new TunnelType(shmPtr);
65  shmSize = tunnel->getTunnelSize();
66 
67  OS_FreeMemory(NATIVE_PID_CURRENT, shmPtr, sizeof(InternalSharedData));
68 
69  // Remap with correct size
70  retval = OS_MapFileToMemory(NATIVE_PID_CURRENT, OS_PAGE_PROTECTION_TYPE_READ | OS_PAGE_PROTECTION_TYPE_WRITE,
71  shmSize, OS_MEMORY_FLAGS_SHARED, fd, 0, &shmPtr);
72 
73  if ( !OS_RETURN_CODE_IS_SUCCESS(retval) ) {
74  // Not using Output because IPC means Output might not be available
75  fprintf(stderr, "mmap failed (%d): %s\n", retval.os_specific_err, strerror(retval.os_specific_err));
76  exit(1);
77  }
78 
79  OS_CloseFD(fd);
80 
81  // Finish setup of tunnel with correctly-sized mmap
82  tunnel->initialize(shmPtr);
83  }
84 
85  /** Close file and shutdown tunnel */
86  virtual ~MMAPChild_Pin3()
87  {
88  delete tunnel;
89  OS_FreeMemory(NATIVE_PID_CURRENT, shmPtr, shmSize);
90  }
91 
92  /** return a pointer to the tunnel */
93  TunnelType* getTunnel() { return tunnel; }
94 
95  /** Return the name of the mmap'd file */
96  const std::string& getRegionName() const { return filename; }
97 
98 private:
99  void* shmPtr;
100  std::string filename;
101  size_t shmSize;
102 
103  TunnelType* tunnel;
104 };
105 
106 } // namespace SST::Core::Interprocess
107 
108 #endif // SST_CORE_INTERPROCESS_TUNNEL_MMAP_CHILD_PIN3_H
Class supports an IPC tunnel between two or more processes, via an mmap&#39;d file This class attaches to...
Definition: mmapchild_pin3.h:31
Definition: circularBuffer.h:20
virtual ~MMAPChild_Pin3()
Close file and shutdown tunnel.
Definition: mmapchild_pin3.h:86
MMAPChild_Pin3(const std::string &file_name)
Child-side tunnel manager for an IPC tunnel Opens an existing file and mmaps it using PinCRT...
Definition: mmapchild_pin3.h:40
TunnelType * getTunnel()
return a pointer to the tunnel
Definition: mmapchild_pin3.h:93
const std::string & getRegionName() const
Return the name of the mmap&#39;d file.
Definition: mmapchild_pin3.h:96