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/api/RemoteApp.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_private.hpp"
16 #include "src/mc/remote/RemoteProcess.hpp"
17 #include "src/surf/HostImpl.hpp"
19 #include <xbt/asserts.h>
21 #include "simgrid/s4u/Host.hpp"
22 #include "xbt/string.hpp"
24 #include "src/smpi/include/smpi_request.hpp"
27 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(Api, mc, "Logging specific to MC Facade APIs ");
28 XBT_LOG_EXTERNAL_CATEGORY(mc_global);
30 namespace simgrid::mc {
32 simgrid::mc::Exploration* Api::initialize(char** argv, const std::unordered_map<std::string, std::string>& env,
33 simgrid::mc::ExplorationAlgorithm algo)
35 remote_app_ = std::make_unique<simgrid::mc::RemoteApp>([argv, &env] {
37 while (argv[i] != nullptr && argv[i][0] == '-')
40 for (auto const& [key, val] : env) {
41 XBT_INFO("setenv '%s'='%s'", key.c_str(), val.c_str());
42 setenv(key.c_str(), val.c_str(), 1);
44 xbt_assert(argv[i] != nullptr,
45 "Unable to find a binary to exec on the command line. Did you only pass config flags?");
46 execvp(argv[i], argv + i);
47 xbt_die("The model-checked process failed to exec(%s): %s", argv[i], strerror(errno));
50 simgrid::mc::Exploration* explo;
52 case ExplorationAlgorithm::CommDeterminism:
53 explo = simgrid::mc::create_communication_determinism_checker(*(remote_app_.get()));
56 case ExplorationAlgorithm::UDPOR:
57 explo = simgrid::mc::create_udpor_checker(*(remote_app_.get()));
60 case ExplorationAlgorithm::Safety:
61 explo = simgrid::mc::create_dfs_exploration(*(remote_app_.get()));
64 case ExplorationAlgorithm::Liveness:
65 explo = simgrid::mc::create_liveness_checker(*(remote_app_.get()));
72 mc_model_checker->set_exploration(explo);
77 std::size_t Api::get_remote_heap_bytes() const
79 RemoteProcess& process = mc_model_checker->get_remote_process();
80 auto heap_bytes_used = mmalloc_get_bytes_used_remote(process.get_heap()->heaplimit, process.get_malloc_info());
81 return heap_bytes_used;
84 void Api::mc_inc_visited_states() const
86 mc_model_checker->inc_visited_states();
89 unsigned long Api::mc_get_visited_states() const
91 return mc_model_checker->get_visited_states();
94 void Api::restore_state(const simgrid::mc::Snapshot* system_state) const
96 system_state->restore(&mc_model_checker->get_remote_process());
99 bool Api::snapshot_equal(const Snapshot* s1, const Snapshot* s2) const
101 return simgrid::mc::snapshot_equal(s1, s2);
104 simgrid::mc::Snapshot* Api::take_snapshot(long num_state) const
106 auto snapshot = new simgrid::mc::Snapshot(num_state);
112 if (simgrid::mc::property_automaton != nullptr) {
113 xbt_automaton_free(simgrid::mc::property_automaton);
114 simgrid::mc::property_automaton = nullptr;
118 void Api::automaton_load(const char* file) const
120 if (simgrid::mc::property_automaton == nullptr)
121 simgrid::mc::property_automaton = xbt_automaton_new();
123 xbt_automaton_load(simgrid::mc::property_automaton, file);
126 std::vector<int> Api::automaton_propositional_symbol_evaluate() const
128 unsigned int cursor = 0;
129 std::vector<int> values;
130 xbt_automaton_propositional_symbol_t ps = nullptr;
131 xbt_dynar_foreach (mc::property_automaton->propositional_symbols, cursor, ps)
132 values.push_back(xbt_automaton_propositional_symbol_evaluate(ps));
136 std::vector<xbt_automaton_state_t> Api::get_automaton_state() const
138 std::vector<xbt_automaton_state_t> automaton_stack;
139 unsigned int cursor = 0;
140 xbt_automaton_state_t automaton_state;
141 xbt_dynar_foreach (mc::property_automaton->states, cursor, automaton_state)
142 if (automaton_state->type == -1)
143 automaton_stack.push_back(automaton_state);
144 return automaton_stack;
147 int Api::compare_automaton_exp_label(const xbt_automaton_exp_label* l) const
149 unsigned int cursor = 0;
150 xbt_automaton_propositional_symbol_t p = nullptr;
151 xbt_dynar_foreach (simgrid::mc::property_automaton->propositional_symbols, cursor, p) {
152 if (std::strcmp(xbt_automaton_propositional_symbol_get_name(p), l->u.predicat) == 0)
158 void Api::set_property_automaton(xbt_automaton_state_t const& automaton_state) const
160 mc::property_automaton->current_state = automaton_state;
163 xbt_automaton_exp_label_t Api::get_automaton_transition_label(xbt_dynar_t const& dynar, int index) const
165 const xbt_automaton_transition* transition = xbt_dynar_get_as(dynar, index, xbt_automaton_transition_t);
166 return transition->label;
169 xbt_automaton_state_t Api::get_automaton_transition_dst(xbt_dynar_t const& dynar, int index) const
171 const xbt_automaton_transition* transition = xbt_dynar_get_as(dynar, index, xbt_automaton_transition_t);
172 return transition->dst;
175 } // namespace simgrid::mc