Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
1417283c0fc3a7635221452708a5536dba01405a
[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 #if 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 #if HAVE_MC
31 using simgrid::mc::remote;
32 #endif
33
34 XBT_LOG_NEW_CATEGORY(mc, "All MC categories");
35
36 int MC_random(int min, int max)
37 {
38   xbt_assert(mc_mode != MC_MODE_SERVER);
39   /* TODO, if the MC is disabled we do not really need to make a simcall for
40    * this :) */
41   return simcall_mc_random(min, max);
42 }
43
44 namespace simgrid {
45 namespace mc {
46
47 void wait_for_requests(void)
48 {
49   assert(mc_mode != MC_MODE_SERVER);
50
51   smx_process_t process;
52   smx_simcall_t req;
53   unsigned int iter;
54
55   while (!xbt_dynar_is_empty(simix_global->process_to_run)) {
56     SIMIX_process_runall();
57     xbt_dynar_foreach(simix_global->process_that_ran, iter, process) {
58       req = &process->simcall;
59       if (req->call != SIMCALL_NONE && !simgrid::mc::request_is_visible(req))
60         SIMIX_simcall_handle(req, 0);
61     }
62   }
63 }
64
65 // Called from both MCer and MCed:
66 bool request_is_enabled(smx_simcall_t req)
67 {
68   unsigned int index = 0;
69   smx_synchro_t act = 0;
70 #if HAVE_MC
71   s_smx_synchro_t temp_synchro;
72 #endif
73
74   switch (req->call) {
75   case SIMCALL_NONE:
76     return false;
77
78   case SIMCALL_COMM_WAIT:
79     /* FIXME: check also that src and dst processes are not suspended */
80     act = simcall_comm_wait__get__comm(req);
81
82 #if HAVE_MC
83     // Fetch from MCed memory:
84     if (mc_mode == MC_MODE_SERVER) {
85       mc_model_checker->process().read(&temp_synchro, remote(act));
86       act = &temp_synchro;
87     }
88 #endif
89
90     if (simcall_comm_wait__get__timeout(req) >= 0) {
91       /* If it has a timeout it will be always be enabled, because even if the
92        * communication is not ready, it can timeout and won't block. */
93       if (_sg_mc_timeout == 1)
94         return true;
95     }
96     /* On the other hand if it hasn't a timeout, check if the comm is ready.*/
97     else if (act->comm.detached && act->comm.src_proc == nullptr
98           && act->comm.type == SIMIX_COMM_READY)
99         return (act->comm.dst_proc != nullptr);
100     return (act->comm.src_proc && act->comm.dst_proc);
101
102   case SIMCALL_COMM_WAITANY: {
103     xbt_dynar_t comms;
104 #if HAVE_MC
105
106     s_xbt_dynar_t comms_buffer;
107     size_t buffer_size = 0;
108     if (mc_mode == MC_MODE_SERVER) {
109       // Read dynar:
110       mc_model_checker->process().read(
111         &comms_buffer, remote(simcall_comm_waitany__get__comms(req)));
112       assert(comms_buffer.elmsize == sizeof(act));
113       buffer_size = comms_buffer.elmsize * comms_buffer.used;
114       comms = &comms_buffer;
115     } else
116       comms = simcall_comm_waitany__get__comms(req);
117
118     // Read all the dynar buffer:
119     char buffer[buffer_size];
120     if (mc_mode == MC_MODE_SERVER)
121       mc_model_checker->process().read_bytes(buffer, sizeof(buffer),
122         remote(comms->data));
123 #else
124     comms = simcall_comm_waitany__get__comms(req);
125 #endif
126
127     for (index = 0; index < comms->used; ++index) {
128 #if HAVE_MC
129       // Fetch act from MCed memory:
130       if (mc_mode == MC_MODE_SERVER) {
131         memcpy(&act, buffer + comms->elmsize * index, sizeof(act));
132         mc_model_checker->process().read(&temp_synchro, remote(act));
133         act = &temp_synchro;
134       }
135       else
136 #endif
137         act = xbt_dynar_get_as(comms, index, smx_synchro_t);
138       if (act->comm.src_proc && act->comm.dst_proc)
139         return true;
140     }
141     return false;
142   }
143
144   case SIMCALL_MUTEX_TRYLOCK:
145     return true;
146
147   case SIMCALL_MUTEX_LOCK: {
148     smx_mutex_t mutex = simcall_mutex_lock__get__mutex(req);
149 #if HAVE_MC
150     s_smx_mutex_t temp_mutex;
151     if (mc_mode == MC_MODE_SERVER) {
152       mc_model_checker->process().read(&temp_mutex, remote(mutex));
153       mutex = &temp_mutex;
154     }
155 #endif
156     if(mutex->owner == nullptr)
157       return true;
158     else
159 #if HAVE_MC
160       // TODO, *(mutex->owner) :/
161       return MC_smx_resolve_process(simgrid::mc::remote(mutex->owner))->pid ==
162         MC_smx_resolve_process(simgrid::mc::remote(req->issuer))->pid;
163 #else
164       return mutex->owner->pid == req->issuer->pid;
165 #endif
166     }
167
168   default:
169     /* The rest of the requests are always enabled */
170     return true;
171   }
172 }
173
174 bool request_is_visible(smx_simcall_t req)
175 {
176   return req->call == SIMCALL_COMM_ISEND
177       || req->call == SIMCALL_COMM_IRECV
178       || req->call == SIMCALL_COMM_WAIT
179       || req->call == SIMCALL_COMM_WAITANY
180       || req->call == SIMCALL_COMM_TEST
181       || req->call == SIMCALL_COMM_TESTANY
182       || req->call == SIMCALL_MC_RANDOM
183       || req->call == SIMCALL_MUTEX_LOCK
184       || req->call == SIMCALL_MUTEX_TRYLOCK
185       ;
186 }
187
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 }