Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
MC: Inline and simplify a function
[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 static inline const char* get_color(int id)
38 {
39   static constexpr std::array<const char*, 13> colors{{"blue", "red", "green3", "goldenrod", "brown", "purple",
40                                                        "magenta", "turquoise4", "gray25", "forestgreen", "hotpink",
41                                                        "lightblue", "tan"}};
42   return colors[id % colors.size()];
43 }
44
45 static std::string pointer_to_string(void* pointer)
46 {
47   return XBT_LOG_ISENABLED(Api, xbt_log_priority_verbose) ? xbt::string_printf("%p", pointer) : "(verbose only)";
48 }
49
50 static std::string buff_size_to_string(size_t buff_size)
51 {
52   return XBT_LOG_ISENABLED(Api, xbt_log_priority_verbose) ? std::to_string(buff_size) : "(verbose only)";
53 }
54
55 /** Statically "upcast" a s_smx_actor_t into an ActorInformation
56  *
57  *  This gets 'actorInfo' from '&actorInfo->copy'. It upcasts in the
58  *  sense that we could achieve the same thing by having ActorInformation
59  *  inherit from s_smx_actor_t but we don't really want to do that.
60  */
61 simgrid::mc::ActorInformation* Api::actor_info_cast(smx_actor_t actor) const
62 {
63   simgrid::mc::ActorInformation temp;
64   std::size_t offset = (char*)temp.copy.get_buffer() - (char*)&temp;
65
66   auto* process_info = reinterpret_cast<simgrid::mc::ActorInformation*>((char*)actor - offset);
67   return process_info;
68 }
69
70 bool Api::requests_are_dependent(RemotePtr<kernel::actor::SimcallObserver> obs1,
71                                  RemotePtr<kernel::actor::SimcallObserver> obs2) const
72 {
73   xbt_assert(mc_model_checker != nullptr, "Must be called from MCer");
74
75   return mc_model_checker->requests_are_dependent(obs1, obs2);
76 }
77
78 xbt::string const& Api::get_actor_host_name(smx_actor_t actor) const
79 {
80   if (mc_model_checker == nullptr)
81     return actor->get_host()->get_name();
82
83   const simgrid::mc::RemoteProcess* process = &mc_model_checker->get_remote_process();
84
85   // Read the simgrid::xbt::string in the MCed process:
86   simgrid::mc::ActorInformation* info = actor_info_cast(actor);
87
88   if (not info->hostname) {
89     Remote<s4u::Host> temp_host = process->read(remote(actor->get_host()));
90     auto remote_string_address  = remote(&xbt::string::to_string_data(temp_host.get_buffer()->get_impl()->get_name()));
91     simgrid::xbt::string_data remote_string = process->read(remote_string_address);
92     std::vector<char> hostname(remote_string.len + 1);
93     // no need to read the terminating null byte, and thus hostname[remote_string.len] is guaranteed to be '\0'
94     process->read_bytes(hostname.data(), remote_string.len, remote(remote_string.data));
95     info->hostname = &mc_model_checker->get_host_name(hostname.data());
96   }
97   return *info->hostname;
98 }
99
100 xbt::string const& Api::get_actor_name(smx_actor_t actor) const
101 {
102   if (mc_model_checker == nullptr)
103     return actor->get_name();
104
105   simgrid::mc::ActorInformation* info = actor_info_cast(actor);
106   if (info->name.empty()) {
107     const simgrid::mc::RemoteProcess* process = &mc_model_checker->get_remote_process();
108
109     simgrid::xbt::string_data string_data = simgrid::xbt::string::to_string_data(actor->name_);
110     info->name = process->read_string(remote(string_data.data), string_data.len);
111   }
112   return info->name;
113 }
114
115 std::string Api::get_actor_string(smx_actor_t actor) const
116 {
117   std::string res;
118   if (actor) {
119     res = "(" + std::to_string(actor->get_pid()) + ")";
120     if (actor->get_host())
121       res += std::string(get_actor_host_name(actor)) + " (" + std::string(get_actor_name(actor)) + ")";
122     else
123       res += get_actor_name(actor);
124   } else
125     res = "(0) ()";
126   return res;
127 }
128
129 std::string Api::get_actor_dot_label(smx_actor_t actor) const
130 {
131   std::string res = "(" + std::to_string(actor->get_pid()) + ")";
132   if (actor->get_host())
133     res += get_actor_host_name(actor);
134   return res;
135 }
136
137 simgrid::mc::Checker* Api::initialize(char** argv, simgrid::mc::CheckerAlgorithm algo) const
138 {
139   auto session = new simgrid::mc::Session([argv] {
140     int i = 1;
141     while (argv[i] != nullptr && argv[i][0] == '-')
142       i++;
143     xbt_assert(argv[i] != nullptr,
144                "Unable to find a binary to exec on the command line. Did you only pass config flags?");
145     execvp(argv[i], argv + i);
146     xbt_die("The model-checked process failed to exec(%s): %s", argv[i], strerror(errno));
147   });
148
149   simgrid::mc::Checker* checker;
150   switch (algo) {
151     case CheckerAlgorithm::CommDeterminism:
152       checker = simgrid::mc::create_communication_determinism_checker(session);
153       break;
154
155     case CheckerAlgorithm::UDPOR:
156       checker = simgrid::mc::create_udpor_checker(session);
157       break;
158
159     case CheckerAlgorithm::Safety:
160       checker = simgrid::mc::create_safety_checker(session);
161       break;
162
163     case CheckerAlgorithm::Liveness:
164       checker = simgrid::mc::create_liveness_checker(session);
165       break;
166
167     default:
168       THROW_IMPOSSIBLE;
169   }
170
171   // FIXME: session and checker are never deleted
172   simgrid::mc::session_singleton = session;
173   mc_model_checker->setChecker(checker);
174   return checker;
175 }
176
177 std::vector<simgrid::mc::ActorInformation>& Api::get_actors() const
178 {
179   return mc_model_checker->get_remote_process().actors();
180 }
181
182 unsigned long Api::get_maxpid() const
183 {
184   return mc_model_checker->get_remote_process().get_maxpid();
185 }
186
187 int Api::get_actors_size() const
188 {
189   return mc_model_checker->get_remote_process().actors().size();
190 }
191
192 RemotePtr<kernel::activity::CommImpl> Api::get_comm_isend_raw_addr(smx_simcall_t request) const
193 {
194   return remote(static_cast<kernel::activity::CommImpl*>(simcall_comm_isend__getraw__result(request)));
195 }
196
197 RemotePtr<kernel::activity::CommImpl> Api::get_comm_waitany_raw_addr(smx_simcall_t request, int value) const
198 {
199   auto addr      = simcall_comm_waitany__getraw__comms(request) + value;
200   auto comm_addr = mc_model_checker->get_remote_process().read(remote(addr));
201   return RemotePtr<kernel::activity::CommImpl>(static_cast<kernel::activity::CommImpl*>(comm_addr));
202 }
203
204 std::string Api::get_pattern_comm_rdv(RemotePtr<kernel::activity::CommImpl> const& addr) const
205 {
206   Remote<kernel::activity::CommImpl> temp_activity;
207   mc_model_checker->get_remote_process().read(temp_activity, addr);
208   const kernel::activity::CommImpl* activity = temp_activity.get_buffer();
209
210   char* remote_name = mc_model_checker->get_remote_process().read<char*>(RemotePtr<char*>(
211       (uint64_t)(activity->get_mailbox() ? &activity->get_mailbox()->get_name() : &activity->mbox_cpy->get_name())));
212   auto rdv          = mc_model_checker->get_remote_process().read_string(RemotePtr<char>(remote_name));
213   return rdv;
214 }
215
216 unsigned long Api::get_pattern_comm_src_proc(RemotePtr<kernel::activity::CommImpl> const& addr) const
217 {
218   Remote<kernel::activity::CommImpl> temp_activity;
219   mc_model_checker->get_remote_process().read(temp_activity, addr);
220   const kernel::activity::CommImpl* activity = temp_activity.get_buffer();
221   auto src_proc =
222       mc_model_checker->get_remote_process().resolve_actor(mc::remote(activity->src_actor_.get()))->get_pid();
223   return src_proc;
224 }
225
226 unsigned long Api::get_pattern_comm_dst_proc(RemotePtr<kernel::activity::CommImpl> const& addr) const
227 {
228   Remote<kernel::activity::CommImpl> temp_activity;
229   mc_model_checker->get_remote_process().read(temp_activity, addr);
230   const kernel::activity::CommImpl* activity = temp_activity.get_buffer();
231   auto src_proc =
232       mc_model_checker->get_remote_process().resolve_actor(mc::remote(activity->dst_actor_.get()))->get_pid();
233   return src_proc;
234 }
235
236 std::vector<char> Api::get_pattern_comm_data(RemotePtr<kernel::activity::CommImpl> const& addr) const
237 {
238   simgrid::mc::Remote<simgrid::kernel::activity::CommImpl> temp_comm;
239   mc_model_checker->get_remote_process().read(temp_comm, addr);
240   const simgrid::kernel::activity::CommImpl* comm = temp_comm.get_buffer();
241
242   std::vector<char> buffer{};
243   if (comm->src_buff_ != nullptr) {
244     buffer.resize(comm->src_buff_size_);
245     mc_model_checker->get_remote_process().read_bytes(buffer.data(), buffer.size(), remote(comm->src_buff_));
246   }
247   return buffer;
248 }
249
250 #if HAVE_SMPI
251 bool Api::check_send_request_detached(smx_simcall_t const& simcall) const
252 {
253   Remote<simgrid::smpi::Request> mpi_request;
254   mc_model_checker->get_remote_process().read(
255       mpi_request, remote(static_cast<smpi::Request*>(simcall_comm_isend__get__data(simcall))));
256   return mpi_request.get_buffer()->detached();
257 }
258 #endif
259
260 smx_actor_t Api::get_src_actor(RemotePtr<kernel::activity::CommImpl> const& comm_addr) const
261 {
262   simgrid::mc::Remote<simgrid::kernel::activity::CommImpl> temp_comm;
263   mc_model_checker->get_remote_process().read(temp_comm, comm_addr);
264   const simgrid::kernel::activity::CommImpl* comm = temp_comm.get_buffer();
265
266   auto src_proc = mc_model_checker->get_remote_process().resolve_actor(simgrid::mc::remote(comm->src_actor_.get()));
267   return src_proc;
268 }
269
270 smx_actor_t Api::get_dst_actor(RemotePtr<kernel::activity::CommImpl> const& comm_addr) const
271 {
272   simgrid::mc::Remote<simgrid::kernel::activity::CommImpl> temp_comm;
273   mc_model_checker->get_remote_process().read(temp_comm, comm_addr);
274   const simgrid::kernel::activity::CommImpl* comm = temp_comm.get_buffer();
275
276   auto dst_proc = mc_model_checker->get_remote_process().resolve_actor(simgrid::mc::remote(comm->dst_actor_.get()));
277   return dst_proc;
278 }
279
280 std::size_t Api::get_remote_heap_bytes() const
281 {
282   RemoteProcess& process    = mc_model_checker->get_remote_process();
283   auto heap_bytes_used      = mmalloc_get_bytes_used_remote(process.get_heap()->heaplimit, process.get_malloc_info());
284   return heap_bytes_used;
285 }
286
287 void Api::mc_inc_visited_states() const
288 {
289   mc_model_checker->visited_states++;
290 }
291
292 void Api::mc_inc_executed_trans() const
293 {
294   mc_model_checker->executed_transitions++;
295 }
296
297 unsigned long Api::mc_get_visited_states() const
298 {
299   return mc_model_checker->visited_states;
300 }
301
302 unsigned long Api::mc_get_executed_trans() const
303 {
304   return mc_model_checker->executed_transitions;
305 }
306
307 void Api::mc_check_deadlock() const
308 {
309   if (mc_model_checker->checkDeadlock()) {
310     XBT_CINFO(mc_global, "**************************");
311     XBT_CINFO(mc_global, "*** DEADLOCK DETECTED ***");
312     XBT_CINFO(mc_global, "**************************");
313     XBT_CINFO(mc_global, "Counter-example execution trace:");
314     for (auto const& s : mc_model_checker->getChecker()->get_textual_trace())
315       XBT_CINFO(mc_global, "  %s", s.c_str());
316     simgrid::mc::dumpRecordPath();
317     simgrid::mc::session_singleton->log_state();
318     throw DeadlockError();
319   }
320 }
321
322 /** Get the issuer of a simcall (`req->issuer`)
323  *
324  *  In split-process mode, it does the black magic necessary to get an address
325  *  of a (shallow) copy of the data structure the issuer SIMIX actor in the local
326  *  address space.
327  *
328  *  @param process the MCed process
329  *  @param req     the simcall (copied in the local process)
330  */
331 smx_actor_t Api::simcall_get_issuer(s_smx_simcall const* req) const
332 {
333   xbt_assert(mc_model_checker != nullptr);
334
335   // This is the address of the smx_actor in the MCed process:
336   auto address = simgrid::mc::remote(req->issuer_);
337
338   // Lookup by address:
339   for (auto& actor : mc_model_checker->get_remote_process().actors())
340     if (actor.address == address)
341       return actor.copy.get_buffer();
342   for (auto& actor : mc_model_checker->get_remote_process().dead_actors())
343     if (actor.address == address)
344       return actor.copy.get_buffer();
345
346   xbt_die("Issuer not found");
347 }
348
349 RemotePtr<kernel::activity::MailboxImpl> Api::get_mbox_remote_addr(smx_simcall_t const req) const
350 {
351   if (req->call_ == Simcall::COMM_ISEND)
352     return remote(simcall_comm_isend__get__mbox(req));
353   if (req->call_ == Simcall::COMM_IRECV)
354     return remote(simcall_comm_irecv__get__mbox(req));
355   THROW_IMPOSSIBLE;
356 }
357
358 RemotePtr<kernel::activity::ActivityImpl> Api::get_comm_remote_addr(smx_simcall_t const req) const
359 {
360   if (req->call_ == Simcall::COMM_ISEND)
361     return remote(simcall_comm_isend__getraw__result(req));
362   if (req->call_ == Simcall::COMM_IRECV)
363     return remote(simcall_comm_irecv__getraw__result(req));
364   THROW_IMPOSSIBLE;
365 }
366
367 void Api::handle_simcall(Transition const& transition) const
368 {
369   mc_model_checker->handle_simcall(transition);
370 }
371
372 void Api::mc_wait_for_requests() const
373 {
374   mc_model_checker->wait_for_requests();
375 }
376
377 void Api::mc_exit(int status) const
378 {
379   mc_model_checker->exit(status);
380 }
381
382 void Api::dump_record_path() const
383 {
384   simgrid::mc::dumpRecordPath();
385 }
386
387 /* Search for an enabled transition amongst actors
388  *
389  * This is the frist actor marked TODO by the checker, and currently enabled in the application.
390  *
391  * Once we found it, prepare its execution (increase the times_considered of its observer and remove it as done on need)
392  *  - marked "to be interleaved" in their ActorState (controlled by the checker algorithm).
393  */
394
395 smx_simcall_t Api::mc_state_choose_request(simgrid::mc::State* state) const
396 {
397   RemoteProcess& process = mc_model_checker->get_remote_process();
398   XBT_DEBUG("Search for an actor to run. %zu actors to consider", process.actors().size());
399   for (auto& actor_info : process.actors()) {
400     auto actor                           = actor_info.copy.get_buffer();
401     simgrid::mc::ActorState* actor_state = &state->actor_states_[actor->get_pid()];
402
403     /* Only consider actors (1) marked as interleaving by the checker and (2) currently enabled in the application*/
404     if (not actor_state->is_todo() || not simgrid::mc::actor_is_enabled(actor))
405       continue;
406
407     /* This actor is ready to be executed. Prepare its execution when simcall_handle will be called on it */
408     if (actor->simcall_.observer_ != nullptr) {
409       state->transition_.times_considered_ = actor_state->get_times_considered_and_inc();
410       if (actor->simcall_.mc_max_consider_ <= actor_state->get_times_considered())
411         actor_state->set_done();
412     } else {
413       state->transition_.times_considered_ = 0;
414       actor_state->set_done();
415     }
416
417     state->transition_.aid_ = actor->get_pid();
418     state->executed_req_    = actor->simcall_;
419
420     XBT_DEBUG("Let's run actor %ld, going for transition %s", actor->get_pid(),
421               SIMIX_simcall_name(state->executed_req_));
422     return &state->executed_req_;
423   }
424   return nullptr;
425 }
426
427 std::string Api::request_to_string(smx_simcall_t req, int value) const
428 {
429   xbt_assert(mc_model_checker != nullptr, "Must be called from MCer");
430
431   smx_actor_t issuer = simcall_get_issuer(req);
432
433   if (issuer->simcall_.observer_ != nullptr)
434     return mc_model_checker->simcall_to_string(issuer->get_pid(), value);
435   else
436     return "[" + get_actor_string(issuer) + "] " + SIMIX_simcall_name(*req) + "(unknown?)";
437 }
438
439 std::string Api::request_get_dot_output(smx_simcall_t req, int value) const
440 {
441   if (req->observer_ != nullptr) {
442     const smx_actor_t issuer = simcall_get_issuer(req);
443     const char* color        = get_color(issuer->get_pid() - 1);
444     return "label = \"" + mc_model_checker->simcall_dot_label(issuer->get_pid(), value) + "\", color = " + color +
445            ", fontcolor = " + color;
446   } else
447     return "UNIMPLEMENTED";
448 }
449
450 #if HAVE_SMPI
451 int Api::get_smpi_request_tag(smx_simcall_t const& simcall, simgrid::simix::Simcall type) const
452 {
453   void* simcall_data = nullptr;
454   if (type == Simcall::COMM_ISEND)
455     simcall_data = simcall_comm_isend__get__data(simcall);
456   else if (type == Simcall::COMM_IRECV)
457     simcall_data = simcall_comm_irecv__get__data(simcall);
458   Remote<simgrid::smpi::Request> mpi_request;
459   mc_model_checker->get_remote_process().read(mpi_request, remote(static_cast<smpi::Request*>(simcall_data)));
460   return mpi_request.get_buffer()->tag();
461 }
462 #endif
463
464 void Api::restore_state(std::shared_ptr<simgrid::mc::Snapshot> system_state) const
465 {
466   system_state->restore(&mc_model_checker->get_remote_process());
467 }
468
469 void Api::log_state() const
470 {
471   session_singleton->log_state();
472 }
473
474 bool Api::snapshot_equal(const Snapshot* s1, const Snapshot* s2) const
475 {
476   return simgrid::mc::snapshot_equal(s1, s2);
477 }
478
479 simgrid::mc::Snapshot* Api::take_snapshot(int num_state) const
480 {
481   auto snapshot = new simgrid::mc::Snapshot(num_state);
482   return snapshot;
483 }
484
485 void Api::s_close() const
486 {
487   session_singleton->close();
488 }
489
490 RemotePtr<simgrid::kernel::actor::SimcallObserver> Api::execute(Transition& transition, smx_simcall_t simcall) const
491 {
492   /* FIXME: once all simcalls have observers, kill the simcall parameter and use mc_model_checker->simcall_to_string() */
493   transition.textual = request_to_string(simcall, transition.times_considered_);
494   return session_singleton->execute(transition);
495 }
496
497 void Api::automaton_load(const char* file) const
498 {
499   MC_automaton_load(file);
500 }
501
502 std::vector<int> Api::automaton_propositional_symbol_evaluate() const
503 {
504   unsigned int cursor = 0;
505   std::vector<int> values;
506   xbt_automaton_propositional_symbol_t ps = nullptr;
507   xbt_dynar_foreach (mc::property_automaton->propositional_symbols, cursor, ps)
508     values.push_back(xbt_automaton_propositional_symbol_evaluate(ps));
509   return values;
510 }
511
512 std::vector<xbt_automaton_state_t> Api::get_automaton_state() const
513 {
514   std::vector<xbt_automaton_state_t> automaton_stack;
515   unsigned int cursor = 0;
516   xbt_automaton_state_t automaton_state;
517   xbt_dynar_foreach (mc::property_automaton->states, cursor, automaton_state)
518     if (automaton_state->type == -1)
519       automaton_stack.push_back(automaton_state);
520   return automaton_stack;
521 }
522
523 int Api::compare_automaton_exp_label(const xbt_automaton_exp_label* l) const
524 {
525   unsigned int cursor                    = 0;
526   xbt_automaton_propositional_symbol_t p = nullptr;
527   xbt_dynar_foreach (simgrid::mc::property_automaton->propositional_symbols, cursor, p) {
528     if (std::strcmp(xbt_automaton_propositional_symbol_get_name(p), l->u.predicat) == 0)
529       return cursor;
530   }
531   return -1;
532 }
533
534 void Api::set_property_automaton(xbt_automaton_state_t const& automaton_state) const
535 {
536   mc::property_automaton->current_state = automaton_state;
537 }
538
539 xbt_automaton_exp_label_t Api::get_automaton_transition_label(xbt_dynar_t const& dynar, int index) const
540 {
541   const xbt_automaton_transition* transition = xbt_dynar_get_as(dynar, index, xbt_automaton_transition_t);
542   return transition->label;
543 }
544
545 xbt_automaton_state_t Api::get_automaton_transition_dst(xbt_dynar_t const& dynar, int index) const
546 {
547   const xbt_automaton_transition* transition = xbt_dynar_get_as(dynar, index, xbt_automaton_transition_t);
548   return transition->dst;
549 }
550
551 } // namespace mc
552 } // namespace simgrid