Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[mc] Remove ugly #include
[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/mc_record.h"
14 #include "mc/mc_replay.h"
15 #include "mc/mc.h"
16
17 #ifdef HAVE_MC
18 #include "mc_process.h"
19 #include "ModelChecker.hpp"
20 #include "mc_protocol.h"
21 #include "mc_smx.h"
22 #include "mc_server.h"
23 #endif
24
25 #ifdef HAVE_MC
26 using simgrid::mc::remote;
27 #endif
28
29 extern "C" {
30
31 XBT_LOG_NEW_CATEGORY(mc, "All MC categories");
32
33 void MC_wait_for_requests(void)
34 {
35 #ifdef HAVE_MC
36   if (mc_mode == MC_MODE_SERVER) {
37     MC_server_wait_client(&mc_model_checker->process());
38     return;
39   }
40 #endif
41
42   smx_process_t process;
43   smx_simcall_t req;
44   unsigned int iter;
45
46   while (!xbt_dynar_is_empty(simix_global->process_to_run)) {
47     SIMIX_process_runall();
48     xbt_dynar_foreach(simix_global->process_that_ran, iter, process) {
49       req = &process->simcall;
50       if (req->call != SIMCALL_NONE && !MC_request_is_visible(req))
51         SIMIX_simcall_handle(req, 0);
52     }
53   }
54 }
55
56 // Called from both MCer and MCed:
57 int MC_request_is_enabled(smx_simcall_t req)
58 {
59   unsigned int index = 0;
60   smx_synchro_t act = 0;
61 #ifdef HAVE_MC
62   s_smx_synchro_t temp_synchro;
63 #endif
64
65   switch (req->call) {
66   case SIMCALL_NONE:
67     return FALSE;
68
69   case SIMCALL_COMM_WAIT:
70     /* FIXME: check also that src and dst processes are not suspended */
71     act = simcall_comm_wait__get__comm(req);
72
73 #ifdef HAVE_MC
74     // Fetch from MCed memory:
75     if (mc_mode == MC_MODE_SERVER) {
76       mc_model_checker->process().read(&temp_synchro, remote(act));
77       act = &temp_synchro;
78     }
79 #endif
80
81     if (simcall_comm_wait__get__timeout(req) >= 0) {
82       /* If it has a timeout it will be always be enabled, because even if the
83        * communication is not ready, it can timeout and won't block. */
84       if (_sg_mc_timeout == 1)
85         return TRUE;
86     } else {
87       /* On the other hand if it hasn't a timeout, check if the comm is ready.*/
88       if (act->comm.detached && act->comm.src_proc == NULL
89           && act->comm.type == SIMIX_COMM_READY)
90         return (act->comm.dst_proc != NULL);
91     }
92     return (act->comm.src_proc && act->comm.dst_proc);
93
94   case SIMCALL_COMM_WAITANY: {
95     xbt_dynar_t comms;
96 #ifdef HAVE_MC
97
98     s_xbt_dynar_t comms_buffer;
99     size_t buffer_size = 0;
100     if (mc_mode == MC_MODE_SERVER) {
101       // Read dynar:
102       mc_model_checker->process().read(
103         &comms_buffer, remote(simcall_comm_waitany__get__comms(req)));
104       assert(comms_buffer.elmsize == sizeof(act));
105       buffer_size = comms_buffer.elmsize * comms_buffer.used;
106       comms = &comms_buffer;
107     } else {
108       comms = simcall_comm_waitany__get__comms(req);
109     }
110     // Read all the dynar buffer:
111     char buffer[buffer_size];
112     if (mc_mode == MC_MODE_SERVER)
113       mc_model_checker->process().read_bytes(buffer, sizeof(buffer),
114         remote(comms->data));
115 #else
116     comms = simcall_comm_waitany__get__comms(req);
117 #endif
118
119     for (index = 0; index < comms->used; ++index) {
120 #ifdef HAVE_MC
121       // Fetch act from MCed memory:
122       if (mc_mode == MC_MODE_SERVER) {
123         memcpy(&act, buffer + comms->elmsize * index, sizeof(act));
124         mc_model_checker->process().read(&temp_synchro, remote(act));
125         act = &temp_synchro;
126       }
127       else
128 #endif
129         act = xbt_dynar_get_as(comms, index, smx_synchro_t);
130       if (act->comm.src_proc && act->comm.dst_proc)
131         return TRUE;
132     }
133     return FALSE;
134   }
135
136   case SIMCALL_MUTEX_LOCK: {
137     smx_mutex_t mutex = simcall_mutex_lock__get__mutex(req);
138 #ifdef HAVE_MC
139     s_smx_mutex_t temp_mutex;
140     if (mc_mode == MC_MODE_SERVER) {
141       mc_model_checker->process().read(&temp_mutex, remote(mutex));
142       mutex = &temp_mutex;
143     }
144 #endif
145     if(mutex->owner == NULL)
146       return TRUE;
147     else
148 #ifdef HAVE_MC
149       // TODO, *(mutex->owner) :/
150       return MC_smx_resolve_process(mutex->owner)->pid ==
151         MC_smx_resolve_process(req->issuer)->pid;
152 #else
153       return mutex->owner->pid == req->issuer->pid;
154 #endif
155     }
156
157   default:
158     /* The rest of the requests are always enabled */
159     return TRUE;
160   }
161 }
162
163 int MC_request_is_visible(smx_simcall_t req)
164 {
165   return req->call == SIMCALL_COMM_ISEND
166       || req->call == SIMCALL_COMM_IRECV
167       || req->call == SIMCALL_COMM_WAIT
168       || req->call == SIMCALL_COMM_WAITANY
169       || req->call == SIMCALL_COMM_TEST
170       || req->call == SIMCALL_COMM_TESTANY
171       || req->call == SIMCALL_MC_RANDOM
172       || req->call == SIMCALL_MUTEX_LOCK
173 #ifdef HAVE_MC
174       || req->call == SIMCALL_MC_SNAPSHOT
175       || req->call == SIMCALL_MC_COMPARE_SNAPSHOTS
176 #endif
177       ;
178 }
179
180 int MC_random(int min, int max)
181 {
182   /*FIXME: return mc_current_state->executed_transition->random.value; */
183   return simcall_mc_random(min, max);
184 }
185
186 static int prng_random(int min, int max)
187 {
188   unsigned long output_size = ((unsigned long) max - (unsigned long) min) + 1;
189   unsigned long input_size = (unsigned long) RAND_MAX + 1;
190   unsigned long reject_size = input_size % output_size;
191   unsigned long accept_size = input_size - reject_size; // module*accept_size
192
193   // Use rejection in order to avoid skew
194   unsigned long x;
195   do {
196 #ifndef _XBT_WIN32
197     x = (unsigned long) random();
198 #else
199     x = (unsigned long) rand();
200 #endif
201   } while( x >= accept_size );
202   return min + (x % output_size);
203 }
204
205 int simcall_HANDLER_mc_random(smx_simcall_t simcall, int min, int max)
206 {
207   if (!MC_is_active() && !MC_record_path){
208     return prng_random(min, max);
209   }
210
211   return simcall->mc_value;
212 }
213
214 void MC_simcall_handle(smx_simcall_t req, int value)
215 {
216 #ifndef HAVE_MC
217   SIMIX_simcall_handle(req, value);
218 #else
219   if (mc_mode == MC_MODE_CLIENT) {
220     SIMIX_simcall_handle(req, value);
221     return;
222   }
223
224   unsigned i;
225   mc_smx_process_info_t pi = NULL;
226
227   xbt_dynar_foreach_ptr(mc_model_checker->process().smx_process_infos, i, pi) {
228     if (req == &pi->copy.simcall) {
229       MC_server_simcall_handle(&mc_model_checker->process(), pi->copy.pid, value);
230       return;
231     }
232   }
233
234   xbt_die("Could not find the request");
235 #endif
236 }
237
238 }