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 using Simcall = simgrid::simix::Simcall;
36 /** Statically "upcast" a s_smx_actor_t into an ActorInformation
38 * This gets 'actorInfo' from '&actorInfo->copy'. It upcasts in the
39 * sense that we could achieve the same thing by having ActorInformation
40 * inherit from s_smx_actor_t but we don't really want to do that.
42 static simgrid::mc::ActorInformation* actor_info_cast(smx_actor_t actor)
44 simgrid::mc::ActorInformation temp;
45 std::size_t offset = (char*)temp.copy.get_buffer() - (char*)&temp;
47 auto* process_info = reinterpret_cast<simgrid::mc::ActorInformation*>((char*)actor - offset);
51 simgrid::mc::Exploration* Api::initialize(char** argv, simgrid::mc::CheckerAlgorithm algo)
53 session_ = std::make_unique<simgrid::mc::Session>([argv] {
55 while (argv[i] != nullptr && argv[i][0] == '-')
57 xbt_assert(argv[i] != nullptr,
58 "Unable to find a binary to exec on the command line. Did you only pass config flags?");
59 execvp(argv[i], argv + i);
60 xbt_die("The model-checked process failed to exec(%s): %s", argv[i], strerror(errno));
63 simgrid::mc::Exploration* explo;
65 case CheckerAlgorithm::CommDeterminism:
66 explo = simgrid::mc::create_communication_determinism_checker(session_.get());
69 case CheckerAlgorithm::UDPOR:
70 explo = simgrid::mc::create_udpor_checker(session_.get());
73 case CheckerAlgorithm::Safety:
74 explo = simgrid::mc::create_safety_checker(session_.get());
77 case CheckerAlgorithm::Liveness:
78 explo = simgrid::mc::create_liveness_checker(session_.get());
85 mc_model_checker->set_exploration(explo);
89 std::vector<simgrid::mc::ActorInformation>& Api::get_actors() const
91 return mc_model_checker->get_remote_process().actors();
94 unsigned long Api::get_maxpid() const
96 return mc_model_checker->get_remote_process().get_maxpid();
99 std::size_t Api::get_remote_heap_bytes() const
101 RemoteProcess& process = mc_model_checker->get_remote_process();
102 auto heap_bytes_used = mmalloc_get_bytes_used_remote(process.get_heap()->heaplimit, process.get_malloc_info());
103 return heap_bytes_used;
106 void Api::mc_inc_visited_states() const
108 mc_model_checker->visited_states++;
111 unsigned long Api::mc_get_visited_states() const
113 return mc_model_checker->visited_states;
116 void Api::mc_exit(int status) const
118 mc_model_checker->exit(status);
121 void Api::restore_state(std::shared_ptr<simgrid::mc::Snapshot> system_state) const
123 system_state->restore(&mc_model_checker->get_remote_process());
126 bool Api::snapshot_equal(const Snapshot* s1, const Snapshot* s2) const
128 return simgrid::mc::snapshot_equal(s1, s2);
131 simgrid::mc::Snapshot* Api::take_snapshot(long num_state) const
133 auto snapshot = new simgrid::mc::Snapshot(num_state);
140 if (simgrid::mc::property_automaton != nullptr) {
141 xbt_automaton_free(simgrid::mc::property_automaton);
142 simgrid::mc::property_automaton = nullptr;
146 void Api::automaton_load(const char* file) const
148 if (simgrid::mc::property_automaton == nullptr)
149 simgrid::mc::property_automaton = xbt_automaton_new();
151 xbt_automaton_load(simgrid::mc::property_automaton, file);
154 std::vector<int> Api::automaton_propositional_symbol_evaluate() const
156 unsigned int cursor = 0;
157 std::vector<int> values;
158 xbt_automaton_propositional_symbol_t ps = nullptr;
159 xbt_dynar_foreach (mc::property_automaton->propositional_symbols, cursor, ps)
160 values.push_back(xbt_automaton_propositional_symbol_evaluate(ps));
164 std::vector<xbt_automaton_state_t> Api::get_automaton_state() const
166 std::vector<xbt_automaton_state_t> automaton_stack;
167 unsigned int cursor = 0;
168 xbt_automaton_state_t automaton_state;
169 xbt_dynar_foreach (mc::property_automaton->states, cursor, automaton_state)
170 if (automaton_state->type == -1)
171 automaton_stack.push_back(automaton_state);
172 return automaton_stack;
175 int Api::compare_automaton_exp_label(const xbt_automaton_exp_label* l) const
177 unsigned int cursor = 0;
178 xbt_automaton_propositional_symbol_t p = nullptr;
179 xbt_dynar_foreach (simgrid::mc::property_automaton->propositional_symbols, cursor, p) {
180 if (std::strcmp(xbt_automaton_propositional_symbol_get_name(p), l->u.predicat) == 0)
186 void Api::set_property_automaton(xbt_automaton_state_t const& automaton_state) const
188 mc::property_automaton->current_state = automaton_state;
191 xbt_automaton_exp_label_t Api::get_automaton_transition_label(xbt_dynar_t const& dynar, int index) const
193 const xbt_automaton_transition* transition = xbt_dynar_get_as(dynar, index, xbt_automaton_transition_t);
194 return transition->label;
197 xbt_automaton_state_t Api::get_automaton_transition_dst(xbt_dynar_t const& dynar, int index) const
199 const xbt_automaton_transition* transition = xbt_dynar_get_as(dynar, index, xbt_automaton_transition_t);
200 return transition->dst;
204 } // namespace simgrid