Logo AND Algorithmique Numérique Distribuée

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