Logo AND Algorithmique Numérique Distribuée

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