1 /* Copyright (c) 2015-2016. The SimGrid Team.
2 * All rights reserved. */
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. */
13 #include <xbt/system_error.hpp>
14 #include <simgrid/sg_config.h>
15 #include <simgrid/modelchecker.h>
18 #include "src/mc/Session.hpp"
19 #include "src/mc/mc_state.h"
20 #include "src/mc/mc_private.h"
22 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_Session, mc, "Model-checker session");
27 static void setup_child_environment(int socket)
30 // Make sure we do not outlive our parent:
33 if (sigprocmask(SIG_SETMASK, &mask, nullptr) < 0)
34 throw simgrid::xbt::errno_error(errno, "Could not unblock signals");
35 if (prctl(PR_SET_PDEATHSIG, SIGHUP) != 0)
36 throw simgrid::xbt::errno_error(errno, "Could not PR_SET_PDEATHSIG");
41 // Remove CLOEXEC in order to pass the socket to the exec-ed program:
42 int fdflags = fcntl(socket, F_GETFD, 0);
43 if (fdflags == -1 || fcntl(socket, F_SETFD, fdflags & ~FD_CLOEXEC) == -1)
44 throw simgrid::xbt::errno_error(errno, "Could not remove CLOEXEC for socket");
47 setenv(MC_ENV_VARIABLE, "1", 1);
49 // Disable lazy relocation in the model-checked process.
50 // We don't want the model-checked process to modify its .got.plt during
52 setenv("LC_BIND_NOW", "1", 1);
55 res = std::snprintf(buffer, sizeof(buffer), "%i", socket);
56 if ((size_t) res >= sizeof(buffer) || res == -1)
58 setenv(MC_ENV_SOCKET_FD, buffer, 1);
61 /** Execute some code in a forked process */
68 throw simgrid::xbt::errno_error(errno, "Could not fork model-checked process");
78 // The callback should catch exceptions:
83 Session::Session(pid_t pid, int socket)
85 std::unique_ptr<simgrid::mc::Process> process(new simgrid::mc::Process(pid, socket));
86 // TODO, automatic detection of the config from the process
88 xbt_cfg_get_boolean("smpi/privatize_global_variables"));
89 modelChecker_ = std::unique_ptr<ModelChecker>(
90 new simgrid::mc::ModelChecker(std::move(process)));
91 xbt_assert(mc_model_checker == nullptr);
92 mc_model_checker = modelChecker_.get();
93 mc_model_checker->start();
101 void Session::execute(Transition const& transition)
103 modelChecker_->handle_simcall(transition);
104 modelChecker_->wait_for_requests();
107 void Session::logState()
109 if(_sg_mc_comms_determinism) {
110 if (!simgrid::mc::initial_global_state->recv_deterministic &&
111 simgrid::mc::initial_global_state->send_deterministic){
112 XBT_INFO("******************************************************");
113 XBT_INFO("**** Only-send-deterministic communication pattern ****");
114 XBT_INFO("******************************************************");
115 XBT_INFO("%s", simgrid::mc::initial_global_state->recv_diff);
116 }else if(!simgrid::mc::initial_global_state->send_deterministic &&
117 simgrid::mc::initial_global_state->recv_deterministic) {
118 XBT_INFO("******************************************************");
119 XBT_INFO("**** Only-recv-deterministic communication pattern ****");
120 XBT_INFO("******************************************************");
121 XBT_INFO("%s", simgrid::mc::initial_global_state->send_diff);
125 if (mc_stats->expanded_pairs == 0) {
126 XBT_INFO("Expanded states = %lu", mc_stats->expanded_states);
127 XBT_INFO("Visited states = %lu", mc_stats->visited_states);
129 XBT_INFO("Expanded pairs = %lu", mc_stats->expanded_pairs);
130 XBT_INFO("Visited pairs = %lu", mc_stats->visited_pairs);
132 XBT_INFO("Executed transitions = %lu", mc_stats->executed_transitions);
133 if ((_sg_mc_dot_output_file != nullptr) && (_sg_mc_dot_output_file[0] != '\0')) {
134 fprintf(dot_output, "}\n");
137 if (simgrid::mc::initial_global_state != nullptr
138 && (_sg_mc_comms_determinism || _sg_mc_send_determinism)) {
139 XBT_INFO("Send-deterministic : %s",
140 !simgrid::mc::initial_global_state->send_deterministic ? "No" : "Yes");
141 if (_sg_mc_comms_determinism)
142 XBT_INFO("Recv-deterministic : %s",
143 !simgrid::mc::initial_global_state->recv_deterministic ? "No" : "Yes");
145 if (getenv("SIMGRID_MC_SYSTEM_STATISTICS")){
146 int ret=system("free");
147 if(ret!=0)XBT_WARN("system call did not return 0, but %d",ret);
152 Session* Session::fork(std::function<void(void)> code)
154 // Create a AF_LOCAL socketpair used for exchanging messages
155 // bewteen the model-checker process (ourselves) and the model-checked
159 res = socketpair(AF_LOCAL, SOCK_DGRAM | SOCK_CLOEXEC, 0, sockets);
161 throw simgrid::xbt::errno_error(errno, "Could not create socketpair");
163 pid_t pid = do_fork([&] {
165 setup_child_environment(sockets[0]);
167 xbt_die("The model-checked process failed to exec()");
170 // Parent (model-checker):
173 return new Session(pid, sockets[1]);
177 Session* Session::spawnv(const char *path, char *const argv[])
179 return Session::fork([&] {
185 Session* Session::spawnvp(const char *path, char *const argv[])
187 return Session::fork([&] {
192 void Session::close()
195 modelChecker_->shutdown();
196 modelChecker_ = nullptr;
197 mc_model_checker = nullptr;
201 simgrid::mc::Session* session;