1 /* Copyright (c) 2020-2022. The SimGrid Team. All rights reserved. */
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. */
8 #include "src/kernel/activity/MailboxImpl.hpp"
9 #include "src/kernel/activity/MutexImpl.hpp"
10 #include "src/kernel/actor/SimcallObserver.hpp"
11 #include "src/mc/Session.hpp"
12 #include "src/mc/explo/Exploration.hpp"
13 #include "src/mc/mc_base.hpp"
14 #include "src/mc/mc_exit.hpp"
15 #include "src/mc/mc_pattern.hpp"
16 #include "src/mc/mc_private.hpp"
17 #include "src/mc/remote/RemoteProcess.hpp"
18 #include "src/surf/HostImpl.hpp"
20 #include <xbt/asserts.h>
22 #include "simgrid/s4u/Host.hpp"
23 #include "xbt/string.hpp"
25 #include "src/smpi/include/smpi_request.hpp"
28 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(Api, mc, "Logging specific to MC Facade APIs ");
29 XBT_LOG_EXTERNAL_CATEGORY(mc_global);
31 namespace simgrid::mc {
33 simgrid::mc::Exploration* Api::initialize(char** argv, const std::unordered_map<std::string, std::string>& env,
34 simgrid::mc::ExplorationAlgorithm algo)
36 session_ = std::make_unique<simgrid::mc::Session>([argv, &env] {
38 while (argv[i] != nullptr && argv[i][0] == '-')
40 if (env.find("LD_PRELOAD") == env.end())
41 setenv("LD_PRELOAD", "libsgmalloc.so", 1);
43 for (auto const& [key, val] : env) {
44 if (key == "LD_PRELOAD") {
45 auto v2 = std::string("libsgmalloc.so:") + val;
46 XBT_INFO("setenv '%s'='%s'", key.c_str(), v2.c_str());
47 setenv(key.c_str(), v2.c_str(), 1);
49 XBT_INFO("setenv '%s'='%s'", key.c_str(), val.c_str());
50 setenv(key.c_str(), val.c_str(), 1);
53 xbt_assert(argv[i] != nullptr,
54 "Unable to find a binary to exec on the command line. Did you only pass config flags?");
55 execvp(argv[i], argv + i);
56 xbt_die("The model-checked process failed to exec(%s): %s", argv[i], strerror(errno));
59 simgrid::mc::Exploration* explo;
61 case ExplorationAlgorithm::CommDeterminism:
62 explo = simgrid::mc::create_communication_determinism_checker(session_.get());
65 case ExplorationAlgorithm::UDPOR:
66 explo = simgrid::mc::create_udpor_checker(session_.get());
69 case ExplorationAlgorithm::Safety:
70 explo = simgrid::mc::create_dfs_exploration(session_.get());
73 case ExplorationAlgorithm::Liveness:
74 explo = simgrid::mc::create_liveness_checker(session_.get());
81 mc_model_checker->set_exploration(explo);
85 unsigned long Api::get_maxpid() const
87 return mc_model_checker->get_remote_process().get_maxpid();
90 std::size_t Api::get_remote_heap_bytes() const
92 RemoteProcess& process = mc_model_checker->get_remote_process();
93 auto heap_bytes_used = mmalloc_get_bytes_used_remote(process.get_heap()->heaplimit, process.get_malloc_info());
94 return heap_bytes_used;
97 void Api::mc_inc_visited_states() const
99 mc_model_checker->inc_visited_states();
102 unsigned long Api::mc_get_visited_states() const
104 return mc_model_checker->get_visited_states();
107 void Api::mc_exit(int status) const
109 mc_model_checker->exit(status);
112 void Api::restore_state(const simgrid::mc::Snapshot* system_state) const
114 system_state->restore(&mc_model_checker->get_remote_process());
117 bool Api::snapshot_equal(const Snapshot* s1, const Snapshot* s2) const
119 return simgrid::mc::snapshot_equal(s1, s2);
122 simgrid::mc::Snapshot* Api::take_snapshot(long num_state) const
124 auto snapshot = new simgrid::mc::Snapshot(num_state);
131 if (simgrid::mc::property_automaton != nullptr) {
132 xbt_automaton_free(simgrid::mc::property_automaton);
133 simgrid::mc::property_automaton = nullptr;
137 void Api::automaton_load(const char* file) const
139 if (simgrid::mc::property_automaton == nullptr)
140 simgrid::mc::property_automaton = xbt_automaton_new();
142 xbt_automaton_load(simgrid::mc::property_automaton, file);
145 std::vector<int> Api::automaton_propositional_symbol_evaluate() const
147 unsigned int cursor = 0;
148 std::vector<int> values;
149 xbt_automaton_propositional_symbol_t ps = nullptr;
150 xbt_dynar_foreach (mc::property_automaton->propositional_symbols, cursor, ps)
151 values.push_back(xbt_automaton_propositional_symbol_evaluate(ps));
155 std::vector<xbt_automaton_state_t> Api::get_automaton_state() const
157 std::vector<xbt_automaton_state_t> automaton_stack;
158 unsigned int cursor = 0;
159 xbt_automaton_state_t automaton_state;
160 xbt_dynar_foreach (mc::property_automaton->states, cursor, automaton_state)
161 if (automaton_state->type == -1)
162 automaton_stack.push_back(automaton_state);
163 return automaton_stack;
166 int Api::compare_automaton_exp_label(const xbt_automaton_exp_label* l) const
168 unsigned int cursor = 0;
169 xbt_automaton_propositional_symbol_t p = nullptr;
170 xbt_dynar_foreach (simgrid::mc::property_automaton->propositional_symbols, cursor, p) {
171 if (std::strcmp(xbt_automaton_propositional_symbol_get_name(p), l->u.predicat) == 0)
177 void Api::set_property_automaton(xbt_automaton_state_t const& automaton_state) const
179 mc::property_automaton->current_state = automaton_state;
182 xbt_automaton_exp_label_t Api::get_automaton_transition_label(xbt_dynar_t const& dynar, int index) const
184 const xbt_automaton_transition* transition = xbt_dynar_get_as(dynar, index, xbt_automaton_transition_t);
185 return transition->label;
188 xbt_automaton_state_t Api::get_automaton_transition_dst(xbt_dynar_t const& dynar, int index) const
190 const xbt_automaton_transition* transition = xbt_dynar_get_as(dynar, index, xbt_automaton_transition_t);
191 return transition->dst;
194 } // namespace simgrid::mc