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