Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of git+ssh://scm.gforge.inria.fr//gitroot/simgrid/simgrid
[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
15 #ifndef WIN32
16 #include <sys/mman.h>
17 #include <unistd.h>
18
19 #include "../xbt/memory_map.hpp"
20
21 #include "private.h"
22 #include "private.hpp"
23
24 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_memory, smpi, "Memory layout support for SMPI");
25
26 static const int PROT_RWX = (PROT_READ | PROT_WRITE | PROT_EXEC);
27 static const int PROT_RW  = (PROT_READ | PROT_WRITE );
28 XBT_ATTRIB_UNUSED static const int PROT_RX  = (PROT_READ | PROT_EXEC );
29
30 void smpi_get_executable_global_size()
31 {
32   char buffer[PATH_MAX];
33   char* full_name = realpath(xbt_binary_name, buffer);
34   if (full_name == nullptr)
35     xbt_die("Could not resolve binary file name");
36
37   std::vector<simgrid::xbt::VmMap> map = simgrid::xbt::get_memory_map(getpid());
38   for (auto i = map.begin(); i != map.end() ; ++i) {
39     // TODO, In practice, this implementation would not detect a completely
40     // anonymous data segment. This does not happen in practice, however.
41
42     // File backed RW entry:
43     if (i->pathname == full_name && (i->prot & PROT_RWX) == PROT_RW) {
44       smpi_start_data_exe = (char*) i->start_addr;
45       smpi_size_data_exe = i->end_addr - i->start_addr;
46       ++i;
47       /* Here we are making the assumption that a suitable empty region
48          following the rw- area is the end of the data segment. It would
49          be better to check with the size of the data segment. */
50       if (i != map.end() && i->pathname.empty() && (i->prot & PROT_RWX) == PROT_RW
51           && (char*)i->start_addr ==  smpi_start_data_exe + smpi_size_data_exe) {
52         smpi_size_data_exe = (char*)i->end_addr - smpi_start_data_exe;
53       }
54       return;
55     }
56   }
57   xbt_die("Did not find my data segment.");
58 }
59 #endif