Logo AND Algorithmique Numérique Distribuée

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