Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
1404c21e6feb0997e1609c71d65e5280bb3ac6dc
[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 <xbt/log.h>
10 #include <xbt/str.h>
11 #include <xbt/sysdep.h>
12 #include <xbt/dynar.h>
13
14 #include "src/mc/mc_request.h"
15 #include "src/mc/mc_safety.h"
16 #include "src/mc/mc_private.h"
17 #include "src/mc/mc_smx.h"
18 #include "src/mc/mc_xbt.hpp"
19
20 using simgrid::mc::remote;
21
22 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_request, mc,
23                                 "Logging specific to MC (request)");
24
25 static char *pointer_to_string(void *pointer);
26 static char *buff_size_to_string(size_t size);
27
28 static inline
29 smx_synchro_t MC_get_comm(smx_simcall_t r)
30 {
31   switch (r->call ) {
32   case SIMCALL_COMM_WAIT:
33     return simcall_comm_wait__get__comm(r);
34   case SIMCALL_COMM_TEST:
35     return simcall_comm_test__get__comm(r);
36   default:
37     return nullptr;
38   }
39 }
40
41 static inline
42 smx_mailbox_t MC_get_rdv(smx_simcall_t r)
43 {
44   switch(r->call) {
45   case SIMCALL_COMM_ISEND:
46     return simcall_comm_isend__get__rdv(r);
47   case SIMCALL_COMM_IRECV:
48     return simcall_comm_irecv__get__rdv(r);
49   default:
50     return nullptr;
51   }
52 }
53
54 namespace simgrid {
55 namespace mc {
56
57 // Does half the job
58 static inline
59 bool request_depend_asymmetric(smx_simcall_t r1, smx_simcall_t r2)
60 {
61   if (r1->call == SIMCALL_COMM_ISEND && r2->call == SIMCALL_COMM_IRECV)
62     return false;
63
64   if (r1->call == SIMCALL_COMM_IRECV && r2->call == SIMCALL_COMM_ISEND)
65     return false;
66
67   // Those are internal requests, we do not need indirection
68   // because those objects are copies:
69   smx_synchro_t synchro1 = MC_get_comm(r1);
70   smx_synchro_t synchro2 = MC_get_comm(r2);
71
72   if ((r1->call == SIMCALL_COMM_ISEND || r1->call == SIMCALL_COMM_IRECV)
73       && r2->call == SIMCALL_COMM_WAIT) {
74
75     smx_mailbox_t rdv = MC_get_rdv(r1);
76
77     if (rdv != synchro2->comm.rdv_cpy
78         && simcall_comm_wait__get__timeout(r2) <= 0)
79       return false;
80
81     if ((r1->issuer != synchro2->comm.src_proc)
82         && (r1->issuer != synchro2->comm.dst_proc)
83         && simcall_comm_wait__get__timeout(r2) <= 0)
84       return false;
85
86     if ((r1->call == SIMCALL_COMM_ISEND)
87         && (synchro2->comm.type == SIMIX_COMM_SEND)
88         && (synchro2->comm.src_buff !=
89             simcall_comm_isend__get__src_buff(r1))
90         && simcall_comm_wait__get__timeout(r2) <= 0)
91       return false;
92
93     if ((r1->call == SIMCALL_COMM_IRECV)
94         && (synchro2->comm.type == SIMIX_COMM_RECEIVE)
95         && (synchro2->comm.dst_buff != simcall_comm_irecv__get__dst_buff(r1))
96         && simcall_comm_wait__get__timeout(r2) <= 0)
97       return false;
98   }
99
100   /* FIXME: the following rule assumes that the result of the
101    * isend/irecv call is not stored in a buffer used in the
102    * test call. */
103   /*if(   (r1->call == SIMCALL_COMM_ISEND || r1->call == SIMCALL_COMM_IRECV)
104      &&  r2->call == SIMCALL_COMM_TEST)
105      return FALSE; */
106
107   if (r1->call == SIMCALL_COMM_WAIT
108       && (r2->call == SIMCALL_COMM_WAIT || r2->call == SIMCALL_COMM_TEST)
109       && (synchro1->comm.src_proc == nullptr || synchro1->comm.dst_proc == NULL))
110     return false;
111
112   if (r1->call == SIMCALL_COMM_TEST &&
113       (simcall_comm_test__get__comm(r1) == nullptr
114        || synchro1->comm.src_buff == nullptr
115        || synchro1->comm.dst_buff == nullptr))
116     return false;
117
118   if (r1->call == SIMCALL_COMM_TEST && r2->call == SIMCALL_COMM_WAIT
119       && synchro1->comm.src_buff == synchro2->comm.src_buff
120       && synchro1->comm.dst_buff == synchro2->comm.dst_buff)
121     return false;
122
123   if (r1->call == SIMCALL_COMM_WAIT && r2->call == SIMCALL_COMM_TEST
124       && synchro1->comm.src_buff != nullptr
125       && synchro1->comm.dst_buff != nullptr
126       && synchro2->comm.src_buff != nullptr
127       && synchro2->comm.dst_buff != nullptr
128       && synchro1->comm.dst_buff != synchro2->comm.src_buff
129       && synchro1->comm.dst_buff != synchro2->comm.dst_buff
130       && synchro2->comm.dst_buff != synchro1->comm.src_buff)
131     return false;
132
133   return true;
134 }
135
136 // Those are MC_state_get_internal_request(state)
137 bool request_depend(smx_simcall_t r1, smx_simcall_t r2)
138 {
139   if (r1->issuer == r2->issuer)
140     return false;
141
142   /* Wait with timeout transitions are not considered by the independance theorem, thus we consider them as dependant with all other transitions */
143   if ((r1->call == SIMCALL_COMM_WAIT && simcall_comm_wait__get__timeout(r1) > 0)
144       || (r2->call == SIMCALL_COMM_WAIT
145           && simcall_comm_wait__get__timeout(r2) > 0))
146     return TRUE;
147
148   if (r1->call != r2->call)
149     return request_depend_asymmetric(r1, r2)
150       && request_depend_asymmetric(r2, r1);
151
152   // Those are internal requests, we do not need indirection
153   // because those objects are copies:
154   smx_synchro_t synchro1 = MC_get_comm(r1);
155   smx_synchro_t synchro2 = MC_get_comm(r2);
156
157   switch(r1->call) {
158   case SIMCALL_COMM_ISEND:
159     return simcall_comm_isend__get__rdv(r1) == simcall_comm_isend__get__rdv(r2);
160   case SIMCALL_COMM_IRECV:
161     return simcall_comm_irecv__get__rdv(r1) == simcall_comm_irecv__get__rdv(r2);
162   case SIMCALL_COMM_WAIT:
163     if (synchro1->comm.src_buff == synchro2->comm.src_buff
164         && synchro1->comm.dst_buff == synchro2->comm.dst_buff)
165       return false;
166     else if (synchro1->comm.src_buff != nullptr
167         && synchro1->comm.dst_buff != nullptr
168         && synchro2->comm.src_buff != nullptr
169         && synchro2->comm.dst_buff != nullptr
170         && synchro1->comm.dst_buff != synchro2->comm.src_buff
171         && synchro1->comm.dst_buff != synchro2->comm.dst_buff
172         && synchro2->comm.dst_buff != synchro1->comm.src_buff)
173       return false;
174     else
175       return true;
176   default:
177     return true;
178   }
179 }
180
181 }
182 }
183
184 static char *pointer_to_string(void *pointer)
185 {
186
187   if (XBT_LOG_ISENABLED(mc_request, xbt_log_priority_verbose))
188     return bprintf("%p", pointer);
189
190   return xbt_strdup("(verbose only)");
191 }
192
193 static char *buff_size_to_string(size_t buff_size)
194 {
195
196   if (XBT_LOG_ISENABLED(mc_request, xbt_log_priority_verbose))
197     return bprintf("%zu", buff_size);
198
199   return xbt_strdup("(verbose only)");
200 }
201
202
203 char *simgrid::mc::request_to_string(smx_simcall_t req, int value, simgrid::mc::RequestType request_type)
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_process_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 =
229           bprintf("src=(%lu)%s (%s), buff=%s, size=%s", issuer->pid,
230                   MC_smx_process_get_host_name(issuer),
231                   MC_smx_process_get_name(issuer),
232                   p, bs);
233     else
234       args =
235           bprintf("src=(%lu)%s, buff=%s, size=%s", issuer->pid,
236                   MC_smx_process_get_name(issuer), p, bs);
237     xbt_free(bs);
238     xbt_free(p);
239     break;
240   }
241
242   case SIMCALL_COMM_IRECV: {
243     size_t* remote_size = simcall_comm_irecv__get__dst_buff_size(req);
244
245     // size_t size = size_pointer ? *size_pointer : 0;
246     size_t size = 0;
247     if (remote_size)
248       mc_model_checker->process().read_bytes(&size, sizeof(size),
249         remote(remote_size));
250
251     type = "iRecv";
252     char* p = pointer_to_string(simcall_comm_irecv__get__dst_buff(req));
253     char* bs = buff_size_to_string(size);
254     if (issuer->host)
255       args =
256           bprintf("dst=(%lu)%s (%s), buff=%s, size=%s", issuer->pid,
257                   MC_smx_process_get_host_name(issuer),
258                   MC_smx_process_get_name(issuer),
259                   p, bs);
260     else
261       args =
262           bprintf("dst=(%lu)%s, buff=%s, size=%s", issuer->pid,
263                   MC_smx_process_get_name(issuer),
264                   p, bs);
265     xbt_free(bs);
266     xbt_free(p);
267     break;
268   }
269
270   case SIMCALL_COMM_WAIT: {
271     smx_synchro_t remote_act = simcall_comm_wait__get__comm(req);
272     char* p;
273     if (value == -1) {
274       type = "WaitTimeout";
275       p = pointer_to_string(remote_act);
276       args = bprintf("comm=%s", p);
277     } else {
278       type = "Wait";
279       p = pointer_to_string(remote_act);
280
281       s_smx_synchro_t synchro;
282       smx_synchro_t act;
283       if (use_remote_comm) {
284         mc_model_checker->process().read_bytes(&synchro,
285           sizeof(synchro), remote(remote_act));
286         act = &synchro;
287       } else
288         act = remote_act;
289
290       smx_process_t src_proc = MC_smx_resolve_process(act->comm.src_proc);
291       smx_process_t dst_proc = MC_smx_resolve_process(act->comm.dst_proc);
292       args = bprintf("comm=%s [(%lu)%s (%s)-> (%lu)%s (%s)]", p,
293                      src_proc ? src_proc->pid : 0,
294                      src_proc ? MC_smx_process_get_host_name(src_proc) : "",
295                      src_proc ? MC_smx_process_get_name(src_proc) : "",
296                      dst_proc ? dst_proc->pid : 0,
297                      dst_proc ? MC_smx_process_get_host_name(dst_proc) : "",
298                      dst_proc ? MC_smx_process_get_name(dst_proc) : "");
299     }
300     xbt_free(p);
301     break;
302   }
303
304   case SIMCALL_COMM_TEST: {
305     smx_synchro_t remote_act = simcall_comm_test__get__comm(req);
306     s_smx_synchro_t synchro;
307       smx_synchro_t act;
308       if (use_remote_comm) {
309         mc_model_checker->process().read_bytes(&synchro,
310           sizeof(synchro), remote(remote_act));
311         act = &synchro;
312       } else
313         act = remote_act;
314
315     char* p;
316     if (act->comm.src_proc == nullptr || act->comm.dst_proc == NULL) {
317       type = "Test FALSE";
318       p = pointer_to_string(remote_act);
319       args = bprintf("comm=%s", p);
320     } else {
321       type = "Test TRUE";
322       p = pointer_to_string(remote_act);
323
324       smx_process_t src_proc = MC_smx_resolve_process(act->comm.src_proc);
325       smx_process_t dst_proc = MC_smx_resolve_process(act->comm.dst_proc);
326       args = bprintf("comm=%s [(%lu)%s (%s) -> (%lu)%s (%s)]", p,
327                      src_proc->pid,
328                      MC_smx_process_get_name(src_proc),
329                      MC_smx_process_get_host_name(src_proc),
330                      dst_proc->pid,
331                      MC_smx_process_get_name(dst_proc),
332                      MC_smx_process_get_host_name(dst_proc));
333     }
334     xbt_free(p);
335     break;
336   }
337
338   case SIMCALL_COMM_WAITANY: {
339     type = "WaitAny";
340     s_xbt_dynar_t comms;
341     mc_model_checker->process().read_bytes(
342       &comms, sizeof(comms), remote(simcall_comm_waitany__get__comms(req)));
343     if (!xbt_dynar_is_empty(&comms)) {
344       smx_synchro_t remote_sync;
345       read_element(mc_model_checker->process(),
346         &remote_sync, remote(simcall_comm_waitany__get__comms(req)), value,
347         sizeof(remote_sync));
348       char* p = pointer_to_string(remote_sync);
349       args = bprintf("comm=%s (%d of %lu)",
350         p, value + 1, xbt_dynar_length(&comms));
351       xbt_free(p);
352     } else
353       args = bprintf("comm at idx %d", value);
354     break;
355   }
356
357   case SIMCALL_COMM_TESTANY:
358     if (value == -1) {
359       type = "TestAny FALSE";
360       args = xbt_strdup("-");
361     } else {
362       type = "TestAny";
363       args =
364           bprintf("(%d of %zu)", value + 1,
365                   read_length(mc_model_checker->process(),
366                     simcall_comm_testany__get__comms(req)));
367     }
368     break;
369
370   case SIMCALL_MUTEX_TRYLOCK:
371   case SIMCALL_MUTEX_LOCK: {
372     if (req->call == SIMCALL_MUTEX_LOCK)
373       type = "Mutex LOCK";
374     else
375       type = "Mutex TRYLOCK";
376
377     s_smx_mutex_t mutex;
378     mc_model_checker->process().read_bytes(&mutex, sizeof(mutex),
379       remote(
380         req->call == SIMCALL_MUTEX_LOCK
381         ? simcall_mutex_lock__get__mutex(req)
382         : simcall_mutex_trylock__get__mutex(req)
383       ));
384     s_xbt_swag_t mutex_sleeping;
385     mc_model_checker->process().read_bytes(&mutex_sleeping, sizeof(mutex_sleeping),
386       remote(mutex.sleeping));
387
388     args = bprintf("locked = %d, owner = %d, sleeping = %d",
389       mutex.locked,
390       mutex.owner != nullptr ? (int) MC_smx_resolve_process(mutex.owner)->pid : -1,
391       mutex_sleeping.count);
392     break;
393   }
394
395   case SIMCALL_MC_RANDOM:
396     type = "MC_RANDOM";
397     args = bprintf("%d", value);
398     break;
399
400   default:
401     THROW_UNIMPLEMENTED;
402   }
403
404   char* str;
405   if (args != nullptr)
406     str =
407         bprintf("[(%lu)%s (%s)] %s(%s)", issuer->pid,
408                 MC_smx_process_get_host_name(issuer),
409                 MC_smx_process_get_name(issuer),
410                 type, args);
411   else
412     str =
413         bprintf("[(%lu)%s (%s)] %s ", issuer->pid,
414                 MC_smx_process_get_host_name(issuer),
415                 MC_smx_process_get_name(issuer),
416                 type);
417   xbt_free(args);
418   return str;
419 }
420
421 namespace simgrid {
422 namespace mc {
423
424 bool request_is_enabled_by_idx(smx_simcall_t req, unsigned int idx)
425 {
426   smx_synchro_t remote_act = nullptr;
427   switch (req->call) {
428
429   case SIMCALL_COMM_WAIT:
430     /* FIXME: check also that src and dst processes are not suspended */
431     remote_act = simcall_comm_wait__get__comm(req);
432     break;
433
434   case SIMCALL_COMM_WAITANY: {
435     read_element(
436       mc_model_checker->process(), &remote_act,
437       remote(simcall_comm_waitany__get__comms(req)),
438       idx, sizeof(remote_act));
439     }
440     break;
441
442   case SIMCALL_COMM_TESTANY: {
443     read_element(
444       mc_model_checker->process(), &remote_act,
445       remote(simcall_comm_testany__get__comms(req)),
446       idx, sizeof(remote_act));
447     }
448     break;
449
450   default:
451     return true;
452   }
453
454   s_smx_synchro_t synchro;
455   mc_model_checker->process().read_bytes(
456     &synchro, sizeof(synchro), remote(remote_act));
457   return synchro.comm.src_proc && synchro.comm.dst_proc;
458 }
459
460 bool process_is_enabled(smx_process_t process)
461 {
462   return simgrid::mc::request_is_enabled(&process->simcall);
463 }
464
465 static const char* colors[] = {
466   "blue",
467   "red",
468   "green3",
469   "goldenrod",
470   "brown",
471   "purple",
472   "magenta",
473   "turquoise4",
474   "gray25",
475   "forestgreen",
476   "hotpink",
477   "lightblue",
478   "tan",
479 };
480
481 static inline const char* get_color(int id)
482 {
483   return colors[id % (sizeof(colors) / sizeof(colors[0])) ];
484 }
485
486 char *request_get_dot_output(smx_simcall_t req, int value)
487 {
488   char *label = nullptr;
489
490   const smx_process_t issuer = MC_smx_simcall_get_issuer(req);
491
492   switch (req->call) {
493   case SIMCALL_COMM_ISEND:
494     if (issuer->host)
495       label =
496           bprintf("[(%lu)%s] iSend", issuer->pid,
497                   MC_smx_process_get_host_name(issuer));
498     else
499       label = bprintf("[(%lu)] iSend", issuer->pid);
500     break;
501
502   case SIMCALL_COMM_IRECV:
503     if (issuer->host)
504       label =
505           bprintf("[(%lu)%s] iRecv", issuer->pid,
506                   MC_smx_process_get_host_name(issuer));
507     else
508       label = bprintf("[(%lu)] iRecv", issuer->pid);
509     break;
510
511   case SIMCALL_COMM_WAIT: {
512     if (value == -1) {
513       if (issuer->host)
514         label =
515             bprintf("[(%lu)%s] WaitTimeout", issuer->pid,
516                     MC_smx_process_get_host_name(issuer));
517       else
518         label = bprintf("[(%lu)] WaitTimeout", issuer->pid);
519     } else {
520       smx_synchro_t remote_act = simcall_comm_wait__get__comm(req);
521       s_smx_synchro_t synchro;
522       mc_model_checker->process().read_bytes(&synchro,
523         sizeof(synchro), remote(remote_act));
524
525       smx_process_t src_proc = MC_smx_resolve_process(synchro.comm.src_proc);
526       smx_process_t dst_proc = MC_smx_resolve_process(synchro.comm.dst_proc);
527       if (issuer->host)
528         label =
529             bprintf("[(%lu)%s] Wait [(%lu)->(%lu)]", issuer->pid,
530                     MC_smx_process_get_host_name(issuer),
531                     src_proc ? src_proc->pid : 0,
532                     dst_proc ? dst_proc->pid : 0);
533       else
534         label =
535             bprintf("[(%lu)] Wait [(%lu)->(%lu)]", issuer->pid,
536                     src_proc ? src_proc->pid : 0,
537                     dst_proc ? dst_proc->pid : 0);
538     }
539     break;
540   }
541
542   case SIMCALL_COMM_TEST: {
543     smx_synchro_t remote_act = simcall_comm_test__get__comm(req);
544     s_smx_synchro_t synchro;
545     mc_model_checker->process().read_bytes(&synchro,
546       sizeof(synchro), remote(remote_act));
547     if (synchro.comm.src_proc == nullptr || synchro.comm.dst_proc == NULL) {
548       if (issuer->host)
549         label =
550             bprintf("[(%lu)%s] Test FALSE", issuer->pid,
551                     MC_smx_process_get_host_name(issuer));
552       else
553         label = bprintf("[(%lu)] Test FALSE", issuer->pid);
554     } else {
555       if (issuer->host)
556         label =
557             bprintf("[(%lu)%s] Test TRUE", issuer->pid,
558                     MC_smx_process_get_host_name(issuer));
559       else
560         label = bprintf("[(%lu)] Test TRUE", issuer->pid);
561     }
562     break;
563   }
564
565   case SIMCALL_COMM_WAITANY: {
566     unsigned long comms_size = read_length(
567       mc_model_checker->process(), remote(simcall_comm_waitany__get__comms(req)));
568     if (issuer->host)
569       label =
570           bprintf("[(%lu)%s] WaitAny [%d of %lu]", issuer->pid,
571                   MC_smx_process_get_host_name(issuer), value + 1,
572                   comms_size);
573     else
574       label =
575           bprintf("[(%lu)] WaitAny [%d of %lu]", issuer->pid, value + 1,
576                   comms_size);
577     break;
578   }
579
580   case SIMCALL_COMM_TESTANY:
581     if (value == -1) {
582       if (issuer->host)
583         label =
584             bprintf("[(%lu)%s] TestAny FALSE", issuer->pid,
585                     MC_smx_process_get_host_name(issuer));
586       else
587         label = bprintf("[(%lu)] TestAny FALSE", issuer->pid);
588     } else {
589       if (issuer->host)
590         label =
591             bprintf("[(%lu)%s] TestAny TRUE [%d of %lu]", issuer->pid,
592                     MC_smx_process_get_host_name(issuer), value + 1,
593                     xbt_dynar_length(simcall_comm_testany__get__comms(req)));
594       else
595         label =
596             bprintf("[(%lu)] TestAny TRUE [%d of %lu]", issuer->pid,
597                     value + 1,
598                     xbt_dynar_length(simcall_comm_testany__get__comms(req)));
599     }
600     break;
601
602   case SIMCALL_MUTEX_TRYLOCK:
603     label = bprintf("[(%lu)] Mutex TRYLOCK", issuer->pid);
604     break;
605
606   case SIMCALL_MUTEX_LOCK:
607     label = bprintf("[(%lu)] Mutex LOCK", issuer->pid);
608     break;
609
610   case SIMCALL_MC_RANDOM:
611     if (issuer->host)
612       label =
613           bprintf("[(%lu)%s] MC_RANDOM (%d)", issuer->pid,
614                   MC_smx_process_get_host_name(issuer), value);
615     else
616       label = bprintf("[(%lu)] MC_RANDOM (%d)", issuer->pid, value);
617     break;
618
619   default:
620     THROW_UNIMPLEMENTED;
621   }
622
623   const char* color = get_color(issuer->pid - 1);
624   char* str =
625       bprintf("label = \"%s\", color = %s, fontcolor = %s", label,
626               color, color);
627   xbt_free(label);
628   return str;
629
630 }
631
632 }
633 }