1 /* Copyright (c) 2008-2014. The SimGrid Team.
2 * All rights reserved. */
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. */
9 #include "mc_request.h"
10 #include "mc_safety.h"
11 #include "mc_private.h"
14 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_request, mc,
15 "Logging specific to MC (request)");
17 static char *pointer_to_string(void *pointer);
18 static char *buff_size_to_string(size_t size);
20 int MC_request_depend(smx_simcall_t r1, smx_simcall_t r2)
22 if (mc_reduce_kind == e_mc_reduce_none)
25 if (r1->issuer == r2->issuer)
28 /* Wait with timeout transitions are not considered by the independance theorem, thus we consider them as dependant with all other transitions */
29 if ((r1->call == SIMCALL_COMM_WAIT && simcall_comm_wait__get__timeout(r1) > 0)
30 || (r2->call == SIMCALL_COMM_WAIT
31 && simcall_comm_wait__get__timeout(r2) > 0))
34 if (r1->call == SIMCALL_COMM_ISEND && r2->call == SIMCALL_COMM_IRECV)
37 if (r1->call == SIMCALL_COMM_IRECV && r2->call == SIMCALL_COMM_ISEND)
40 // Read object from MCed memory:
41 s_smx_synchro_t synchro1, synchro2;
42 if (r1->call == SIMCALL_COMM_WAIT) {
43 MC_process_read_simple(&mc_model_checker->process, &synchro1,
44 simcall_comm_wait__get__comm(r1), sizeof(synchro1));
46 if (r2->call == SIMCALL_COMM_WAIT) {
47 MC_process_read_simple(&mc_model_checker->process, &synchro2,
48 simcall_comm_wait__get__comm(r2), sizeof(synchro2));
50 if (r1->call == SIMCALL_COMM_TEST) {
51 MC_process_read_simple(&mc_model_checker->process, &synchro1,
52 simcall_comm_test__get__comm(r1), sizeof(synchro1));
54 if (r2->call == SIMCALL_COMM_TEST) {
55 MC_process_read_simple(&mc_model_checker->process, &synchro2,
56 simcall_comm_test__get__comm(r2), sizeof(synchro2));
59 if ((r1->call == SIMCALL_COMM_ISEND || r1->call == SIMCALL_COMM_IRECV)
60 && r2->call == SIMCALL_COMM_WAIT) {
64 SIMCALL_COMM_ISEND ? simcall_comm_isend__get__rdv(r1) :
65 simcall_comm_irecv__get__rdv(r1);
67 if (rdv != synchro2.comm.rdv_cpy
68 && simcall_comm_wait__get__timeout(r2) <= 0)
71 if ((r1->issuer != synchro2.comm.src_proc)
72 && (r1->issuer != synchro2.comm.dst_proc)
73 && simcall_comm_wait__get__timeout(r2) <= 0)
76 if ((r1->call == SIMCALL_COMM_ISEND)
77 && (synchro2.comm.type == SIMIX_COMM_SEND)
78 && (synchro2.comm.src_buff !=
79 simcall_comm_isend__get__src_buff(r1))
80 && simcall_comm_wait__get__timeout(r2) <= 0)
83 if ((r1->call == SIMCALL_COMM_IRECV)
84 && (synchro2.comm.type == SIMIX_COMM_RECEIVE)
85 && (synchro2.comm.dst_buff != simcall_comm_irecv__get__dst_buff(r1))
86 && simcall_comm_wait__get__timeout(r2) <= 0)
90 if ((r2->call == SIMCALL_COMM_ISEND || r2->call == SIMCALL_COMM_IRECV)
91 && r1->call == SIMCALL_COMM_WAIT) {
95 SIMCALL_COMM_ISEND ? simcall_comm_isend__get__rdv(r2) :
96 simcall_comm_irecv__get__rdv(r2);
98 if (rdv != synchro1.comm.rdv_cpy
99 && simcall_comm_wait__get__timeout(r1) <= 0)
102 if ((r2->issuer != synchro1.comm.src_proc)
103 && (r2->issuer != synchro1.comm.dst_proc)
104 && simcall_comm_wait__get__timeout(r1) <= 0)
107 if ((r2->call == SIMCALL_COMM_ISEND)
108 && (synchro1.comm.type == SIMIX_COMM_SEND)
109 && (synchro1.comm.src_buff !=
110 simcall_comm_isend__get__src_buff(r2))
111 && simcall_comm_wait__get__timeout(r1) <= 0)
114 if ((r2->call == SIMCALL_COMM_IRECV)
115 && (synchro1.comm.type == SIMIX_COMM_RECEIVE)
116 && (synchro1.comm.dst_buff !=
117 simcall_comm_irecv__get__dst_buff(r2))
118 && simcall_comm_wait__get__timeout(r1) <= 0)
122 /* FIXME: the following rule assumes that the result of the
123 * isend/irecv call is not stored in a buffer used in the
125 /*if( (r1->call == SIMCALL_COMM_ISEND || r1->call == SIMCALL_COMM_IRECV)
126 && r2->call == SIMCALL_COMM_TEST)
129 /* FIXME: the following rule assumes that the result of the
130 * isend/irecv call is not stored in a buffer used in the
132 /*if( (r2->call == SIMCALL_COMM_ISEND || r2->call == SIMCALL_COMM_IRECV)
133 && r1->call == SIMCALL_COMM_TEST)
136 if (r1->call == SIMCALL_COMM_ISEND && r2->call == SIMCALL_COMM_ISEND
137 && simcall_comm_isend__get__rdv(r1) != simcall_comm_isend__get__rdv(r2))
140 if (r1->call == SIMCALL_COMM_IRECV && r2->call == SIMCALL_COMM_IRECV
141 && simcall_comm_irecv__get__rdv(r1) != simcall_comm_irecv__get__rdv(r2))
144 if (r1->call == SIMCALL_COMM_WAIT
145 && (r2->call == SIMCALL_COMM_WAIT || r2->call == SIMCALL_COMM_TEST)
146 && (synchro1.comm.src_proc == NULL || synchro1.comm.dst_proc == NULL))
149 if (r2->call == SIMCALL_COMM_WAIT
150 && (r1->call == SIMCALL_COMM_WAIT || r1->call == SIMCALL_COMM_TEST)
151 && (synchro2.comm.src_proc == NULL || synchro2.comm.dst_proc == NULL))
154 if (r1->call == SIMCALL_COMM_WAIT && r2->call == SIMCALL_COMM_WAIT
155 && synchro1.comm.src_buff == synchro2.comm.src_buff
156 && synchro2.comm.dst_buff == synchro2.comm.dst_buff)
159 if (r1->call == SIMCALL_COMM_WAIT && r2->call == SIMCALL_COMM_WAIT
160 && synchro1.comm.src_buff != NULL
161 && synchro1.comm.dst_buff != NULL
162 && synchro2.comm.src_buff != NULL
163 && synchro2.comm.dst_buff != NULL
164 && synchro1.comm.dst_buff != synchro2.comm.src_buff
165 && synchro1.comm.dst_buff != synchro2.comm.dst_buff
166 && synchro2.comm.dst_buff != synchro1.comm.src_buff)
169 if (r1->call == SIMCALL_COMM_TEST &&
170 (simcall_comm_test__get__comm(r1) == NULL
171 || synchro1.comm.src_buff == NULL
172 || synchro1.comm.dst_buff == NULL))
175 if (r2->call == SIMCALL_COMM_TEST &&
176 (simcall_comm_test__get__comm(r2) == NULL
177 || synchro2.comm.src_buff == NULL
178 || synchro2.comm.dst_buff == NULL))
181 if (r1->call == SIMCALL_COMM_TEST && r2->call == SIMCALL_COMM_WAIT
182 && synchro1.comm.src_buff == synchro2.comm.src_buff
183 && synchro1.comm.dst_buff == synchro2.comm.dst_buff)
186 if (r1->call == SIMCALL_COMM_WAIT && r2->call == SIMCALL_COMM_TEST
187 && synchro1.comm.src_buff == synchro2.comm.src_buff
188 && synchro1.comm.dst_buff == synchro2.comm.dst_buff)
191 if (r1->call == SIMCALL_COMM_WAIT && r2->call == SIMCALL_COMM_TEST
192 && synchro1.comm.src_buff != NULL
193 && synchro1.comm.dst_buff != NULL
194 && synchro2.comm.src_buff != NULL
195 && synchro2.comm.dst_buff != NULL
196 && synchro1.comm.dst_buff != synchro2.comm.src_buff
197 && synchro1.comm.dst_buff != synchro2.comm.dst_buff
198 && synchro2.comm.dst_buff != synchro1.comm.src_buff)
201 if (r1->call == SIMCALL_COMM_TEST && r2->call == SIMCALL_COMM_WAIT
202 && synchro1.comm.src_buff != NULL
203 && synchro1.comm.dst_buff != NULL
204 && synchro2.comm.src_buff != NULL
205 && synchro2.comm.dst_buff != NULL
206 && synchro1.comm.dst_buff != synchro2.comm.src_buff
207 && synchro1.comm.dst_buff != synchro2.comm.dst_buff
208 && synchro2.comm.dst_buff != synchro1.comm.src_buff)
214 static char *pointer_to_string(void *pointer)
217 if (XBT_LOG_ISENABLED(mc_request, xbt_log_priority_verbose))
218 return bprintf("%p", pointer);
220 return xbt_strdup("(verbose only)");
223 static char *buff_size_to_string(size_t buff_size)
226 if (XBT_LOG_ISENABLED(mc_request, xbt_log_priority_verbose))
227 return bprintf("%zu", buff_size);
229 return xbt_strdup("(verbose only)");
233 char *MC_request_to_string(smx_simcall_t req, int value)
235 const char* type = NULL;
238 smx_process_t issuer = MC_smx_simcall_get_issuer(req);
242 case SIMCALL_COMM_ISEND: {
244 char* p = pointer_to_string(simcall_comm_isend__get__src_buff(req));
245 char* bs = buff_size_to_string(simcall_comm_isend__get__src_buff_size(req));
246 if (issuer->smx_host)
248 bprintf("src=(%lu)%s (%s), buff=%s, size=%s", issuer->pid,
249 MC_smx_process_get_host_name(issuer),
250 MC_smx_process_get_name(issuer),
254 bprintf("src=(%lu)%s, buff=%s, size=%s", issuer->pid,
255 MC_smx_process_get_name(issuer), p, bs);
261 case SIMCALL_COMM_IRECV: {
262 size_t* remote_size = simcall_comm_irecv__get__dst_buff_size(req);
264 // size_t size = size_pointer ? *size_pointer : 0;
267 MC_process_read_simple(&mc_model_checker->process, &size,
268 remote_size, sizeof(size));
271 char* p = pointer_to_string(simcall_comm_irecv__get__dst_buff(req));
272 char* bs = buff_size_to_string(size);
273 if (issuer->smx_host)
275 bprintf("dst=(%lu)%s (%s), buff=%s, size=%s", issuer->pid,
276 MC_smx_process_get_host_name(issuer),
277 MC_smx_process_get_name(issuer),
281 bprintf("dst=(%lu)%s, buff=%s, size=%s", issuer->pid,
282 MC_smx_process_get_name(issuer),
289 case SIMCALL_COMM_WAIT: {
290 smx_synchro_t remote_act = simcall_comm_wait__get__comm(req);
293 type = "WaitTimeout";
294 p = pointer_to_string(remote_act);
295 args = bprintf("comm=%s", p);
298 p = pointer_to_string(remote_act);
300 s_smx_synchro_t synchro;
301 MC_process_read_simple(&mc_model_checker->process, &synchro,
302 remote_act, sizeof(synchro));
304 smx_process_t src_proc = MC_smx_resolve_process(synchro.comm.src_proc);
305 smx_process_t dst_proc = MC_smx_resolve_process(synchro.comm.dst_proc);
306 args = bprintf("comm=%s [(%lu)%s (%s)-> (%lu)%s (%s)]", p,
307 src_proc ? src_proc->pid : 0,
308 src_proc ? MC_smx_process_get_host_name(src_proc) : "",
309 src_proc ? MC_smx_process_get_name(src_proc) : "",
310 dst_proc ? dst_proc->pid : 0,
311 dst_proc ? MC_smx_process_get_host_name(dst_proc) : "",
312 dst_proc ? MC_smx_process_get_name(dst_proc) : "");
318 case SIMCALL_COMM_TEST: {
319 smx_synchro_t remote_act = simcall_comm_test__get__comm(req);
320 s_smx_synchro_t synchro;
321 MC_process_read_simple(&mc_model_checker->process, &synchro,
322 remote_act, sizeof(synchro));
325 if (synchro.comm.src_proc == NULL || synchro.comm.dst_proc == NULL) {
327 p = pointer_to_string(remote_act);
328 args = bprintf("comm=%s", p);
331 p = pointer_to_string(remote_act);
333 smx_process_t src_proc = MC_smx_resolve_process(synchro.comm.src_proc);
334 smx_process_t dst_proc = MC_smx_resolve_process(synchro.comm.dst_proc);
335 args = bprintf("comm=%s [(%lu)%s (%s) -> (%lu)%s (%s)]", p,
337 MC_smx_process_get_name(src_proc),
338 MC_smx_process_get_host_name(src_proc),
340 MC_smx_process_get_name(dst_proc),
341 MC_smx_process_get_host_name(dst_proc));
347 case SIMCALL_COMM_WAITANY: {
350 MC_process_read_simple(&mc_model_checker->process,
351 &comms, simcall_comm_waitany__get__comms(req), sizeof(comms));
352 if (!xbt_dynar_is_empty(&comms)) {
353 smx_synchro_t remote_sync;
354 MC_process_read_dynar_element(&mc_model_checker->process,
355 &remote_sync, simcall_comm_waitany__get__comms(req), value);
356 char* p = pointer_to_string(remote_sync);
357 args = bprintf("comm=%s (%d of %lu)",
358 p, value + 1, xbt_dynar_length(&comms));
361 args = bprintf("comm at idx %d", value);
366 case SIMCALL_COMM_TESTANY:
368 type = "TestAny FALSE";
369 args = xbt_strdup("-");
373 bprintf("(%d of %lu)", value + 1,
374 MC_process_read_dynar_length(&mc_model_checker->process,
375 simcall_comm_testany__get__comms(req)));
379 case SIMCALL_MUTEX_LOCK: {
383 MC_process_read_simple(&mc_model_checker->process, &mutex,
384 simcall_mutex_lock__get__mutex(req), sizeof(mutex));
385 s_xbt_swag_t mutex_sleeping;
386 MC_process_read_simple(&mc_model_checker->process, &mutex_sleeping,
387 mutex.sleeping, sizeof(mutex_sleeping));
389 args = bprintf("locked = %d, owner = %d, sleeping = %d",
391 mutex.owner != NULL ? (int) MC_smx_resolve_process(mutex.owner)->pid : -1,
392 mutex_sleeping.count);
396 case SIMCALL_MC_SNAPSHOT:
397 type = "MC_SNAPSHOT";
401 case SIMCALL_MC_COMPARE_SNAPSHOTS:
402 type = "MC_COMPARE_SNAPSHOTS";
406 case SIMCALL_MC_RANDOM:
408 args = bprintf("%d", value);
418 bprintf("[(%lu)%s (%s)] %s(%s)", issuer->pid,
419 MC_smx_process_get_host_name(issuer),
420 MC_smx_process_get_name(issuer),
424 bprintf("[(%lu)%s (%s)] %s ", issuer->pid,
425 MC_smx_process_get_host_name(issuer),
426 MC_smx_process_get_name(issuer),
433 unsigned int MC_request_testany_fail(smx_simcall_t req)
435 // Remote xbt_dynar_foreach on simcall_comm_testany__get__comms(req).
439 MC_process_read_simple(&mc_model_checker->process,
440 &comms, simcall_comm_testany__get__comms(req), sizeof(comms));
442 // Read ther dynar buffer:
443 size_t buffer_size = comms.elmsize * comms.used;
444 char buffer[buffer_size];
445 MC_process_read_simple(&mc_model_checker->process,
446 buffer, comms.data, buffer_size);
448 // Iterate over the elements:
449 assert(comms.elmsize == sizeof(smx_synchro_t));
451 for (cursor=0; cursor != comms.used; ++cursor) {
454 smx_synchro_t remote_action;
455 memcpy(buffer + comms.elmsize * cursor, &remote_action, sizeof(remote_action));
457 // Dereference the pointer:
458 s_smx_synchro_t action;
459 MC_process_read_simple(&mc_model_checker->process,
460 &action, remote_action, sizeof(action));
462 // Finally so something useful about it:
463 if (action.comm.src_proc && action.comm.dst_proc)
470 int MC_request_is_enabled_by_idx(smx_simcall_t req, unsigned int idx)
472 smx_synchro_t remote_act = NULL;
475 case SIMCALL_COMM_WAIT:
476 /* FIXME: check also that src and dst processes are not suspended */
477 remote_act = simcall_comm_wait__get__comm(req);
480 case SIMCALL_COMM_WAITANY: {
482 MC_process_read_dynar_element(
483 &mc_model_checker->process, &act, simcall_comm_waitany__get__comms(req),
488 case SIMCALL_COMM_TESTANY: {
490 MC_process_read_dynar_element(
491 &mc_model_checker->process, &act, simcall_comm_testany__get__comms(req),
500 s_smx_synchro_t synchro;
501 MC_process_read_simple(&mc_model_checker->process,
502 &synchro, remote_act, sizeof(synchro));
503 return synchro.comm.src_proc && synchro.comm.dst_proc;
506 int MC_process_is_enabled(smx_process_t process)
508 return MC_request_is_enabled(&process->simcall);
511 char *MC_request_get_dot_output(smx_simcall_t req, int value)
515 const smx_process_t issuer = MC_smx_simcall_get_issuer(req);
518 case SIMCALL_COMM_ISEND:
519 if (issuer->smx_host)
521 bprintf("[(%lu)%s] iSend", issuer->pid,
522 MC_smx_process_get_host_name(issuer));
524 label = bprintf("[(%lu)] iSend", issuer->pid);
527 case SIMCALL_COMM_IRECV:
528 if (issuer->smx_host)
530 bprintf("[(%lu)%s] iRecv", issuer->pid,
531 MC_smx_process_get_host_name(issuer));
533 label = bprintf("[(%lu)] iRecv", issuer->pid);
536 case SIMCALL_COMM_WAIT: {
538 if (issuer->smx_host)
540 bprintf("[(%lu)%s] WaitTimeout", issuer->pid,
541 MC_smx_process_get_host_name(issuer));
543 label = bprintf("[(%lu)] WaitTimeout", issuer->pid);
545 smx_synchro_t remote_act = simcall_comm_wait__get__comm(req);
546 s_smx_synchro_t synchro;
547 MC_process_read_simple(&mc_model_checker->process, &synchro,
548 remote_act, sizeof(synchro));
550 smx_process_t src_proc = MC_smx_resolve_process(synchro.comm.src_proc);
551 smx_process_t dst_proc = MC_smx_resolve_process(synchro.comm.dst_proc);
552 if (issuer->smx_host)
554 bprintf("[(%lu)%s] Wait [(%lu)->(%lu)]", issuer->pid,
555 MC_smx_process_get_host_name(issuer),
556 src_proc ? src_proc->pid : 0,
557 dst_proc ? dst_proc->pid : 0);
560 bprintf("[(%lu)] Wait [(%lu)->(%lu)]", issuer->pid,
561 src_proc ? src_proc->pid : 0,
562 dst_proc ? dst_proc->pid : 0);
567 case SIMCALL_COMM_TEST: {
568 smx_synchro_t remote_act = simcall_comm_test__get__comm(req);
569 s_smx_synchro_t synchro;
570 MC_process_read_simple(&mc_model_checker->process, &synchro,
571 remote_act, sizeof(synchro));
572 if (synchro.comm.src_proc == NULL || synchro.comm.dst_proc == NULL) {
573 if (issuer->smx_host)
575 bprintf("[(%lu)%s] Test FALSE", issuer->pid,
576 MC_smx_process_get_host_name(issuer));
578 label = bprintf("[(%lu)] Test FALSE", issuer->pid);
580 if (issuer->smx_host)
582 bprintf("[(%lu)%s] Test TRUE", issuer->pid,
583 MC_smx_process_get_host_name(issuer));
585 label = bprintf("[(%lu)] Test TRUE", issuer->pid);
590 case SIMCALL_COMM_WAITANY: {
591 unsigned long comms_size = MC_process_read_dynar_length(
592 &mc_model_checker->process, simcall_comm_waitany__get__comms(req));
593 if (issuer->smx_host)
595 bprintf("[(%lu)%s] WaitAny [%d of %lu]", issuer->pid,
596 MC_smx_process_get_host_name(issuer), value + 1,
600 bprintf("[(%lu)] WaitAny [%d of %lu]", issuer->pid, value + 1,
605 case SIMCALL_COMM_TESTANY:
607 if (issuer->smx_host)
609 bprintf("[(%lu)%s] TestAny FALSE", issuer->pid,
610 MC_smx_process_get_host_name(issuer));
612 label = bprintf("[(%lu)] TestAny FALSE", issuer->pid);
614 if (issuer->smx_host)
616 bprintf("[(%lu)%s] TestAny TRUE [%d of %lu]", issuer->pid,
617 MC_smx_process_get_host_name(issuer), value + 1,
618 xbt_dynar_length(simcall_comm_testany__get__comms(req)));
621 bprintf("[(%lu)] TestAny TRUE [%d of %lu]", issuer->pid,
623 xbt_dynar_length(simcall_comm_testany__get__comms(req)));
627 case SIMCALL_MUTEX_LOCK:
628 label = bprintf("[(%lu)] Mutex LOCK", req->issuer->pid);
631 case SIMCALL_MC_RANDOM:
632 if (issuer->smx_host)
634 bprintf("[(%lu)%s] MC_RANDOM (%d)", issuer->pid,
635 MC_smx_process_get_host_name(issuer), value);
637 label = bprintf("[(%lu)] MC_RANDOM (%d)", issuer->pid, value);
640 case SIMCALL_MC_SNAPSHOT:
641 if (issuer->smx_host)
643 bprintf("[(%lu)%s] MC_SNAPSHOT", issuer->pid,
644 MC_smx_process_get_host_name(issuer));
646 label = bprintf("[(%lu)] MC_SNAPSHOT", issuer->pid);
649 case SIMCALL_MC_COMPARE_SNAPSHOTS:
650 if (issuer->smx_host)
652 bprintf("[(%lu)%s] MC_COMPARE_SNAPSHOTS", issuer->pid,
653 MC_smx_process_get_host_name(issuer));
655 label = bprintf("[(%lu)] MC_COMPARE_SNAPSHOTS", issuer->pid);
663 bprintf("label = \"%s\", color = %s, fontcolor = %s", label,
664 colors[issuer->pid - 1], colors[issuer->pid - 1]);