Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[sonar] Convert integer literals to bool literals.
[simgrid.git] / src / mc / mc_request.cpp
1 /* Copyright (c) 2008-2020. 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 "src/mc/mc_request.hpp"
7 #include "src/include/mc/mc.h"
8 #include "src/kernel/activity/CommImpl.hpp"
9 #include "src/kernel/activity/MutexImpl.hpp"
10 #include "src/mc/ModelChecker.hpp"
11 #include "src/mc/checker/SimcallInspector.hpp"
12 #include "src/mc/mc_smx.hpp"
13
14 using simgrid::mc::remote;
15
16 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_request, mc, "Logging specific to MC (request)");
17
18 static char *pointer_to_string(void *pointer);
19 static char *buff_size_to_string(size_t size);
20
21 static inline simgrid::kernel::activity::CommImpl* MC_get_comm(smx_simcall_t r)
22 {
23   switch (r->call_) {
24     case SIMCALL_COMM_WAIT:
25       return simcall_comm_wait__getraw__comm(r);
26     case SIMCALL_COMM_TEST:
27       return simcall_comm_test__getraw__comm(r);
28     default:
29       return nullptr;
30   }
31 }
32
33 static inline
34 smx_mailbox_t MC_get_mbox(smx_simcall_t r)
35 {
36   switch (r->call_) {
37     case SIMCALL_COMM_ISEND:
38       return simcall_comm_isend__get__mbox(r);
39     case SIMCALL_COMM_IRECV:
40       return simcall_comm_irecv__get__mbox(r);
41     default:
42       return nullptr;
43   }
44 }
45
46 namespace simgrid {
47 namespace mc {
48
49 // Does half the job
50 static inline
51 bool request_depend_asymmetric(smx_simcall_t r1, smx_simcall_t r2)
52 {
53   if (r1->call_ == SIMCALL_COMM_ISEND && r2->call_ == SIMCALL_COMM_IRECV)
54     return false;
55
56   if (r1->call_ == SIMCALL_COMM_IRECV && r2->call_ == SIMCALL_COMM_ISEND)
57     return false;
58
59   // Those are internal requests, we do not need indirection because those objects are copies:
60   const kernel::activity::CommImpl* synchro1 = MC_get_comm(r1);
61   const kernel::activity::CommImpl* synchro2 = MC_get_comm(r2);
62
63   if ((r1->call_ == SIMCALL_COMM_ISEND || r1->call_ == SIMCALL_COMM_IRECV) && r2->call_ == SIMCALL_COMM_WAIT) {
64     const kernel::activity::MailboxImpl* mbox = MC_get_mbox(r1);
65
66     if (mbox != synchro2->mbox_cpy
67         && simcall_comm_wait__get__timeout(r2) <= 0)
68       return false;
69
70     if ((r1->issuer_ != synchro2->src_actor_.get()) && (r1->issuer_ != synchro2->dst_actor_.get()) &&
71         simcall_comm_wait__get__timeout(r2) <= 0)
72       return false;
73
74     if ((r1->call_ == SIMCALL_COMM_ISEND) && (synchro2->type_ == kernel::activity::CommImpl::Type::SEND) &&
75         (synchro2->src_buff_ != simcall_comm_isend__get__src_buff(r1)) && simcall_comm_wait__get__timeout(r2) <= 0)
76       return false;
77
78     if ((r1->call_ == SIMCALL_COMM_IRECV) && (synchro2->type_ == kernel::activity::CommImpl::Type::RECEIVE) &&
79         (synchro2->dst_buff_ != simcall_comm_irecv__get__dst_buff(r1)) && simcall_comm_wait__get__timeout(r2) <= 0)
80       return false;
81   }
82
83   /* FIXME: the following rule assumes that the result of the isend/irecv call is not stored in a buffer used in the
84    * test call. */
85 #if 0
86   if((r1->call == SIMCALL_COMM_ISEND || r1->call == SIMCALL_COMM_IRECV)
87       &&  r2->call == SIMCALL_COMM_TEST)
88     return false;
89 #endif
90
91   if (r1->call_ == SIMCALL_COMM_WAIT && (r2->call_ == SIMCALL_COMM_WAIT || r2->call_ == SIMCALL_COMM_TEST) &&
92       (synchro1->src_actor_.get() == nullptr || synchro1->dst_actor_.get() == nullptr))
93     return false;
94
95   if (r1->call_ == SIMCALL_COMM_TEST &&
96       (simcall_comm_test__get__comm(r1) == nullptr || synchro1->src_buff_ == nullptr || synchro1->dst_buff_ == nullptr))
97     return false;
98
99   if (r1->call_ == SIMCALL_COMM_TEST && r2->call_ == SIMCALL_COMM_WAIT && synchro1->src_buff_ == synchro2->src_buff_ &&
100       synchro1->dst_buff_ == synchro2->dst_buff_)
101     return false;
102
103   if (r1->call_ == SIMCALL_COMM_WAIT && r2->call_ == SIMCALL_COMM_TEST && synchro1->src_buff_ != nullptr &&
104       synchro1->dst_buff_ != nullptr && synchro2->src_buff_ != nullptr && synchro2->dst_buff_ != nullptr &&
105       synchro1->dst_buff_ != synchro2->src_buff_ && synchro1->dst_buff_ != synchro2->dst_buff_ &&
106       synchro2->dst_buff_ != synchro1->src_buff_)
107     return false;
108
109   return true;
110 }
111
112 // Those are internal_req
113 bool request_depend(smx_simcall_t req1, smx_simcall_t req2)
114 {
115   if (req1->issuer_ == req2->issuer_)
116     return false;
117
118   /* Wait with timeout transitions are not considered by the independence theorem, thus we consider them as dependent with all other transitions */
119   if ((req1->call_ == SIMCALL_COMM_WAIT && simcall_comm_wait__get__timeout(req1) > 0) ||
120       (req2->call_ == SIMCALL_COMM_WAIT && simcall_comm_wait__get__timeout(req2) > 0))
121     return true;
122
123   if (req1->call_ != req2->call_)
124     return request_depend_asymmetric(req1, req2) && request_depend_asymmetric(req2, req1);
125
126   // Those are internal requests, we do not need indirection because those objects are copies:
127   const kernel::activity::CommImpl* synchro1 = MC_get_comm(req1);
128   const kernel::activity::CommImpl* synchro2 = MC_get_comm(req2);
129
130   switch (req1->call_) {
131     case SIMCALL_COMM_ISEND:
132       return simcall_comm_isend__get__mbox(req1) == simcall_comm_isend__get__mbox(req2);
133     case SIMCALL_COMM_IRECV:
134       return simcall_comm_irecv__get__mbox(req1) == simcall_comm_irecv__get__mbox(req2);
135     case SIMCALL_COMM_WAIT:
136       if (synchro1->src_buff_ == synchro2->src_buff_ && synchro1->dst_buff_ == synchro2->dst_buff_)
137         return false;
138       if (synchro1->src_buff_ != nullptr && synchro1->dst_buff_ != nullptr && synchro2->src_buff_ != nullptr &&
139           synchro2->dst_buff_ != nullptr && synchro1->dst_buff_ != synchro2->src_buff_ &&
140           synchro1->dst_buff_ != synchro2->dst_buff_ && synchro2->dst_buff_ != synchro1->src_buff_)
141         return false;
142       return true;
143     default:
144       return true;
145   }
146 }
147
148 } // namespace mc
149 } // namespace simgrid
150
151 static char *pointer_to_string(void *pointer)
152 {
153   if (XBT_LOG_ISENABLED(mc_request, xbt_log_priority_verbose))
154     return bprintf("%p", pointer);
155
156   return xbt_strdup("(verbose only)");
157 }
158
159 static char *buff_size_to_string(size_t buff_size)
160 {
161   if (XBT_LOG_ISENABLED(mc_request, xbt_log_priority_verbose))
162     return bprintf("%zu", buff_size);
163
164   return xbt_strdup("(verbose only)");
165 }
166
167
168 std::string simgrid::mc::request_to_string(smx_simcall_t req, int value, simgrid::mc::RequestType request_type)
169 {
170   xbt_assert(mc_model_checker != nullptr, "Must be called from MCer");
171
172   if (req->inspector_ != nullptr)
173     return req->inspector_->to_string();
174
175   bool use_remote_comm = true;
176   switch(request_type) {
177   case simgrid::mc::RequestType::simix:
178     use_remote_comm = true;
179     break;
180   case simgrid::mc::RequestType::executed:
181   case simgrid::mc::RequestType::internal:
182     use_remote_comm = false;
183     break;
184   default:
185     THROW_IMPOSSIBLE;
186   }
187
188   const char* type = nullptr;
189   char *args = nullptr;
190
191   smx_actor_t issuer = MC_smx_simcall_get_issuer(req);
192
193   switch (req->call_) {
194     case SIMCALL_COMM_ISEND: {
195       type     = "iSend";
196       char* p  = pointer_to_string(simcall_comm_isend__get__src_buff(req));
197       char* bs = buff_size_to_string(simcall_comm_isend__get__src_buff_size(req));
198       if (issuer->get_host())
199         args = bprintf("src=(%ld)%s (%s), buff=%s, size=%s", issuer->get_pid(), MC_smx_actor_get_host_name(issuer),
200                        MC_smx_actor_get_name(issuer), p, bs);
201       else
202         args = bprintf("src=(%ld)%s, buff=%s, size=%s", issuer->get_pid(), MC_smx_actor_get_name(issuer), p, bs);
203       xbt_free(bs);
204       xbt_free(p);
205       break;
206     }
207
208     case SIMCALL_COMM_IRECV: {
209       size_t* remote_size = simcall_comm_irecv__get__dst_buff_size(req);
210       size_t size         = 0;
211       if (remote_size)
212         mc_model_checker->get_remote_simulation().read_bytes(&size, sizeof(size), remote(remote_size));
213
214       type     = "iRecv";
215       char* p  = pointer_to_string(simcall_comm_irecv__get__dst_buff(req));
216       char* bs = buff_size_to_string(size);
217       if (issuer->get_host())
218         args = bprintf("dst=(%ld)%s (%s), buff=%s, size=%s", issuer->get_pid(), MC_smx_actor_get_host_name(issuer),
219                        MC_smx_actor_get_name(issuer), p, bs);
220       else
221         args = bprintf("dst=(%ld)%s, buff=%s, size=%s", issuer->get_pid(), MC_smx_actor_get_name(issuer), p, bs);
222       xbt_free(bs);
223       xbt_free(p);
224       break;
225     }
226
227     case SIMCALL_COMM_WAIT: {
228       simgrid::kernel::activity::CommImpl* remote_act = simcall_comm_wait__getraw__comm(req);
229       char* p;
230       if (value == -1) {
231         type = "WaitTimeout";
232         p    = pointer_to_string(remote_act);
233         args = bprintf("comm=%s", p);
234       } else {
235         type = "Wait";
236         p    = pointer_to_string(remote_act);
237
238         simgrid::mc::Remote<simgrid::kernel::activity::CommImpl> temp_synchro;
239         const simgrid::kernel::activity::CommImpl* act;
240         if (use_remote_comm) {
241           mc_model_checker->get_remote_simulation().read(temp_synchro, remote(remote_act));
242           act = temp_synchro.get_buffer();
243         } else
244           act = remote_act;
245
246         smx_actor_t src_proc =
247             mc_model_checker->get_remote_simulation().resolve_actor(simgrid::mc::remote(act->src_actor_.get()));
248         smx_actor_t dst_proc =
249             mc_model_checker->get_remote_simulation().resolve_actor(simgrid::mc::remote(act->dst_actor_.get()));
250         args                 = bprintf("comm=%s [(%ld)%s (%s)-> (%ld)%s (%s)]", p, src_proc ? src_proc->get_pid() : 0,
251                        src_proc ? MC_smx_actor_get_host_name(src_proc) : "",
252                        src_proc ? MC_smx_actor_get_name(src_proc) : "", dst_proc ? dst_proc->get_pid() : 0,
253                        dst_proc ? MC_smx_actor_get_host_name(dst_proc) : "",
254                        dst_proc ? MC_smx_actor_get_name(dst_proc) : "");
255       }
256       xbt_free(p);
257       break;
258     }
259
260     case SIMCALL_COMM_TEST: {
261       simgrid::kernel::activity::CommImpl* remote_act = simcall_comm_test__getraw__comm(req);
262       simgrid::mc::Remote<simgrid::kernel::activity::CommImpl> temp_synchro;
263       const simgrid::kernel::activity::CommImpl* act;
264       if (use_remote_comm) {
265         mc_model_checker->get_remote_simulation().read(temp_synchro, remote(remote_act));
266         act = temp_synchro.get_buffer();
267       } else
268         act = remote_act;
269
270       char* p;
271       if (act->src_actor_.get() == nullptr || act->dst_actor_.get() == nullptr) {
272         type = "Test FALSE";
273         p    = pointer_to_string(remote_act);
274         args = bprintf("comm=%s", p);
275       } else {
276         type = "Test TRUE";
277         p    = pointer_to_string(remote_act);
278
279         smx_actor_t src_proc =
280             mc_model_checker->get_remote_simulation().resolve_actor(simgrid::mc::remote(act->src_actor_.get()));
281         smx_actor_t dst_proc =
282             mc_model_checker->get_remote_simulation().resolve_actor(simgrid::mc::remote(act->dst_actor_.get()));
283         args                 = bprintf("comm=%s [(%ld)%s (%s) -> (%ld)%s (%s)]", p, src_proc->get_pid(),
284                        MC_smx_actor_get_name(src_proc), MC_smx_actor_get_host_name(src_proc), dst_proc->get_pid(),
285                        MC_smx_actor_get_name(dst_proc), MC_smx_actor_get_host_name(dst_proc));
286       }
287       xbt_free(p);
288       break;
289     }
290
291     case SIMCALL_COMM_WAITANY: {
292       type         = "WaitAny";
293       size_t count = simcall_comm_waitany__get__count(req);
294       if (count > 0) {
295         simgrid::kernel::activity::CommImpl* remote_sync;
296         remote_sync =
297             mc_model_checker->get_remote_simulation().read(remote(simcall_comm_waitany__get__comms(req) + value));
298         char* p     = pointer_to_string(remote_sync);
299         args        = bprintf("comm=%s (%d of %zu)", p, value + 1, count);
300         xbt_free(p);
301       } else
302         args = bprintf("comm at idx %d", value);
303       break;
304     }
305
306     case SIMCALL_COMM_TESTANY:
307       if (value == -1) {
308         type = "TestAny FALSE";
309         args = xbt_strdup("-");
310       } else {
311         type = "TestAny";
312         args = bprintf("(%d of %zu)", value + 1, simcall_comm_testany__get__count(req));
313       }
314       break;
315
316     case SIMCALL_MUTEX_TRYLOCK:
317     case SIMCALL_MUTEX_LOCK: {
318       if (req->call_ == SIMCALL_MUTEX_LOCK)
319         type = "Mutex LOCK";
320       else
321         type = "Mutex TRYLOCK";
322
323       simgrid::mc::Remote<simgrid::kernel::activity::MutexImpl> mutex;
324       mc_model_checker->get_remote_simulation().read_bytes(mutex.get_buffer(), sizeof(mutex),
325                                                            remote(req->call_ == SIMCALL_MUTEX_LOCK
326                                                                       ? simcall_mutex_lock__get__mutex(req)
327                                                                       : simcall_mutex_trylock__get__mutex(req)));
328       args = bprintf("locked = %d, owner = %d, sleeping = n/a", mutex.get_buffer()->is_locked(),
329                      mutex.get_buffer()->get_owner() != nullptr
330                          ? (int)mc_model_checker->get_remote_simulation()
331                                .resolve_actor(simgrid::mc::remote(mutex.get_buffer()->get_owner()))
332                                ->get_pid()
333                          : -1);
334       break;
335     }
336
337     case SIMCALL_MC_RANDOM:
338       type = "MC_RANDOM";
339       args = bprintf("%d", value);
340       break;
341
342     default:
343       type = SIMIX_simcall_name(req->call_);
344       args = bprintf("??");
345       break;
346   }
347
348   std::string str;
349   if (args != nullptr)
350     str = simgrid::xbt::string_printf("[(%ld)%s (%s)] %s(%s)", issuer->get_pid(), MC_smx_actor_get_host_name(issuer),
351                                       MC_smx_actor_get_name(issuer), type, args);
352   else
353     str = simgrid::xbt::string_printf("[(%ld)%s (%s)] %s ", issuer->get_pid(), MC_smx_actor_get_host_name(issuer),
354                                       MC_smx_actor_get_name(issuer), type);
355   xbt_free(args);
356   return str;
357 }
358
359 namespace simgrid {
360 namespace mc {
361
362 bool request_is_enabled_by_idx(smx_simcall_t req, unsigned int idx)
363 {
364   kernel::activity::CommImpl* remote_act = nullptr;
365   switch (req->call_) {
366     case SIMCALL_COMM_WAIT:
367       /* FIXME: check also that src and dst processes are not suspended */
368       remote_act = simcall_comm_wait__getraw__comm(req);
369       break;
370
371     case SIMCALL_COMM_WAITANY:
372       remote_act = mc_model_checker->get_remote_simulation().read(remote(simcall_comm_testany__get__comms(req) + idx));
373       break;
374
375     case SIMCALL_COMM_TESTANY:
376       remote_act = mc_model_checker->get_remote_simulation().read(remote(simcall_comm_testany__get__comms(req) + idx));
377       break;
378
379     default:
380       return true;
381   }
382
383   Remote<kernel::activity::CommImpl> temp_comm;
384   mc_model_checker->get_remote_simulation().read(temp_comm, remote(remote_act));
385   const kernel::activity::CommImpl* comm = temp_comm.get_buffer();
386   return comm->src_actor_.get() && comm->dst_actor_.get();
387 }
388
389 static const char* colors[] = {
390     "blue",       "red",    "green3",      "goldenrod", "brown",     "purple", "magenta",
391     "turquoise4", "gray25", "forestgreen", "hotpink",   "lightblue", "tan",
392 };
393
394 static inline const char* get_color(int id)
395 {
396   return colors[id % (sizeof(colors) / sizeof(colors[0])) ];
397 }
398
399 std::string request_get_dot_output(smx_simcall_t req, int value)
400 {
401   const smx_actor_t issuer = MC_smx_simcall_get_issuer(req);
402   const char* color        = get_color(issuer->get_pid() - 1);
403
404   if (req->inspector_ != nullptr)
405     return simgrid::xbt::string_printf("label = \"%s\", color = %s, fontcolor = %s",
406                                        req->inspector_->dot_label().c_str(), color, color);
407
408   std::string label;
409
410   switch (req->call_) {
411     case SIMCALL_COMM_ISEND:
412       if (issuer->get_host())
413         label = xbt::string_printf("[(%ld)%s] iSend", issuer->get_pid(), MC_smx_actor_get_host_name(issuer));
414       else
415         label = bprintf("[(%ld)] iSend", issuer->get_pid());
416       break;
417
418     case SIMCALL_COMM_IRECV:
419       if (issuer->get_host())
420         label = xbt::string_printf("[(%ld)%s] iRecv", issuer->get_pid(), MC_smx_actor_get_host_name(issuer));
421       else
422         label = xbt::string_printf("[(%ld)] iRecv", issuer->get_pid());
423       break;
424
425     case SIMCALL_COMM_WAIT:
426       if (value == -1) {
427         if (issuer->get_host())
428           label = xbt::string_printf("[(%ld)%s] WaitTimeout", issuer->get_pid(), MC_smx_actor_get_host_name(issuer));
429         else
430           label = xbt::string_printf("[(%ld)] WaitTimeout", issuer->get_pid());
431       } else {
432         kernel::activity::ActivityImpl* remote_act = simcall_comm_wait__getraw__comm(req);
433         Remote<kernel::activity::CommImpl> temp_comm;
434         mc_model_checker->get_remote_simulation().read(temp_comm,
435                                                        remote(static_cast<kernel::activity::CommImpl*>(remote_act)));
436         const kernel::activity::CommImpl* comm = temp_comm.get_buffer();
437
438         const kernel::actor::ActorImpl* src_proc =
439             mc_model_checker->get_remote_simulation().resolve_actor(mc::remote(comm->src_actor_.get()));
440         const kernel::actor::ActorImpl* dst_proc =
441             mc_model_checker->get_remote_simulation().resolve_actor(mc::remote(comm->dst_actor_.get()));
442         if (issuer->get_host())
443           label =
444               xbt::string_printf("[(%ld)%s] Wait [(%ld)->(%ld)]", issuer->get_pid(), MC_smx_actor_get_host_name(issuer),
445                                  src_proc ? src_proc->get_pid() : 0, dst_proc ? dst_proc->get_pid() : 0);
446         else
447           label = xbt::string_printf("[(%ld)] Wait [(%ld)->(%ld)]", issuer->get_pid(),
448                                      src_proc ? src_proc->get_pid() : 0, dst_proc ? dst_proc->get_pid() : 0);
449       }
450       break;
451
452     case SIMCALL_COMM_TEST: {
453       kernel::activity::ActivityImpl* remote_act = simcall_comm_test__getraw__comm(req);
454       Remote<simgrid::kernel::activity::CommImpl> temp_comm;
455       mc_model_checker->get_remote_simulation().read(temp_comm,
456                                                      remote(static_cast<kernel::activity::CommImpl*>(remote_act)));
457       const kernel::activity::CommImpl* comm = temp_comm.get_buffer();
458       if (comm->src_actor_.get() == nullptr || comm->dst_actor_.get() == nullptr) {
459         if (issuer->get_host())
460           label = xbt::string_printf("[(%ld)%s] Test FALSE", issuer->get_pid(), MC_smx_actor_get_host_name(issuer));
461         else
462           label = bprintf("[(%ld)] Test FALSE", issuer->get_pid());
463       } else {
464         if (issuer->get_host())
465           label = xbt::string_printf("[(%ld)%s] Test TRUE", issuer->get_pid(), MC_smx_actor_get_host_name(issuer));
466         else
467           label = xbt::string_printf("[(%ld)] Test TRUE", issuer->get_pid());
468       }
469       break;
470     }
471
472     case SIMCALL_COMM_WAITANY: {
473       size_t comms_size = simcall_comm_waitany__get__count(req);
474       if (issuer->get_host())
475         label = xbt::string_printf("[(%ld)%s] WaitAny [%d of %zu]", issuer->get_pid(),
476                                    MC_smx_actor_get_host_name(issuer), value + 1, comms_size);
477       else
478         label = xbt::string_printf("[(%ld)] WaitAny [%d of %zu]", issuer->get_pid(), value + 1, comms_size);
479       break;
480     }
481
482     case SIMCALL_COMM_TESTANY:
483       if (value == -1) {
484         if (issuer->get_host())
485           label = xbt::string_printf("[(%ld)%s] TestAny FALSE", issuer->get_pid(), MC_smx_actor_get_host_name(issuer));
486         else
487           label = xbt::string_printf("[(%ld)] TestAny FALSE", issuer->get_pid());
488       } else {
489         if (issuer->get_host())
490           label =
491               xbt::string_printf("[(%ld)%s] TestAny TRUE [%d of %lu]", issuer->get_pid(),
492                                  MC_smx_actor_get_host_name(issuer), value + 1, simcall_comm_testany__get__count(req));
493         else
494           label = xbt::string_printf("[(%ld)] TestAny TRUE [%d of %lu]", issuer->get_pid(), value + 1,
495                                      simcall_comm_testany__get__count(req));
496       }
497       break;
498
499     case SIMCALL_MUTEX_TRYLOCK:
500       label = xbt::string_printf("[(%ld)] Mutex TRYLOCK", issuer->get_pid());
501       break;
502
503     case SIMCALL_MUTEX_LOCK:
504       label = xbt::string_printf("[(%ld)] Mutex LOCK", issuer->get_pid());
505       break;
506
507     case SIMCALL_MC_RANDOM:
508       if (issuer->get_host())
509         label = xbt::string_printf("[(%ld)%s] MC_RANDOM (%d)", issuer->get_pid(), MC_smx_actor_get_host_name(issuer),
510                                    value);
511       else
512         label = xbt::string_printf("[(%ld)] MC_RANDOM (%d)", issuer->get_pid(), value);
513       break;
514
515     default:
516       THROW_UNIMPLEMENTED;
517   }
518
519   return xbt::string_printf("label = \"%s\", color = %s, fontcolor = %s", label.c_str(), color, color);
520 }
521
522 } // namespace mc
523 } // namespace simgrid