Logo AND Algorithmique Numérique Distribuée

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