Logo AND Algorithmique Numérique Distribuée

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