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