Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
rename simix::Mutex to simix::MutexImpl
[simgrid.git] / src / mc / mc_base.cpp
1 /* Copyright (c) 2008-2017. The SimGrid Team. All rights reserved.          */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 #include <simgrid_config.h>
7
8 #include "mc/mc.h"
9 #include "src/mc/mc_base.h"
10 #include "src/mc/mc_replay.h"
11 #include "src/simix/smx_private.h"
12
13 #if SIMGRID_HAVE_MC
14 #include "src/mc/ModelChecker.hpp"
15
16 using simgrid::mc::remote;
17 #endif
18
19 XBT_LOG_NEW_DEFAULT_CATEGORY(mc, "All MC categories");
20
21 int MC_random(int min, int max)
22 {
23 #if SIMGRID_HAVE_MC
24   xbt_assert(mc_model_checker == nullptr);
25 #endif
26   /* TODO, if the MC is disabled we do not really need to make a simcall for this :) */
27   return simcall_mc_random(min, max);
28 }
29
30 namespace simgrid {
31 namespace mc {
32
33 void wait_for_requests()
34 {
35 #if SIMGRID_HAVE_MC
36   xbt_assert(mc_model_checker == nullptr);
37 #endif
38
39   smx_actor_t process;
40   smx_simcall_t req;
41   unsigned int iter;
42
43   while (not xbt_dynar_is_empty(simix_global->process_to_run)) {
44     SIMIX_process_runall();
45     xbt_dynar_foreach(simix_global->process_that_ran, iter, process) {
46       req = &process->simcall;
47       if (req->call != SIMCALL_NONE && not simgrid::mc::request_is_visible(req))
48         SIMIX_simcall_handle(req, 0);
49     }
50   }
51 #if SIMGRID_HAVE_MC
52   xbt_dynar_reset(simix_global->actors_vector);
53   for (std::pair<aid_t, smx_actor_t> kv : simix_global->process_list) {
54     xbt_dynar_push_as(simix_global->actors_vector, smx_actor_t, kv.second);
55   }
56 #endif
57 }
58
59 /** @brief returns if there this transition can proceed in a finite amount of time
60  *
61  * It is used in the model-checker to not get into self-deadlock where it would execute a never ending transition.
62  *
63  * Only WAIT operations (on comm, on mutex, etc) can ever return false because they could lock the MC exploration.
64  * Wait operations are OK and return true in only two situations:
65  *  - if the wait will succeed immediately (if both peer of the comm are there already or if the mutex is available)
66  *  - if a timeout is provided, because we can fire the timeout if the transition is not ready without blocking in this transition for ever.
67  *
68  */
69 // Called from both MCer and MCed:
70 bool request_is_enabled(smx_simcall_t req)
71 {
72   unsigned int index = 0;
73   // TODO, add support for the subtypes?
74
75   switch (req->call) {
76   case SIMCALL_NONE:
77     return false;
78
79   case SIMCALL_COMM_WAIT:
80   {
81     /* FIXME: check also that src and dst processes are not suspended */
82     simgrid::kernel::activity::Comm *act =
83         static_cast<simgrid::kernel::activity::Comm*>(simcall_comm_wait__get__comm(req));
84
85 #if SIMGRID_HAVE_MC
86     // Fetch from MCed memory:
87     // HACK, type puning
88     if (mc_model_checker != nullptr) {
89       simgrid::mc::Remote<simgrid::kernel::activity::Comm> temp_comm;
90       mc_model_checker->process().read(temp_comm, remote(act));
91       act = static_cast<simgrid::kernel::activity::Comm*>(temp_comm.getBuffer());
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->detached && act->src_proc == nullptr
103           && act->type == SIMIX_COMM_READY)
104         return (act->dst_proc != nullptr);
105     return (act->src_proc && act->dst_proc);
106   }
107
108   case SIMCALL_COMM_WAITANY: {
109     xbt_dynar_t comms;
110     simgrid::kernel::activity::Comm *act =
111         static_cast<simgrid::kernel::activity::Comm*>(simcall_comm_wait__get__comm(req));
112
113 #if SIMGRID_HAVE_MC
114     s_xbt_dynar_t comms_buffer;
115     size_t buffer_size = 0;
116     if (mc_model_checker != nullptr) {
117       // Read dynar:
118       mc_model_checker->process().read(
119         &comms_buffer, remote(simcall_comm_waitany__get__comms(req)));
120       assert(comms_buffer.elmsize == sizeof(act));
121       buffer_size = comms_buffer.elmsize * comms_buffer.used;
122       comms = &comms_buffer;
123     } else
124       comms = simcall_comm_waitany__get__comms(req);
125
126     // Read all the dynar buffer:
127     char buffer[buffer_size];
128     if (mc_model_checker != nullptr)
129       mc_model_checker->process().read_bytes(buffer, sizeof(buffer),
130         remote(comms->data));
131 #else
132     comms = simcall_comm_waitany__get__comms(req);
133 #endif
134
135     for (index = 0; index < comms->used; ++index) {
136 #if SIMGRID_HAVE_MC
137       // Fetch act from MCed memory:
138       // HACK, type puning
139       simgrid::mc::Remote<simgrid::kernel::activity::Comm> temp_comm;
140       if (mc_model_checker != nullptr) {
141         memcpy(&act, buffer + comms->elmsize * index, sizeof(act));
142         mc_model_checker->process().read(temp_comm, remote(act));
143         act = static_cast<simgrid::kernel::activity::Comm*>(temp_comm.getBuffer());
144       }
145       else
146 #endif
147         act = xbt_dynar_get_as(comms, index, simgrid::kernel::activity::Comm*);
148       if (act->src_proc && act->dst_proc)
149         return true;
150     }
151     return false;
152   }
153
154   case SIMCALL_MUTEX_LOCK: {
155     smx_mutex_t mutex = simcall_mutex_lock__get__mutex(req);
156 #if SIMGRID_HAVE_MC
157     simgrid::mc::Remote<simgrid::simix::MutexImpl> temp_mutex;
158     if (mc_model_checker != nullptr) {
159       mc_model_checker->process().read(temp_mutex.getBuffer(), remote(mutex));
160       mutex = temp_mutex.getBuffer();
161     }
162 #endif
163
164     if(mutex->owner == nullptr)
165       return true;
166 #if SIMGRID_HAVE_MC
167     else if (mc_model_checker != nullptr) {
168       simgrid::mc::Process& modelchecked = mc_model_checker->process();
169       // TODO, *(mutex->owner) :/
170       return modelchecked.resolveActor(simgrid::mc::remote(mutex->owner))->pid ==
171              modelchecked.resolveActor(simgrid::mc::remote(req->issuer))->pid;
172     }
173 #endif
174     else
175       return mutex->owner->pid == req->issuer->pid;
176     }
177
178     case SIMCALL_SEM_ACQUIRE: {
179       static int warned = 0;
180       if (not warned)
181         XBT_INFO("Using semaphore in model-checked code is still experimental. Use at your own risk");
182       warned = 1;
183       return true;
184     }
185
186     case SIMCALL_COND_WAIT: {
187       static int warned = 0;
188       if (not warned)
189         XBT_INFO("Using condition variables in model-checked code is still experimental. Use at your own risk");
190       warned = 1;
191       return true;
192     }
193
194     default:
195       /* The rest of the requests are always enabled */
196       return true;
197   }
198 }
199
200 bool request_is_visible(smx_simcall_t req)
201 {
202   return req->call == SIMCALL_COMM_ISEND
203       || req->call == SIMCALL_COMM_IRECV
204       || req->call == SIMCALL_COMM_WAIT
205       || req->call == SIMCALL_COMM_WAITANY
206       || req->call == SIMCALL_COMM_TEST
207       || req->call == SIMCALL_COMM_TESTANY
208       || req->call == SIMCALL_MC_RANDOM
209       || req->call == SIMCALL_MUTEX_LOCK
210       || req->call == SIMCALL_MUTEX_TRYLOCK
211       ;
212 }
213
214 }
215 }
216
217 static int prng_random(int min, int max)
218 {
219   unsigned long output_size = ((unsigned long) max - (unsigned long) min) + 1;
220   unsigned long input_size = (unsigned long) RAND_MAX + 1;
221   unsigned long reject_size = input_size % output_size;
222   unsigned long accept_size = input_size - reject_size; // module*accept_size
223
224   // Use rejection in order to avoid skew
225   unsigned long x;
226   do {
227 #ifndef _WIN32
228     x = (unsigned long) random();
229 #else
230     x = (unsigned long) rand();
231 #endif
232   } while( x >= accept_size );
233   return min + (x % output_size);
234 }
235
236 int simcall_HANDLER_mc_random(smx_simcall_t simcall, int min, int max)
237 {
238   if (not MC_is_active() && not MC_record_path)
239     return prng_random(min, max);
240   return simcall->mc_value;
241 }