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. */
9 #include <simgrid_config.h>
12 #include <xbt/asserts.h>
13 #include <xbt/dynar.h>
15 #include <simgrid/simix.h>
17 #include "src/mc/mc_base.h"
18 #include "src/simix/smx_private.h"
19 #include "src/mc/mc_replay.h"
21 #include "src/mc/mc_protocol.h"
23 #include "src/simix/Synchro.h"
24 #include "src/simix/SynchroComm.hpp"
27 #include "src/mc/mc_request.h"
28 #include "src/mc/Process.hpp"
29 #include "src/mc/ModelChecker.hpp"
30 #include "src/mc/mc_smx.h"
34 using simgrid::mc::remote;
37 XBT_LOG_NEW_CATEGORY(mc, "All MC categories");
39 int MC_random(int min, int max)
42 xbt_assert(mc_model_checker == nullptr);
43 /* TODO, if the MC is disabled we do not really need to make a simcall for
46 return simcall_mc_random(min, max);
52 void wait_for_requests(void)
55 xbt_assert(mc_model_checker == nullptr);
58 smx_process_t process;
62 while (!xbt_dynar_is_empty(simix_global->process_to_run)) {
63 SIMIX_process_runall();
64 xbt_dynar_foreach(simix_global->process_that_ran, iter, process) {
65 req = &process->simcall;
66 if (req->call != SIMCALL_NONE && !simgrid::mc::request_is_visible(req))
67 SIMIX_simcall_handle(req, 0);
72 // Called from both MCer and MCed:
73 bool request_is_enabled(smx_simcall_t req)
75 unsigned int index = 0;
77 simgrid::simix::Synchro temp_synchro;
84 case SIMCALL_COMM_WAIT:
86 /* FIXME: check also that src and dst processes are not suspended */
87 simgrid::simix::Comm *act = static_cast<simgrid::simix::Comm*>(simcall_comm_wait__get__comm(req));
90 // Fetch from MCed memory:
91 if (mc_model_checker != nullptr) {
92 mc_model_checker->process().read(&temp_synchro, remote(act));
97 if (simcall_comm_wait__get__timeout(req) >= 0) {
98 /* If it has a timeout it will be always be enabled, because even if the
99 * communication is not ready, it can timeout and won't block. */
100 if (_sg_mc_timeout == 1)
103 /* On the other hand if it hasn't a timeout, check if the comm is ready.*/
104 else if (act->detached && act->src_proc == nullptr
105 && act->type == SIMIX_COMM_READY)
106 return (act->dst_proc != nullptr);
107 return (act->src_proc && act->dst_proc);
110 case SIMCALL_COMM_WAITANY: {
112 simgrid::simix::Comm *act = static_cast<simgrid::simix::Comm*>(simcall_comm_wait__get__comm(req));
115 s_xbt_dynar_t comms_buffer;
116 size_t buffer_size = 0;
117 if (mc_model_checker != nullptr) {
119 mc_model_checker->process().read(
120 &comms_buffer, remote(simcall_comm_waitany__get__comms(req)));
121 assert(comms_buffer.elmsize == sizeof(act));
122 buffer_size = comms_buffer.elmsize * comms_buffer.used;
123 comms = &comms_buffer;
125 comms = simcall_comm_waitany__get__comms(req);
127 // Read all the dynar buffer:
128 char buffer[buffer_size];
129 if (mc_model_checker != nullptr)
130 mc_model_checker->process().read_bytes(buffer, sizeof(buffer),
131 remote(comms->data));
133 comms = simcall_comm_waitany__get__comms(req);
136 for (index = 0; index < comms->used; ++index) {
138 // Fetch act from MCed memory:
139 if (mc_model_checker != nullptr) {
140 memcpy(&act, buffer + comms->elmsize * index, sizeof(act));
141 mc_model_checker->process().read(&temp_synchro, remote(act));
146 act = xbt_dynar_get_as(comms, index, simgrid::simix::Comm*);
147 if (act->src_proc && act->dst_proc)
153 case SIMCALL_MUTEX_TRYLOCK:
156 case SIMCALL_MUTEX_LOCK: {
157 smx_mutex_t mutex = simcall_mutex_lock__get__mutex(req);
159 s_smx_mutex_t temp_mutex;
160 if (mc_model_checker != nullptr) {
161 mc_model_checker->process().read(&temp_mutex, remote(mutex));
166 if(mutex->owner == nullptr)
169 else if (mc_model_checker != nullptr) {
170 simgrid::mc::Process& modelchecked = mc_model_checker->process();
171 // TODO, *(mutex->owner) :/
172 return modelchecked.resolveProcess(simgrid::mc::remote(mutex->owner))->pid
173 == modelchecked.resolveProcess(simgrid::mc::remote(req->issuer))->pid;
177 return mutex->owner->pid == req->issuer->pid;
181 /* The rest of the requests are always enabled */
186 bool request_is_visible(smx_simcall_t req)
188 return req->call == SIMCALL_COMM_ISEND
189 || req->call == SIMCALL_COMM_IRECV
190 || req->call == SIMCALL_COMM_WAIT
191 || req->call == SIMCALL_COMM_WAITANY
192 || req->call == SIMCALL_COMM_TEST
193 || req->call == SIMCALL_COMM_TESTANY
194 || req->call == SIMCALL_MC_RANDOM
195 || req->call == SIMCALL_MUTEX_LOCK
196 || req->call == SIMCALL_MUTEX_TRYLOCK
203 static int prng_random(int min, int max)
205 unsigned long output_size = ((unsigned long) max - (unsigned long) min) + 1;
206 unsigned long input_size = (unsigned long) RAND_MAX + 1;
207 unsigned long reject_size = input_size % output_size;
208 unsigned long accept_size = input_size - reject_size; // module*accept_size
210 // Use rejection in order to avoid skew
214 x = (unsigned long) random();
216 x = (unsigned long) rand();
218 } while( x >= accept_size );
219 return min + (x % output_size);
222 int simcall_HANDLER_mc_random(smx_simcall_t simcall, int min, int max)
224 if (!MC_is_active() && !MC_record_path)
225 return prng_random(min, max);
226 return simcall->mc_value;