Logo AND Algorithmique Numérique Distribuée

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