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"
23 XBT_LOG_NEW_CATEGORY(mc, "All MC categories");
25 void MC_wait_for_requests(void)
27 if (mc_mode == MC_MODE_SERVER) {
28 MC_server_wait_client(&mc_model_checker->process);
32 smx_process_t process;
36 while (!xbt_dynar_is_empty(simix_global->process_to_run)) {
37 SIMIX_process_runall();
38 xbt_dynar_foreach(simix_global->process_that_ran, iter, process) {
39 req = &process->simcall;
40 if (req->call != SIMCALL_NONE && !MC_request_is_visible(req))
41 SIMIX_simcall_handle(req, 0);
46 int MC_request_is_enabled(smx_simcall_t req)
48 unsigned int index = 0;
49 smx_synchro_t act = 0;
51 s_smx_synchro_t temp_synchro;
58 case SIMCALL_COMM_WAIT:
59 /* FIXME: check also that src and dst processes are not suspended */
60 act = simcall_comm_wait__get__comm(req);
63 // Fetch from MCed memory:
64 if (!MC_process_is_self(&mc_model_checker->process)) {
65 MC_process_read(&mc_model_checker->process, MC_PROCESS_NO_FLAG,
66 &temp_synchro, act, sizeof(temp_synchro),
67 MC_PROCESS_INDEX_ANY);
72 if (simcall_comm_wait__get__timeout(req) >= 0) {
73 /* If it has a timeout it will be always be enabled, because even if the
74 * communication is not ready, it can timeout and won't block. */
75 if (_sg_mc_timeout == 1)
78 /* On the other hand if it hasn't a timeout, check if the comm is ready.*/
79 if (act->comm.detached && act->comm.src_proc == NULL
80 && act->comm.type == SIMIX_COMM_READY)
81 return (act->comm.dst_proc != NULL);
83 return (act->comm.src_proc && act->comm.dst_proc);
85 case SIMCALL_COMM_WAITANY: {
89 MC_process_read_simple(&mc_model_checker->process,
90 &comms, simcall_comm_waitany__get__comms(req), sizeof(comms));
92 assert(comms.elmsize == sizeof(act));
93 size_t buffer_size = comms.elmsize * comms.used;
94 char buffer[buffer_size];
95 MC_process_read_simple(&mc_model_checker->process,
96 buffer, comms.data, sizeof(buffer));
100 for (index = 0; index < comms.used; ++index) {
101 memcpy(&act, buffer + comms.elmsize * index, sizeof(act));
103 xbt_dynar_foreach(simcall_comm_waitany__get__comms(req), index, act) {
107 // Fetch from MCed memory:
108 if (!MC_process_is_self(&mc_model_checker->process)) {
109 MC_process_read(&mc_model_checker->process, MC_PROCESS_NO_FLAG,
110 &temp_synchro, act, sizeof(temp_synchro),
111 MC_PROCESS_INDEX_ANY);
116 if (act->comm.src_proc && act->comm.dst_proc)
122 case SIMCALL_MUTEX_LOCK: {
123 smx_mutex_t mutex = simcall_mutex_lock__get__mutex(req);
125 s_smx_mutex_t temp_mutex;
126 if (!MC_process_is_self(&mc_model_checker->process)) {
127 MC_process_read(&mc_model_checker->process, MC_PROCESS_NO_FLAG,
128 &temp_mutex, mutex, sizeof(temp_mutex),
129 MC_PROCESS_INDEX_ANY);
133 if(mutex->owner == NULL)
137 return (mutex->owner->pid == MC_smx_resolve_process(req->issuer)->pid);
139 return (mutex->owner->pid == req->issuer->pid);
144 /* The rest of the requests are always enabled */
149 int MC_request_is_visible(smx_simcall_t req)
151 return req->call == SIMCALL_COMM_ISEND
152 || req->call == SIMCALL_COMM_IRECV
153 || req->call == SIMCALL_COMM_WAIT
154 || req->call == SIMCALL_COMM_WAITANY
155 || req->call == SIMCALL_COMM_TEST
156 || req->call == SIMCALL_COMM_TESTANY
157 || req->call == SIMCALL_MC_RANDOM
158 || req->call == SIMCALL_MUTEX_LOCK
160 || req->call == SIMCALL_MC_SNAPSHOT
161 || req->call == SIMCALL_MC_COMPARE_SNAPSHOTS
166 int MC_random(int min, int max)
168 /*FIXME: return mc_current_state->executed_transition->random.value; */
169 return simcall_mc_random(min, max);
172 static int prng_random(int min, int max)
174 unsigned long output_size = ((unsigned long) max - (unsigned long) min) + 1;
175 unsigned long input_size = (unsigned long) RAND_MAX + 1;
176 unsigned long reject_size = input_size % output_size;
177 unsigned long accept_size = input_size - reject_size; // module*accept_size
179 // Use rejection in order to avoid skew
187 } while( x >= accept_size );
188 return min + (x % output_size);
191 int simcall_HANDLER_mc_random(smx_simcall_t simcall, int min, int max)
193 if (!MC_is_active() && !MC_record_path){
194 return prng_random(min, max);
197 return simcall->mc_value;