Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
only 3 more functions to go in mc::api
[simgrid.git] / src / mc / explo / simgrid_mc.cpp
1 /* Copyright (c) 2015-2022. 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 "simgrid/sg_config.hpp"
7 #include "src/internal_config.h"
8 #include "src/mc/explo/Exploration.hpp"
9 #include "src/mc/mc_config.hpp"
10 #include "src/mc/mc_exit.hpp"
11
12 #if HAVE_SMPI
13 #include "smpi/smpi.h"
14 #endif
15
16 #include <boost/tokenizer.hpp>
17 #include <cstring>
18 #include <memory>
19 #include <unistd.h>
20
21 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(mc);
22
23 static simgrid::config::Flag<std::string> _sg_mc_setenv{
24     "model-check/setenv", "Extra environment variables to pass to the child process (ex: 'AZE=aze;QWE=qwe').", "",
25     [](std::string_view value) {
26       xbt_assert(value.empty() || value.find('=', 0) != std::string_view::npos,
27                  "The 'model-check/setenv' parameter must be like 'AZE=aze', but it does not contain an equal sign.");
28     }};
29
30 int main(int argc, char** argv)
31 {
32   xbt_assert(argc >= 2, "Missing arguments");
33
34   // Currently, we need this before sg_config_init:
35   _sg_do_model_check = 1;
36
37   // The initialization function can touch argv.
38   // We make a copy of argv before modifying it in order to pass the original value to the model-checked application:
39   std::vector<char*> argv_copy{argv, argv + argc + 1};
40
41   xbt_log_init(&argc, argv);
42 #if HAVE_SMPI
43   smpi_init_options(); // only performed once
44 #endif
45   sg_config_init(&argc, argv);
46
47   simgrid::mc::ExplorationAlgorithm algo;
48   if (_sg_mc_comms_determinism || _sg_mc_send_determinism)
49     algo = simgrid::mc::ExplorationAlgorithm::CommDeterminism;
50   else if (_sg_mc_unfolding_checker)
51     algo = simgrid::mc::ExplorationAlgorithm::UDPOR;
52   else if (_sg_mc_property_file.get().empty())
53     algo = simgrid::mc::ExplorationAlgorithm::Safety;
54   else
55     algo = simgrid::mc::ExplorationAlgorithm::Liveness;
56
57   std::unordered_map<std::string, std::string> environment;
58   /** Setup the tokenizer that parses the string **/
59   using Tokenizer = boost::tokenizer<boost::char_separator<char>>;
60   boost::char_separator<char> semicol_sep(";");
61   boost::char_separator<char> equal_sep("=");
62   Tokenizer token_vars(_sg_mc_setenv.get(), semicol_sep); /* Iterate over all FOO=foo parts */
63   for (const auto& token : token_vars) {
64     std::vector<std::string> kv;
65     Tokenizer token_kv(token, equal_sep);
66     for (const auto& t : token_kv) /* Iterate over 'FOO' and then 'foo' in that 'FOO=foo' */
67       kv.push_back(t);
68     xbt_assert(kv.size() == 2, "Parse error on 'model-check/setenv' value %s. Does it contain an equal sign?",
69                token.c_str());
70     environment[kv[0]] = kv[1];
71   }
72   auto remote_app = std::make_unique<simgrid::mc::RemoteApp>([argv_copy, &environment] {
73     int i = 1;
74     while (argv_copy[i] != nullptr && argv_copy[i][0] == '-')
75       i++;
76
77     for (auto const& [key, val] : environment) {
78       XBT_INFO("setenv '%s'='%s'", key.c_str(), val.c_str());
79       setenv(key.c_str(), val.c_str(), 1);
80     }
81     xbt_assert(argv_copy[i] != nullptr,
82                "Unable to find a binary to exec on the command line. Did you only pass config flags?");
83     execvp(argv_copy[i], argv_copy.data() + i);
84     xbt_die("The model-checked process failed to exec(%s): %s", argv_copy[i], strerror(errno));
85   });
86
87   simgrid::mc::Exploration* explo;
88   switch (algo) {
89     case simgrid::mc::ExplorationAlgorithm::CommDeterminism:
90       explo = simgrid::mc::create_communication_determinism_checker(*remote_app.get());
91       break;
92
93     case simgrid::mc::ExplorationAlgorithm::UDPOR:
94       explo = simgrid::mc::create_udpor_checker(*remote_app.get());
95       break;
96
97     case simgrid::mc::ExplorationAlgorithm::Safety:
98       explo = simgrid::mc::create_dfs_exploration(*remote_app.get());
99       break;
100
101     case simgrid::mc::ExplorationAlgorithm::Liveness:
102       explo = simgrid::mc::create_liveness_checker(*remote_app.get());
103       break;
104
105     default:
106       THROW_IMPOSSIBLE;
107   }
108   mc_model_checker->set_exploration(explo);
109   std::unique_ptr<simgrid::mc::Exploration> checker{explo};
110
111   try {
112     checker->run();
113   } catch (const simgrid::mc::DeadlockError&) {
114     return SIMGRID_MC_EXIT_DEADLOCK;
115   } catch (const simgrid::mc::TerminationError&) {
116     return SIMGRID_MC_EXIT_NON_TERMINATION;
117   } catch (const simgrid::mc::LivenessError&) {
118     return SIMGRID_MC_EXIT_LIVENESS;
119   }
120   return SIMGRID_MC_EXIT_SUCCESS;
121 }