Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[mc] Use std::equal_range in get_search_range()
[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 <cassert>
8
9 #include <simgrid_config.h>
10
11 #include <xbt/log.h>
12 #include <xbt/asserts.h>
13 #include <xbt/dynar.h>
14
15 #include <simgrid/simix.h>
16
17 #include "src/mc/mc_base.h"
18 #include "src/simix/smx_private.h"
19 #include "src/mc/mc_replay.h"
20 #include "mc/mc.h"
21 #include "src/mc/mc_protocol.h"
22
23 #if HAVE_MC
24 #include "src/mc/mc_request.h"
25 #include "src/mc/Process.hpp"
26 #include "src/mc/ModelChecker.hpp"
27 #include "src/mc/mc_smx.h"
28 #endif
29
30 #if HAVE_MC
31 using simgrid::mc::remote;
32 #endif
33
34 XBT_LOG_NEW_CATEGORY(mc, "All MC categories");
35
36 int MC_random(int min, int max)
37 {
38   xbt_assert(mc_mode != MC_MODE_SERVER);
39   /* TODO, if the MC is disabled we do not really need to make a simcall for
40    * this :) */
41   /* FIXME: return mc_current_state->executed_transition->random.value; */
42   return simcall_mc_random(min, max);
43 }
44
45 namespace simgrid {
46 namespace mc {
47
48 void wait_for_requests(void)
49 {
50   assert(mc_mode != MC_MODE_SERVER);
51
52   smx_process_t process;
53   smx_simcall_t req;
54   unsigned int iter;
55
56   while (!xbt_dynar_is_empty(simix_global->process_to_run)) {
57     SIMIX_process_runall();
58     xbt_dynar_foreach(simix_global->process_that_ran, iter, process) {
59       req = &process->simcall;
60       if (req->call != SIMCALL_NONE && !simgrid::mc::request_is_visible(req))
61         SIMIX_simcall_handle(req, 0);
62     }
63   }
64 }
65
66 // Called from both MCer and MCed:
67 bool request_is_enabled(smx_simcall_t req)
68 {
69   unsigned int index = 0;
70   smx_synchro_t act = 0;
71 #if HAVE_MC
72   s_smx_synchro_t temp_synchro;
73 #endif
74
75   switch (req->call) {
76   case SIMCALL_NONE:
77     return false;
78
79   case SIMCALL_COMM_WAIT:
80     /* FIXME: check also that src and dst processes are not suspended */
81     act = simcall_comm_wait__get__comm(req);
82
83 #if HAVE_MC
84     // Fetch from MCed memory:
85     if (mc_mode == MC_MODE_SERVER) {
86       mc_model_checker->process().read(&temp_synchro, remote(act));
87       act = &temp_synchro;
88     }
89 #endif
90
91     if (simcall_comm_wait__get__timeout(req) >= 0) {
92       /* If it has a timeout it will be always be enabled, because even if the
93        * communication is not ready, it can timeout and won't block. */
94       if (_sg_mc_timeout == 1)
95         return true;
96     }
97     /* On the other hand if it hasn't a timeout, check if the comm is ready.*/
98     else if (act->comm.detached && act->comm.src_proc == nullptr
99           && act->comm.type == SIMIX_COMM_READY)
100         return (act->comm.dst_proc != nullptr);
101     return (act->comm.src_proc && act->comm.dst_proc);
102
103   case SIMCALL_COMM_WAITANY: {
104     xbt_dynar_t comms;
105 #if 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 #if 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_TRYLOCK:
146     return true;
147
148   case SIMCALL_MUTEX_LOCK: {
149     smx_mutex_t mutex = simcall_mutex_lock__get__mutex(req);
150 #if HAVE_MC
151     s_smx_mutex_t temp_mutex;
152     if (mc_mode == MC_MODE_SERVER) {
153       mc_model_checker->process().read(&temp_mutex, remote(mutex));
154       mutex = &temp_mutex;
155     }
156 #endif
157     if(mutex->owner == nullptr)
158       return true;
159     else
160 #if HAVE_MC
161       // TODO, *(mutex->owner) :/
162       return MC_smx_resolve_process(mutex->owner)->pid ==
163         MC_smx_resolve_process(req->issuer)->pid;
164 #else
165       return mutex->owner->pid == req->issuer->pid;
166 #endif
167     }
168
169   default:
170     /* The rest of the requests are always enabled */
171     return true;
172   }
173 }
174
175 bool request_is_visible(smx_simcall_t req)
176 {
177   return req->call == SIMCALL_COMM_ISEND
178       || req->call == SIMCALL_COMM_IRECV
179       || req->call == SIMCALL_COMM_WAIT
180       || req->call == SIMCALL_COMM_WAITANY
181       || req->call == SIMCALL_COMM_TEST
182       || req->call == SIMCALL_COMM_TESTANY
183       || req->call == SIMCALL_MC_RANDOM
184       || req->call == SIMCALL_MUTEX_LOCK
185       || req->call == SIMCALL_MUTEX_TRYLOCK
186       ;
187 }
188
189 }
190 }
191
192 static int prng_random(int min, int max)
193 {
194   unsigned long output_size = ((unsigned long) max - (unsigned long) min) + 1;
195   unsigned long input_size = (unsigned long) RAND_MAX + 1;
196   unsigned long reject_size = input_size % output_size;
197   unsigned long accept_size = input_size - reject_size; // module*accept_size
198
199   // Use rejection in order to avoid skew
200   unsigned long x;
201   do {
202 #ifndef _WIN32
203     x = (unsigned long) random();
204 #else
205     x = (unsigned long) rand();
206 #endif
207   } while( x >= accept_size );
208   return min + (x % output_size);
209 }
210
211 int simcall_HANDLER_mc_random(smx_simcall_t simcall, int min, int max)
212 {
213   if (!MC_is_active() && !MC_record_path)
214     return prng_random(min, max);
215   return simcall->mc_value;
216 }
217
218 namespace simgrid {
219 namespace mc {
220
221 void handle_simcall(smx_simcall_t req, int value)
222 {
223 #if !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   for (auto& pi : mc_model_checker->process().smx_process_infos)
232     if (req == &pi.copy.simcall) {
233       mc_model_checker->simcall_handle(
234         mc_model_checker->process(), pi.copy.pid, value);
235       return;
236     }
237
238   xbt_die("Could not find the request");
239 #endif
240 }
241
242 }
243 }