Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of scm.gforge.inria.fr:/gitroot/simgrid/simgrid
[simgrid.git] / src / smpi / internals / smpi_memory.cpp
1 /* Copyright (c) 2015-2018. The SimGrid Team. All rights reserved.          */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 #include <algorithm>
7 #include <cerrno>
8 #include <climits>
9 #include <cstdint>
10 #include <cstdio>
11 #include <cstdlib>
12 #include <cstring>
13 #include <deque>
14 #include <fcntl.h>
15 #include <sys/stat.h>
16 #include <sys/types.h>
17 #include <vector>
18
19 #ifndef WIN32
20 #include <sys/mman.h>
21 #include <unistd.h>
22
23 #include "src/internal_config.h"
24 #include "src/xbt/memory_map.hpp"
25
26 #include "private.hpp"
27 #include "src/smpi/include/smpi_actor.hpp"
28
29 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_memory, smpi, "Memory layout support for SMPI");
30
31 int smpi_loaded_page      = -1;
32 char* smpi_data_exe_start = nullptr;
33 int smpi_data_exe_size    = 0;
34 SmpiPrivStrategies smpi_privatize_global_variables;
35 static void* smpi_data_exe_copy;
36
37 // Initialized by smpi_prepare_global_memory_segment().
38 static std::vector<simgrid::xbt::VmMap> initial_vm_map;
39
40 // We keep a copy of all the privatization regions: We can then delete everything easily by iterating over this
41 // collection and nothing can be leaked. We could also iterate over all actors but we would have to be diligent when two
42 // actors use the same privatization region (so, smart pointers would have to be used etc.)
43 // Use a std::deque so that pointers remain valid after push_back().
44 static std::deque<s_smpi_privatization_region_t> smpi_privatization_regions;
45
46 static constexpr int PROT_RWX = PROT_READ | PROT_WRITE | PROT_EXEC;
47 static constexpr int PROT_RW  = PROT_READ | PROT_WRITE;
48
49 /** Take a snapshot of the process' memory map.
50  */
51 void smpi_prepare_global_memory_segment()
52 {
53   initial_vm_map = simgrid::xbt::get_memory_map(getpid());
54 }
55
56 static void smpi_get_executable_global_size()
57 {
58   char buffer[PATH_MAX];
59   char* full_name = realpath(xbt_binary_name, buffer);
60   if (full_name == nullptr)
61     xbt_die("Could not resolve binary file name");
62
63   std::vector<simgrid::xbt::VmMap> map = simgrid::xbt::get_memory_map(getpid());
64   for (auto i = map.begin(); i != map.end() ; ++i) {
65     // TODO, In practice, this implementation would not detect a completely
66     // anonymous data segment. This does not happen in practice, however.
67
68     // File backed RW entry:
69     if (i->pathname == full_name && (i->prot & PROT_RWX) == PROT_RW) {
70       smpi_data_exe_start = (char*)i->start_addr;
71       smpi_data_exe_size  = i->end_addr - i->start_addr;
72       /* Here we are making the assumption that a suitable empty region
73          following the rw- area is the end of the data segment. It would
74          be better to check with the size of the data segment. */
75       ++i;
76       if (i != map.end() && i->pathname.empty() && (i->prot & PROT_RWX) == PROT_RW &&
77           (char*)i->start_addr == smpi_data_exe_start + smpi_data_exe_size) {
78         // Only count this region if it was not already present in the initial map.
79         auto found = std::find_if(begin(initial_vm_map), end(initial_vm_map),
80                                   [&i](const simgrid::xbt::VmMap& m) { return m.start_addr == i->start_addr; });
81         if (found == end(initial_vm_map))
82           smpi_data_exe_size = (char*)i->end_addr - smpi_data_exe_start;
83       }
84       return;
85     }
86   }
87   xbt_die("Did not find my data segment.");
88 }
89 #endif
90
91 #if HAVE_SANITIZE_ADDRESS
92 #include <sanitizer/asan_interface.h>
93 static void* asan_safe_memcpy(void* dest, void* src, size_t n)
94 {
95   char* psrc  = static_cast<char*>(src);
96   char* pdest = static_cast<char*>(dest);
97   for (size_t i = 0; i < n;) {
98     while (i < n && __asan_address_is_poisoned(psrc + i))
99       ++i;
100     if (i < n) {
101       char* p  = static_cast<char*>(__asan_region_is_poisoned(psrc + i, n - i));
102       size_t j = p ? (p - psrc) : n;
103       memcpy(pdest + i, psrc + i, j - i);
104       i = j;
105     }
106   }
107   return dest;
108 }
109 #else
110 #define asan_safe_memcpy(dest, src, n) memcpy(dest, src, n)
111 #endif
112
113 /** Map a given SMPI privatization segment (make a SMPI process active) */
114 void smpi_switch_data_segment(simgrid::s4u::ActorPtr actor)
115 {
116   if (smpi_loaded_page == actor->get_pid()) // no need to switch, we've already loaded the one we want
117     return;
118
119   // So the job:
120   smpi_really_switch_data_segment(actor);
121 }
122
123 /** Map a given SMPI privatization segment (make a SMPI process active)  even if SMPI thinks it is already active
124  *
125  *  When doing a state restoration, the state of the restored variables  might not be consistent with the state of the
126  *  virtual memory. In this case, we to change the data segment.
127  */
128 void smpi_really_switch_data_segment(simgrid::s4u::ActorPtr actor)
129 {
130   if (smpi_data_exe_size == 0) // no need to switch
131     return;
132
133 #if HAVE_PRIVATIZATION
134   // FIXME, cross-process support (mmap across process when necessary)
135   XBT_DEBUG("Switching data frame to the one of process %ld", actor->get_pid());
136   simgrid::smpi::ActorExt* process = smpi_process_remote(actor);
137   int current                     = process->privatized_region()->file_descriptor;
138   void* tmp = mmap(TOPAGE(smpi_data_exe_start), smpi_data_exe_size, PROT_RW, MAP_FIXED | MAP_SHARED, current, 0);
139   if (tmp != TOPAGE(smpi_data_exe_start))
140     xbt_die("Couldn't map the new region (errno %d): %s", errno, strerror(errno));
141   smpi_loaded_page = actor->get_pid();
142 #endif
143 }
144
145 int smpi_is_privatization_file(char* file)
146 {
147   const std::string buffer_path("/dev/shm/my-buffer-");
148   return buffer_path.compare(0, std::string::npos, file, buffer_path.length()) == 0;
149 }
150
151 /**
152  * @brief Makes a backup of the segment in memory that stores the global variables of a process.
153  *        This backup is then used to initialize the global variables for every single
154  *        process that is added, regardless of the progress of the simulation.
155  */
156 void smpi_backup_global_memory_segment()
157 {
158 #if HAVE_PRIVATIZATION
159   smpi_get_executable_global_size();
160   initial_vm_map.clear();
161   initial_vm_map.shrink_to_fit();
162
163   XBT_DEBUG("bss+data segment found : size %d starting at %p", smpi_data_exe_size, smpi_data_exe_start);
164
165   if (smpi_data_exe_size == 0) { // no need to do anything as global variables don't exist
166     smpi_privatize_global_variables = SmpiPrivStrategies::NONE;
167     return;
168   }
169
170   smpi_data_exe_copy = ::operator new(smpi_data_exe_size);
171   // Make a copy of the data segment. This clean copy is retained over the whole runtime
172   // of the simulation and can be used to initialize a dynamically added, new process.
173   asan_safe_memcpy(smpi_data_exe_copy, TOPAGE(smpi_data_exe_start), smpi_data_exe_size);
174 #else /* ! HAVE_PRIVATIZATION */
175   xbt_die("You are trying to use privatization on a system that does not support it. Don't.");
176 #endif
177 }
178
179 // Initializes the memory mapping for a single process and returns the privatization region
180 smpi_privatization_region_t smpi_init_global_memory_segment_process()
181 {
182   int file_descriptor;
183   void* address = nullptr;
184   char path[24];
185   int status;
186
187   do {
188     snprintf(path, sizeof(path), "/smpi-buffer-%06x", rand() % 0xffffffU);
189     file_descriptor = shm_open(path, O_RDWR | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR);
190   } while (file_descriptor == -1 && errno == EEXIST);
191   if (file_descriptor < 0) {
192     if (errno == EMFILE) {
193       xbt_die("Impossible to create temporary file for memory mapping: %s\n\
194 The open() system call failed with the EMFILE error code (too many files). \n\n\
195 This means that you reached the system limits concerning the amount of files per process. \
196 This is not a surprise if you are trying to virtualize many processes on top of SMPI. \
197 Don't panic -- you should simply increase your system limits and try again. \n\n\
198 First, check what your limits are:\n\
199   cat /proc/sys/fs/file-max # Gives you the system-wide limit\n\
200   ulimit -Hn                # Gives you the per process hard limit\n\
201   ulimit -Sn                # Gives you the per process soft limit\n\
202   cat /proc/self/limits     # Displays any per-process limitation (including the one given above)\n\n\
203 If one of these values is less than the amount of MPI processes that you try to run, then you got the explanation of this error. \
204 Ask the Internet about tutorials on how to increase the files limit such as: https://rtcamp.com/tutorials/linux/increase-open-files-limit/",
205               strerror(errno));
206     }
207     xbt_die("Impossible to create temporary file for memory mapping: %s", strerror(errno));
208   }
209
210   status = ftruncate(file_descriptor, smpi_data_exe_size);
211   if (status)
212     xbt_die("Impossible to set the size of the temporary file for memory mapping");
213
214   /* Ask for a free region */
215   address = mmap(nullptr, smpi_data_exe_size, PROT_RW, MAP_SHARED, file_descriptor, 0);
216   if (address == MAP_FAILED)
217     xbt_die("Couldn't find a free region for memory mapping");
218
219   status = shm_unlink(path);
220   if (status)
221     xbt_die("Impossible to unlink temporary file for memory mapping");
222
223   // initialize the values
224   asan_safe_memcpy(address, smpi_data_exe_copy, smpi_data_exe_size);
225
226   // store the address of the mapping for further switches
227   smpi_privatization_regions.emplace_back(s_smpi_privatization_region_t{address, file_descriptor});
228
229   return &smpi_privatization_regions.back();
230 }
231
232 void smpi_destroy_global_memory_segments(){
233   if (smpi_data_exe_size == 0) // no need to switch
234     return;
235 #if HAVE_PRIVATIZATION
236   for (auto const& region : smpi_privatization_regions) {
237     if (munmap(region.address, smpi_data_exe_size) < 0)
238       XBT_WARN("Unmapping of fd %d failed: %s", region.file_descriptor, strerror(errno));
239     close(region.file_descriptor);
240   }
241   smpi_privatization_regions.clear();
242   ::operator delete(smpi_data_exe_copy);
243 #endif
244 }
245
246 static int sendbuffer_size = 0;
247 static char* sendbuffer    = nullptr;
248 static int recvbuffer_size = 0;
249 static char* recvbuffer    = nullptr;
250
251 //allocate a single buffer for all sends, growing it if needed
252 void* smpi_get_tmp_sendbuffer(int size)
253 {
254   if (not smpi_process()->replaying())
255     return xbt_malloc(size);
256   if (sendbuffer_size<size){
257     sendbuffer=static_cast<char*>(xbt_realloc(sendbuffer,size));
258     sendbuffer_size=size;
259   }
260   return sendbuffer;
261 }
262
263 //allocate a single buffer for all recv
264 void* smpi_get_tmp_recvbuffer(int size){
265   if (not smpi_process()->replaying())
266     return xbt_malloc(size);
267   if (recvbuffer_size<size){
268     recvbuffer=static_cast<char*>(xbt_realloc(recvbuffer,size));
269     recvbuffer_size=size;
270   }
271   return recvbuffer;
272 }
273
274 void smpi_free_tmp_buffer(void* buf){
275   if (not smpi_process()->replaying())
276     xbt_free(buf);
277 }
278
279 void smpi_free_replay_tmp_buffers(){
280   xbt_free(sendbuffer);
281   xbt_free(recvbuffer);
282 }