Logo AND Algorithmique Numérique Distribuée

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