Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
7811d9570ae9e996e7d3b35a1f4a10fffd44dc53
[simgrid.git] / src / smpi / internals / smpi_memory.cpp
1 /* Copyright (c) 2015-2017. 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 <cstdint>
7 #include <climits>
8 #include <cstring>
9
10 #include <vector>
11
12 #include <cerrno>
13 #include <cstdio>
14 #include <cstdlib>
15 #include <cstring>
16 #include <fcntl.h>
17 #include <sys/stat.h>
18 #include <sys/types.h>
19
20 #ifndef WIN32
21 #include <sys/mman.h>
22 #include <unistd.h>
23
24 #include "src/internal_config.h"
25 #include "src/xbt/memory_map.hpp"
26
27 #include "private.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 int smpi_privatize_global_variables;
35
36 static const int PROT_RWX = (PROT_READ | PROT_WRITE | PROT_EXEC);
37 static const int PROT_RW  = (PROT_READ | PROT_WRITE );
38 XBT_ATTRIB_UNUSED static const int PROT_RX  = (PROT_READ | PROT_EXEC );
39
40 void smpi_get_executable_global_size()
41 {
42   char buffer[PATH_MAX];
43   char* full_name = realpath(xbt_binary_name, buffer);
44   if (full_name == nullptr)
45     xbt_die("Could not resolve binary file name");
46
47   std::vector<simgrid::xbt::VmMap> map = simgrid::xbt::get_memory_map(getpid());
48   for (auto i = map.begin(); i != map.end() ; ++i) {
49     // TODO, In practice, this implementation would not detect a completely
50     // anonymous data segment. This does not happen in practice, however.
51
52     // File backed RW entry:
53     if (i->pathname == full_name && (i->prot & PROT_RWX) == PROT_RW) {
54       smpi_data_exe_start = (char*)i->start_addr;
55       smpi_data_exe_size  = i->end_addr - i->start_addr;
56       ++i;
57       /* Here we are making the assumption that a suitable empty region
58          following the rw- area is the end of the data segment. It would
59          be better to check with the size of the data segment. */
60       if (i != map.end() && i->pathname.empty() && (i->prot & PROT_RWX) == PROT_RW &&
61           (char*)i->start_addr == smpi_data_exe_start + smpi_data_exe_size) {
62         smpi_data_exe_size = (char*)i->end_addr - smpi_data_exe_start;
63       }
64       return;
65     }
66   }
67   xbt_die("Did not find my data segment.");
68 }
69 #endif
70
71 #if HAVE_SANITIZE_ADDRESS
72 #include <sanitizer/asan_interface.h>
73 static void* asan_safe_memcpy(void* dest, void* src, size_t n)
74 {
75   char* psrc  = static_cast<char*>(src);
76   char* pdest = static_cast<char*>(dest);
77   for (size_t i = 0; i < n;) {
78     while (i < n && __asan_address_is_poisoned(psrc + i))
79       ++i;
80     if (i < n) {
81       char* p  = static_cast<char*>(__asan_region_is_poisoned(psrc + i, n - i));
82       size_t j = p ? (p - psrc) : n;
83       memcpy(pdest + i, psrc + i, j - i);
84       i = j;
85     }
86   }
87   return dest;
88 }
89 #else
90 #define asan_safe_memcpy(dest, src, n) memcpy(dest, src, n)
91 #endif
92
93 /** Map a given SMPI privatization segment (make a SMPI process active) */
94 void smpi_switch_data_segment(int dest) {
95   if (smpi_loaded_page == dest)//no need to switch, we've already loaded the one we want
96     return;
97
98   // So the job:
99   smpi_really_switch_data_segment(dest);
100 }
101
102 /** Map a given SMPI privatization segment (make a SMPI process active)  even if SMPI thinks it is already active
103  *
104  *  When doing a state restoration, the state of the restored variables  might not be consistent with the state of the
105  *  virtual memory. In this case, we to change the data segment.
106  */
107 void smpi_really_switch_data_segment(int dest)
108 {
109   if (smpi_data_exe_size == 0) // no need to switch
110     return;
111
112 #if HAVE_PRIVATIZATION
113   if(smpi_loaded_page==-1){//initial switch, do the copy from the real page here
114     for (int i=0; i< smpi_process_count(); i++){
115       asan_safe_memcpy(smpi_privatization_regions[i].address, TOPAGE(smpi_data_exe_start), smpi_data_exe_size);
116     }
117   }
118
119   // FIXME, cross-process support (mmap across process when necessary)
120   int current = smpi_privatization_regions[dest].file_descriptor;
121   XBT_DEBUG("Switching data frame to the one of process %d", dest);
122   void* tmp =
123       mmap(TOPAGE(smpi_data_exe_start), smpi_data_exe_size, PROT_READ | PROT_WRITE, MAP_FIXED | MAP_SHARED, current, 0);
124   if (tmp != TOPAGE(smpi_data_exe_start))
125     xbt_die("Couldn't map the new region (errno %d): %s", errno, strerror(errno));
126   smpi_loaded_page = dest;
127 #endif
128 }
129
130 int smpi_is_privatization_file(char* file)
131 {
132   const std::string buffer_path("/dev/shm/my-buffer-");
133   return buffer_path.compare(0, std::string::npos, file, buffer_path.length()) == 0;
134 }
135
136 void smpi_initialize_global_memory_segments()
137 {
138
139 #if HAVE_PRIVATIZATION
140   smpi_get_executable_global_size();
141
142   XBT_DEBUG("bss+data segment found : size %d starting at %p", smpi_data_exe_size, smpi_data_exe_start);
143
144   if (smpi_data_exe_size == 0) { // no need to switch
145     smpi_privatize_global_variables=false;
146     return;
147   }
148
149   smpi_privatization_regions = static_cast<smpi_privatization_region_t>(
150       xbt_malloc(smpi_process_count() * sizeof(s_smpi_privatization_region_t)));
151
152   for (int i=0; i< smpi_process_count(); i++){
153     // create SIMIX_process_count() mappings of this size with the same data inside
154     int file_descriptor;
155     void* address = nullptr;
156     char path[24];
157     int status;
158
159     do {
160       snprintf(path, sizeof(path), "/smpi-buffer-%06x", rand() % 0xffffffU);
161       file_descriptor = shm_open(path, O_RDWR | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR);
162     } while (file_descriptor == -1 && errno == EEXIST);
163     if (file_descriptor < 0) {
164       if (errno == EMFILE) {
165         xbt_die("Impossible to create temporary file for memory mapping: %s\n\
166 The open() system call failed with the EMFILE error code (too many files). \n\n\
167 This means that you reached the system limits concerning the amount of files per process. \
168 This is not a surprise if you are trying to virtualize many processes on top of SMPI. \
169 Don't panic -- you should simply increase your system limits and try again. \n\n\
170 First, check what your limits are:\n\
171   cat /proc/sys/fs/file-max # Gives you the system-wide limit\n\
172   ulimit -Hn                # Gives you the per process hard limit\n\
173   ulimit -Sn                # Gives you the per process soft limit\n\
174   cat /proc/self/limits     # Displays any per-process limitation (including the one given above)\n\n\
175 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. \
176 Ask the Internet about tutorials on how to increase the files limit such as: https://rtcamp.com/tutorials/linux/increase-open-files-limit/",
177                 strerror(errno));
178       }
179       xbt_die("Impossible to create temporary file for memory mapping: %s", strerror(errno));
180     }
181
182     status = ftruncate(file_descriptor, smpi_data_exe_size);
183     if (status)
184       xbt_die("Impossible to set the size of the temporary file for memory mapping");
185
186     /* Ask for a free region */
187     address = mmap(nullptr, smpi_data_exe_size, PROT_READ | PROT_WRITE, MAP_SHARED, file_descriptor, 0);
188     if (address == MAP_FAILED)
189       xbt_die("Couldn't find a free region for memory mapping");
190
191     status = shm_unlink(path);
192     if (status)
193       xbt_die("Impossible to unlink temporary file for memory mapping");
194
195     // initialize the values
196     asan_safe_memcpy(address, TOPAGE(smpi_data_exe_start), smpi_data_exe_size);
197
198     // store the address of the mapping for further switches
199     smpi_privatization_regions[i].file_descriptor = file_descriptor;
200     smpi_privatization_regions[i].address         = address;
201   }
202 #else /* ! HAVE_PRIVATIZATION */
203   smpi_privatize_global_variables = false;
204   xbt_die("You are trying to use privatization on a system that does not support it. Don't.");
205   return;
206 #endif
207 }
208
209 void smpi_destroy_global_memory_segments(){
210   if (smpi_data_exe_size == 0) // no need to switch
211     return;
212 #if HAVE_PRIVATIZATION
213   for (int i=0; i< smpi_process_count(); i++) {
214     if (munmap(smpi_privatization_regions[i].address, smpi_data_exe_size) < 0)
215       XBT_WARN("Unmapping of fd %d failed: %s", smpi_privatization_regions[i].file_descriptor, strerror(errno));
216     close(smpi_privatization_regions[i].file_descriptor);
217   }
218   xbt_free(smpi_privatization_regions);
219 #endif
220 }
221