Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
9e205d67c9c478d317c0b9ca098a56d9f347cb6f
[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 <iostream>
8
9 #include <limits.h>
10 #include <stdlib.h>
11 #include <sys/types.h>
12 #ifndef WIN32
13 #include <sys/mman.h>
14 #include <unistd.h>
15
16 #include "../xbt/memory_map.hpp"
17
18 #include "private.h"
19
20 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_memory, smpi,
21                                 "Memory layout support for SMPI");
22
23 #define TOPAGE(addr) (void *)(((unsigned long)(addr) / xbt_pagesize) * xbt_pagesize)
24
25 #define PROT_RWX (PROT_READ | PROT_WRITE | PROT_EXEC)
26 #define PROT_RW (PROT_READ | PROT_WRITE )
27 #define PROT_RX (PROT_READ | PROT_EXEC )
28
29 void smpi_get_executable_global_size(void)
30 {
31   char buffer[PATH_MAX];
32   char* full_name = realpath(xbt_binary_name, buffer);
33   if (full_name == nullptr)
34     xbt_die("Could not resolve binary file name");
35
36   std::vector<simgrid::xbt::VmMap> map = simgrid::xbt::get_memory_map(getpid());
37   for (auto i = map.begin(); i != map.end() ; ++i) {
38     // TODO, In practice, this implementation would not detect a completely
39     // anonymous data segment. This does not happen in practice, however.
40
41     // File backed RW entry:
42     if (i->pathname == full_name
43         && (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()
51           && i->pathname.empty()
52           && (i->prot & PROT_RWX) == PROT_RW
53           && i->start_addr == (std::uint64_t) smpi_start_data_exe + smpi_size_data_exe) {
54         smpi_size_data_exe = i->end_addr - (std::uint64_t) smpi_start_data_exe;
55       }
56       return;
57     }
58   }
59   xbt_die("Did not find my data segment.");
60 }
61
62 #endif