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;
24 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_request, mc,
25 "Logging specific to MC (request)");
27 static char *pointer_to_string(void *pointer);
28 static char *buff_size_to_string(size_t size);
31 smx_synchro_t MC_get_comm(smx_simcall_t r)
34 case SIMCALL_COMM_WAIT:
35 return simcall_comm_wait__get__comm(r);
36 case SIMCALL_COMM_TEST:
37 return simcall_comm_test__get__comm(r);
44 smx_mailbox_t MC_get_rdv(smx_simcall_t r)
47 case SIMCALL_COMM_ISEND:
48 return simcall_comm_isend__get__rdv(r);
49 case SIMCALL_COMM_IRECV:
50 return simcall_comm_irecv__get__rdv(r);
58 int MC_request_depend_asymmetric(smx_simcall_t r1, smx_simcall_t r2)
60 if (r1->call == SIMCALL_COMM_ISEND && r2->call == SIMCALL_COMM_IRECV)
63 if (r1->call == SIMCALL_COMM_IRECV && r2->call == SIMCALL_COMM_ISEND)
66 // Those are internal requests, we do not need indirection
67 // because those objects are copies:
68 smx_synchro_t synchro1 = MC_get_comm(r1);
69 smx_synchro_t synchro2 = MC_get_comm(r2);
71 if ((r1->call == SIMCALL_COMM_ISEND || r1->call == SIMCALL_COMM_IRECV)
72 && r2->call == SIMCALL_COMM_WAIT) {
74 smx_mailbox_t rdv = MC_get_rdv(r1);
76 if (rdv != synchro2->comm.rdv_cpy
77 && simcall_comm_wait__get__timeout(r2) <= 0)
80 if ((r1->issuer != synchro2->comm.src_proc)
81 && (r1->issuer != synchro2->comm.dst_proc)
82 && simcall_comm_wait__get__timeout(r2) <= 0)
85 if ((r1->call == SIMCALL_COMM_ISEND)
86 && (synchro2->comm.type == SIMIX_COMM_SEND)
87 && (synchro2->comm.src_buff !=
88 simcall_comm_isend__get__src_buff(r1))
89 && simcall_comm_wait__get__timeout(r2) <= 0)
92 if ((r1->call == SIMCALL_COMM_IRECV)
93 && (synchro2->comm.type == SIMIX_COMM_RECEIVE)
94 && (synchro2->comm.dst_buff != simcall_comm_irecv__get__dst_buff(r1))
95 && simcall_comm_wait__get__timeout(r2) <= 0)
99 /* FIXME: the following rule assumes that the result of the
100 * isend/irecv call is not stored in a buffer used in the
102 /*if( (r1->call == SIMCALL_COMM_ISEND || r1->call == SIMCALL_COMM_IRECV)
103 && r2->call == SIMCALL_COMM_TEST)
106 if (r1->call == SIMCALL_COMM_WAIT
107 && (r2->call == SIMCALL_COMM_WAIT || r2->call == SIMCALL_COMM_TEST)
108 && (synchro1->comm.src_proc == nullptr || synchro1->comm.dst_proc == NULL))
111 if (r1->call == SIMCALL_COMM_TEST &&
112 (simcall_comm_test__get__comm(r1) == nullptr
113 || synchro1->comm.src_buff == nullptr
114 || synchro1->comm.dst_buff == nullptr))
117 if (r1->call == SIMCALL_COMM_TEST && r2->call == SIMCALL_COMM_WAIT
118 && synchro1->comm.src_buff == synchro2->comm.src_buff
119 && synchro1->comm.dst_buff == synchro2->comm.dst_buff)
122 if (r1->call == SIMCALL_COMM_WAIT && r2->call == SIMCALL_COMM_TEST
123 && synchro1->comm.src_buff != nullptr
124 && synchro1->comm.dst_buff != nullptr
125 && synchro2->comm.src_buff != nullptr
126 && synchro2->comm.dst_buff != nullptr
127 && synchro1->comm.dst_buff != synchro2->comm.src_buff
128 && synchro1->comm.dst_buff != synchro2->comm.dst_buff
129 && synchro2->comm.dst_buff != synchro1->comm.src_buff)
135 // Those are MC_state_get_internal_request(state)
136 int MC_request_depend(smx_simcall_t r1, smx_simcall_t r2)
138 if (mc_reduce_kind == e_mc_reduce_none)
141 if (r1->issuer == r2->issuer)
144 /* Wait with timeout transitions are not considered by the independance theorem, thus we consider them as dependant with all other transitions */
145 if ((r1->call == SIMCALL_COMM_WAIT && simcall_comm_wait__get__timeout(r1) > 0)
146 || (r2->call == SIMCALL_COMM_WAIT
147 && simcall_comm_wait__get__timeout(r2) > 0))
150 if (r1->call != r2->call)
151 return MC_request_depend_asymmetric(r1, r2)
152 && MC_request_depend_asymmetric(r2, r1);
154 // Those are internal requests, we do not need indirection
155 // because those objects are copies:
156 smx_synchro_t synchro1 = MC_get_comm(r1);
157 smx_synchro_t synchro2 = MC_get_comm(r2);
160 case SIMCALL_COMM_ISEND:
161 return simcall_comm_isend__get__rdv(r1) == simcall_comm_isend__get__rdv(r2);
162 case SIMCALL_COMM_IRECV:
163 return simcall_comm_irecv__get__rdv(r1) == simcall_comm_irecv__get__rdv(r2);
164 case SIMCALL_COMM_WAIT:
165 if (synchro1->comm.src_buff == synchro2->comm.src_buff
166 && synchro1->comm.dst_buff == synchro2->comm.dst_buff)
168 else if (synchro1->comm.src_buff != nullptr
169 && synchro1->comm.dst_buff != nullptr
170 && synchro2->comm.src_buff != nullptr
171 && synchro2->comm.dst_buff != nullptr
172 && synchro1->comm.dst_buff != synchro2->comm.src_buff
173 && synchro1->comm.dst_buff != synchro2->comm.dst_buff
174 && synchro2->comm.dst_buff != synchro1->comm.src_buff)
183 static char *pointer_to_string(void *pointer)
186 if (XBT_LOG_ISENABLED(mc_request, xbt_log_priority_verbose))
187 return bprintf("%p", pointer);
189 return xbt_strdup("(verbose only)");
192 static char *buff_size_to_string(size_t buff_size)
195 if (XBT_LOG_ISENABLED(mc_request, xbt_log_priority_verbose))
196 return bprintf("%zu", buff_size);
198 return xbt_strdup("(verbose only)");
202 char *MC_request_to_string(smx_simcall_t req, int value, e_mc_request_type_t request_type)
204 bool use_remote_comm = true;
205 switch(request_type) {
206 case MC_REQUEST_SIMIX:
207 use_remote_comm = true;
209 case MC_REQUEST_EXECUTED:
210 case MC_REQUEST_INTERNAL:
211 use_remote_comm = false;
215 const char* type = nullptr;
216 char *args = nullptr;
218 smx_process_t issuer = MC_smx_simcall_get_issuer(req);
222 case SIMCALL_COMM_ISEND: {
224 char* p = pointer_to_string(simcall_comm_isend__get__src_buff(req));
225 char* bs = buff_size_to_string(simcall_comm_isend__get__src_buff_size(req));
228 bprintf("src=(%lu)%s (%s), buff=%s, size=%s", issuer->pid,
229 MC_smx_process_get_host_name(issuer),
230 MC_smx_process_get_name(issuer),
234 bprintf("src=(%lu)%s, buff=%s, size=%s", issuer->pid,
235 MC_smx_process_get_name(issuer), p, bs);
241 case SIMCALL_COMM_IRECV: {
242 size_t* remote_size = simcall_comm_irecv__get__dst_buff_size(req);
244 // size_t size = size_pointer ? *size_pointer : 0;
247 mc_model_checker->process().read_bytes(&size, sizeof(size),
248 remote(remote_size));
251 char* p = pointer_to_string(simcall_comm_irecv__get__dst_buff(req));
252 char* bs = buff_size_to_string(size);
255 bprintf("dst=(%lu)%s (%s), buff=%s, size=%s", issuer->pid,
256 MC_smx_process_get_host_name(issuer),
257 MC_smx_process_get_name(issuer),
261 bprintf("dst=(%lu)%s, buff=%s, size=%s", issuer->pid,
262 MC_smx_process_get_name(issuer),
269 case SIMCALL_COMM_WAIT: {
270 smx_synchro_t remote_act = simcall_comm_wait__get__comm(req);
273 type = "WaitTimeout";
274 p = pointer_to_string(remote_act);
275 args = bprintf("comm=%s", p);
278 p = pointer_to_string(remote_act);
280 s_smx_synchro_t synchro;
282 if (use_remote_comm) {
283 mc_model_checker->process().read_bytes(&synchro,
284 sizeof(synchro), remote(remote_act));
289 smx_process_t src_proc = MC_smx_resolve_process(act->comm.src_proc);
290 smx_process_t dst_proc = MC_smx_resolve_process(act->comm.dst_proc);
291 args = bprintf("comm=%s [(%lu)%s (%s)-> (%lu)%s (%s)]", p,
292 src_proc ? src_proc->pid : 0,
293 src_proc ? MC_smx_process_get_host_name(src_proc) : "",
294 src_proc ? MC_smx_process_get_name(src_proc) : "",
295 dst_proc ? dst_proc->pid : 0,
296 dst_proc ? MC_smx_process_get_host_name(dst_proc) : "",
297 dst_proc ? MC_smx_process_get_name(dst_proc) : "");
303 case SIMCALL_COMM_TEST: {
304 smx_synchro_t remote_act = simcall_comm_test__get__comm(req);
305 s_smx_synchro_t synchro;
307 if (use_remote_comm) {
308 mc_model_checker->process().read_bytes(&synchro,
309 sizeof(synchro), remote(remote_act));
315 if (act->comm.src_proc == nullptr || act->comm.dst_proc == NULL) {
317 p = pointer_to_string(remote_act);
318 args = bprintf("comm=%s", p);
321 p = pointer_to_string(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,
327 MC_smx_process_get_name(src_proc),
328 MC_smx_process_get_host_name(src_proc),
330 MC_smx_process_get_name(dst_proc),
331 MC_smx_process_get_host_name(dst_proc));
337 case SIMCALL_COMM_WAITANY: {
340 mc_model_checker->process().read_bytes(
341 &comms, sizeof(comms), remote(simcall_comm_waitany__get__comms(req)));
342 if (!xbt_dynar_is_empty(&comms)) {
343 smx_synchro_t remote_sync;
344 read_element(mc_model_checker->process(),
345 &remote_sync, remote(simcall_comm_waitany__get__comms(req)), value,
346 sizeof(remote_sync));
347 char* p = pointer_to_string(remote_sync);
348 args = bprintf("comm=%s (%d of %lu)",
349 p, value + 1, xbt_dynar_length(&comms));
352 args = bprintf("comm at idx %d", value);
356 case SIMCALL_COMM_TESTANY:
358 type = "TestAny FALSE";
359 args = xbt_strdup("-");
363 bprintf("(%d of %zu)", value + 1,
364 read_length(mc_model_checker->process(),
365 simcall_comm_testany__get__comms(req)));
369 case SIMCALL_MUTEX_TRYLOCK:
370 case SIMCALL_MUTEX_LOCK: {
371 if (req->call == SIMCALL_MUTEX_LOCK)
374 type = "Mutex TRYLOCK";
377 mc_model_checker->process().read_bytes(&mutex, sizeof(mutex),
379 req->call == SIMCALL_MUTEX_LOCK
380 ? simcall_mutex_lock__get__mutex(req)
381 : simcall_mutex_trylock__get__mutex(req)
383 s_xbt_swag_t mutex_sleeping;
384 mc_model_checker->process().read_bytes(&mutex_sleeping, sizeof(mutex_sleeping),
385 remote(mutex.sleeping));
387 args = bprintf("locked = %d, owner = %d, sleeping = %d",
389 mutex.owner != nullptr ? (int) MC_smx_resolve_process(mutex.owner)->pid : -1,
390 mutex_sleeping.count);
394 case SIMCALL_MC_SNAPSHOT:
395 type = "MC_SNAPSHOT";
399 case SIMCALL_MC_COMPARE_SNAPSHOTS:
400 type = "MC_COMPARE_SNAPSHOTS";
404 case SIMCALL_MC_RANDOM:
406 args = bprintf("%d", value);
416 bprintf("[(%lu)%s (%s)] %s(%s)", issuer->pid,
417 MC_smx_process_get_host_name(issuer),
418 MC_smx_process_get_name(issuer),
422 bprintf("[(%lu)%s (%s)] %s ", issuer->pid,
423 MC_smx_process_get_host_name(issuer),
424 MC_smx_process_get_name(issuer),
430 unsigned int MC_request_testany_fail(smx_simcall_t req)
432 // Remote xbt_dynar_foreach on simcall_comm_testany__get__comms(req).
436 mc_model_checker->process().read_bytes(
437 &comms, sizeof(comms), remote(simcall_comm_testany__get__comms(req)));
439 // Read ther dynar buffer:
440 size_t buffer_size = comms.elmsize * comms.used;
441 char buffer[buffer_size];
442 mc_model_checker->process().read_bytes(
443 buffer, buffer_size, remote(comms.data));
445 // Iterate over the elements:
446 assert(comms.elmsize == sizeof(smx_synchro_t));
448 for (cursor=0; cursor != comms.used; ++cursor) {
451 smx_synchro_t remote_action = nullptr;
452 memcpy(&remote_action, buffer + comms.elmsize * cursor, sizeof(remote_action));
454 // Dereference the pointer:
455 s_smx_synchro_t action;
456 mc_model_checker->process().read_bytes(
457 &action, sizeof(action), remote(remote_action));
459 // Finally so something useful about it:
460 if (action.comm.src_proc && action.comm.dst_proc)
467 int MC_request_is_enabled_by_idx(smx_simcall_t req, unsigned int idx)
469 smx_synchro_t remote_act = nullptr;
472 case SIMCALL_COMM_WAIT:
473 /* FIXME: check also that src and dst processes are not suspended */
474 remote_act = simcall_comm_wait__get__comm(req);
477 case SIMCALL_COMM_WAITANY: {
479 mc_model_checker->process(), &remote_act,
480 remote(simcall_comm_waitany__get__comms(req)),
481 idx, sizeof(remote_act));
485 case SIMCALL_COMM_TESTANY: {
487 mc_model_checker->process(), &remote_act,
488 remote(simcall_comm_testany__get__comms(req)),
489 idx, sizeof(remote_act));
497 s_smx_synchro_t synchro;
498 mc_model_checker->process().read_bytes(
499 &synchro, sizeof(synchro), remote(remote_act));
500 return synchro.comm.src_proc && synchro.comm.dst_proc;
503 int MC_process_is_enabled(smx_process_t process)
505 return MC_request_is_enabled(&process->simcall);
508 char *MC_request_get_dot_output(smx_simcall_t req, int value)
510 char *label = nullptr;
512 const smx_process_t issuer = MC_smx_simcall_get_issuer(req);
515 case SIMCALL_COMM_ISEND:
518 bprintf("[(%lu)%s] iSend", issuer->pid,
519 MC_smx_process_get_host_name(issuer));
521 label = bprintf("[(%lu)] iSend", issuer->pid);
524 case SIMCALL_COMM_IRECV:
527 bprintf("[(%lu)%s] iRecv", issuer->pid,
528 MC_smx_process_get_host_name(issuer));
530 label = bprintf("[(%lu)] iRecv", issuer->pid);
533 case SIMCALL_COMM_WAIT: {
537 bprintf("[(%lu)%s] WaitTimeout", issuer->pid,
538 MC_smx_process_get_host_name(issuer));
540 label = bprintf("[(%lu)] WaitTimeout", issuer->pid);
542 smx_synchro_t remote_act = simcall_comm_wait__get__comm(req);
543 s_smx_synchro_t synchro;
544 mc_model_checker->process().read_bytes(&synchro,
545 sizeof(synchro), remote(remote_act));
547 smx_process_t src_proc = MC_smx_resolve_process(synchro.comm.src_proc);
548 smx_process_t dst_proc = MC_smx_resolve_process(synchro.comm.dst_proc);
551 bprintf("[(%lu)%s] Wait [(%lu)->(%lu)]", issuer->pid,
552 MC_smx_process_get_host_name(issuer),
553 src_proc ? src_proc->pid : 0,
554 dst_proc ? dst_proc->pid : 0);
557 bprintf("[(%lu)] Wait [(%lu)->(%lu)]", issuer->pid,
558 src_proc ? src_proc->pid : 0,
559 dst_proc ? dst_proc->pid : 0);
564 case SIMCALL_COMM_TEST: {
565 smx_synchro_t remote_act = simcall_comm_test__get__comm(req);
566 s_smx_synchro_t synchro;
567 mc_model_checker->process().read_bytes(&synchro,
568 sizeof(synchro), remote(remote_act));
569 if (synchro.comm.src_proc == nullptr || synchro.comm.dst_proc == NULL) {
572 bprintf("[(%lu)%s] Test FALSE", issuer->pid,
573 MC_smx_process_get_host_name(issuer));
575 label = bprintf("[(%lu)] Test FALSE", issuer->pid);
579 bprintf("[(%lu)%s] Test TRUE", issuer->pid,
580 MC_smx_process_get_host_name(issuer));
582 label = bprintf("[(%lu)] Test TRUE", issuer->pid);
587 case SIMCALL_COMM_WAITANY: {
588 unsigned long comms_size = read_length(
589 mc_model_checker->process(), remote(simcall_comm_waitany__get__comms(req)));
592 bprintf("[(%lu)%s] WaitAny [%d of %lu]", issuer->pid,
593 MC_smx_process_get_host_name(issuer), value + 1,
597 bprintf("[(%lu)] WaitAny [%d of %lu]", issuer->pid, value + 1,
602 case SIMCALL_COMM_TESTANY:
606 bprintf("[(%lu)%s] TestAny FALSE", issuer->pid,
607 MC_smx_process_get_host_name(issuer));
609 label = bprintf("[(%lu)] TestAny FALSE", issuer->pid);
613 bprintf("[(%lu)%s] TestAny TRUE [%d of %lu]", issuer->pid,
614 MC_smx_process_get_host_name(issuer), value + 1,
615 xbt_dynar_length(simcall_comm_testany__get__comms(req)));
618 bprintf("[(%lu)] TestAny TRUE [%d of %lu]", issuer->pid,
620 xbt_dynar_length(simcall_comm_testany__get__comms(req)));
624 case SIMCALL_MUTEX_TRYLOCK:
625 label = bprintf("[(%lu)] Mutex TRYLOCK", issuer->pid);
628 case SIMCALL_MUTEX_LOCK:
629 label = bprintf("[(%lu)] Mutex LOCK", issuer->pid);
632 case SIMCALL_MC_RANDOM:
635 bprintf("[(%lu)%s] MC_RANDOM (%d)", issuer->pid,
636 MC_smx_process_get_host_name(issuer), value);
638 label = bprintf("[(%lu)] MC_RANDOM (%d)", issuer->pid, value);
641 case SIMCALL_MC_SNAPSHOT:
644 bprintf("[(%lu)%s] MC_SNAPSHOT", issuer->pid,
645 MC_smx_process_get_host_name(issuer));
647 label = bprintf("[(%lu)] MC_SNAPSHOT", issuer->pid);
650 case SIMCALL_MC_COMPARE_SNAPSHOTS:
653 bprintf("[(%lu)%s] MC_COMPARE_SNAPSHOTS", issuer->pid,
654 MC_smx_process_get_host_name(issuer));
656 label = bprintf("[(%lu)] MC_COMPARE_SNAPSHOTS", issuer->pid);
664 bprintf("label = \"%s\", color = %s, fontcolor = %s", label,
665 colors[issuer->pid - 1], colors[issuer->pid - 1]);