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"
21 XBT_LOG_NEW_CATEGORY(mc, "All MC categories");
23 void MC_wait_for_requests(void)
25 smx_process_t process;
29 while (!xbt_dynar_is_empty(simix_global->process_to_run)) {
30 SIMIX_process_runall();
31 xbt_dynar_foreach(simix_global->process_that_ran, iter, process) {
32 req = &process->simcall;
33 if (req->call != SIMCALL_NONE && !MC_request_is_visible(req))
34 SIMIX_simcall_handle(req, 0);
39 int MC_request_is_enabled(smx_simcall_t req)
41 unsigned int index = 0;
42 smx_synchro_t act = 0;
44 s_smx_synchro_t temp_synchro;
51 case SIMCALL_COMM_WAIT:
52 /* FIXME: check also that src and dst processes are not suspended */
53 act = simcall_comm_wait__get__comm(req);
56 // Fetch from MCed memory:
57 if (!MC_process_is_self(&mc_model_checker->process)) {
58 MC_process_read(&mc_model_checker->process, MC_PROCESS_NO_FLAG,
59 &temp_synchro, act, sizeof(temp_synchro),
60 MC_PROCESS_INDEX_ANY);
65 if (simcall_comm_wait__get__timeout(req) >= 0) {
66 /* If it has a timeout it will be always be enabled, because even if the
67 * communication is not ready, it can timeout and won't block. */
68 if (_sg_mc_timeout == 1)
71 /* On the other hand if it hasn't a timeout, check if the comm is ready.*/
72 if (act->comm.detached && act->comm.src_proc == NULL
73 && act->comm.type == SIMIX_COMM_READY)
74 return (act->comm.dst_proc != NULL);
76 return (act->comm.src_proc && act->comm.dst_proc);
78 case SIMCALL_COMM_WAITANY: {
82 MC_process_read_simple(&mc_model_checker->process,
83 &comms, simcall_comm_waitany__get__comms(req), sizeof(comms));
85 assert(comms.elmsize == sizeof(act));
86 size_t buffer_size = comms.elmsize * comms.used;
87 char buffer[buffer_size];
88 MC_process_read_simple(&mc_model_checker->process,
89 buffer, comms.data, sizeof(buffer));
93 for (index = 0; index < comms.used; ++index) {
94 memcpy(&act, buffer + comms.elmsize * index, sizeof(act));
96 xbt_dynar_foreach(simcall_comm_waitany__get__comms(req), index, act) {
100 // Fetch from MCed memory:
101 if (!MC_process_is_self(&mc_model_checker->process)) {
102 MC_process_read(&mc_model_checker->process, MC_PROCESS_NO_FLAG,
103 &temp_synchro, act, sizeof(temp_synchro),
104 MC_PROCESS_INDEX_ANY);
109 if (act->comm.src_proc && act->comm.dst_proc)
115 case SIMCALL_MUTEX_LOCK: {
116 smx_mutex_t mutex = simcall_mutex_lock__get__mutex(req);
118 s_smx_mutex_t temp_mutex;
119 if (!MC_process_is_self(&mc_model_checker->process)) {
120 MC_process_read(&mc_model_checker->process, MC_PROCESS_NO_FLAG,
121 &temp_mutex, mutex, sizeof(temp_mutex),
122 MC_PROCESS_INDEX_ANY);
126 if(mutex->owner == NULL)
129 return (mutex->owner->pid == req->issuer->pid);
133 /* The rest of the requests are always enabled */
138 int MC_request_is_visible(smx_simcall_t req)
140 return req->call == SIMCALL_COMM_ISEND
141 || req->call == SIMCALL_COMM_IRECV
142 || req->call == SIMCALL_COMM_WAIT
143 || req->call == SIMCALL_COMM_WAITANY
144 || req->call == SIMCALL_COMM_TEST
145 || req->call == SIMCALL_COMM_TESTANY
146 || req->call == SIMCALL_MC_RANDOM
147 || req->call == SIMCALL_MUTEX_LOCK
149 || req->call == SIMCALL_MC_SNAPSHOT
150 || req->call == SIMCALL_MC_COMPARE_SNAPSHOTS
155 int MC_random(int min, int max)
157 /*FIXME: return mc_current_state->executed_transition->random.value; */
158 return simcall_mc_random(min, max);
161 static int prng_random(int min, int max)
163 unsigned long output_size = ((unsigned long) max - (unsigned long) min) + 1;
164 unsigned long input_size = (unsigned long) RAND_MAX + 1;
165 unsigned long reject_size = input_size % output_size;
166 unsigned long accept_size = input_size - reject_size; // module*accept_size
168 // Use rejection in order to avoid skew
176 } while( x >= accept_size );
177 return min + (x % output_size);
180 int simcall_HANDLER_mc_random(smx_simcall_t simcall, int min, int max)
182 if (!MC_is_active() && !MC_record_path){
183 return prng_random(min, max);
186 return simcall->mc_value;