Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
add missing include for smpi_options
[simgrid.git] / src / mc / Session.cpp
1 /* Copyright (c) 2015-2019. 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 "src/mc/Session.hpp"
7 #include "src/mc/checker/Checker.hpp"
8 #include "src/mc/mc_config.hpp"
9 #if HAVE_SMPI
10 #include "smpi/smpi.h"
11 #endif
12 #include "src/mc/mc_private.hpp"
13 #include "src/mc/mc_state.hpp"
14 #include "xbt/log.h"
15 #include "xbt/system_error.hpp"
16
17 #include <fcntl.h>
18 #ifdef __linux__
19 #include <sys/prctl.h>
20 #endif
21
22 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_Session, mc, "Model-checker session");
23
24 namespace simgrid {
25 namespace mc {
26
27 static void setup_child_environment(int socket)
28 {
29 #ifdef __linux__
30   // Make sure we do not outlive our parent
31   sigset_t mask;
32   sigemptyset (&mask);
33   xbt_assert(sigprocmask(SIG_SETMASK, &mask, nullptr) >= 0, "Could not unblock signals");
34   xbt_assert(prctl(PR_SET_PDEATHSIG, SIGHUP) == 0, "Could not PR_SET_PDEATHSIG");
35 #endif
36
37   // Remove CLOEXEC to pass the socket to the application
38   int fdflags = fcntl(socket, F_GETFD, 0);
39   xbt_assert(fdflags != -1 && fcntl(socket, F_SETFD, fdflags & ~FD_CLOEXEC) != -1,
40              "Could not remove CLOEXEC for socket");
41
42   // Set environment so that mmalloc gets used in application
43   setenv(MC_ENV_VARIABLE, "1", 1);
44
45   // Disable lazy relocation in the model-checked process to prevent the application from
46   // modifying its .got.plt during snapshot.
47   setenv("LC_BIND_NOW", "1", 1);
48
49   char buffer[64];
50   int res = std::snprintf(buffer, sizeof(buffer), "%i", socket);
51   xbt_assert((size_t)res < sizeof(buffer) && res != -1);
52   setenv(MC_ENV_SOCKET_FD, buffer, 1);
53 }
54
55 Session::Session(const std::function<void()>& code)
56 {
57 #if HAVE_SMPI
58   smpi_init_options();//only performed once
59   xbt_assert(smpi_cfg_privatization() != SmpiPrivStrategies::MMAP,
60              "Please use the dlopen privatization schema when model-checking SMPI code");
61 #endif
62
63   // Create a AF_LOCAL socketpair used for exchanging messages
64   // between the model-checker process (ourselves) and the model-checked
65   // process:
66   int sockets[2];
67   int res = socketpair(AF_LOCAL, SOCK_SEQPACKET | SOCK_CLOEXEC, 0, sockets);
68   xbt_assert(res != -1, "Could not create socketpair");
69
70   pid_t pid = fork();
71   xbt_assert(pid >= 0, "Could not fork model-checked process");
72
73   if (pid == 0) { // Child
74     ::close(sockets[1]);
75     setup_child_environment(sockets[0]);
76     code();
77     xbt_die("The model-checked process failed to exec()");
78   }
79
80   // Parent (model-checker):
81   ::close(sockets[0]);
82
83   xbt_assert(mc_model_checker == nullptr, "Did you manage to start the MC twice in this process?");
84
85   std::unique_ptr<simgrid::mc::RemoteClient> process(new simgrid::mc::RemoteClient(pid, sockets[1]));
86   model_checker_.reset(new simgrid::mc::ModelChecker(std::move(process)));
87
88   mc_model_checker = model_checker_.get();
89   mc_model_checker->start();
90 }
91
92 Session::~Session()
93 {
94   this->close();
95 }
96
97 void Session::initialize()
98 {
99   xbt_assert(initial_snapshot_ == nullptr);
100   mc_model_checker->wait_for_requests();
101   initial_snapshot_ = std::make_shared<simgrid::mc::Snapshot>(0);
102 }
103
104 void Session::execute(Transition const& transition)
105 {
106   model_checker_->handle_simcall(transition);
107   model_checker_->wait_for_requests();
108 }
109
110 void Session::restore_initial_state()
111 {
112   this->initial_snapshot_->restore(&mc_model_checker->process());
113 }
114
115 void Session::log_state()
116 {
117   mc_model_checker->getChecker()->log_state();
118
119   if (not _sg_mc_dot_output_file.get().empty()) {
120     fprintf(dot_output, "}\n");
121     fclose(dot_output);
122   }
123   if (getenv("SIMGRID_MC_SYSTEM_STATISTICS")){
124     int ret=system("free");
125     if(ret!=0)XBT_WARN("system call did not return 0, but %d",ret);
126   }
127 }
128
129 void Session::close()
130 {
131   initial_snapshot_ = nullptr;
132   if (model_checker_) {
133     model_checker_->shutdown();
134     model_checker_   = nullptr;
135     mc_model_checker = nullptr;
136   }
137 }
138
139 simgrid::mc::Session* session;
140
141 }
142 }