Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Simplify the MC initialization code
[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 static void mc_exec_user_code(std::vector<char*> const& argv)
31 {
32   int i = 1;
33   while (argv[i] != nullptr && argv[i][0] == '-')
34     i++;
35
36   /* Setup the tokenizer that parses the cfg:model-check/setenv parameter */
37   using Tokenizer = boost::tokenizer<boost::char_separator<char>>;
38   boost::char_separator<char> semicol_sep(";");
39   boost::char_separator<char> equal_sep("=");
40   Tokenizer token_vars(_sg_mc_setenv.get(), semicol_sep); /* Iterate over all FOO=foo parts */
41   for (const auto& token : token_vars) {
42     std::vector<std::string> kv;
43     Tokenizer token_kv(token, equal_sep);
44     for (const auto& t : token_kv) /* Iterate over 'FOO' and then 'foo' in that 'FOO=foo' */
45       kv.push_back(t);
46     xbt_assert(kv.size() == 2, "Parse error on 'model-check/setenv' value %s. Does it contain an equal sign?",
47                token.c_str());
48     XBT_INFO("setenv '%s'='%s'", kv[0].c_str(), kv[1].c_str());
49     setenv(kv[0].c_str(), kv[1].c_str(), 1);
50   }
51   xbt_assert(argv[i] != nullptr,
52              "Unable to find a binary to exec on the command line. Did you only pass config flags?");
53
54   execvp(argv[i], argv.data() + i);
55   xbt_die("The model-checked process failed to exec(%s): %s", argv[i], strerror(errno));
56 }
57
58 int main(int argc, char** argv)
59 {
60   xbt_assert(argc >= 2, "Missing arguments");
61
62   // Currently, we need this before sg_config_init:
63   _sg_do_model_check = 1;
64
65   // The initialization function can touch argv.
66   // We make a copy of argv before modifying it in order to pass the original value to the model-checked application:
67   std::vector<char*> argv_copy{argv, argv + argc + 1};
68
69   xbt_log_init(&argc, argv);
70 #if HAVE_SMPI
71   smpi_init_options(); // that's OK to call it twice, and we need it ASAP
72 #endif
73   sg_config_init(&argc, argv);
74
75   auto remote_app = std::make_unique<simgrid::mc::RemoteApp>([argv_copy] { mc_exec_user_code(argv_copy); });
76
77   simgrid::mc::Exploration* explo;
78   if (_sg_mc_comms_determinism || _sg_mc_send_determinism)
79     explo = simgrid::mc::create_communication_determinism_checker(*remote_app.get());
80   else if (_sg_mc_unfolding_checker)
81     explo = simgrid::mc::create_udpor_checker(*remote_app.get());
82   else if (_sg_mc_property_file.get().empty())
83     explo = simgrid::mc::create_dfs_exploration(*remote_app.get());
84   else
85     explo = simgrid::mc::create_liveness_checker(*remote_app.get());
86
87   mc_model_checker->set_exploration(explo);
88   std::unique_ptr<simgrid::mc::Exploration> checker{explo};
89
90   try {
91     checker->run();
92   } catch (const simgrid::mc::DeadlockError&) {
93     return SIMGRID_MC_EXIT_DEADLOCK;
94   } catch (const simgrid::mc::TerminationError&) {
95     return SIMGRID_MC_EXIT_NON_TERMINATION;
96   } catch (const simgrid::mc::LivenessError&) {
97     return SIMGRID_MC_EXIT_LIVENESS;
98   }
99   return SIMGRID_MC_EXIT_SUCCESS;
100 }