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. */
11 #include <xbt/sysdep.h>
12 #include <xbt/dynar.h>
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"
20 using simgrid::mc::remote;
22 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_request, mc,
23 "Logging specific to MC (request)");
25 static char *pointer_to_string(void *pointer);
26 static char *buff_size_to_string(size_t size);
29 smx_synchro_t MC_get_comm(smx_simcall_t r)
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);
42 smx_mailbox_t MC_get_rdv(smx_simcall_t r)
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);
59 bool request_depend_asymmetric(smx_simcall_t r1, smx_simcall_t r2)
61 if (r1->call == SIMCALL_COMM_ISEND && r2->call == SIMCALL_COMM_IRECV)
64 if (r1->call == SIMCALL_COMM_IRECV && r2->call == SIMCALL_COMM_ISEND)
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);
72 if ((r1->call == SIMCALL_COMM_ISEND || r1->call == SIMCALL_COMM_IRECV)
73 && r2->call == SIMCALL_COMM_WAIT) {
75 smx_mailbox_t rdv = MC_get_rdv(r1);
77 if (rdv != synchro2->comm.rdv_cpy
78 && simcall_comm_wait__get__timeout(r2) <= 0)
81 if ((r1->issuer != synchro2->comm.src_proc)
82 && (r1->issuer != synchro2->comm.dst_proc)
83 && simcall_comm_wait__get__timeout(r2) <= 0)
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)
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)
100 /* FIXME: the following rule assumes that the result of the
101 * isend/irecv call is not stored in a buffer used in the
103 /*if( (r1->call == SIMCALL_COMM_ISEND || r1->call == SIMCALL_COMM_IRECV)
104 && r2->call == SIMCALL_COMM_TEST)
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))
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))
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)
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)
136 // Those are MC_state_get_internal_request(state)
137 bool request_depend(smx_simcall_t r1, smx_simcall_t r2)
139 if (r1->issuer == r2->issuer)
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))
148 if (r1->call != r2->call)
149 return request_depend_asymmetric(r1, r2)
150 && request_depend_asymmetric(r2, r1);
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);
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)
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)
184 static char *pointer_to_string(void *pointer)
187 if (XBT_LOG_ISENABLED(mc_request, xbt_log_priority_verbose))
188 return bprintf("%p", pointer);
190 return xbt_strdup("(verbose only)");
193 static char *buff_size_to_string(size_t buff_size)
196 if (XBT_LOG_ISENABLED(mc_request, xbt_log_priority_verbose))
197 return bprintf("%zu", buff_size);
199 return xbt_strdup("(verbose only)");
203 char *simgrid::mc::request_to_string(smx_simcall_t req, int value, simgrid::mc::RequestType request_type)
205 bool use_remote_comm = true;
206 switch(request_type) {
207 case simgrid::mc::RequestType::simix:
208 use_remote_comm = true;
210 case simgrid::mc::RequestType::executed:
211 case simgrid::mc::RequestType::internal:
212 use_remote_comm = false;
216 const char* type = nullptr;
217 char *args = nullptr;
219 smx_process_t issuer = MC_smx_simcall_get_issuer(req);
223 case SIMCALL_COMM_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));
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),
235 bprintf("src=(%lu)%s, buff=%s, size=%s", issuer->pid,
236 MC_smx_process_get_name(issuer), p, bs);
242 case SIMCALL_COMM_IRECV: {
243 size_t* remote_size = simcall_comm_irecv__get__dst_buff_size(req);
245 // size_t size = size_pointer ? *size_pointer : 0;
248 mc_model_checker->process().read_bytes(&size, sizeof(size),
249 remote(remote_size));
252 char* p = pointer_to_string(simcall_comm_irecv__get__dst_buff(req));
253 char* bs = buff_size_to_string(size);
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),
262 bprintf("dst=(%lu)%s, buff=%s, size=%s", issuer->pid,
263 MC_smx_process_get_name(issuer),
270 case SIMCALL_COMM_WAIT: {
271 smx_synchro_t remote_act = simcall_comm_wait__get__comm(req);
274 type = "WaitTimeout";
275 p = pointer_to_string(remote_act);
276 args = bprintf("comm=%s", p);
279 p = pointer_to_string(remote_act);
281 s_smx_synchro_t synchro;
283 if (use_remote_comm) {
284 mc_model_checker->process().read_bytes(&synchro,
285 sizeof(synchro), remote(remote_act));
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) : "");
304 case SIMCALL_COMM_TEST: {
305 smx_synchro_t remote_act = simcall_comm_test__get__comm(req);
306 s_smx_synchro_t synchro;
308 if (use_remote_comm) {
309 mc_model_checker->process().read_bytes(&synchro,
310 sizeof(synchro), remote(remote_act));
316 if (act->comm.src_proc == nullptr || act->comm.dst_proc == NULL) {
318 p = pointer_to_string(remote_act);
319 args = bprintf("comm=%s", p);
322 p = pointer_to_string(remote_act);
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,
328 MC_smx_process_get_name(src_proc),
329 MC_smx_process_get_host_name(src_proc),
331 MC_smx_process_get_name(dst_proc),
332 MC_smx_process_get_host_name(dst_proc));
338 case SIMCALL_COMM_WAITANY: {
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));
353 args = bprintf("comm at idx %d", value);
357 case SIMCALL_COMM_TESTANY:
359 type = "TestAny FALSE";
360 args = xbt_strdup("-");
364 bprintf("(%d of %zu)", value + 1,
365 read_length(mc_model_checker->process(),
366 simcall_comm_testany__get__comms(req)));
370 case SIMCALL_MUTEX_TRYLOCK:
371 case SIMCALL_MUTEX_LOCK: {
372 if (req->call == SIMCALL_MUTEX_LOCK)
375 type = "Mutex TRYLOCK";
378 mc_model_checker->process().read_bytes(&mutex, sizeof(mutex),
380 req->call == SIMCALL_MUTEX_LOCK
381 ? simcall_mutex_lock__get__mutex(req)
382 : simcall_mutex_trylock__get__mutex(req)
384 s_xbt_swag_t mutex_sleeping;
385 mc_model_checker->process().read_bytes(&mutex_sleeping, sizeof(mutex_sleeping),
386 remote(mutex.sleeping));
388 args = bprintf("locked = %d, owner = %d, sleeping = %d",
390 mutex.owner != nullptr ? (int) MC_smx_resolve_process(mutex.owner)->pid : -1,
391 mutex_sleeping.count);
395 case SIMCALL_MC_RANDOM:
397 args = bprintf("%d", value);
407 bprintf("[(%lu)%s (%s)] %s(%s)", issuer->pid,
408 MC_smx_process_get_host_name(issuer),
409 MC_smx_process_get_name(issuer),
413 bprintf("[(%lu)%s (%s)] %s ", issuer->pid,
414 MC_smx_process_get_host_name(issuer),
415 MC_smx_process_get_name(issuer),
424 bool request_is_enabled_by_idx(smx_simcall_t req, unsigned int idx)
426 smx_synchro_t remote_act = nullptr;
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);
434 case SIMCALL_COMM_WAITANY: {
436 mc_model_checker->process(), &remote_act,
437 remote(simcall_comm_waitany__get__comms(req)),
438 idx, sizeof(remote_act));
442 case SIMCALL_COMM_TESTANY: {
444 mc_model_checker->process(), &remote_act,
445 remote(simcall_comm_testany__get__comms(req)),
446 idx, sizeof(remote_act));
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;
460 bool process_is_enabled(smx_process_t process)
462 return simgrid::mc::request_is_enabled(&process->simcall);
465 static const char* colors[] = {
481 static inline const char* get_color(int id)
483 return colors[id % (sizeof(colors) / sizeof(colors[0])) ];
486 char *request_get_dot_output(smx_simcall_t req, int value)
488 char *label = nullptr;
490 const smx_process_t issuer = MC_smx_simcall_get_issuer(req);
493 case SIMCALL_COMM_ISEND:
496 bprintf("[(%lu)%s] iSend", issuer->pid,
497 MC_smx_process_get_host_name(issuer));
499 label = bprintf("[(%lu)] iSend", issuer->pid);
502 case SIMCALL_COMM_IRECV:
505 bprintf("[(%lu)%s] iRecv", issuer->pid,
506 MC_smx_process_get_host_name(issuer));
508 label = bprintf("[(%lu)] iRecv", issuer->pid);
511 case SIMCALL_COMM_WAIT: {
515 bprintf("[(%lu)%s] WaitTimeout", issuer->pid,
516 MC_smx_process_get_host_name(issuer));
518 label = bprintf("[(%lu)] WaitTimeout", issuer->pid);
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));
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);
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);
535 bprintf("[(%lu)] Wait [(%lu)->(%lu)]", issuer->pid,
536 src_proc ? src_proc->pid : 0,
537 dst_proc ? dst_proc->pid : 0);
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) {
550 bprintf("[(%lu)%s] Test FALSE", issuer->pid,
551 MC_smx_process_get_host_name(issuer));
553 label = bprintf("[(%lu)] Test FALSE", issuer->pid);
557 bprintf("[(%lu)%s] Test TRUE", issuer->pid,
558 MC_smx_process_get_host_name(issuer));
560 label = bprintf("[(%lu)] Test TRUE", issuer->pid);
565 case SIMCALL_COMM_WAITANY: {
566 unsigned long comms_size = read_length(
567 mc_model_checker->process(), remote(simcall_comm_waitany__get__comms(req)));
570 bprintf("[(%lu)%s] WaitAny [%d of %lu]", issuer->pid,
571 MC_smx_process_get_host_name(issuer), value + 1,
575 bprintf("[(%lu)] WaitAny [%d of %lu]", issuer->pid, value + 1,
580 case SIMCALL_COMM_TESTANY:
584 bprintf("[(%lu)%s] TestAny FALSE", issuer->pid,
585 MC_smx_process_get_host_name(issuer));
587 label = bprintf("[(%lu)] TestAny FALSE", issuer->pid);
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)));
596 bprintf("[(%lu)] TestAny TRUE [%d of %lu]", issuer->pid,
598 xbt_dynar_length(simcall_comm_testany__get__comms(req)));
602 case SIMCALL_MUTEX_TRYLOCK:
603 label = bprintf("[(%lu)] Mutex TRYLOCK", issuer->pid);
606 case SIMCALL_MUTEX_LOCK:
607 label = bprintf("[(%lu)] Mutex LOCK", issuer->pid);
610 case SIMCALL_MC_RANDOM:
613 bprintf("[(%lu)%s] MC_RANDOM (%d)", issuer->pid,
614 MC_smx_process_get_host_name(issuer), value);
616 label = bprintf("[(%lu)] MC_RANDOM (%d)", issuer->pid, value);
623 const char* color = get_color(issuer->pid - 1);
625 bprintf("label = \"%s\", color = %s, fontcolor = %s", label,