Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[sonar] don't mix public/private data members
[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 static_cast<simgrid::kernel::activity::CommImpl*>(simcall_comm_wait__getraw__comm(r));
26     case SIMCALL_COMM_TEST:
27       return static_cast<simgrid::kernel::activity::CommImpl*>(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->process().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 =
229           static_cast<simgrid::kernel::activity::CommImpl*>(simcall_comm_wait__getraw__comm(req));
230       char* p;
231       if (value == -1) {
232         type = "WaitTimeout";
233         p    = pointer_to_string(remote_act);
234         args = bprintf("comm=%s", p);
235       } else {
236         type = "Wait";
237         p    = pointer_to_string(remote_act);
238
239         simgrid::mc::Remote<simgrid::kernel::activity::CommImpl> temp_synchro;
240         const simgrid::kernel::activity::CommImpl* act;
241         if (use_remote_comm) {
242           mc_model_checker->process().read(temp_synchro,
243                                            remote(static_cast<simgrid::kernel::activity::CommImpl*>(remote_act)));
244           act = temp_synchro.get_buffer();
245         } else
246           act = remote_act;
247
248         smx_actor_t src_proc = mc_model_checker->process().resolve_actor(simgrid::mc::remote(act->src_actor_.get()));
249         smx_actor_t dst_proc = mc_model_checker->process().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 =
262           static_cast<simgrid::kernel::activity::CommImpl*>(simcall_comm_test__getraw__comm(req));
263       simgrid::mc::Remote<simgrid::kernel::activity::CommImpl> temp_synchro;
264       const simgrid::kernel::activity::CommImpl* act;
265       if (use_remote_comm) {
266         mc_model_checker->process().read(temp_synchro,
267                                          remote(static_cast<simgrid::kernel::activity::CommImpl*>(remote_act)));
268         act = temp_synchro.get_buffer();
269       } else
270         act = remote_act;
271
272       char* p;
273       if (act->src_actor_.get() == nullptr || act->dst_actor_.get() == nullptr) {
274         type = "Test FALSE";
275         p    = pointer_to_string(remote_act);
276         args = bprintf("comm=%s", p);
277       } else {
278         type = "Test TRUE";
279         p    = pointer_to_string(remote_act);
280
281         smx_actor_t src_proc = mc_model_checker->process().resolve_actor(simgrid::mc::remote(act->src_actor_.get()));
282         smx_actor_t dst_proc = mc_model_checker->process().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 = mc_model_checker->process().read(remote(simcall_comm_waitany__get__comms(req) + value));
297         char* p     = pointer_to_string(remote_sync);
298         args        = bprintf("comm=%s (%d of %zu)", p, value + 1, count);
299         xbt_free(p);
300       } else
301         args = bprintf("comm at idx %d", value);
302       break;
303     }
304
305     case SIMCALL_COMM_TESTANY:
306       if (value == -1) {
307         type = "TestAny FALSE";
308         args = xbt_strdup("-");
309       } else {
310         type = "TestAny";
311         args = bprintf("(%d of %zu)", value + 1, simcall_comm_testany__get__count(req));
312       }
313       break;
314
315     case SIMCALL_MUTEX_TRYLOCK:
316     case SIMCALL_MUTEX_LOCK: {
317       if (req->call_ == SIMCALL_MUTEX_LOCK)
318         type = "Mutex LOCK";
319       else
320         type = "Mutex TRYLOCK";
321
322       simgrid::mc::Remote<simgrid::kernel::activity::MutexImpl> mutex;
323       mc_model_checker->process().read_bytes(mutex.get_buffer(), sizeof(mutex),
324                                              remote(req->call_ == SIMCALL_MUTEX_LOCK
325                                                         ? simcall_mutex_lock__get__mutex(req)
326                                                         : simcall_mutex_trylock__get__mutex(req)));
327       args = bprintf("locked = %d, owner = %d, sleeping = n/a", mutex.get_buffer()->is_locked(),
328                      mutex.get_buffer()->get_owner() != nullptr
329                          ? (int)mc_model_checker->process()
330                                .resolve_actor(simgrid::mc::remote(mutex.get_buffer()->get_owner()))
331                                ->get_pid()
332                          : -1);
333       break;
334     }
335
336     case SIMCALL_MC_RANDOM:
337       type = "MC_RANDOM";
338       args = bprintf("%d", value);
339       break;
340
341     default:
342       type = SIMIX_simcall_name(req->call_);
343       args = bprintf("??");
344       break;
345   }
346
347   std::string str;
348   if (args != nullptr)
349     str = simgrid::xbt::string_printf("[(%ld)%s (%s)] %s(%s)", issuer->get_pid(), MC_smx_actor_get_host_name(issuer),
350                                       MC_smx_actor_get_name(issuer), type, args);
351   else
352     str = simgrid::xbt::string_printf("[(%ld)%s (%s)] %s ", issuer->get_pid(), MC_smx_actor_get_host_name(issuer),
353                                       MC_smx_actor_get_name(issuer), type);
354   xbt_free(args);
355   return str;
356 }
357
358 namespace simgrid {
359 namespace mc {
360
361 bool request_is_enabled_by_idx(smx_simcall_t req, unsigned int idx)
362 {
363   kernel::activity::CommImpl* remote_act = nullptr;
364   switch (req->call_) {
365     case SIMCALL_COMM_WAIT:
366       /* FIXME: check also that src and dst processes are not suspended */
367       remote_act = simcall_comm_wait__getraw__comm(req);
368       break;
369
370     case SIMCALL_COMM_WAITANY:
371       remote_act = mc_model_checker->process().read(remote(simcall_comm_testany__get__comms(req) + idx));
372       break;
373
374     case SIMCALL_COMM_TESTANY:
375       remote_act = mc_model_checker->process().read(remote(simcall_comm_testany__get__comms(req) + idx));
376       break;
377
378     default:
379       return true;
380   }
381
382   Remote<kernel::activity::CommImpl> temp_comm;
383   mc_model_checker->process().read(temp_comm, remote(remote_act));
384   const kernel::activity::CommImpl* comm = temp_comm.get_buffer();
385   return comm->src_actor_.get() && comm->dst_actor_.get();
386 }
387
388 static const char* colors[] = {
389     "blue",       "red",    "green3",      "goldenrod", "brown",     "purple", "magenta",
390     "turquoise4", "gray25", "forestgreen", "hotpink",   "lightblue", "tan",
391 };
392
393 static inline const char* get_color(int id)
394 {
395   return colors[id % (sizeof(colors) / sizeof(colors[0])) ];
396 }
397
398 std::string request_get_dot_output(smx_simcall_t req, int value)
399 {
400   const smx_actor_t issuer = MC_smx_simcall_get_issuer(req);
401   const char* color        = get_color(issuer->get_pid() - 1);
402
403   if (req->inspector_ != nullptr)
404     return simgrid::xbt::string_printf("label = \"%s\", color = %s, fontcolor = %s",
405                                        req->inspector_->dot_label().c_str(), color, color);
406
407   std::string label;
408
409   switch (req->call_) {
410     case SIMCALL_COMM_ISEND:
411       if (issuer->get_host())
412         label = xbt::string_printf("[(%ld)%s] iSend", issuer->get_pid(), MC_smx_actor_get_host_name(issuer));
413       else
414         label = bprintf("[(%ld)] iSend", issuer->get_pid());
415       break;
416
417     case SIMCALL_COMM_IRECV:
418       if (issuer->get_host())
419         label = xbt::string_printf("[(%ld)%s] iRecv", issuer->get_pid(), MC_smx_actor_get_host_name(issuer));
420       else
421         label = xbt::string_printf("[(%ld)] iRecv", issuer->get_pid());
422       break;
423
424     case SIMCALL_COMM_WAIT:
425       if (value == -1) {
426         if (issuer->get_host())
427           label = xbt::string_printf("[(%ld)%s] WaitTimeout", issuer->get_pid(), MC_smx_actor_get_host_name(issuer));
428         else
429           label = xbt::string_printf("[(%ld)] WaitTimeout", issuer->get_pid());
430       } else {
431         kernel::activity::ActivityImpl* remote_act = simcall_comm_wait__getraw__comm(req);
432         Remote<kernel::activity::CommImpl> temp_comm;
433         mc_model_checker->process().read(temp_comm, remote(static_cast<kernel::activity::CommImpl*>(remote_act)));
434         const kernel::activity::CommImpl* comm = temp_comm.get_buffer();
435
436         const kernel::actor::ActorImpl* src_proc =
437             mc_model_checker->process().resolve_actor(mc::remote(comm->src_actor_.get()));
438         const kernel::actor::ActorImpl* dst_proc =
439             mc_model_checker->process().resolve_actor(mc::remote(comm->dst_actor_.get()));
440         if (issuer->get_host())
441           label =
442               xbt::string_printf("[(%ld)%s] Wait [(%ld)->(%ld)]", issuer->get_pid(), MC_smx_actor_get_host_name(issuer),
443                                  src_proc ? src_proc->get_pid() : 0, dst_proc ? dst_proc->get_pid() : 0);
444         else
445           label = xbt::string_printf("[(%ld)] Wait [(%ld)->(%ld)]", issuer->get_pid(),
446                                      src_proc ? src_proc->get_pid() : 0, dst_proc ? dst_proc->get_pid() : 0);
447       }
448       break;
449
450     case SIMCALL_COMM_TEST: {
451       kernel::activity::ActivityImpl* remote_act = simcall_comm_test__getraw__comm(req);
452       Remote<simgrid::kernel::activity::CommImpl> temp_comm;
453       mc_model_checker->process().read(temp_comm, remote(static_cast<kernel::activity::CommImpl*>(remote_act)));
454       const kernel::activity::CommImpl* comm = temp_comm.get_buffer();
455       if (comm->src_actor_.get() == nullptr || comm->dst_actor_.get() == nullptr) {
456         if (issuer->get_host())
457           label = xbt::string_printf("[(%ld)%s] Test FALSE", issuer->get_pid(), MC_smx_actor_get_host_name(issuer));
458         else
459           label = bprintf("[(%ld)] Test FALSE", issuer->get_pid());
460       } else {
461         if (issuer->get_host())
462           label = xbt::string_printf("[(%ld)%s] Test TRUE", issuer->get_pid(), MC_smx_actor_get_host_name(issuer));
463         else
464           label = xbt::string_printf("[(%ld)] Test TRUE", issuer->get_pid());
465       }
466       break;
467     }
468
469     case SIMCALL_COMM_WAITANY: {
470       size_t comms_size = simcall_comm_waitany__get__count(req);
471       if (issuer->get_host())
472         label = xbt::string_printf("[(%ld)%s] WaitAny [%d of %zu]", issuer->get_pid(),
473                                    MC_smx_actor_get_host_name(issuer), value + 1, comms_size);
474       else
475         label = xbt::string_printf("[(%ld)] WaitAny [%d of %zu]", issuer->get_pid(), value + 1, comms_size);
476       break;
477     }
478
479     case SIMCALL_COMM_TESTANY:
480       if (value == -1) {
481         if (issuer->get_host())
482           label = xbt::string_printf("[(%ld)%s] TestAny FALSE", issuer->get_pid(), MC_smx_actor_get_host_name(issuer));
483         else
484           label = xbt::string_printf("[(%ld)] TestAny FALSE", issuer->get_pid());
485       } else {
486         if (issuer->get_host())
487           label =
488               xbt::string_printf("[(%ld)%s] TestAny TRUE [%d of %lu]", issuer->get_pid(),
489                                  MC_smx_actor_get_host_name(issuer), value + 1, simcall_comm_testany__get__count(req));
490         else
491           label = xbt::string_printf("[(%ld)] TestAny TRUE [%d of %lu]", issuer->get_pid(), value + 1,
492                                      simcall_comm_testany__get__count(req));
493       }
494       break;
495
496     case SIMCALL_MUTEX_TRYLOCK:
497       label = xbt::string_printf("[(%ld)] Mutex TRYLOCK", issuer->get_pid());
498       break;
499
500     case SIMCALL_MUTEX_LOCK:
501       label = xbt::string_printf("[(%ld)] Mutex LOCK", issuer->get_pid());
502       break;
503
504     case SIMCALL_MC_RANDOM:
505       if (issuer->get_host())
506         label = xbt::string_printf("[(%ld)%s] MC_RANDOM (%d)", issuer->get_pid(), MC_smx_actor_get_host_name(issuer),
507                                    value);
508       else
509         label = xbt::string_printf("[(%ld)] MC_RANDOM (%d)", issuer->get_pid(), value);
510       break;
511
512     default:
513       THROW_UNIMPLEMENTED;
514   }
515
516   return xbt::string_printf("label = \"%s\", color = %s, fontcolor = %s", label.c_str(), color, color);
517 }
518
519 } // namespace mc
520 } // namespace simgrid