Logo AND Algorithmique Numérique Distribuée

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