Logo AND Algorithmique Numérique Distribuée

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