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/mc_record.h"
14 #include "mc/mc_replay.h"
18 #include "mc_process.h"
19 #include "ModelChecker.hpp"
20 #include "mc_protocol.h"
22 #include "mc_server.h"
27 XBT_LOG_NEW_CATEGORY(mc, "All MC categories");
29 void MC_wait_for_requests(void)
32 if (mc_mode == MC_MODE_SERVER) {
33 MC_server_wait_client(&mc_model_checker->process());
38 smx_process_t process;
42 while (!xbt_dynar_is_empty(simix_global->process_to_run)) {
43 SIMIX_process_runall();
44 xbt_dynar_foreach(simix_global->process_that_ran, iter, process) {
45 req = &process->simcall;
46 if (req->call != SIMCALL_NONE && !MC_request_is_visible(req))
47 SIMIX_simcall_handle(req, 0);
52 int MC_request_is_enabled(smx_simcall_t req)
54 unsigned int index = 0;
55 smx_synchro_t act = 0;
57 s_smx_synchro_t temp_synchro;
64 case SIMCALL_COMM_WAIT:
65 /* FIXME: check also that src and dst processes are not suspended */
66 act = simcall_comm_wait__get__comm(req);
69 // Fetch from MCed memory:
70 if (mc_mode == MC_MODE_SERVER) {
71 MC_process_read(&mc_model_checker->process(), MC_ADDRESS_SPACE_READ_FLAGS_NONE,
72 &temp_synchro, act, sizeof(temp_synchro),
73 MC_PROCESS_INDEX_ANY);
78 if (simcall_comm_wait__get__timeout(req) >= 0) {
79 /* If it has a timeout it will be always be enabled, because even if the
80 * communication is not ready, it can timeout and won't block. */
81 if (_sg_mc_timeout == 1)
84 /* On the other hand if it hasn't a timeout, check if the comm is ready.*/
85 if (act->comm.detached && act->comm.src_proc == NULL
86 && act->comm.type == SIMIX_COMM_READY)
87 return (act->comm.dst_proc != NULL);
89 return (act->comm.src_proc && act->comm.dst_proc);
91 case SIMCALL_COMM_WAITANY: {
95 MC_process_read_simple(&mc_model_checker->process(),
96 &comms, simcall_comm_waitany__get__comms(req), sizeof(comms));
98 assert(comms.elmsize == sizeof(act));
99 size_t buffer_size = comms.elmsize * comms.used;
100 char buffer[buffer_size];
101 MC_process_read_simple(&mc_model_checker->process(),
102 buffer, comms.data, sizeof(buffer));
106 for (index = 0; index < comms.used; ++index) {
107 memcpy(&act, buffer + comms.elmsize * index, sizeof(act));
109 xbt_dynar_foreach(simcall_comm_waitany__get__comms(req), index, act) {
113 // Fetch from MCed memory:
114 if (mc_mode == MC_MODE_SERVER) {
115 MC_process_read(&mc_model_checker->process(), MC_ADDRESS_SPACE_READ_FLAGS_NONE,
116 &temp_synchro, act, sizeof(temp_synchro),
117 MC_PROCESS_INDEX_ANY);
122 if (act->comm.src_proc && act->comm.dst_proc)
128 case SIMCALL_MUTEX_LOCK: {
129 smx_mutex_t mutex = simcall_mutex_lock__get__mutex(req);
131 s_smx_mutex_t temp_mutex;
132 if (mc_mode == MC_MODE_SERVER) {
133 MC_process_read(&mc_model_checker->process(), MC_ADDRESS_SPACE_READ_FLAGS_NONE,
134 &temp_mutex, mutex, sizeof(temp_mutex),
135 MC_PROCESS_INDEX_ANY);
139 if(mutex->owner == NULL)
143 // TODO, *(mutex->owner) :/
144 return MC_smx_resolve_process(mutex->owner)->pid ==
145 MC_smx_resolve_process(req->issuer)->pid;
147 return mutex->owner->pid == req->issuer->pid;
152 /* The rest of the requests are always enabled */
157 int MC_request_is_visible(smx_simcall_t req)
159 return req->call == SIMCALL_COMM_ISEND
160 || req->call == SIMCALL_COMM_IRECV
161 || req->call == SIMCALL_COMM_WAIT
162 || req->call == SIMCALL_COMM_WAITANY
163 || req->call == SIMCALL_COMM_TEST
164 || req->call == SIMCALL_COMM_TESTANY
165 || req->call == SIMCALL_MC_RANDOM
166 || req->call == SIMCALL_MUTEX_LOCK
168 || req->call == SIMCALL_MC_SNAPSHOT
169 || req->call == SIMCALL_MC_COMPARE_SNAPSHOTS
174 int MC_random(int min, int max)
176 /*FIXME: return mc_current_state->executed_transition->random.value; */
177 return simcall_mc_random(min, max);
180 static int prng_random(int min, int max)
182 unsigned long output_size = ((unsigned long) max - (unsigned long) min) + 1;
183 unsigned long input_size = (unsigned long) RAND_MAX + 1;
184 unsigned long reject_size = input_size % output_size;
185 unsigned long accept_size = input_size - reject_size; // module*accept_size
187 // Use rejection in order to avoid skew
191 x = (unsigned long) random();
193 x = (unsigned long) rand();
195 } while( x >= accept_size );
196 return min + (x % output_size);
199 int simcall_HANDLER_mc_random(smx_simcall_t simcall, int min, int max)
201 if (!MC_is_active() && !MC_record_path){
202 return prng_random(min, max);
205 return simcall->mc_value;
208 void MC_simcall_handle(smx_simcall_t req, int value)
211 SIMIX_simcall_handle(req, value);
213 if (mc_mode == MC_MODE_CLIENT) {
214 SIMIX_simcall_handle(req, value);
219 mc_smx_process_info_t pi = NULL;
221 xbt_dynar_foreach_ptr(mc_model_checker->process().smx_process_infos, i, pi) {
222 if (req == &pi->copy.simcall) {
223 MC_server_simcall_handle(&mc_model_checker->process(), pi->copy.pid, value);
228 xbt_die("Could not find the request");