Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[mc] Switch to C++
[simgrid.git] / src / mc / mc_base.cpp
1 /* Copyright (c) 2008-2014. The SimGrid Team.
2  * All rights reserved.                                                     */
3
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. */
6
7 #include <assert.h>
8
9 #include <simgrid/simix.h>
10
11 #include "mc_base.h"
12 #include "../simix/smx_private.h"
13 #include "mc_record.h"
14
15 #ifdef HAVE_MC
16 #include "mc_process.h"
17 #include "mc_model_checker.h"
18 #include "mc_protocol.h"
19 #include "mc_smx.h"
20 #include "mc_server.h"
21 #endif
22
23 extern "C" {
24
25 XBT_LOG_NEW_CATEGORY(mc, "All MC categories");
26
27 void MC_wait_for_requests(void)
28 {
29 #ifdef HAVE_MC
30   if (mc_mode == MC_MODE_SERVER) {
31     MC_server_wait_client(&mc_model_checker->process);
32     return;
33   }
34 #endif
35
36   smx_process_t process;
37   smx_simcall_t req;
38   unsigned int iter;
39
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);
46     }
47   }
48 }
49
50 int MC_request_is_enabled(smx_simcall_t req)
51 {
52   unsigned int index = 0;
53   smx_synchro_t act = 0;
54 #ifdef HAVE_MC
55   s_smx_synchro_t temp_synchro;
56 #endif
57
58   switch (req->call) {
59   case SIMCALL_NONE:
60     return FALSE;
61
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);
65
66 #ifdef HAVE_MC
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);
72       act = &temp_synchro;
73     }
74 #endif
75
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)
80         return TRUE;
81     } else {
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);
86     }
87     return (act->comm.src_proc && act->comm.dst_proc);
88
89   case SIMCALL_COMM_WAITANY: {
90 #ifdef HAVE_MC
91     // Read dynar:
92     s_xbt_dynar_t comms;
93     MC_process_read_simple(&mc_model_checker->process,
94       &comms, simcall_comm_waitany__get__comms(req), sizeof(comms));
95     // Read dynar buffer:
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));
101 #endif
102
103 #ifdef HAVE_MC
104     for (index = 0; index < comms.used; ++index) {
105       memcpy(&act, buffer + comms.elmsize * index, sizeof(act));
106 #else
107     xbt_dynar_foreach(simcall_comm_waitany__get__comms(req), index, act) {
108 #endif
109
110 #ifdef HAVE_MC
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);
116         act = &temp_synchro;
117       }
118 #endif
119
120       if (act->comm.src_proc && act->comm.dst_proc)
121         return TRUE;
122     }
123     return FALSE;
124   }
125
126   case SIMCALL_MUTEX_LOCK: {
127     smx_mutex_t mutex = simcall_mutex_lock__get__mutex(req);
128 #ifdef HAVE_MC
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);
134       mutex = &temp_mutex;
135     }
136 #endif
137     if(mutex->owner == NULL)
138       return TRUE;
139     else
140 #ifdef HAVE_MC
141       // TODO, *(mutex->owner) :/
142       return MC_smx_resolve_process(mutex->owner)->pid ==
143         MC_smx_resolve_process(req->issuer)->pid;
144 #else
145       return mutex->owner->pid == req->issuer->pid;
146 #endif
147     }
148
149   default:
150     /* The rest of the requests are always enabled */
151     return TRUE;
152   }
153 }
154
155 int MC_request_is_visible(smx_simcall_t req)
156 {
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
165 #ifdef HAVE_MC
166       || req->call == SIMCALL_MC_SNAPSHOT
167       || req->call == SIMCALL_MC_COMPARE_SNAPSHOTS
168 #endif
169       ;
170 }
171
172 int MC_random(int min, int max)
173 {
174   /*FIXME: return mc_current_state->executed_transition->random.value; */
175   return simcall_mc_random(min, max);
176 }
177
178 static int prng_random(int min, int max)
179 {
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
184
185   // Use rejection in order to avoid skew
186   unsigned long x;
187   do {
188 #ifndef _XBT_WIN32
189     x = (unsigned long) random();
190 #else
191     x = (unsigned long) rand();
192 #endif
193   } while( x >= accept_size );
194   return min + (x % output_size);
195 }
196
197 int simcall_HANDLER_mc_random(smx_simcall_t simcall, int min, int max)
198 {
199   if (!MC_is_active() && !MC_record_path){
200     return prng_random(min, max);
201   }
202
203   return simcall->mc_value;
204 }
205
206 void MC_simcall_handle(smx_simcall_t req, int value)
207 {
208 #ifndef HAVE_MC
209   SIMIX_simcall_handle(req, value);
210 #else
211   if (MC_process_is_self(&mc_model_checker->process)) {
212     SIMIX_simcall_handle(req, value);
213     return;
214   }
215
216   unsigned i;
217   mc_smx_process_info_t pi = NULL;
218
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);
222       return;
223     }
224   }
225
226   xbt_die("Could not find the request");
227 #endif
228 }
229
230 }