Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
ok, let's mess up the includes a bit more.
[simgrid.git] / src / smpi / smpi_memory.cpp
1 /* Copyright (c) 2015. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 #include <cstdint>
8 #include <climits>
9
10 #include <vector>
11
12 #include <stdlib.h>
13 #include <sys/types.h>
14 #include <string.h>
15 #include <stdio.h>
16 #include <fcntl.h>
17 #include <sys/stat.h>
18 #include <errno.h>
19
20 #ifndef WIN32
21 #include <sys/mman.h>
22 #include <unistd.h>
23
24 #include "../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 bool 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
72 /** Map a given SMPI privatization segment (make a SMPI process active) */
73 void smpi_switch_data_segment(int dest) {
74   if (smpi_loaded_page == dest)//no need to switch, we've already loaded the one we want
75     return;
76
77   // So the job:
78   smpi_really_switch_data_segment(dest);
79 }
80
81 /** Map a given SMPI privatization segment (make a SMPI process active)  even if SMPI thinks it is already active
82  *
83  *  When doing a state restoration, the state of the restored variables  might not be consistent with the state of the
84  *  virtual memory. In this case, we to change the data segment.
85  */
86 void smpi_really_switch_data_segment(int dest)
87 {
88   if(smpi_size_data_exe == 0)//no need to switch
89     return;
90
91 #if HAVE_PRIVATIZATION
92   if(smpi_loaded_page==-1){//initial switch, do the copy from the real page here
93     for (int i=0; i< smpi_process_count(); i++){
94       memcpy(smpi_privatisation_regions[i].address, TOPAGE(smpi_start_data_exe), smpi_size_data_exe);
95     }
96   }
97
98   // FIXME, cross-process support (mmap across process when necessary)
99   int current = smpi_privatisation_regions[dest].file_descriptor;
100   XBT_DEBUG("Switching data frame to the one of process %d", dest);
101   void* tmp =
102       mmap(TOPAGE(smpi_start_data_exe), smpi_size_data_exe, PROT_READ | PROT_WRITE, MAP_FIXED | MAP_SHARED, current, 0);
103   if (tmp != TOPAGE(smpi_start_data_exe))
104     xbt_die("Couldn't map the new region");
105   smpi_loaded_page = dest;
106 #endif
107 }
108
109 int smpi_is_privatisation_file(char* file)
110 {
111   return strncmp("/dev/shm/my-buffer-", file, std::strlen("/dev/shm/my-buffer-")) == 0;
112 }
113
114 void smpi_initialize_global_memory_segments()
115 {
116
117 #if !HAVE_PRIVATIZATION
118   smpi_privatize_global_variables=false;
119   xbt_die("You are trying to use privatization on a system that does not support it. Don't.");
120   return;
121 #else
122
123   smpi_get_executable_global_size();
124
125   XBT_DEBUG ("bss+data segment found : size %d starting at %p", smpi_size_data_exe, smpi_start_data_exe );
126
127   if (smpi_size_data_exe == 0){//no need to switch
128     smpi_privatize_global_variables=false;
129     return;
130   }
131
132   smpi_privatisation_regions = static_cast<smpi_privatisation_region_t>(
133       xbt_malloc(smpi_process_count() * sizeof(struct s_smpi_privatisation_region)));
134
135   for (int i=0; i< smpi_process_count(); i++){
136     // create SIMIX_process_count() mappings of this size with the same data inside
137     int file_descriptor;
138     void* address = nullptr;
139     char path[24];
140     int status;
141
142     do {
143       snprintf(path, sizeof(path), "/smpi-buffer-%06x", rand() % 0xffffff);
144       file_descriptor = shm_open(path, O_RDWR | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR);
145     } while (file_descriptor == -1 && errno == EEXIST);
146     if (file_descriptor < 0) {
147       if (errno == EMFILE) {
148         xbt_die("Impossible to create temporary file for memory mapping: %s\n\
149 The open() system call failed with the EMFILE error code (too many files). \n\n\
150 This means that you reached the system limits concerning the amount of files per process. \
151 This is not a surprise if you are trying to virtualize many processes on top of SMPI. \
152 Don't panic -- you should simply increase your system limits and try again. \n\n\
153 First, check what your limits are:\n\
154   cat /proc/sys/fs/file-max # Gives you the system-wide limit\n\
155   ulimit -Hn                # Gives you the per process hard limit\n\
156   ulimit -Sn                # Gives you the per process soft limit\n\
157   cat /proc/self/limits     # Displays any per-process limitation (including the one given above)\n\n\
158 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. \
159 Ask the Internet about tutorials on how to increase the files limit such as: https://rtcamp.com/tutorials/linux/increase-open-files-limit/",
160                 strerror(errno));
161       }
162       xbt_die("Impossible to create temporary file for memory mapping: %s", strerror(errno));
163     }
164
165     status = ftruncate(file_descriptor, smpi_size_data_exe);
166     if (status)
167       xbt_die("Impossible to set the size of the temporary file for memory mapping");
168
169     /* Ask for a free region */
170     address = mmap(nullptr, smpi_size_data_exe, PROT_READ | PROT_WRITE, MAP_SHARED, file_descriptor, 0);
171     if (address == MAP_FAILED)
172       xbt_die("Couldn't find a free region for memory mapping");
173
174     status = shm_unlink(path);
175     if (status)
176       xbt_die("Impossible to unlink temporary file for memory mapping");
177
178     // initialize the values
179     memcpy(address, TOPAGE(smpi_start_data_exe), smpi_size_data_exe);
180
181     // store the address of the mapping for further switches
182     smpi_privatisation_regions[i].file_descriptor = file_descriptor;
183     smpi_privatisation_regions[i].address         = address;
184   }
185 #endif
186 }
187
188 void smpi_destroy_global_memory_segments(){
189   if (smpi_size_data_exe == 0)//no need to switch
190     return;
191 #if HAVE_PRIVATIZATION
192   for (int i=0; i< smpi_process_count(); i++) {
193     if (munmap(smpi_privatisation_regions[i].address, smpi_size_data_exe) < 0)
194       XBT_WARN("Unmapping of fd %d failed: %s", smpi_privatisation_regions[i].file_descriptor, strerror(errno));
195     close(smpi_privatisation_regions[i].file_descriptor);
196   }
197   xbt_free(smpi_privatisation_regions);
198 #endif
199 }
200