Logo AND Algorithmique Numérique Distribuée

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