Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Enum class for MC call types, and MC comm pattern differences.
[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 using simgrid::simix::Simcall;
17
18 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_request, mc, "Logging specific to MC (request)");
19
20 static char *pointer_to_string(void *pointer);
21 static char *buff_size_to_string(size_t size);
22
23 static inline simgrid::kernel::activity::CommImpl* MC_get_comm(smx_simcall_t r)
24 {
25   switch (r->call_) {
26     case Simcall::COMM_WAIT:
27       return simcall_comm_wait__getraw__comm(r);
28     case Simcall::COMM_TEST:
29       return simcall_comm_test__getraw__comm(r);
30     default:
31       return nullptr;
32   }
33 }
34
35 static inline
36 smx_mailbox_t MC_get_mbox(smx_simcall_t r)
37 {
38   switch (r->call_) {
39     case Simcall::COMM_ISEND:
40       return simcall_comm_isend__get__mbox(r);
41     case Simcall::COMM_IRECV:
42       return simcall_comm_irecv__get__mbox(r);
43     default:
44       return nullptr;
45   }
46 }
47
48 namespace simgrid {
49 namespace mc {
50
51 // Does half the job
52 static inline
53 bool request_depend_asymmetric(smx_simcall_t r1, smx_simcall_t r2)
54 {
55   if (r1->call_ == Simcall::COMM_ISEND && r2->call_ == Simcall::COMM_IRECV)
56     return false;
57
58   if (r1->call_ == Simcall::COMM_IRECV && r2->call_ == Simcall::COMM_ISEND)
59     return false;
60
61   // Those are internal requests, we do not need indirection because those objects are copies:
62   const kernel::activity::CommImpl* synchro1 = MC_get_comm(r1);
63   const kernel::activity::CommImpl* synchro2 = MC_get_comm(r2);
64
65   if ((r1->call_ == Simcall::COMM_ISEND || r1->call_ == Simcall::COMM_IRECV) && r2->call_ == Simcall::COMM_WAIT) {
66     const kernel::activity::MailboxImpl* mbox = MC_get_mbox(r1);
67
68     if (mbox != synchro2->mbox_cpy
69         && simcall_comm_wait__get__timeout(r2) <= 0)
70       return false;
71
72     if ((r1->issuer_ != synchro2->src_actor_.get()) && (r1->issuer_ != synchro2->dst_actor_.get()) &&
73         simcall_comm_wait__get__timeout(r2) <= 0)
74       return false;
75
76     if ((r1->call_ == Simcall::COMM_ISEND) && (synchro2->type_ == kernel::activity::CommImpl::Type::SEND) &&
77         (synchro2->src_buff_ != simcall_comm_isend__get__src_buff(r1)) && simcall_comm_wait__get__timeout(r2) <= 0)
78       return false;
79
80     if ((r1->call_ == Simcall::COMM_IRECV) && (synchro2->type_ == kernel::activity::CommImpl::Type::RECEIVE) &&
81         (synchro2->dst_buff_ != simcall_comm_irecv__get__dst_buff(r1)) && simcall_comm_wait__get__timeout(r2) <= 0)
82       return false;
83   }
84
85   /* FIXME: the following rule assumes that the result of the isend/irecv call is not stored in a buffer used in the
86    * test call. */
87 #if 0
88   if((r1->call == Simcall::COMM_ISEND || r1->call == Simcall::COMM_IRECV)
89       &&  r2->call == Simcall::COMM_TEST)
90     return false;
91 #endif
92
93   if (r1->call_ == Simcall::COMM_WAIT && (r2->call_ == Simcall::COMM_WAIT || r2->call_ == Simcall::COMM_TEST) &&
94       (synchro1->src_actor_.get() == nullptr || synchro1->dst_actor_.get() == nullptr))
95     return false;
96
97   if (r1->call_ == Simcall::COMM_TEST &&
98       (simcall_comm_test__get__comm(r1) == nullptr || synchro1->src_buff_ == nullptr || synchro1->dst_buff_ == nullptr))
99     return false;
100
101   if (r1->call_ == Simcall::COMM_TEST && r2->call_ == Simcall::COMM_WAIT &&
102       synchro1->src_buff_ == synchro2->src_buff_ && synchro1->dst_buff_ == synchro2->dst_buff_)
103     return false;
104
105   if (r1->call_ == Simcall::COMM_WAIT && r2->call_ == Simcall::COMM_TEST && synchro1->src_buff_ != nullptr &&
106       synchro1->dst_buff_ != nullptr && synchro2->src_buff_ != nullptr && synchro2->dst_buff_ != nullptr &&
107       synchro1->dst_buff_ != synchro2->src_buff_ && synchro1->dst_buff_ != synchro2->dst_buff_ &&
108       synchro2->dst_buff_ != synchro1->src_buff_)
109     return false;
110
111   return true;
112 }
113
114 // Those are internal_req
115 bool request_depend(smx_simcall_t req1, smx_simcall_t req2)
116 {
117   if (req1->issuer_ == req2->issuer_)
118     return false;
119
120   /* Wait with timeout transitions are not considered by the independence theorem, thus we consider them as dependent with all other transitions */
121   if ((req1->call_ == Simcall::COMM_WAIT && simcall_comm_wait__get__timeout(req1) > 0) ||
122       (req2->call_ == Simcall::COMM_WAIT && simcall_comm_wait__get__timeout(req2) > 0))
123     return true;
124
125   if (req1->call_ != req2->call_)
126     return request_depend_asymmetric(req1, req2) && request_depend_asymmetric(req2, req1);
127
128   // Those are internal requests, we do not need indirection because those objects are copies:
129   const kernel::activity::CommImpl* synchro1 = MC_get_comm(req1);
130   const kernel::activity::CommImpl* synchro2 = MC_get_comm(req2);
131
132   switch (req1->call_) {
133     case Simcall::COMM_ISEND:
134       return simcall_comm_isend__get__mbox(req1) == simcall_comm_isend__get__mbox(req2);
135     case Simcall::COMM_IRECV:
136       return simcall_comm_irecv__get__mbox(req1) == simcall_comm_irecv__get__mbox(req2);
137     case Simcall::COMM_WAIT:
138       if (synchro1->src_buff_ == synchro2->src_buff_ && synchro1->dst_buff_ == synchro2->dst_buff_)
139         return false;
140       if (synchro1->src_buff_ != nullptr && synchro1->dst_buff_ != nullptr && synchro2->src_buff_ != nullptr &&
141           synchro2->dst_buff_ != nullptr && synchro1->dst_buff_ != synchro2->src_buff_ &&
142           synchro1->dst_buff_ != synchro2->dst_buff_ && synchro2->dst_buff_ != synchro1->src_buff_)
143         return false;
144       return true;
145     default:
146       return true;
147   }
148 }
149
150 } // namespace mc
151 } // namespace simgrid
152
153 static char *pointer_to_string(void *pointer)
154 {
155   if (XBT_LOG_ISENABLED(mc_request, xbt_log_priority_verbose))
156     return bprintf("%p", pointer);
157
158   return xbt_strdup("(verbose only)");
159 }
160
161 static char *buff_size_to_string(size_t buff_size)
162 {
163   if (XBT_LOG_ISENABLED(mc_request, xbt_log_priority_verbose))
164     return bprintf("%zu", buff_size);
165
166   return xbt_strdup("(verbose only)");
167 }
168
169
170 std::string simgrid::mc::request_to_string(smx_simcall_t req, int value, simgrid::mc::RequestType request_type)
171 {
172   xbt_assert(mc_model_checker != nullptr, "Must be called from MCer");
173
174   if (req->inspector_ != nullptr)
175     return req->inspector_->to_string();
176
177   bool use_remote_comm = true;
178   switch(request_type) {
179   case simgrid::mc::RequestType::simix:
180     use_remote_comm = true;
181     break;
182   case simgrid::mc::RequestType::executed:
183   case simgrid::mc::RequestType::internal:
184     use_remote_comm = false;
185     break;
186   default:
187     THROW_IMPOSSIBLE;
188   }
189
190   const char* type = nullptr;
191   char *args = nullptr;
192
193   smx_actor_t issuer = MC_smx_simcall_get_issuer(req);
194
195   switch (req->call_) {
196     case Simcall::COMM_ISEND: {
197       type     = "iSend";
198       char* p  = pointer_to_string(simcall_comm_isend__get__src_buff(req));
199       char* bs = buff_size_to_string(simcall_comm_isend__get__src_buff_size(req));
200       if (issuer->get_host())
201         args = bprintf("src=(%ld)%s (%s), buff=%s, size=%s", issuer->get_pid(), MC_smx_actor_get_host_name(issuer),
202                        MC_smx_actor_get_name(issuer), p, bs);
203       else
204         args = bprintf("src=(%ld)%s, buff=%s, size=%s", issuer->get_pid(), MC_smx_actor_get_name(issuer), p, bs);
205       xbt_free(bs);
206       xbt_free(p);
207       break;
208     }
209
210     case Simcall::COMM_IRECV: {
211       size_t* remote_size = simcall_comm_irecv__get__dst_buff_size(req);
212       size_t size         = 0;
213       if (remote_size)
214         mc_model_checker->get_remote_simulation().read_bytes(&size, sizeof(size), remote(remote_size));
215
216       type     = "iRecv";
217       char* p  = pointer_to_string(simcall_comm_irecv__get__dst_buff(req));
218       char* bs = buff_size_to_string(size);
219       if (issuer->get_host())
220         args = bprintf("dst=(%ld)%s (%s), buff=%s, size=%s", issuer->get_pid(), MC_smx_actor_get_host_name(issuer),
221                        MC_smx_actor_get_name(issuer), p, bs);
222       else
223         args = bprintf("dst=(%ld)%s, buff=%s, size=%s", issuer->get_pid(), MC_smx_actor_get_name(issuer), p, bs);
224       xbt_free(bs);
225       xbt_free(p);
226       break;
227     }
228
229     case Simcall::COMM_WAIT: {
230       simgrid::kernel::activity::CommImpl* remote_act = simcall_comm_wait__getraw__comm(req);
231       char* p;
232       if (value == -1) {
233         type = "WaitTimeout";
234         p    = pointer_to_string(remote_act);
235         args = bprintf("comm=%s", p);
236       } else {
237         type = "Wait";
238         p    = pointer_to_string(remote_act);
239
240         simgrid::mc::Remote<simgrid::kernel::activity::CommImpl> temp_synchro;
241         const simgrid::kernel::activity::CommImpl* act;
242         if (use_remote_comm) {
243           mc_model_checker->get_remote_simulation().read(temp_synchro, remote(remote_act));
244           act = temp_synchro.get_buffer();
245         } else
246           act = remote_act;
247
248         smx_actor_t src_proc =
249             mc_model_checker->get_remote_simulation().resolve_actor(simgrid::mc::remote(act->src_actor_.get()));
250         smx_actor_t dst_proc =
251             mc_model_checker->get_remote_simulation().resolve_actor(simgrid::mc::remote(act->dst_actor_.get()));
252         args                 = bprintf("comm=%s [(%ld)%s (%s)-> (%ld)%s (%s)]", p, src_proc ? src_proc->get_pid() : 0,
253                        src_proc ? MC_smx_actor_get_host_name(src_proc) : "",
254                        src_proc ? MC_smx_actor_get_name(src_proc) : "", dst_proc ? dst_proc->get_pid() : 0,
255                        dst_proc ? MC_smx_actor_get_host_name(dst_proc) : "",
256                        dst_proc ? MC_smx_actor_get_name(dst_proc) : "");
257       }
258       xbt_free(p);
259       break;
260     }
261
262     case Simcall::COMM_TEST: {
263       simgrid::kernel::activity::CommImpl* remote_act = simcall_comm_test__getraw__comm(req);
264       simgrid::mc::Remote<simgrid::kernel::activity::CommImpl> temp_synchro;
265       const simgrid::kernel::activity::CommImpl* act;
266       if (use_remote_comm) {
267         mc_model_checker->get_remote_simulation().read(temp_synchro, remote(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 =
282             mc_model_checker->get_remote_simulation().resolve_actor(simgrid::mc::remote(act->src_actor_.get()));
283         smx_actor_t dst_proc =
284             mc_model_checker->get_remote_simulation().resolve_actor(simgrid::mc::remote(act->dst_actor_.get()));
285         args                 = bprintf("comm=%s [(%ld)%s (%s) -> (%ld)%s (%s)]", p, src_proc->get_pid(),
286                        MC_smx_actor_get_name(src_proc), MC_smx_actor_get_host_name(src_proc), dst_proc->get_pid(),
287                        MC_smx_actor_get_name(dst_proc), MC_smx_actor_get_host_name(dst_proc));
288       }
289       xbt_free(p);
290       break;
291     }
292
293     case Simcall::COMM_WAITANY: {
294       type         = "WaitAny";
295       size_t count = simcall_comm_waitany__get__count(req);
296       if (count > 0) {
297         simgrid::kernel::activity::CommImpl* remote_sync;
298         remote_sync =
299             mc_model_checker->get_remote_simulation().read(remote(simcall_comm_waitany__get__comms(req) + value));
300         char* p     = pointer_to_string(remote_sync);
301         args        = bprintf("comm=%s (%d of %zu)", p, value + 1, count);
302         xbt_free(p);
303       } else
304         args = bprintf("comm at idx %d", value);
305       break;
306     }
307
308     case Simcall::COMM_TESTANY:
309       if (value == -1) {
310         type = "TestAny FALSE";
311         args = xbt_strdup("-");
312       } else {
313         type = "TestAny";
314         args = bprintf("(%d of %zu)", value + 1, simcall_comm_testany__get__count(req));
315       }
316       break;
317
318     case Simcall::MUTEX_TRYLOCK:
319     case Simcall::MUTEX_LOCK: {
320       if (req->call_ == Simcall::MUTEX_LOCK)
321         type = "Mutex LOCK";
322       else
323         type = "Mutex TRYLOCK";
324
325       simgrid::mc::Remote<simgrid::kernel::activity::MutexImpl> mutex;
326       mc_model_checker->get_remote_simulation().read_bytes(mutex.get_buffer(), sizeof(mutex),
327                                                            remote(req->call_ == Simcall::MUTEX_LOCK
328                                                                       ? simcall_mutex_lock__get__mutex(req)
329                                                                       : simcall_mutex_trylock__get__mutex(req)));
330       args = bprintf("locked = %d, owner = %d, sleeping = n/a", mutex.get_buffer()->is_locked(),
331                      mutex.get_buffer()->get_owner() != nullptr
332                          ? (int)mc_model_checker->get_remote_simulation()
333                                .resolve_actor(simgrid::mc::remote(mutex.get_buffer()->get_owner()))
334                                ->get_pid()
335                          : -1);
336       break;
337     }
338
339     case Simcall::MC_RANDOM:
340       type = "MC_RANDOM";
341       args = bprintf("%d", value);
342       break;
343
344     default:
345       type = SIMIX_simcall_name(req->call_);
346       args = bprintf("??");
347       break;
348   }
349
350   std::string str;
351   if (args != nullptr)
352     str = simgrid::xbt::string_printf("[(%ld)%s (%s)] %s(%s)", issuer->get_pid(), MC_smx_actor_get_host_name(issuer),
353                                       MC_smx_actor_get_name(issuer), type, args);
354   else
355     str = simgrid::xbt::string_printf("[(%ld)%s (%s)] %s ", issuer->get_pid(), MC_smx_actor_get_host_name(issuer),
356                                       MC_smx_actor_get_name(issuer), type);
357   xbt_free(args);
358   return str;
359 }
360
361 namespace simgrid {
362 namespace mc {
363
364 bool request_is_enabled_by_idx(smx_simcall_t req, unsigned int idx)
365 {
366   kernel::activity::CommImpl* remote_act = nullptr;
367   switch (req->call_) {
368     case Simcall::COMM_WAIT:
369       /* FIXME: check also that src and dst processes are not suspended */
370       remote_act = simcall_comm_wait__getraw__comm(req);
371       break;
372
373     case Simcall::COMM_WAITANY:
374       remote_act = mc_model_checker->get_remote_simulation().read(remote(simcall_comm_testany__get__comms(req) + idx));
375       break;
376
377     case Simcall::COMM_TESTANY:
378       remote_act = mc_model_checker->get_remote_simulation().read(remote(simcall_comm_testany__get__comms(req) + idx));
379       break;
380
381     default:
382       return true;
383   }
384
385   Remote<kernel::activity::CommImpl> temp_comm;
386   mc_model_checker->get_remote_simulation().read(temp_comm, remote(remote_act));
387   const kernel::activity::CommImpl* comm = temp_comm.get_buffer();
388   return comm->src_actor_.get() && comm->dst_actor_.get();
389 }
390
391 static inline const char* get_color(int id)
392 {
393   static constexpr std::array<const char*, 13> colors{{"blue", "red", "green3", "goldenrod", "brown", "purple",
394                                                        "magenta", "turquoise4", "gray25", "forestgreen", "hotpink",
395                                                        "lightblue", "tan"}};
396   return colors[id % colors.size()];
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