Logo AND Algorithmique Numérique Distribuée

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