Logo AND Algorithmique Numérique Distribuée

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