Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
model-checker : correction of dfs algorithm for liveness properties
[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   MC_dfs_init(a);
81 }
82
83
84 void MC_modelcheck(void)
85 {
86   MC_init();
87   MC_dpor();
88   MC_exit();
89 }
90
91 void MC_modelcheck_with_automaton(xbt_automaton_t a){
92   MC_init_with_automaton(a);
93   MC_exit_with_automaton();
94 }
95
96 void MC_exit_with_automaton(void)
97 {
98   MC_print_statistics_pairs(mc_stats_pair);
99   xbt_free(mc_time);
100   MC_memory_exit();
101 }
102
103 void MC_exit(void)
104 {
105   MC_print_statistics(mc_stats);
106   xbt_free(mc_time);
107   MC_memory_exit();
108 }
109
110 int MC_random(int min, int max)
111 {
112   /*FIXME: return mc_current_state->executed_transition->random.value;*/
113   return 0;
114 }
115
116 /**
117  * \brief Schedules all the process that are ready to run
118  */
119 void MC_wait_for_requests(void)
120 {
121   smx_process_t process;
122   smx_req_t req;
123   unsigned int iter;
124
125   while (xbt_dynar_length(simix_global->process_to_run)) {
126     SIMIX_process_runall();
127     xbt_dynar_foreach(simix_global->process_that_ran, iter, process) {
128       req = &process->request;
129       if (req->call != REQ_NO_REQ && !MC_request_is_visible(req))
130           SIMIX_request_pre(req, 0);
131     }
132   }
133 }
134
135 int MC_deadlock_check()
136 {
137   int deadlock = FALSE;
138   smx_process_t process;
139   if(xbt_swag_size(simix_global->process_list)){
140     deadlock = TRUE;
141     xbt_swag_foreach(process, simix_global->process_list){
142       if(process->request.call != REQ_NO_REQ
143          && MC_request_is_enabled(&process->request)){
144         deadlock = FALSE;
145         break;
146       }
147     }
148   }
149   return deadlock;
150 }
151
152 /**
153  * \brief Re-executes from the initial state all the transitions indicated by
154  *        a given model-checker stack.
155  * \param stack The stack with the transitions to execute.
156 */
157 void MC_replay(xbt_fifo_t stack)
158 {
159   int value;
160   char *req_str;
161   smx_req_t req = NULL, saved_req = NULL;
162   xbt_fifo_item_t item;
163   mc_state_t state;
164
165   XBT_DEBUG("**** Begin Replay ****");
166
167   /* Restore the initial state */
168   MC_restore_snapshot(initial_snapshot);
169   /* At the moment of taking the snapshot the raw heap was set, so restoring
170    * it will set it back again, we have to unset it to continue  */
171   MC_UNSET_RAW_MEM;
172
173   /* Traverse the stack from the initial state and re-execute the transitions */
174   for (item = xbt_fifo_get_last_item(stack);
175        item != xbt_fifo_get_first_item(stack);
176        item = xbt_fifo_get_prev_item(item)) {
177
178     state = (mc_state_t) xbt_fifo_get_item_content(item);
179     saved_req = MC_state_get_executed_request(state, &value);
180    
181     if(saved_req){
182       /* because we got a copy of the executed request, we have to fetch the  
183          real one, pointed by the request field of the issuer process */
184       req = &saved_req->issuer->request;
185
186       /* Debug information */
187       if(XBT_LOG_ISENABLED(mc_global, xbt_log_priority_debug)){
188         req_str = MC_request_to_string(req, value);
189         XBT_DEBUG("Replay: %s (%p)", req_str, state);
190         xbt_free(req_str);
191       }
192     }
193          
194     SIMIX_request_pre(req, value);
195     MC_wait_for_requests();
196          
197     /* Update statistics */
198     mc_stats->visited_states++;
199     mc_stats->executed_transitions++;
200   }
201   XBT_DEBUG("**** End Replay ****");
202 }
203
204 /**
205  * \brief Dumps the contents of a model-checker's stack and shows the actual
206  *        execution trace
207  * \param stack The stack to dump
208 */
209 void MC_dump_stack(xbt_fifo_t stack)
210 {
211   mc_state_t state;
212
213   MC_show_stack(stack);
214
215   MC_SET_RAW_MEM;
216   while ((state = (mc_state_t) xbt_fifo_pop(stack)) != NULL)
217     MC_state_delete(state);
218   MC_UNSET_RAW_MEM;
219 }
220
221 void MC_show_stack(xbt_fifo_t stack)
222 {
223   int value;
224   mc_state_t state;
225   xbt_fifo_item_t item;
226   smx_req_t req;
227   char *req_str = NULL;
228   
229   for (item = xbt_fifo_get_last_item(stack);
230        (item ? (state = (mc_state_t) (xbt_fifo_get_item_content(item)))
231         : (NULL)); item = xbt_fifo_get_prev_item(item)) {
232     req = MC_state_get_executed_request(state, &value);
233     if(req){
234       req_str = MC_request_to_string(req, value);
235       XBT_INFO("%s", req_str);
236       xbt_free(req_str);
237     }
238   }
239 }
240
241 void MC_show_deadlock(smx_req_t req)
242 {
243   /*char *req_str = NULL;*/
244   XBT_INFO("**************************");
245   XBT_INFO("*** DEAD-LOCK DETECTED ***");
246   XBT_INFO("**************************");
247   XBT_INFO("Locked request:");
248   /*req_str = MC_request_to_string(req);
249   XBT_INFO("%s", req_str);
250   xbt_free(req_str);*/
251   XBT_INFO("Counter-example execution trace:");
252   MC_dump_stack(mc_stack);
253 }
254
255 void MC_print_statistics(mc_stats_t stats)
256 {
257   XBT_INFO("State space size ~= %lu", stats->state_size);
258   XBT_INFO("Expanded states = %lu", stats->expanded_states);
259   XBT_INFO("Visited states = %lu", stats->visited_states);
260   XBT_INFO("Executed transitions = %lu", stats->executed_transitions);
261   XBT_INFO("Expanded / Visited = %lf",
262         (double) stats->visited_states / stats->expanded_states);
263   /*XBT_INFO("Exploration coverage = %lf",
264      (double)stats->expanded_states / stats->state_size); */
265 }
266
267 void MC_print_statistics_pairs(mc_stats_pair_t stats)
268 {
269   XBT_INFO("Expanded pairs = %lu", stats->expanded_pairs);
270   XBT_INFO("Visited pairs = %lu", stats->visited_pairs);
271   //XBT_INFO("Executed transitions = %lu", stats->executed_transitions);
272   XBT_INFO("Expanded / Visited = %lf",
273         (double) stats->visited_pairs / stats->expanded_pairs);
274   /*XBT_INFO("Exploration coverage = %lf",
275      (double)stats->expanded_states / stats->state_size); */
276 }
277
278 void MC_assert(int prop)
279 {
280   if (MC_IS_ENABLED && !prop) {
281     XBT_INFO("**************************");
282     XBT_INFO("*** PROPERTY NOT VALID ***");
283     XBT_INFO("**************************");
284     XBT_INFO("Counter-example execution trace:");
285     MC_dump_stack(mc_stack);
286     MC_print_statistics(mc_stats);
287     xbt_abort();
288   }
289 }
290
291 void MC_assert_pair(int prop){
292   if (MC_IS_ENABLED && !prop) {
293     XBT_INFO("**************************");
294     XBT_INFO("*** PROPERTY NOT VALID ***");
295     XBT_INFO("**************************");
296     //XBT_INFO("Counter-example execution trace:");
297     MC_show_snapshot_stack(mc_snapshot_stack);
298     //MC_dump_snapshot_stack(mc_snapshot_stack);
299     MC_print_statistics_pairs(mc_stats_pair);
300     xbt_abort();
301   }
302 }
303
304 void MC_process_clock_add(smx_process_t process, double amount)
305 {
306   mc_time[process->pid] += amount;
307 }
308
309 double MC_process_clock_get(smx_process_t process)
310 {
311   if(mc_time)
312     return mc_time[process->pid];
313   else
314     return 0;
315 }