Logo AND Algorithmique Numérique Distribuée

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