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"
20 #include "mc_server.h"
25 XBT_LOG_NEW_CATEGORY(mc, "All MC categories");
27 void MC_wait_for_requests(void)
30 if (mc_mode == MC_MODE_SERVER) {
31 MC_server_wait_client(&mc_model_checker->process);
36 smx_process_t process;
40 while (!xbt_dynar_is_empty(simix_global->process_to_run)) {
41 SIMIX_process_runall();
42 xbt_dynar_foreach(simix_global->process_that_ran, iter, process) {
43 req = &process->simcall;
44 if (req->call != SIMCALL_NONE && !MC_request_is_visible(req))
45 SIMIX_simcall_handle(req, 0);
50 int MC_request_is_enabled(smx_simcall_t req)
52 unsigned int index = 0;
53 smx_synchro_t act = 0;
55 s_smx_synchro_t temp_synchro;
62 case SIMCALL_COMM_WAIT:
63 /* FIXME: check also that src and dst processes are not suspended */
64 act = simcall_comm_wait__get__comm(req);
67 // Fetch from MCed memory:
68 if (!MC_process_is_self(&mc_model_checker->process)) {
69 MC_process_read(&mc_model_checker->process, MC_ADDRESS_SPACE_READ_FLAGS_NONE,
70 &temp_synchro, act, sizeof(temp_synchro),
71 MC_PROCESS_INDEX_ANY);
76 if (simcall_comm_wait__get__timeout(req) >= 0) {
77 /* If it has a timeout it will be always be enabled, because even if the
78 * communication is not ready, it can timeout and won't block. */
79 if (_sg_mc_timeout == 1)
82 /* On the other hand if it hasn't a timeout, check if the comm is ready.*/
83 if (act->comm.detached && act->comm.src_proc == NULL
84 && act->comm.type == SIMIX_COMM_READY)
85 return (act->comm.dst_proc != NULL);
87 return (act->comm.src_proc && act->comm.dst_proc);
89 case SIMCALL_COMM_WAITANY: {
93 MC_process_read_simple(&mc_model_checker->process,
94 &comms, simcall_comm_waitany__get__comms(req), sizeof(comms));
96 assert(comms.elmsize == sizeof(act));
97 size_t buffer_size = comms.elmsize * comms.used;
98 char buffer[buffer_size];
99 MC_process_read_simple(&mc_model_checker->process,
100 buffer, comms.data, sizeof(buffer));
104 for (index = 0; index < comms.used; ++index) {
105 memcpy(&act, buffer + comms.elmsize * index, sizeof(act));
107 xbt_dynar_foreach(simcall_comm_waitany__get__comms(req), index, act) {
111 // Fetch from MCed memory:
112 if (!MC_process_is_self(&mc_model_checker->process)) {
113 MC_process_read(&mc_model_checker->process, MC_ADDRESS_SPACE_READ_FLAGS_NONE,
114 &temp_synchro, act, sizeof(temp_synchro),
115 MC_PROCESS_INDEX_ANY);
120 if (act->comm.src_proc && act->comm.dst_proc)
126 case SIMCALL_MUTEX_LOCK: {
127 smx_mutex_t mutex = simcall_mutex_lock__get__mutex(req);
129 s_smx_mutex_t temp_mutex;
130 if (!MC_process_is_self(&mc_model_checker->process)) {
131 MC_process_read(&mc_model_checker->process, MC_ADDRESS_SPACE_READ_FLAGS_NONE,
132 &temp_mutex, mutex, sizeof(temp_mutex),
133 MC_PROCESS_INDEX_ANY);
137 if(mutex->owner == NULL)
141 // TODO, *(mutex->owner) :/
142 return MC_smx_resolve_process(mutex->owner)->pid ==
143 MC_smx_resolve_process(req->issuer)->pid;
145 return mutex->owner->pid == req->issuer->pid;
150 /* The rest of the requests are always enabled */
155 int MC_request_is_visible(smx_simcall_t req)
157 return req->call == SIMCALL_COMM_ISEND
158 || req->call == SIMCALL_COMM_IRECV
159 || req->call == SIMCALL_COMM_WAIT
160 || req->call == SIMCALL_COMM_WAITANY
161 || req->call == SIMCALL_COMM_TEST
162 || req->call == SIMCALL_COMM_TESTANY
163 || req->call == SIMCALL_MC_RANDOM
164 || req->call == SIMCALL_MUTEX_LOCK
166 || req->call == SIMCALL_MC_SNAPSHOT
167 || req->call == SIMCALL_MC_COMPARE_SNAPSHOTS
172 int MC_random(int min, int max)
174 /*FIXME: return mc_current_state->executed_transition->random.value; */
175 return simcall_mc_random(min, max);
178 static int prng_random(int min, int max)
180 unsigned long output_size = ((unsigned long) max - (unsigned long) min) + 1;
181 unsigned long input_size = (unsigned long) RAND_MAX + 1;
182 unsigned long reject_size = input_size % output_size;
183 unsigned long accept_size = input_size - reject_size; // module*accept_size
185 // Use rejection in order to avoid skew
189 x = (unsigned long) random();
191 x = (unsigned long) rand();
193 } while( x >= accept_size );
194 return min + (x % output_size);
197 int simcall_HANDLER_mc_random(smx_simcall_t simcall, int min, int max)
199 if (!MC_is_active() && !MC_record_path){
200 return prng_random(min, max);
203 return simcall->mc_value;
206 void MC_simcall_handle(smx_simcall_t req, int value)
209 SIMIX_simcall_handle(req, value);
211 if (MC_process_is_self(&mc_model_checker->process)) {
212 SIMIX_simcall_handle(req, value);
217 mc_smx_process_info_t pi = NULL;
219 xbt_dynar_foreach_ptr(mc_model_checker->process.smx_process_infos, i, pi) {
220 if (req == &pi->copy.simcall) {
221 MC_server_simcall_handle(&mc_model_checker->process, pi->copy.pid, value);
226 xbt_die("Could not find the request");