Logo AND Algorithmique Numérique Distribuée

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