Logo AND Algorithmique Numérique Distribuée

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