Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Kill two more unused functions in mc::api
[simgrid.git] / src / mc / api.cpp
1 /* Copyright (c) 2020-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 "api.hpp"
7
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/checker/Checker.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"
19
20 #include <xbt/asserts.h>
21 #include <xbt/log.h>
22 #include "simgrid/s4u/Host.hpp"
23 #include "xbt/string.hpp"
24 #if HAVE_SMPI
25 #include "src/smpi/include/smpi_request.hpp"
26 #endif
27
28 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(Api, mc, "Logging specific to MC Facade APIs ");
29 XBT_LOG_EXTERNAL_CATEGORY(mc_global);
30
31 using Simcall = simgrid::simix::Simcall;
32
33 namespace simgrid {
34 namespace mc {
35
36 /** Statically "upcast" a s_smx_actor_t into an ActorInformation
37  *
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.
41  */
42 static simgrid::mc::ActorInformation* actor_info_cast(smx_actor_t actor)
43 {
44   simgrid::mc::ActorInformation temp;
45   std::size_t offset = (char*)temp.copy.get_buffer() - (char*)&temp;
46
47   auto* process_info = reinterpret_cast<simgrid::mc::ActorInformation*>((char*)actor - offset);
48   return process_info;
49 }
50
51 xbt::string const& Api::get_actor_host_name(smx_actor_t actor) const
52 {
53   if (mc_model_checker == nullptr)
54     return actor->get_host()->get_name();
55
56   const simgrid::mc::RemoteProcess* process = &mc_model_checker->get_remote_process();
57
58   // Read the simgrid::xbt::string in the MCed process:
59   simgrid::mc::ActorInformation* info = actor_info_cast(actor);
60
61   if (not info->hostname) {
62     Remote<s4u::Host> temp_host = process->read(remote(actor->get_host()));
63     auto remote_string_address  = remote(&xbt::string::to_string_data(temp_host.get_buffer()->get_impl()->get_name()));
64     simgrid::xbt::string_data remote_string = process->read(remote_string_address);
65     std::vector<char> hostname(remote_string.len + 1);
66     // no need to read the terminating null byte, and thus hostname[remote_string.len] is guaranteed to be '\0'
67     process->read_bytes(hostname.data(), remote_string.len, remote(remote_string.data));
68     info->hostname = &mc_model_checker->get_host_name(hostname.data());
69   }
70   return *info->hostname;
71 }
72
73 xbt::string const& Api::get_actor_name(smx_actor_t actor) const
74 {
75   if (mc_model_checker == nullptr)
76     return actor->get_name();
77
78   simgrid::mc::ActorInformation* info = actor_info_cast(actor);
79   if (info->name.empty()) {
80     const simgrid::mc::RemoteProcess* process = &mc_model_checker->get_remote_process();
81
82     simgrid::xbt::string_data string_data = simgrid::xbt::string::to_string_data(actor->name_);
83     info->name = process->read_string(remote(string_data.data), string_data.len);
84   }
85   return info->name;
86 }
87
88 simgrid::mc::Checker* Api::initialize(char** argv, simgrid::mc::CheckerAlgorithm algo) const
89 {
90   auto session = new simgrid::mc::Session([argv] {
91     int i = 1;
92     while (argv[i] != nullptr && argv[i][0] == '-')
93       i++;
94     xbt_assert(argv[i] != nullptr,
95                "Unable to find a binary to exec on the command line. Did you only pass config flags?");
96     execvp(argv[i], argv + i);
97     xbt_die("The model-checked process failed to exec(%s): %s", argv[i], strerror(errno));
98   });
99
100   simgrid::mc::Checker* checker;
101   switch (algo) {
102     case CheckerAlgorithm::CommDeterminism:
103       checker = simgrid::mc::create_communication_determinism_checker(session);
104       break;
105
106     case CheckerAlgorithm::UDPOR:
107       checker = simgrid::mc::create_udpor_checker(session);
108       break;
109
110     case CheckerAlgorithm::Safety:
111       checker = simgrid::mc::create_safety_checker(session);
112       break;
113
114     case CheckerAlgorithm::Liveness:
115       checker = simgrid::mc::create_liveness_checker(session);
116       break;
117
118     default:
119       THROW_IMPOSSIBLE;
120   }
121
122   // FIXME: session and checker are never deleted
123   simgrid::mc::session_singleton = session;
124   mc_model_checker->setChecker(checker);
125   return checker;
126 }
127
128 std::vector<simgrid::mc::ActorInformation>& Api::get_actors() const
129 {
130   return mc_model_checker->get_remote_process().actors();
131 }
132
133 unsigned long Api::get_maxpid() const
134 {
135   return mc_model_checker->get_remote_process().get_maxpid();
136 }
137
138 std::size_t Api::get_remote_heap_bytes() const
139 {
140   RemoteProcess& process    = mc_model_checker->get_remote_process();
141   auto heap_bytes_used      = mmalloc_get_bytes_used_remote(process.get_heap()->heaplimit, process.get_malloc_info());
142   return heap_bytes_used;
143 }
144
145 void Api::mc_inc_visited_states() const
146 {
147   mc_model_checker->visited_states++;
148 }
149
150 unsigned long Api::mc_get_visited_states() const
151 {
152   return mc_model_checker->visited_states;
153 }
154
155 void Api::mc_exit(int status) const
156 {
157   mc_model_checker->exit(status);
158 }
159
160 std::string Api::request_get_dot_output(const Transition* t) const
161 {
162   static constexpr std::array<const char*, 13> colors{{"blue", "red", "green3", "goldenrod", "brown", "purple",
163                                                        "magenta", "turquoise4", "gray25", "forestgreen", "hotpink",
164                                                        "lightblue", "tan"}};
165   const char* color = colors[(t->aid_ - 1) % colors.size()];
166
167   return "label = \"" + t->dot_label() + "\", color = " + color + ", fontcolor = " + color;
168 }
169
170 void Api::restore_state(std::shared_ptr<simgrid::mc::Snapshot> system_state) const
171 {
172   system_state->restore(&mc_model_checker->get_remote_process());
173 }
174
175 bool Api::snapshot_equal(const Snapshot* s1, const Snapshot* s2) const
176 {
177   return simgrid::mc::snapshot_equal(s1, s2);
178 }
179
180 simgrid::mc::Snapshot* Api::take_snapshot(long num_state) const
181 {
182   auto snapshot = new simgrid::mc::Snapshot(num_state);
183   return snapshot;
184 }
185
186 void Api::s_close() const
187 {
188   session_singleton->close();
189 }
190
191 void Api::automaton_load(const char* file) const
192 {
193   if (simgrid::mc::property_automaton == nullptr)
194     simgrid::mc::property_automaton = xbt_automaton_new();
195
196   xbt_automaton_load(simgrid::mc::property_automaton, file);
197 }
198
199 std::vector<int> Api::automaton_propositional_symbol_evaluate() const
200 {
201   unsigned int cursor = 0;
202   std::vector<int> values;
203   xbt_automaton_propositional_symbol_t ps = nullptr;
204   xbt_dynar_foreach (mc::property_automaton->propositional_symbols, cursor, ps)
205     values.push_back(xbt_automaton_propositional_symbol_evaluate(ps));
206   return values;
207 }
208
209 std::vector<xbt_automaton_state_t> Api::get_automaton_state() const
210 {
211   std::vector<xbt_automaton_state_t> automaton_stack;
212   unsigned int cursor = 0;
213   xbt_automaton_state_t automaton_state;
214   xbt_dynar_foreach (mc::property_automaton->states, cursor, automaton_state)
215     if (automaton_state->type == -1)
216       automaton_stack.push_back(automaton_state);
217   return automaton_stack;
218 }
219
220 int Api::compare_automaton_exp_label(const xbt_automaton_exp_label* l) const
221 {
222   unsigned int cursor                    = 0;
223   xbt_automaton_propositional_symbol_t p = nullptr;
224   xbt_dynar_foreach (simgrid::mc::property_automaton->propositional_symbols, cursor, p) {
225     if (std::strcmp(xbt_automaton_propositional_symbol_get_name(p), l->u.predicat) == 0)
226       return cursor;
227   }
228   return -1;
229 }
230
231 void Api::set_property_automaton(xbt_automaton_state_t const& automaton_state) const
232 {
233   mc::property_automaton->current_state = automaton_state;
234 }
235
236 xbt_automaton_exp_label_t Api::get_automaton_transition_label(xbt_dynar_t const& dynar, int index) const
237 {
238   const xbt_automaton_transition* transition = xbt_dynar_get_as(dynar, index, xbt_automaton_transition_t);
239   return transition->label;
240 }
241
242 xbt_automaton_state_t Api::get_automaton_transition_dst(xbt_dynar_t const& dynar, int index) const
243 {
244   const xbt_automaton_transition* transition = xbt_dynar_get_as(dynar, index, xbt_automaton_transition_t);
245   return transition->dst;
246 }
247
248 } // namespace mc
249 } // namespace simgrid