Logo AND Algorithmique Numérique Distribuée

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