Logo AND Algorithmique Numérique Distribuée

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