1 /* Copyright (c) 2008-2015. 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"
15 using simgrid::mc::remote;
19 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_request, mc,
20 "Logging specific to MC (request)");
22 static char *pointer_to_string(void *pointer);
23 static char *buff_size_to_string(size_t size);
25 // Those are MC_state_get_internal_request(state)
26 int MC_request_depend(smx_simcall_t r1, smx_simcall_t r2)
28 if (mc_reduce_kind == e_mc_reduce_none)
31 if (r1->issuer == r2->issuer)
34 /* Wait with timeout transitions are not considered by the independance theorem, thus we consider them as dependant with all other transitions */
35 if ((r1->call == SIMCALL_COMM_WAIT && simcall_comm_wait__get__timeout(r1) > 0)
36 || (r2->call == SIMCALL_COMM_WAIT
37 && simcall_comm_wait__get__timeout(r2) > 0))
40 if (r1->call == SIMCALL_COMM_ISEND && r2->call == SIMCALL_COMM_IRECV)
43 if (r1->call == SIMCALL_COMM_IRECV && r2->call == SIMCALL_COMM_ISEND)
46 // Those are internal requests, we do not need indirection
47 // because those objects are copies:
48 smx_synchro_t synchro1 = NULL, synchro2 = NULL;
49 if (r1->call == SIMCALL_COMM_WAIT) {
50 synchro1 = simcall_comm_wait__get__comm(r1);
52 if (r2->call == SIMCALL_COMM_WAIT) {
53 synchro2 = simcall_comm_wait__get__comm(r2);
55 if (r1->call == SIMCALL_COMM_TEST) {
56 synchro1 = simcall_comm_test__get__comm(r1);
58 if (r2->call == SIMCALL_COMM_TEST) {
59 synchro2 = simcall_comm_test__get__comm(r2);
62 if ((r1->call == SIMCALL_COMM_ISEND || r1->call == SIMCALL_COMM_IRECV)
63 && r2->call == SIMCALL_COMM_WAIT) {
67 SIMCALL_COMM_ISEND ? simcall_comm_isend__get__rdv(r1) :
68 simcall_comm_irecv__get__rdv(r1);
70 if (rdv != synchro2->comm.rdv_cpy
71 && simcall_comm_wait__get__timeout(r2) <= 0)
74 if ((r1->issuer != synchro2->comm.src_proc)
75 && (r1->issuer != synchro2->comm.dst_proc)
76 && simcall_comm_wait__get__timeout(r2) <= 0)
79 if ((r1->call == SIMCALL_COMM_ISEND)
80 && (synchro2->comm.type == SIMIX_COMM_SEND)
81 && (synchro2->comm.src_buff !=
82 simcall_comm_isend__get__src_buff(r1))
83 && simcall_comm_wait__get__timeout(r2) <= 0)
86 if ((r1->call == SIMCALL_COMM_IRECV)
87 && (synchro2->comm.type == SIMIX_COMM_RECEIVE)
88 && (synchro2->comm.dst_buff != simcall_comm_irecv__get__dst_buff(r1))
89 && simcall_comm_wait__get__timeout(r2) <= 0)
93 if ((r2->call == SIMCALL_COMM_ISEND || r2->call == SIMCALL_COMM_IRECV)
94 && r1->call == SIMCALL_COMM_WAIT) {
98 SIMCALL_COMM_ISEND ? simcall_comm_isend__get__rdv(r2) :
99 simcall_comm_irecv__get__rdv(r2);
101 if (rdv != synchro1->comm.rdv_cpy
102 && simcall_comm_wait__get__timeout(r1) <= 0)
105 if ((r2->issuer != synchro1->comm.src_proc)
106 && (r2->issuer != synchro1->comm.dst_proc)
107 && simcall_comm_wait__get__timeout(r1) <= 0)
110 if ((r2->call == SIMCALL_COMM_ISEND)
111 && (synchro1->comm.type == SIMIX_COMM_SEND)
112 && (synchro1->comm.src_buff !=
113 simcall_comm_isend__get__src_buff(r2))
114 && simcall_comm_wait__get__timeout(r1) <= 0)
117 if ((r2->call == SIMCALL_COMM_IRECV)
118 && (synchro1->comm.type == SIMIX_COMM_RECEIVE)
119 && (synchro1->comm.dst_buff !=
120 simcall_comm_irecv__get__dst_buff(r2))
121 && simcall_comm_wait__get__timeout(r1) <= 0)
125 /* FIXME: the following rule assumes that the result of the
126 * isend/irecv call is not stored in a buffer used in the
128 /*if( (r1->call == SIMCALL_COMM_ISEND || r1->call == SIMCALL_COMM_IRECV)
129 && r2->call == SIMCALL_COMM_TEST)
132 /* FIXME: the following rule assumes that the result of the
133 * isend/irecv call is not stored in a buffer used in the
135 /*if( (r2->call == SIMCALL_COMM_ISEND || r2->call == SIMCALL_COMM_IRECV)
136 && r1->call == SIMCALL_COMM_TEST)
139 if (r1->call == SIMCALL_COMM_ISEND && r2->call == SIMCALL_COMM_ISEND
140 && simcall_comm_isend__get__rdv(r1) != simcall_comm_isend__get__rdv(r2))
143 if (r1->call == SIMCALL_COMM_IRECV && r2->call == SIMCALL_COMM_IRECV
144 && simcall_comm_irecv__get__rdv(r1) != simcall_comm_irecv__get__rdv(r2))
147 if (r1->call == SIMCALL_COMM_WAIT
148 && (r2->call == SIMCALL_COMM_WAIT || r2->call == SIMCALL_COMM_TEST)
149 && (synchro1->comm.src_proc == NULL || synchro1->comm.dst_proc == NULL))
152 if (r2->call == SIMCALL_COMM_WAIT
153 && (r1->call == SIMCALL_COMM_WAIT || r1->call == SIMCALL_COMM_TEST)
154 && (synchro2->comm.src_proc == NULL || synchro2->comm.dst_proc == NULL))
157 if (r1->call == SIMCALL_COMM_WAIT && r2->call == SIMCALL_COMM_WAIT
158 && synchro1->comm.src_buff == synchro2->comm.src_buff
159 && synchro2->comm.dst_buff == synchro2->comm.dst_buff)
162 if (r1->call == SIMCALL_COMM_WAIT && r2->call == SIMCALL_COMM_WAIT
163 && synchro1->comm.src_buff != NULL
164 && synchro1->comm.dst_buff != NULL
165 && synchro2->comm.src_buff != NULL
166 && synchro2->comm.dst_buff != NULL
167 && synchro1->comm.dst_buff != synchro2->comm.src_buff
168 && synchro1->comm.dst_buff != synchro2->comm.dst_buff
169 && synchro2->comm.dst_buff != synchro1->comm.src_buff)
172 if (r1->call == SIMCALL_COMM_TEST &&
173 (simcall_comm_test__get__comm(r1) == NULL
174 || synchro1->comm.src_buff == NULL
175 || synchro1->comm.dst_buff == NULL))
178 if (r2->call == SIMCALL_COMM_TEST &&
179 (simcall_comm_test__get__comm(r2) == NULL
180 || synchro2->comm.src_buff == NULL
181 || synchro2->comm.dst_buff == NULL))
184 if (r1->call == SIMCALL_COMM_TEST && r2->call == SIMCALL_COMM_WAIT
185 && synchro1->comm.src_buff == synchro2->comm.src_buff
186 && synchro1->comm.dst_buff == synchro2->comm.dst_buff)
189 if (r1->call == SIMCALL_COMM_WAIT && r2->call == SIMCALL_COMM_TEST
190 && synchro1->comm.src_buff == synchro2->comm.src_buff
191 && synchro1->comm.dst_buff == synchro2->comm.dst_buff)
194 if (r1->call == SIMCALL_COMM_WAIT && r2->call == SIMCALL_COMM_TEST
195 && synchro1->comm.src_buff != NULL
196 && synchro1->comm.dst_buff != NULL
197 && synchro2->comm.src_buff != NULL
198 && synchro2->comm.dst_buff != NULL
199 && synchro1->comm.dst_buff != synchro2->comm.src_buff
200 && synchro1->comm.dst_buff != synchro2->comm.dst_buff
201 && synchro2->comm.dst_buff != synchro1->comm.src_buff)
204 if (r1->call == SIMCALL_COMM_TEST && r2->call == SIMCALL_COMM_WAIT
205 && synchro1->comm.src_buff != NULL
206 && synchro1->comm.dst_buff != NULL
207 && synchro2->comm.src_buff != NULL
208 && synchro2->comm.dst_buff != NULL
209 && synchro1->comm.dst_buff != synchro2->comm.src_buff
210 && synchro1->comm.dst_buff != synchro2->comm.dst_buff
211 && synchro2->comm.dst_buff != synchro1->comm.src_buff)
217 static char *pointer_to_string(void *pointer)
220 if (XBT_LOG_ISENABLED(mc_request, xbt_log_priority_verbose))
221 return bprintf("%p", pointer);
223 return xbt_strdup("(verbose only)");
226 static char *buff_size_to_string(size_t buff_size)
229 if (XBT_LOG_ISENABLED(mc_request, xbt_log_priority_verbose))
230 return bprintf("%zu", buff_size);
232 return xbt_strdup("(verbose only)");
236 char *MC_request_to_string(smx_simcall_t req, int value, e_mc_request_type_t request_type)
238 bool use_remote_comm = true;
239 switch(request_type) {
240 case MC_REQUEST_SIMIX:
241 use_remote_comm = true;
243 case MC_REQUEST_EXECUTED:
244 case MC_REQUEST_INTERNAL:
245 use_remote_comm = false;
249 const char* type = NULL;
252 smx_process_t issuer = MC_smx_simcall_get_issuer(req);
256 case SIMCALL_COMM_ISEND: {
258 char* p = pointer_to_string(simcall_comm_isend__get__src_buff(req));
259 char* bs = buff_size_to_string(simcall_comm_isend__get__src_buff_size(req));
262 bprintf("src=(%lu)%s (%s), buff=%s, size=%s", issuer->pid,
263 MC_smx_process_get_host_name(issuer),
264 MC_smx_process_get_name(issuer),
268 bprintf("src=(%lu)%s, buff=%s, size=%s", issuer->pid,
269 MC_smx_process_get_name(issuer), p, bs);
275 case SIMCALL_COMM_IRECV: {
276 size_t* remote_size = simcall_comm_irecv__get__dst_buff_size(req);
278 // size_t size = size_pointer ? *size_pointer : 0;
281 mc_model_checker->process().read_bytes(&size, sizeof(size),
282 remote(remote_size));
285 char* p = pointer_to_string(simcall_comm_irecv__get__dst_buff(req));
286 char* bs = buff_size_to_string(size);
289 bprintf("dst=(%lu)%s (%s), buff=%s, size=%s", issuer->pid,
290 MC_smx_process_get_host_name(issuer),
291 MC_smx_process_get_name(issuer),
295 bprintf("dst=(%lu)%s, buff=%s, size=%s", issuer->pid,
296 MC_smx_process_get_name(issuer),
303 case SIMCALL_COMM_WAIT: {
304 smx_synchro_t remote_act = simcall_comm_wait__get__comm(req);
307 type = "WaitTimeout";
308 p = pointer_to_string(remote_act);
309 args = bprintf("comm=%s", p);
312 p = pointer_to_string(remote_act);
314 s_smx_synchro_t synchro;
316 if (use_remote_comm) {
317 mc_model_checker->process().read_bytes(&synchro,
318 sizeof(synchro), remote(remote_act));
323 smx_process_t src_proc = MC_smx_resolve_process(act->comm.src_proc);
324 smx_process_t dst_proc = MC_smx_resolve_process(act->comm.dst_proc);
325 args = bprintf("comm=%s [(%lu)%s (%s)-> (%lu)%s (%s)]", p,
326 src_proc ? src_proc->pid : 0,
327 src_proc ? MC_smx_process_get_host_name(src_proc) : "",
328 src_proc ? MC_smx_process_get_name(src_proc) : "",
329 dst_proc ? dst_proc->pid : 0,
330 dst_proc ? MC_smx_process_get_host_name(dst_proc) : "",
331 dst_proc ? MC_smx_process_get_name(dst_proc) : "");
337 case SIMCALL_COMM_TEST: {
338 smx_synchro_t remote_act = simcall_comm_test__get__comm(req);
339 s_smx_synchro_t synchro;
341 if (use_remote_comm) {
342 mc_model_checker->process().read_bytes(&synchro,
343 sizeof(synchro), remote(remote_act));
349 if (act->comm.src_proc == NULL || act->comm.dst_proc == NULL) {
351 p = pointer_to_string(remote_act);
352 args = bprintf("comm=%s", p);
355 p = pointer_to_string(remote_act);
357 smx_process_t src_proc = MC_smx_resolve_process(act->comm.src_proc);
358 smx_process_t dst_proc = MC_smx_resolve_process(act->comm.dst_proc);
359 args = bprintf("comm=%s [(%lu)%s (%s) -> (%lu)%s (%s)]", p,
361 MC_smx_process_get_name(src_proc),
362 MC_smx_process_get_host_name(src_proc),
364 MC_smx_process_get_name(dst_proc),
365 MC_smx_process_get_host_name(dst_proc));
371 case SIMCALL_COMM_WAITANY: {
374 mc_model_checker->process().read_bytes(
375 &comms, sizeof(comms), remote(simcall_comm_waitany__get__comms(req)));
376 if (!xbt_dynar_is_empty(&comms)) {
377 smx_synchro_t remote_sync;
378 read_element(mc_model_checker->process(),
379 &remote_sync, remote(simcall_comm_waitany__get__comms(req)), value,
380 sizeof(remote_sync));
381 char* p = pointer_to_string(remote_sync);
382 args = bprintf("comm=%s (%d of %lu)",
383 p, value + 1, xbt_dynar_length(&comms));
386 args = bprintf("comm at idx %d", value);
391 case SIMCALL_COMM_TESTANY:
393 type = "TestAny FALSE";
394 args = xbt_strdup("-");
398 bprintf("(%d of %zu)", value + 1,
399 read_length(mc_model_checker->process(),
400 simcall_comm_testany__get__comms(req)));
404 case SIMCALL_MUTEX_TRYLOCK:
405 case SIMCALL_MUTEX_LOCK: {
406 if (req->call == SIMCALL_MUTEX_LOCK)
409 type = "Mutex TRYLOCK";
412 mc_model_checker->process().read_bytes(&mutex, sizeof(mutex),
414 req->call == SIMCALL_MUTEX_LOCK
415 ? simcall_mutex_lock__get__mutex(req)
416 : simcall_mutex_trylock__get__mutex(req)
418 s_xbt_swag_t mutex_sleeping;
419 mc_model_checker->process().read_bytes(&mutex_sleeping, sizeof(mutex_sleeping),
420 remote(mutex.sleeping));
422 args = bprintf("locked = %d, owner = %d, sleeping = %d",
424 mutex.owner != NULL ? (int) MC_smx_resolve_process(mutex.owner)->pid : -1,
425 mutex_sleeping.count);
429 case SIMCALL_MC_SNAPSHOT:
430 type = "MC_SNAPSHOT";
434 case SIMCALL_MC_COMPARE_SNAPSHOTS:
435 type = "MC_COMPARE_SNAPSHOTS";
439 case SIMCALL_MC_RANDOM:
441 args = bprintf("%d", value);
451 bprintf("[(%lu)%s (%s)] %s(%s)", issuer->pid,
452 MC_smx_process_get_host_name(issuer),
453 MC_smx_process_get_name(issuer),
457 bprintf("[(%lu)%s (%s)] %s ", issuer->pid,
458 MC_smx_process_get_host_name(issuer),
459 MC_smx_process_get_name(issuer),
466 unsigned int MC_request_testany_fail(smx_simcall_t req)
468 // Remote xbt_dynar_foreach on simcall_comm_testany__get__comms(req).
472 mc_model_checker->process().read_bytes(
473 &comms, sizeof(comms), remote(simcall_comm_testany__get__comms(req)));
475 // Read ther dynar buffer:
476 size_t buffer_size = comms.elmsize * comms.used;
477 char buffer[buffer_size];
478 mc_model_checker->process().read_bytes(
479 buffer, buffer_size, remote(comms.data));
481 // Iterate over the elements:
482 assert(comms.elmsize == sizeof(smx_synchro_t));
484 for (cursor=0; cursor != comms.used; ++cursor) {
487 smx_synchro_t remote_action = NULL;
488 memcpy(&remote_action, buffer + comms.elmsize * cursor, sizeof(remote_action));
490 // Dereference the pointer:
491 s_smx_synchro_t action;
492 mc_model_checker->process().read_bytes(
493 &action, sizeof(action), remote(remote_action));
495 // Finally so something useful about it:
496 if (action.comm.src_proc && action.comm.dst_proc)
503 int MC_request_is_enabled_by_idx(smx_simcall_t req, unsigned int idx)
505 smx_synchro_t remote_act = NULL;
508 case SIMCALL_COMM_WAIT:
509 /* FIXME: check also that src and dst processes are not suspended */
510 remote_act = simcall_comm_wait__get__comm(req);
513 case SIMCALL_COMM_WAITANY: {
515 mc_model_checker->process(), &remote_act,
516 remote(simcall_comm_waitany__get__comms(req)),
517 idx, sizeof(remote_act));
521 case SIMCALL_COMM_TESTANY: {
523 mc_model_checker->process(), &remote_act,
524 remote(simcall_comm_testany__get__comms(req)),
525 idx, sizeof(remote_act));
533 s_smx_synchro_t synchro;
534 mc_model_checker->process().read_bytes(
535 &synchro, sizeof(synchro), remote(remote_act));
536 return synchro.comm.src_proc && synchro.comm.dst_proc;
539 int MC_process_is_enabled(smx_process_t process)
541 return MC_request_is_enabled(&process->simcall);
544 char *MC_request_get_dot_output(smx_simcall_t req, int value)
548 const smx_process_t issuer = MC_smx_simcall_get_issuer(req);
551 case SIMCALL_COMM_ISEND:
554 bprintf("[(%lu)%s] iSend", issuer->pid,
555 MC_smx_process_get_host_name(issuer));
557 label = bprintf("[(%lu)] iSend", issuer->pid);
560 case SIMCALL_COMM_IRECV:
563 bprintf("[(%lu)%s] iRecv", issuer->pid,
564 MC_smx_process_get_host_name(issuer));
566 label = bprintf("[(%lu)] iRecv", issuer->pid);
569 case SIMCALL_COMM_WAIT: {
573 bprintf("[(%lu)%s] WaitTimeout", issuer->pid,
574 MC_smx_process_get_host_name(issuer));
576 label = bprintf("[(%lu)] WaitTimeout", issuer->pid);
578 smx_synchro_t remote_act = simcall_comm_wait__get__comm(req);
579 s_smx_synchro_t synchro;
580 mc_model_checker->process().read_bytes(&synchro,
581 sizeof(synchro), remote(remote_act));
583 smx_process_t src_proc = MC_smx_resolve_process(synchro.comm.src_proc);
584 smx_process_t dst_proc = MC_smx_resolve_process(synchro.comm.dst_proc);
587 bprintf("[(%lu)%s] Wait [(%lu)->(%lu)]", issuer->pid,
588 MC_smx_process_get_host_name(issuer),
589 src_proc ? src_proc->pid : 0,
590 dst_proc ? dst_proc->pid : 0);
593 bprintf("[(%lu)] Wait [(%lu)->(%lu)]", issuer->pid,
594 src_proc ? src_proc->pid : 0,
595 dst_proc ? dst_proc->pid : 0);
600 case SIMCALL_COMM_TEST: {
601 smx_synchro_t remote_act = simcall_comm_test__get__comm(req);
602 s_smx_synchro_t synchro;
603 mc_model_checker->process().read_bytes(&synchro,
604 sizeof(synchro), remote(remote_act));
605 if (synchro.comm.src_proc == NULL || synchro.comm.dst_proc == NULL) {
608 bprintf("[(%lu)%s] Test FALSE", issuer->pid,
609 MC_smx_process_get_host_name(issuer));
611 label = bprintf("[(%lu)] Test FALSE", issuer->pid);
615 bprintf("[(%lu)%s] Test TRUE", issuer->pid,
616 MC_smx_process_get_host_name(issuer));
618 label = bprintf("[(%lu)] Test TRUE", issuer->pid);
623 case SIMCALL_COMM_WAITANY: {
624 unsigned long comms_size = read_length(
625 mc_model_checker->process(), remote(simcall_comm_waitany__get__comms(req)));
628 bprintf("[(%lu)%s] WaitAny [%d of %lu]", issuer->pid,
629 MC_smx_process_get_host_name(issuer), value + 1,
633 bprintf("[(%lu)] WaitAny [%d of %lu]", issuer->pid, value + 1,
638 case SIMCALL_COMM_TESTANY:
642 bprintf("[(%lu)%s] TestAny FALSE", issuer->pid,
643 MC_smx_process_get_host_name(issuer));
645 label = bprintf("[(%lu)] TestAny FALSE", issuer->pid);
649 bprintf("[(%lu)%s] TestAny TRUE [%d of %lu]", issuer->pid,
650 MC_smx_process_get_host_name(issuer), value + 1,
651 xbt_dynar_length(simcall_comm_testany__get__comms(req)));
654 bprintf("[(%lu)] TestAny TRUE [%d of %lu]", issuer->pid,
656 xbt_dynar_length(simcall_comm_testany__get__comms(req)));
660 case SIMCALL_MUTEX_TRYLOCK:
661 label = bprintf("[(%lu)] Mutex TRYLOCK", issuer->pid);
664 case SIMCALL_MUTEX_LOCK:
665 label = bprintf("[(%lu)] Mutex LOCK", issuer->pid);
668 case SIMCALL_MC_RANDOM:
671 bprintf("[(%lu)%s] MC_RANDOM (%d)", issuer->pid,
672 MC_smx_process_get_host_name(issuer), value);
674 label = bprintf("[(%lu)] MC_RANDOM (%d)", issuer->pid, value);
677 case SIMCALL_MC_SNAPSHOT:
680 bprintf("[(%lu)%s] MC_SNAPSHOT", issuer->pid,
681 MC_smx_process_get_host_name(issuer));
683 label = bprintf("[(%lu)] MC_SNAPSHOT", issuer->pid);
686 case SIMCALL_MC_COMPARE_SNAPSHOTS:
689 bprintf("[(%lu)%s] MC_COMPARE_SNAPSHOTS", issuer->pid,
690 MC_smx_process_get_host_name(issuer));
692 label = bprintf("[(%lu)] MC_COMPARE_SNAPSHOTS", issuer->pid);
700 bprintf("label = \"%s\", color = %s, fontcolor = %s", label,
701 colors[issuer->pid - 1], colors[issuer->pid - 1]);