Logo AND Algorithmique Numérique Distribuée

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