Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
d363a57da1f78747d8065a49aec8195c8b77ae49
[simgrid.git] / src / mc / mc_global.c
1 #include <unistd.h>
2 #include <sys/types.h>
3 #include <sys/wait.h>
4
5 #include "../surf/surf_private.h"
6 #include "../simix/private.h"
7 #include "xbt/fifo.h"
8 #include "private.h"
9
10
11 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_global, mc,
12                                 "Logging specific to MC (global)");
13
14 /* MC global data structures */
15 mc_snapshot_t initial_snapshot = NULL;
16 xbt_fifo_t mc_stack = NULL;
17 mc_stats_t mc_stats = NULL;
18 mc_stats_pair_t mc_stats_pair = NULL;
19 mc_state_t mc_current_state = NULL;
20 char mc_replay_mode = FALSE;
21 double *mc_time = NULL;
22 xbt_fifo_t mc_snapshot_stack = NULL;
23
24 /**
25  *  \brief Initialize the model-checker data structures
26  */
27 void MC_init(void)
28 {
29   /* Check if MC is already initialized */
30   if (initial_snapshot)
31     return;
32
33   mc_time = xbt_new0(double, simix_process_maxpid);
34
35   /* Initialize the data structures that must be persistent across every
36      iteration of the model-checker (in RAW memory) */
37   MC_SET_RAW_MEM;
38
39   /* Initialize statistics */
40   mc_stats = xbt_new0(s_mc_stats_t, 1);
41   mc_stats->state_size = 1;
42
43   /* Create exploration stack */
44   mc_stack = xbt_fifo_new();
45
46   MC_UNSET_RAW_MEM;
47
48   MC_dpor_init();
49
50   MC_SET_RAW_MEM;
51   /* Save the initial state */
52   initial_snapshot = xbt_new0(s_mc_snapshot_t, 1);
53   MC_take_snapshot(initial_snapshot);
54   MC_UNSET_RAW_MEM;
55 }
56
57 void MC_init_with_automaton(xbt_automaton_t a){
58
59   XBT_DEBUG("Start init mc");
60   
61   mc_time = xbt_new0(double, simix_process_maxpid);
62
63   /* Initialize the data structures that must be persistent across every
64      iteration of the model-checker (in RAW memory) */
65
66   MC_SET_RAW_MEM;
67
68   /* Initialize statistics */
69   mc_stats_pair = xbt_new0(s_mc_stats_pair_t, 1);
70   //mc_stats_pair->pair_size = 1;
71
72   XBT_DEBUG("Creating snapshot_stack");
73
74  /* Create exploration stack */
75   mc_snapshot_stack = xbt_fifo_new();
76
77   MC_UNSET_RAW_MEM;
78
79   MC_dfs_init(a);
80 }
81
82
83 void MC_modelcheck(void)
84 {
85   MC_init();
86   MC_dpor();
87   MC_exit();
88 }
89
90 void MC_modelcheck_with_automaton(xbt_automaton_t a){
91   MC_init_with_automaton(a);
92   MC_exit_with_automaton();
93 }
94
95 void MC_exit_with_automaton(void)
96 {
97   MC_print_statistics_pairs(mc_stats_pair);
98   xbt_free(mc_time);
99   MC_memory_exit();
100 }
101
102 void MC_exit(void)
103 {
104   MC_print_statistics(mc_stats);
105   xbt_free(mc_time);
106   MC_memory_exit();
107 }
108
109 int MC_random(int min, int max)
110 {
111   /*FIXME: return mc_current_state->executed_transition->random.value;*/
112   return 0;
113 }
114
115 /**
116  * \brief Schedules all the process that are ready to run
117  */
118 void MC_wait_for_requests(void)
119 {
120   smx_process_t process;
121   smx_req_t req;
122   unsigned int iter;
123
124   while (xbt_dynar_length(simix_global->process_to_run)) {
125     SIMIX_process_runall();
126     xbt_dynar_foreach(simix_global->process_that_ran, iter, process) {
127       req = &process->request;
128       if (req->call != REQ_NO_REQ && !MC_request_is_visible(req))
129           SIMIX_request_pre(req, 0);
130     }
131   }
132 }
133
134 int MC_deadlock_check()
135 {
136   int deadlock = FALSE;
137   smx_process_t process;
138   if(xbt_swag_size(simix_global->process_list)){
139     deadlock = TRUE;
140     xbt_swag_foreach(process, simix_global->process_list){
141       if(process->request.call != REQ_NO_REQ
142          && MC_request_is_enabled(&process->request)){
143         deadlock = FALSE;
144         break;
145       }
146     }
147   }
148   return deadlock;
149 }
150
151 /**
152  * \brief Re-executes from the initial state all the transitions indicated by
153  *        a given model-checker stack.
154  * \param stack The stack with the transitions to execute.
155 */
156 void MC_replay(xbt_fifo_t stack)
157 {
158   int value;
159   char *req_str;
160   smx_req_t req = NULL, saved_req = NULL;
161   xbt_fifo_item_t item;
162   mc_state_t state;
163
164   XBT_DEBUG("**** Begin Replay ****");
165
166   /* Restore the initial state */
167   MC_restore_snapshot(initial_snapshot);
168   /* At the moment of taking the snapshot the raw heap was set, so restoring
169    * it will set it back again, we have to unset it to continue  */
170   MC_UNSET_RAW_MEM;
171
172   /* Traverse the stack from the initial state and re-execute the transitions */
173   for (item = xbt_fifo_get_last_item(stack);
174        item != xbt_fifo_get_first_item(stack);
175        item = xbt_fifo_get_prev_item(item)) {
176
177     state = (mc_state_t) xbt_fifo_get_item_content(item);
178     saved_req = MC_state_get_executed_request(state, &value);
179    
180     if(saved_req){
181       /* because we got a copy of the executed request, we have to fetch the  
182          real one, pointed by the request field of the issuer process */
183       req = &saved_req->issuer->request;
184
185       /* Debug information */
186       if(XBT_LOG_ISENABLED(mc_global, xbt_log_priority_debug)){
187         req_str = MC_request_to_string(req, value);
188         XBT_DEBUG("Replay: %s (%p)", req_str, state);
189         xbt_free(req_str);
190       }
191     }
192          
193     SIMIX_request_pre(req, value);
194     MC_wait_for_requests();
195          
196     /* Update statistics */
197     mc_stats->visited_states++;
198     mc_stats->executed_transitions++;
199   }
200   XBT_DEBUG("**** End Replay ****");
201 }
202
203 /**
204  * \brief Dumps the contents of a model-checker's stack and shows the actual
205  *        execution trace
206  * \param stack The stack to dump
207 */
208 void MC_dump_stack(xbt_fifo_t stack)
209 {
210   mc_state_t state;
211
212   MC_show_stack(stack);
213
214   MC_SET_RAW_MEM;
215   while ((state = (mc_state_t) xbt_fifo_pop(stack)) != NULL)
216     MC_state_delete(state);
217   MC_UNSET_RAW_MEM;
218 }
219
220 void MC_show_stack(xbt_fifo_t stack)
221 {
222   int value;
223   mc_state_t state;
224   xbt_fifo_item_t item;
225   smx_req_t req;
226   char *req_str = NULL;
227   
228   for (item = xbt_fifo_get_last_item(stack);
229        (item ? (state = (mc_state_t) (xbt_fifo_get_item_content(item)))
230         : (NULL)); item = xbt_fifo_get_prev_item(item)) {
231     req = MC_state_get_executed_request(state, &value);
232     if(req){
233       req_str = MC_request_to_string(req, value);
234       XBT_INFO("%s", req_str);
235       xbt_free(req_str);
236     }
237   }
238 }
239
240 void MC_show_deadlock(smx_req_t req)
241 {
242   /*char *req_str = NULL;*/
243   XBT_INFO("**************************");
244   XBT_INFO("*** DEAD-LOCK DETECTED ***");
245   XBT_INFO("**************************");
246   XBT_INFO("Locked request:");
247   /*req_str = MC_request_to_string(req);
248   XBT_INFO("%s", req_str);
249   xbt_free(req_str);*/
250   XBT_INFO("Counter-example execution trace:");
251   MC_dump_stack(mc_stack);
252 }
253
254 void MC_print_statistics(mc_stats_t stats)
255 {
256   XBT_INFO("State space size ~= %lu", stats->state_size);
257   XBT_INFO("Expanded states = %lu", stats->expanded_states);
258   XBT_INFO("Visited states = %lu", stats->visited_states);
259   XBT_INFO("Executed transitions = %lu", stats->executed_transitions);
260   XBT_INFO("Expanded / Visited = %lf",
261         (double) stats->visited_states / stats->expanded_states);
262   /*XBT_INFO("Exploration coverage = %lf",
263      (double)stats->expanded_states / stats->state_size); */
264 }
265
266 void MC_print_statistics_pairs(mc_stats_pair_t stats)
267 {
268   XBT_INFO("Expanded pairs = %lu", stats->expanded_pairs);
269   XBT_INFO("Visited pairs = %lu", stats->visited_pairs);
270   XBT_INFO("Executed transitions = %lu", stats->executed_transitions);
271   XBT_INFO("Expanded / Visited = %lf",
272         (double) stats->visited_pairs / stats->expanded_pairs);
273   /*XBT_INFO("Exploration coverage = %lf",
274      (double)stats->expanded_states / stats->state_size); */
275 }
276
277 void MC_assert(int prop)
278 {
279   if (MC_IS_ENABLED && !prop) {
280     XBT_INFO("**************************");
281     XBT_INFO("*** PROPERTY NOT VALID ***");
282     XBT_INFO("**************************");
283     XBT_INFO("Counter-example execution trace:");
284     MC_dump_stack(mc_stack);
285     MC_print_statistics(mc_stats);
286     xbt_abort();
287   }
288 }
289
290 void MC_assert_pair(int prop){
291   if (MC_IS_ENABLED && !prop) {
292     XBT_INFO("**************************");
293     XBT_INFO("*** PROPERTY NOT VALID ***");
294     XBT_INFO("**************************");
295     //XBT_INFO("Counter-example execution trace:");
296     //MC_show_snapshot_stack(mc_snapshot_stack);
297     //MC_dump_snapshot_stack(mc_snapshot_stack);
298     //MC_print_statistics(mc_stats);
299     xbt_abort();
300   }
301 }
302
303 void MC_process_clock_add(smx_process_t process, double amount)
304 {
305   mc_time[process->pid] += amount;
306 }
307
308 double MC_process_clock_get(smx_process_t process)
309 {
310   if(mc_time)
311     return mc_time[process->pid];
312   else
313     return 0;
314 }