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 <simgrid/simix.h>
12 #include "../simix/smx_private.h"
13 #include "mc_record.h"
16 #include "mc_process.h"
17 #include "mc_model_checker.h"
18 #include "mc_protocol.h"
22 XBT_LOG_NEW_CATEGORY(mc, "All MC categories");
24 void MC_wait_for_requests(void)
26 smx_process_t process;
30 while (!xbt_dynar_is_empty(simix_global->process_to_run)) {
31 SIMIX_process_runall();
32 xbt_dynar_foreach(simix_global->process_that_ran, iter, process) {
33 req = &process->simcall;
34 if (req->call != SIMCALL_NONE && !MC_request_is_visible(req))
35 SIMIX_simcall_handle(req, 0);
40 int MC_request_is_enabled(smx_simcall_t req)
42 unsigned int index = 0;
43 smx_synchro_t act = 0;
45 s_smx_synchro_t temp_synchro;
52 case SIMCALL_COMM_WAIT:
53 /* FIXME: check also that src and dst processes are not suspended */
54 act = simcall_comm_wait__get__comm(req);
57 // Fetch from MCed memory:
58 if (!MC_process_is_self(&mc_model_checker->process)) {
59 MC_process_read(&mc_model_checker->process, MC_PROCESS_NO_FLAG,
60 &temp_synchro, act, sizeof(temp_synchro),
61 MC_PROCESS_INDEX_ANY);
66 if (simcall_comm_wait__get__timeout(req) >= 0) {
67 /* If it has a timeout it will be always be enabled, because even if the
68 * communication is not ready, it can timeout and won't block. */
69 if (_sg_mc_timeout == 1)
72 /* On the other hand if it hasn't a timeout, check if the comm is ready.*/
73 if (act->comm.detached && act->comm.src_proc == NULL
74 && act->comm.type == SIMIX_COMM_READY)
75 return (act->comm.dst_proc != NULL);
77 return (act->comm.src_proc && act->comm.dst_proc);
79 case SIMCALL_COMM_WAITANY: {
83 MC_process_read_simple(&mc_model_checker->process,
84 &comms, simcall_comm_waitany__get__comms(req), sizeof(comms));
86 assert(comms.elmsize == sizeof(act));
87 size_t buffer_size = comms.elmsize * comms.used;
88 char buffer[buffer_size];
89 MC_process_read_simple(&mc_model_checker->process,
90 buffer, comms.data, sizeof(buffer));
94 for (index = 0; index < comms.used; ++index) {
95 memcpy(&act, buffer + comms.elmsize * index, sizeof(act));
97 xbt_dynar_foreach(simcall_comm_waitany__get__comms(req), index, act) {
101 // Fetch from MCed memory:
102 if (!MC_process_is_self(&mc_model_checker->process)) {
103 MC_process_read(&mc_model_checker->process, MC_PROCESS_NO_FLAG,
104 &temp_synchro, act, sizeof(temp_synchro),
105 MC_PROCESS_INDEX_ANY);
110 if (act->comm.src_proc && act->comm.dst_proc)
116 case SIMCALL_MUTEX_LOCK: {
117 smx_mutex_t mutex = simcall_mutex_lock__get__mutex(req);
119 s_smx_mutex_t temp_mutex;
120 if (!MC_process_is_self(&mc_model_checker->process)) {
121 MC_process_read(&mc_model_checker->process, MC_PROCESS_NO_FLAG,
122 &temp_mutex, mutex, sizeof(temp_mutex),
123 MC_PROCESS_INDEX_ANY);
127 if(mutex->owner == NULL)
131 return (mutex->owner->pid == MC_smx_resolve_process(req->issuer)->pid);
133 return (mutex->owner->pid == req->issuer->pid);
138 /* The rest of the requests are always enabled */
143 int MC_request_is_visible(smx_simcall_t req)
145 return req->call == SIMCALL_COMM_ISEND
146 || req->call == SIMCALL_COMM_IRECV
147 || req->call == SIMCALL_COMM_WAIT
148 || req->call == SIMCALL_COMM_WAITANY
149 || req->call == SIMCALL_COMM_TEST
150 || req->call == SIMCALL_COMM_TESTANY
151 || req->call == SIMCALL_MC_RANDOM
152 || req->call == SIMCALL_MUTEX_LOCK
154 || req->call == SIMCALL_MC_SNAPSHOT
155 || req->call == SIMCALL_MC_COMPARE_SNAPSHOTS
160 int MC_random(int min, int max)
162 /*FIXME: return mc_current_state->executed_transition->random.value; */
163 return simcall_mc_random(min, max);
166 static int prng_random(int min, int max)
168 unsigned long output_size = ((unsigned long) max - (unsigned long) min) + 1;
169 unsigned long input_size = (unsigned long) RAND_MAX + 1;
170 unsigned long reject_size = input_size % output_size;
171 unsigned long accept_size = input_size - reject_size; // module*accept_size
173 // Use rejection in order to avoid skew
181 } while( x >= accept_size );
182 return min + (x % output_size);
185 int simcall_HANDLER_mc_random(smx_simcall_t simcall, int min, int max)
187 if (!MC_is_active() && !MC_record_path){
188 return prng_random(min, max);
191 return simcall->mc_value;