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 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(session_.get());
56 case ExplorationAlgorithm::UDPOR:
57 explo = simgrid::mc::create_udpor_checker(session_.get());
60 case ExplorationAlgorithm::Safety:
61 explo = simgrid::mc::create_dfs_exploration(session_.get());
64 case ExplorationAlgorithm::Liveness:
65 explo = simgrid::mc::create_liveness_checker(session_.get());
72 mc_model_checker->set_exploration(explo);
76 std::vector<simgrid::mc::ActorInformation>& Api::get_actors() const
78 return mc_model_checker->get_remote_process().actors();
81 unsigned long Api::get_maxpid() const
83 return mc_model_checker->get_remote_process().get_maxpid();
86 std::size_t Api::get_remote_heap_bytes() const
88 RemoteProcess& process = mc_model_checker->get_remote_process();
89 auto heap_bytes_used = mmalloc_get_bytes_used_remote(process.get_heap()->heaplimit, process.get_malloc_info());
90 return heap_bytes_used;
93 void Api::mc_inc_visited_states() const
95 mc_model_checker->inc_visited_states();
98 unsigned long Api::mc_get_visited_states() const
100 return mc_model_checker->get_visited_states();
103 void Api::mc_exit(int status) const
105 mc_model_checker->exit(status);
108 void Api::restore_state(const simgrid::mc::Snapshot* system_state) const
110 system_state->restore(&mc_model_checker->get_remote_process());
113 bool Api::snapshot_equal(const Snapshot* s1, const Snapshot* s2) const
115 return simgrid::mc::snapshot_equal(s1, s2);
118 simgrid::mc::Snapshot* Api::take_snapshot(long num_state) const
120 auto snapshot = new simgrid::mc::Snapshot(num_state);
127 if (simgrid::mc::property_automaton != nullptr) {
128 xbt_automaton_free(simgrid::mc::property_automaton);
129 simgrid::mc::property_automaton = nullptr;
133 void Api::automaton_load(const char* file) const
135 if (simgrid::mc::property_automaton == nullptr)
136 simgrid::mc::property_automaton = xbt_automaton_new();
138 xbt_automaton_load(simgrid::mc::property_automaton, file);
141 std::vector<int> Api::automaton_propositional_symbol_evaluate() const
143 unsigned int cursor = 0;
144 std::vector<int> values;
145 xbt_automaton_propositional_symbol_t ps = nullptr;
146 xbt_dynar_foreach (mc::property_automaton->propositional_symbols, cursor, ps)
147 values.push_back(xbt_automaton_propositional_symbol_evaluate(ps));
151 std::vector<xbt_automaton_state_t> Api::get_automaton_state() const
153 std::vector<xbt_automaton_state_t> automaton_stack;
154 unsigned int cursor = 0;
155 xbt_automaton_state_t automaton_state;
156 xbt_dynar_foreach (mc::property_automaton->states, cursor, automaton_state)
157 if (automaton_state->type == -1)
158 automaton_stack.push_back(automaton_state);
159 return automaton_stack;
162 int Api::compare_automaton_exp_label(const xbt_automaton_exp_label* l) const
164 unsigned int cursor = 0;
165 xbt_automaton_propositional_symbol_t p = nullptr;
166 xbt_dynar_foreach (simgrid::mc::property_automaton->propositional_symbols, cursor, p) {
167 if (std::strcmp(xbt_automaton_propositional_symbol_get_name(p), l->u.predicat) == 0)
173 void Api::set_property_automaton(xbt_automaton_state_t const& automaton_state) const
175 mc::property_automaton->current_state = automaton_state;
178 xbt_automaton_exp_label_t Api::get_automaton_transition_label(xbt_dynar_t const& dynar, int index) const
180 const xbt_automaton_transition* transition = xbt_dynar_get_as(dynar, index, xbt_automaton_transition_t);
181 return transition->label;
184 xbt_automaton_state_t Api::get_automaton_transition_dst(xbt_dynar_t const& dynar, int index) const
186 const xbt_automaton_transition* transition = xbt_dynar_get_as(dynar, index, xbt_automaton_transition_t);
187 return transition->dst;
190 } // namespace simgrid::mc