Logo AND Algorithmique Numérique Distribuée

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