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>
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"
21 using simgrid::mc::remote;
23 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_request, mc,
24 "Logging specific to MC (request)");
26 static char *pointer_to_string(void *pointer);
27 static char *buff_size_to_string(size_t size);
30 smx_synchro_t MC_get_comm(smx_simcall_t r)
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);
43 smx_mailbox_t MC_get_rdv(smx_simcall_t r)
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);
60 bool request_depend_asymmetric(smx_simcall_t r1, smx_simcall_t r2)
62 if (r1->call == SIMCALL_COMM_ISEND && r2->call == SIMCALL_COMM_IRECV)
65 if (r1->call == SIMCALL_COMM_IRECV && r2->call == SIMCALL_COMM_ISEND)
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);
73 if ((r1->call == SIMCALL_COMM_ISEND || r1->call == SIMCALL_COMM_IRECV)
74 && r2->call == SIMCALL_COMM_WAIT) {
76 smx_mailbox_t rdv = MC_get_rdv(r1);
78 if (rdv != synchro2->comm.rdv_cpy
79 && simcall_comm_wait__get__timeout(r2) <= 0)
82 if ((r1->issuer != synchro2->comm.src_proc)
83 && (r1->issuer != synchro2->comm.dst_proc)
84 && simcall_comm_wait__get__timeout(r2) <= 0)
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)
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)
101 /* FIXME: the following rule assumes that the result of the
102 * isend/irecv call is not stored in a buffer used in the
105 if((r1->call == SIMCALL_COMM_ISEND || r1->call == SIMCALL_COMM_IRECV)
106 && r2->call == SIMCALL_COMM_TEST)
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))
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))
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)
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)
139 // Those are internal_req
140 bool request_depend(smx_simcall_t r1, smx_simcall_t r2)
142 if (r1->issuer == r2->issuer)
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))
151 if (r1->call != r2->call)
152 return request_depend_asymmetric(r1, r2)
153 && request_depend_asymmetric(r2, r1);
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);
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)
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)
187 static char *pointer_to_string(void *pointer)
190 if (XBT_LOG_ISENABLED(mc_request, xbt_log_priority_verbose))
191 return bprintf("%p", pointer);
193 return xbt_strdup("(verbose only)");
196 static char *buff_size_to_string(size_t buff_size)
199 if (XBT_LOG_ISENABLED(mc_request, xbt_log_priority_verbose))
200 return bprintf("%zu", buff_size);
202 return xbt_strdup("(verbose only)");
206 std::string simgrid::mc::request_to_string(smx_simcall_t req, int value, simgrid::mc::RequestType request_type)
208 xbt_assert(mc_mode == MC_MODE_SERVER);
210 bool use_remote_comm = true;
211 switch(request_type) {
212 case simgrid::mc::RequestType::simix:
213 use_remote_comm = true;
215 case simgrid::mc::RequestType::executed:
216 case simgrid::mc::RequestType::internal:
217 use_remote_comm = false;
221 const char* type = nullptr;
222 char *args = nullptr;
224 smx_process_t issuer = MC_smx_simcall_get_issuer(req);
228 case SIMCALL_COMM_ISEND: {
230 char* p = pointer_to_string(simcall_comm_isend__get__src_buff(req));
231 char* bs = buff_size_to_string(simcall_comm_isend__get__src_buff_size(req));
234 bprintf("src=(%lu)%s (%s), buff=%s, size=%s", issuer->pid,
235 MC_smx_process_get_host_name(issuer),
236 MC_smx_process_get_name(issuer),
240 bprintf("src=(%lu)%s, buff=%s, size=%s", issuer->pid,
241 MC_smx_process_get_name(issuer), p, bs);
247 case SIMCALL_COMM_IRECV: {
248 size_t* remote_size = simcall_comm_irecv__get__dst_buff_size(req);
250 // size_t size = size_pointer ? *size_pointer : 0;
253 mc_model_checker->process().read_bytes(&size, sizeof(size),
254 remote(remote_size));
257 char* p = pointer_to_string(simcall_comm_irecv__get__dst_buff(req));
258 char* bs = buff_size_to_string(size);
261 bprintf("dst=(%lu)%s (%s), buff=%s, size=%s", issuer->pid,
262 MC_smx_process_get_host_name(issuer),
263 MC_smx_process_get_name(issuer),
267 bprintf("dst=(%lu)%s, buff=%s, size=%s", issuer->pid,
268 MC_smx_process_get_name(issuer),
275 case SIMCALL_COMM_WAIT: {
276 smx_synchro_t remote_act = simcall_comm_wait__get__comm(req);
279 type = "WaitTimeout";
280 p = pointer_to_string(remote_act);
281 args = bprintf("comm=%s", p);
284 p = pointer_to_string(remote_act);
286 s_smx_synchro_t synchro;
288 if (use_remote_comm) {
289 mc_model_checker->process().read_bytes(&synchro,
290 sizeof(synchro), remote(remote_act));
295 smx_process_t src_proc = MC_smx_resolve_process(simgrid::mc::remote(act->comm.src_proc));
296 smx_process_t dst_proc = MC_smx_resolve_process(simgrid::mc::remote(act->comm.dst_proc));
297 args = bprintf("comm=%s [(%lu)%s (%s)-> (%lu)%s (%s)]", p,
298 src_proc ? src_proc->pid : 0,
299 src_proc ? MC_smx_process_get_host_name(src_proc) : "",
300 src_proc ? MC_smx_process_get_name(src_proc) : "",
301 dst_proc ? dst_proc->pid : 0,
302 dst_proc ? MC_smx_process_get_host_name(dst_proc) : "",
303 dst_proc ? MC_smx_process_get_name(dst_proc) : "");
309 case SIMCALL_COMM_TEST: {
310 smx_synchro_t remote_act = simcall_comm_test__get__comm(req);
311 s_smx_synchro_t synchro;
313 if (use_remote_comm) {
314 mc_model_checker->process().read_bytes(&synchro,
315 sizeof(synchro), remote(remote_act));
321 if (act->comm.src_proc == nullptr || act->comm.dst_proc == nullptr) {
323 p = pointer_to_string(remote_act);
324 args = bprintf("comm=%s", p);
327 p = pointer_to_string(remote_act);
329 smx_process_t src_proc = MC_smx_resolve_process(simgrid::mc::remote(act->comm.src_proc));
330 smx_process_t dst_proc = MC_smx_resolve_process(simgrid::mc::remote(act->comm.dst_proc));
331 args = bprintf("comm=%s [(%lu)%s (%s) -> (%lu)%s (%s)]", p,
333 MC_smx_process_get_name(src_proc),
334 MC_smx_process_get_host_name(src_proc),
336 MC_smx_process_get_name(dst_proc),
337 MC_smx_process_get_host_name(dst_proc));
343 case SIMCALL_COMM_WAITANY: {
346 mc_model_checker->process().read_bytes(
347 &comms, sizeof(comms), remote(simcall_comm_waitany__get__comms(req)));
348 if (!xbt_dynar_is_empty(&comms)) {
349 smx_synchro_t remote_sync;
350 read_element(mc_model_checker->process(),
351 &remote_sync, remote(simcall_comm_waitany__get__comms(req)), value,
352 sizeof(remote_sync));
353 char* p = pointer_to_string(remote_sync);
354 args = bprintf("comm=%s (%d of %lu)",
355 p, value + 1, xbt_dynar_length(&comms));
358 args = bprintf("comm at idx %d", value);
362 case SIMCALL_COMM_TESTANY:
364 type = "TestAny FALSE";
365 args = xbt_strdup("-");
369 bprintf("(%d of %zu)", value + 1,
370 read_length(mc_model_checker->process(),
371 simcall_comm_testany__get__comms(req)));
375 case SIMCALL_MUTEX_TRYLOCK:
376 case SIMCALL_MUTEX_LOCK: {
377 if (req->call == SIMCALL_MUTEX_LOCK)
380 type = "Mutex TRYLOCK";
383 mc_model_checker->process().read_bytes(&mutex, sizeof(mutex),
385 req->call == SIMCALL_MUTEX_LOCK
386 ? simcall_mutex_lock__get__mutex(req)
387 : simcall_mutex_trylock__get__mutex(req)
389 s_xbt_swag_t mutex_sleeping;
390 mc_model_checker->process().read_bytes(&mutex_sleeping, sizeof(mutex_sleeping),
391 remote(mutex.sleeping));
393 args = bprintf("locked = %d, owner = %d, sleeping = %d",
395 mutex.owner != nullptr ? (int) MC_smx_resolve_process(simgrid::mc::remote(mutex.owner))->pid : -1,
396 mutex_sleeping.count);
400 case SIMCALL_MC_RANDOM:
402 args = bprintf("%d", value);
411 str = simgrid::xbt::string_printf("[(%lu)%s (%s)] %s(%s)", issuer->pid,
412 MC_smx_process_get_host_name(issuer),
413 MC_smx_process_get_name(issuer),
416 str = simgrid::xbt::string_printf("[(%lu)%s (%s)] %s ", issuer->pid,
417 MC_smx_process_get_host_name(issuer),
418 MC_smx_process_get_name(issuer),
427 bool request_is_enabled_by_idx(smx_simcall_t req, unsigned int idx)
429 smx_synchro_t remote_act = nullptr;
432 case SIMCALL_COMM_WAIT:
433 /* FIXME: check also that src and dst processes are not suspended */
434 remote_act = simcall_comm_wait__get__comm(req);
437 case SIMCALL_COMM_WAITANY: {
439 mc_model_checker->process(), &remote_act,
440 remote(simcall_comm_waitany__get__comms(req)),
441 idx, sizeof(remote_act));
445 case SIMCALL_COMM_TESTANY: {
447 mc_model_checker->process(), &remote_act,
448 remote(simcall_comm_testany__get__comms(req)),
449 idx, sizeof(remote_act));
457 s_smx_synchro_t synchro;
458 mc_model_checker->process().read_bytes(
459 &synchro, sizeof(synchro), remote(remote_act));
460 return synchro.comm.src_proc && synchro.comm.dst_proc;
463 bool process_is_enabled(smx_process_t process)
465 return simgrid::mc::request_is_enabled(&process->simcall);
468 static const char* colors[] = {
484 static inline const char* get_color(int id)
486 return colors[id % (sizeof(colors) / sizeof(colors[0])) ];
489 std::string request_get_dot_output(smx_simcall_t req, int value)
493 const smx_process_t issuer = MC_smx_simcall_get_issuer(req);
496 case SIMCALL_COMM_ISEND:
498 label = simgrid::xbt::string_printf("[(%lu)%s] iSend", issuer->pid,
499 MC_smx_process_get_host_name(issuer));
501 label = bprintf("[(%lu)] iSend", issuer->pid);
504 case SIMCALL_COMM_IRECV:
506 label = simgrid::xbt::string_printf("[(%lu)%s] iRecv", issuer->pid,
507 MC_smx_process_get_host_name(issuer));
509 label = simgrid::xbt::string_printf("[(%lu)] iRecv", issuer->pid);
512 case SIMCALL_COMM_WAIT: {
515 label = simgrid::xbt::string_printf("[(%lu)%s] WaitTimeout", issuer->pid,
516 MC_smx_process_get_host_name(issuer));
518 label = simgrid::xbt::string_printf("[(%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(simgrid::mc::remote(synchro.comm.src_proc));
526 smx_process_t dst_proc = MC_smx_resolve_process(simgrid::mc::remote(synchro.comm.dst_proc));
528 label = simgrid::xbt::string_printf("[(%lu)%s] Wait [(%lu)->(%lu)]",
530 MC_smx_process_get_host_name(issuer),
531 src_proc ? src_proc->pid : 0,
532 dst_proc ? dst_proc->pid : 0);
534 label = simgrid::xbt::string_printf("[(%lu)] Wait [(%lu)->(%lu)]",
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 == nullptr) {
549 label = simgrid::xbt::string_printf("[(%lu)%s] Test FALSE",
551 MC_smx_process_get_host_name(issuer));
553 label = bprintf("[(%lu)] Test FALSE", issuer->pid);
556 label = simgrid::xbt::string_printf("[(%lu)%s] Test TRUE", issuer->pid,
557 MC_smx_process_get_host_name(issuer));
559 label = simgrid::xbt::string_printf("[(%lu)] Test TRUE", issuer->pid);
564 case SIMCALL_COMM_WAITANY: {
565 unsigned long comms_size = read_length(
566 mc_model_checker->process(), remote(simcall_comm_waitany__get__comms(req)));
568 label = simgrid::xbt::string_printf("[(%lu)%s] WaitAny [%d of %lu]",
570 MC_smx_process_get_host_name(issuer), value + 1,
573 label = simgrid::xbt::string_printf("[(%lu)] WaitAny [%d of %lu]",
574 issuer->pid, value + 1, comms_size);
578 case SIMCALL_COMM_TESTANY:
581 label = simgrid::xbt::string_printf("[(%lu)%s] TestAny FALSE",
582 issuer->pid, MC_smx_process_get_host_name(issuer));
584 label = simgrid::xbt::string_printf("[(%lu)] TestAny FALSE", issuer->pid);
587 label = simgrid::xbt::string_printf("[(%lu)%s] TestAny TRUE [%d of %lu]",
589 MC_smx_process_get_host_name(issuer), value + 1,
590 xbt_dynar_length(simcall_comm_testany__get__comms(req)));
592 label = simgrid::xbt::string_printf("[(%lu)] TestAny TRUE [%d of %lu]",
595 xbt_dynar_length(simcall_comm_testany__get__comms(req)));
599 case SIMCALL_MUTEX_TRYLOCK:
600 label = simgrid::xbt::string_printf("[(%lu)] Mutex TRYLOCK", issuer->pid);
603 case SIMCALL_MUTEX_LOCK:
604 label = simgrid::xbt::string_printf("[(%lu)] Mutex LOCK", issuer->pid);
607 case SIMCALL_MC_RANDOM:
609 label = simgrid::xbt::string_printf("[(%lu)%s] MC_RANDOM (%d)",
610 issuer->pid, MC_smx_process_get_host_name(issuer), value);
612 label = simgrid::xbt::string_printf("[(%lu)] MC_RANDOM (%d)", issuer->pid, value);
619 const char* color = get_color(issuer->pid - 1);
620 return simgrid::xbt::string_printf(
621 "label = \"%s\", color = %s, fontcolor = %s", label.c_str(),