Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
a888c11ef956a6cd5d0735e7f9c466f700d8c538
[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_comm_pattern.hpp"
15 #include "src/mc/mc_exit.hpp"
16 #include "src/mc/mc_pattern.hpp"
17 #include "src/mc/mc_private.hpp"
18 #include "src/mc/remote/RemoteProcess.hpp"
19 #include "src/surf/HostImpl.hpp"
20
21 #include <xbt/asserts.h>
22 #include <xbt/log.h>
23 #include "simgrid/s4u/Host.hpp"
24 #include "xbt/string.hpp"
25 #if HAVE_SMPI
26 #include "src/smpi/include/smpi_request.hpp"
27 #endif
28
29 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(Api, mc, "Logging specific to MC Facade APIs ");
30 XBT_LOG_EXTERNAL_CATEGORY(mc_global);
31
32 using Simcall = simgrid::simix::Simcall;
33
34 namespace simgrid {
35 namespace mc {
36
37 /** Statically "upcast" a s_smx_actor_t into an ActorInformation
38  *
39  *  This gets 'actorInfo' from '&actorInfo->copy'. It upcasts in the
40  *  sense that we could achieve the same thing by having ActorInformation
41  *  inherit from s_smx_actor_t but we don't really want to do that.
42  */
43 simgrid::mc::ActorInformation* Api::actor_info_cast(smx_actor_t actor) const
44 {
45   simgrid::mc::ActorInformation temp;
46   std::size_t offset = (char*)temp.copy.get_buffer() - (char*)&temp;
47
48   auto* process_info = reinterpret_cast<simgrid::mc::ActorInformation*>((char*)actor - offset);
49   return process_info;
50 }
51
52 xbt::string const& Api::get_actor_host_name(smx_actor_t actor) const
53 {
54   if (mc_model_checker == nullptr)
55     return actor->get_host()->get_name();
56
57   const simgrid::mc::RemoteProcess* process = &mc_model_checker->get_remote_process();
58
59   // Read the simgrid::xbt::string in the MCed process:
60   simgrid::mc::ActorInformation* info = actor_info_cast(actor);
61
62   if (not info->hostname) {
63     Remote<s4u::Host> temp_host = process->read(remote(actor->get_host()));
64     auto remote_string_address  = remote(&xbt::string::to_string_data(temp_host.get_buffer()->get_impl()->get_name()));
65     simgrid::xbt::string_data remote_string = process->read(remote_string_address);
66     std::vector<char> hostname(remote_string.len + 1);
67     // no need to read the terminating null byte, and thus hostname[remote_string.len] is guaranteed to be '\0'
68     process->read_bytes(hostname.data(), remote_string.len, remote(remote_string.data));
69     info->hostname = &mc_model_checker->get_host_name(hostname.data());
70   }
71   return *info->hostname;
72 }
73
74 xbt::string const& Api::get_actor_name(smx_actor_t actor) const
75 {
76   if (mc_model_checker == nullptr)
77     return actor->get_name();
78
79   simgrid::mc::ActorInformation* info = actor_info_cast(actor);
80   if (info->name.empty()) {
81     const simgrid::mc::RemoteProcess* process = &mc_model_checker->get_remote_process();
82
83     simgrid::xbt::string_data string_data = simgrid::xbt::string::to_string_data(actor->name_);
84     info->name = process->read_string(remote(string_data.data), string_data.len);
85   }
86   return info->name;
87 }
88
89 std::string Api::get_actor_string(smx_actor_t actor) const
90 {
91   std::string res;
92   if (actor) {
93     res = "(" + std::to_string(actor->get_pid()) + ")";
94     if (actor->get_host())
95       res += std::string(get_actor_host_name(actor)) + " (" + std::string(get_actor_name(actor)) + ")";
96     else
97       res += get_actor_name(actor);
98   } else
99     res = "(0) ()";
100   return res;
101 }
102
103 std::string Api::get_actor_dot_label(smx_actor_t actor) const
104 {
105   std::string res = "(" + std::to_string(actor->get_pid()) + ")";
106   if (actor->get_host())
107     res += get_actor_host_name(actor);
108   return res;
109 }
110
111 simgrid::mc::Checker* Api::initialize(char** argv, simgrid::mc::CheckerAlgorithm algo) const
112 {
113   auto session = new simgrid::mc::Session([argv] {
114     int i = 1;
115     while (argv[i] != nullptr && argv[i][0] == '-')
116       i++;
117     xbt_assert(argv[i] != nullptr,
118                "Unable to find a binary to exec on the command line. Did you only pass config flags?");
119     execvp(argv[i], argv + i);
120     xbt_die("The model-checked process failed to exec(%s): %s", argv[i], strerror(errno));
121   });
122
123   simgrid::mc::Checker* checker;
124   switch (algo) {
125     case CheckerAlgorithm::CommDeterminism:
126       checker = simgrid::mc::create_communication_determinism_checker(session);
127       break;
128
129     case CheckerAlgorithm::UDPOR:
130       checker = simgrid::mc::create_udpor_checker(session);
131       break;
132
133     case CheckerAlgorithm::Safety:
134       checker = simgrid::mc::create_safety_checker(session);
135       break;
136
137     case CheckerAlgorithm::Liveness:
138       checker = simgrid::mc::create_liveness_checker(session);
139       break;
140
141     default:
142       THROW_IMPOSSIBLE;
143   }
144
145   // FIXME: session and checker are never deleted
146   simgrid::mc::session_singleton = session;
147   mc_model_checker->setChecker(checker);
148   return checker;
149 }
150
151 std::vector<simgrid::mc::ActorInformation>& Api::get_actors() const
152 {
153   return mc_model_checker->get_remote_process().actors();
154 }
155
156 unsigned long Api::get_maxpid() const
157 {
158   return mc_model_checker->get_remote_process().get_maxpid();
159 }
160
161 int Api::get_actors_size() const
162 {
163   return mc_model_checker->get_remote_process().actors().size();
164 }
165
166 RemotePtr<kernel::activity::CommImpl> Api::get_comm_isend_raw_addr(smx_simcall_t request) const
167 {
168   return remote(static_cast<kernel::activity::CommImpl*>(simcall_comm_isend__getraw__result(request)));
169 }
170
171 RemotePtr<kernel::activity::CommImpl> Api::get_comm_waitany_raw_addr(smx_simcall_t request, int value) const
172 {
173   auto addr      = simcall_comm_waitany__getraw__comms(request) + value;
174   auto comm_addr = mc_model_checker->get_remote_process().read(remote(addr));
175   return RemotePtr<kernel::activity::CommImpl>(static_cast<kernel::activity::CommImpl*>(comm_addr));
176 }
177
178 std::string Api::get_pattern_comm_rdv(RemotePtr<kernel::activity::CommImpl> const& addr) const
179 {
180   Remote<kernel::activity::CommImpl> temp_activity;
181   mc_model_checker->get_remote_process().read(temp_activity, addr);
182   const kernel::activity::CommImpl* activity = temp_activity.get_buffer();
183
184   char* remote_name = mc_model_checker->get_remote_process().read<char*>(RemotePtr<char*>(
185       (uint64_t)(activity->get_mailbox() ? &activity->get_mailbox()->get_name() : &activity->mbox_cpy->get_name())));
186   auto rdv          = mc_model_checker->get_remote_process().read_string(RemotePtr<char>(remote_name));
187   return rdv;
188 }
189
190 unsigned long Api::get_pattern_comm_src_proc(RemotePtr<kernel::activity::CommImpl> const& addr) const
191 {
192   Remote<kernel::activity::CommImpl> temp_activity;
193   mc_model_checker->get_remote_process().read(temp_activity, addr);
194   const kernel::activity::CommImpl* activity = temp_activity.get_buffer();
195   auto src_proc =
196       mc_model_checker->get_remote_process().resolve_actor(mc::remote(activity->src_actor_.get()))->get_pid();
197   return src_proc;
198 }
199
200 unsigned long Api::get_pattern_comm_dst_proc(RemotePtr<kernel::activity::CommImpl> const& addr) const
201 {
202   Remote<kernel::activity::CommImpl> temp_activity;
203   mc_model_checker->get_remote_process().read(temp_activity, addr);
204   const kernel::activity::CommImpl* activity = temp_activity.get_buffer();
205   auto src_proc =
206       mc_model_checker->get_remote_process().resolve_actor(mc::remote(activity->dst_actor_.get()))->get_pid();
207   return src_proc;
208 }
209
210 std::vector<char> Api::get_pattern_comm_data(RemotePtr<kernel::activity::CommImpl> const& addr) const
211 {
212   simgrid::mc::Remote<simgrid::kernel::activity::CommImpl> temp_comm;
213   mc_model_checker->get_remote_process().read(temp_comm, addr);
214   const simgrid::kernel::activity::CommImpl* comm = temp_comm.get_buffer();
215
216   std::vector<char> buffer{};
217   if (comm->src_buff_ != nullptr) {
218     buffer.resize(comm->src_buff_size_);
219     mc_model_checker->get_remote_process().read_bytes(buffer.data(), buffer.size(), remote(comm->src_buff_));
220   }
221   return buffer;
222 }
223
224 #if HAVE_SMPI
225 bool Api::check_send_request_detached(smx_simcall_t const& simcall) const
226 {
227   Remote<simgrid::smpi::Request> mpi_request;
228   mc_model_checker->get_remote_process().read(
229       mpi_request, remote(static_cast<smpi::Request*>(simcall_comm_isend__get__data(simcall))));
230   return mpi_request.get_buffer()->detached();
231 }
232 #endif
233
234 smx_actor_t Api::get_src_actor(RemotePtr<kernel::activity::CommImpl> const& comm_addr) const
235 {
236   simgrid::mc::Remote<simgrid::kernel::activity::CommImpl> temp_comm;
237   mc_model_checker->get_remote_process().read(temp_comm, comm_addr);
238   const simgrid::kernel::activity::CommImpl* comm = temp_comm.get_buffer();
239
240   auto src_proc = mc_model_checker->get_remote_process().resolve_actor(simgrid::mc::remote(comm->src_actor_.get()));
241   return src_proc;
242 }
243
244 smx_actor_t Api::get_dst_actor(RemotePtr<kernel::activity::CommImpl> const& comm_addr) const
245 {
246   simgrid::mc::Remote<simgrid::kernel::activity::CommImpl> temp_comm;
247   mc_model_checker->get_remote_process().read(temp_comm, comm_addr);
248   const simgrid::kernel::activity::CommImpl* comm = temp_comm.get_buffer();
249
250   auto dst_proc = mc_model_checker->get_remote_process().resolve_actor(simgrid::mc::remote(comm->dst_actor_.get()));
251   return dst_proc;
252 }
253
254 std::size_t Api::get_remote_heap_bytes() const
255 {
256   RemoteProcess& process    = mc_model_checker->get_remote_process();
257   auto heap_bytes_used      = mmalloc_get_bytes_used_remote(process.get_heap()->heaplimit, process.get_malloc_info());
258   return heap_bytes_used;
259 }
260
261 void Api::mc_inc_visited_states() const
262 {
263   mc_model_checker->visited_states++;
264 }
265
266 unsigned long Api::mc_get_visited_states() const
267 {
268   return mc_model_checker->visited_states;
269 }
270
271 void Api::mc_check_deadlock() const
272 {
273   if (mc_model_checker->checkDeadlock()) {
274     XBT_CINFO(mc_global, "**************************");
275     XBT_CINFO(mc_global, "*** DEADLOCK DETECTED ***");
276     XBT_CINFO(mc_global, "**************************");
277     XBT_CINFO(mc_global, "Counter-example execution trace:");
278     for (auto const& s : mc_model_checker->getChecker()->get_textual_trace())
279       XBT_CINFO(mc_global, "  %s", s.c_str());
280     XBT_INFO("Path = %s", mc_model_checker->getChecker()->get_record_trace().to_string().c_str());
281     simgrid::mc::session_singleton->log_state();
282     throw DeadlockError();
283   }
284 }
285
286 /** Get the issuer of a simcall (`req->issuer`)
287  *
288  *  In split-process mode, it does the black magic necessary to get an address
289  *  of a (shallow) copy of the data structure the issuer SIMIX actor in the local
290  *  address space.
291  *
292  *  @param process the MCed process
293  *  @param req     the simcall (copied in the local process)
294  */
295 smx_actor_t Api::simcall_get_issuer(s_smx_simcall const* req) const
296 {
297   xbt_assert(mc_model_checker != nullptr);
298
299   // This is the address of the smx_actor in the MCed process:
300   auto address = simgrid::mc::remote(req->issuer_);
301
302   // Lookup by address:
303   for (auto& actor : mc_model_checker->get_remote_process().actors())
304     if (actor.address == address)
305       return actor.copy.get_buffer();
306   for (auto& actor : mc_model_checker->get_remote_process().dead_actors())
307     if (actor.address == address)
308       return actor.copy.get_buffer();
309
310   xbt_die("Issuer not found");
311 }
312
313 RemotePtr<kernel::activity::MailboxImpl> Api::get_mbox_remote_addr(smx_simcall_t const req) const
314 {
315   if (req->call_ == Simcall::COMM_ISEND)
316     return remote(simcall_comm_isend__get__mbox(req));
317   if (req->call_ == Simcall::COMM_IRECV)
318     return remote(simcall_comm_irecv__get__mbox(req));
319   THROW_IMPOSSIBLE;
320 }
321
322 RemotePtr<kernel::activity::ActivityImpl> Api::get_comm_remote_addr(smx_simcall_t const req) const
323 {
324   if (req->call_ == Simcall::COMM_ISEND)
325     return remote(simcall_comm_isend__getraw__result(req));
326   if (req->call_ == Simcall::COMM_IRECV)
327     return remote(simcall_comm_irecv__getraw__result(req));
328   THROW_IMPOSSIBLE;
329 }
330
331 void Api::mc_exit(int status) const
332 {
333   mc_model_checker->exit(status);
334 }
335
336 std::string Api::request_get_dot_output(const Transition* t) const
337 {
338   static constexpr std::array<const char*, 13> colors{{"blue", "red", "green3", "goldenrod", "brown", "purple",
339                                                        "magenta", "turquoise4", "gray25", "forestgreen", "hotpink",
340                                                        "lightblue", "tan"}};
341   const char* color = colors[(t->aid_ - 1) % colors.size()];
342
343   return "label = \"" + t->dot_label() + "\", color = " + color + ", fontcolor = " + color;
344 }
345
346 #if HAVE_SMPI
347 int Api::get_smpi_request_tag(smx_simcall_t const& simcall, simgrid::simix::Simcall type) const
348 {
349   void* simcall_data = nullptr;
350   if (type == Simcall::COMM_ISEND)
351     simcall_data = simcall_comm_isend__get__data(simcall);
352   else if (type == Simcall::COMM_IRECV)
353     simcall_data = simcall_comm_irecv__get__data(simcall);
354   Remote<simgrid::smpi::Request> mpi_request;
355   mc_model_checker->get_remote_process().read(mpi_request, remote(static_cast<smpi::Request*>(simcall_data)));
356   return mpi_request.get_buffer()->tag();
357 }
358 #endif
359
360 void Api::restore_state(std::shared_ptr<simgrid::mc::Snapshot> system_state) const
361 {
362   system_state->restore(&mc_model_checker->get_remote_process());
363 }
364
365 void Api::log_state() const
366 {
367   session_singleton->log_state();
368 }
369
370 bool Api::snapshot_equal(const Snapshot* s1, const Snapshot* s2) const
371 {
372   return simgrid::mc::snapshot_equal(s1, s2);
373 }
374
375 simgrid::mc::Snapshot* Api::take_snapshot(long num_state) const
376 {
377   auto snapshot = new simgrid::mc::Snapshot(num_state);
378   return snapshot;
379 }
380
381 void Api::s_close() const
382 {
383   session_singleton->close();
384 }
385
386 void Api::automaton_load(const char* file) const
387 {
388   if (simgrid::mc::property_automaton == nullptr)
389     simgrid::mc::property_automaton = xbt_automaton_new();
390
391   xbt_automaton_load(simgrid::mc::property_automaton, file);
392 }
393
394 std::vector<int> Api::automaton_propositional_symbol_evaluate() const
395 {
396   unsigned int cursor = 0;
397   std::vector<int> values;
398   xbt_automaton_propositional_symbol_t ps = nullptr;
399   xbt_dynar_foreach (mc::property_automaton->propositional_symbols, cursor, ps)
400     values.push_back(xbt_automaton_propositional_symbol_evaluate(ps));
401   return values;
402 }
403
404 std::vector<xbt_automaton_state_t> Api::get_automaton_state() const
405 {
406   std::vector<xbt_automaton_state_t> automaton_stack;
407   unsigned int cursor = 0;
408   xbt_automaton_state_t automaton_state;
409   xbt_dynar_foreach (mc::property_automaton->states, cursor, automaton_state)
410     if (automaton_state->type == -1)
411       automaton_stack.push_back(automaton_state);
412   return automaton_stack;
413 }
414
415 int Api::compare_automaton_exp_label(const xbt_automaton_exp_label* l) const
416 {
417   unsigned int cursor                    = 0;
418   xbt_automaton_propositional_symbol_t p = nullptr;
419   xbt_dynar_foreach (simgrid::mc::property_automaton->propositional_symbols, cursor, p) {
420     if (std::strcmp(xbt_automaton_propositional_symbol_get_name(p), l->u.predicat) == 0)
421       return cursor;
422   }
423   return -1;
424 }
425
426 void Api::set_property_automaton(xbt_automaton_state_t const& automaton_state) const
427 {
428   mc::property_automaton->current_state = automaton_state;
429 }
430
431 xbt_automaton_exp_label_t Api::get_automaton_transition_label(xbt_dynar_t const& dynar, int index) const
432 {
433   const xbt_automaton_transition* transition = xbt_dynar_get_as(dynar, index, xbt_automaton_transition_t);
434   return transition->label;
435 }
436
437 xbt_automaton_state_t Api::get_automaton_transition_dst(xbt_dynar_t const& dynar, int index) const
438 {
439   const xbt_automaton_transition* transition = xbt_dynar_get_as(dynar, index, xbt_automaton_transition_t);
440   return transition->dst;
441 }
442
443 } // namespace mc
444 } // namespace simgrid