Logo AND Algorithmique Numérique Distribuée

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