Logo AND Algorithmique Numérique Distribuée

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