Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
model-checker : end of cleanup for stateful and stateless model checking
[simgrid.git] / src / mc / mc_global.c
1 /* Copyright (c) 2008-2012 Da SimGrid Team. All rights reserved.            */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 #include <unistd.h>
7 #include <sys/types.h>
8 #include <sys/wait.h>
9 #include <sys/time.h>
10
11 #include "../surf/surf_private.h"
12 #include "../simix/smx_private.h"
13 #include "xbt/fifo.h"
14 #include "mc_private.h"
15 #include "xbt/automaton/automaton_create.h"
16
17 XBT_LOG_NEW_CATEGORY(mc, "All MC categories");
18 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_global, mc,
19                                 "Logging specific to MC (global)");
20
21 /* MC global data structures */
22
23 mc_state_t mc_current_state = NULL;
24 char mc_replay_mode = FALSE;
25 double *mc_time = NULL;
26 mc_snapshot_t initial_snapshot = NULL;
27
28 /* Safety */
29
30 xbt_fifo_t mc_stack_safety = NULL;
31 mc_stats_t mc_stats = NULL;
32
33 /* Liveness */
34
35 mc_stats_pair_t mc_stats_pair = NULL;
36 xbt_fifo_t mc_stack_liveness = NULL;
37 mc_snapshot_t initial_snapshot_liveness = NULL;
38
39 xbt_automaton_t automaton;
40
41 static void MC_init_liveness(xbt_automaton_t a);
42 static void MC_assert_pair(int prop);
43
44
45 /**
46  *  \brief Initialize the model-checker data structures
47  */
48 void MC_init_safety(void)
49 {
50
51   /* Check if MC is already initialized */
52   if (initial_snapshot)
53     return;
54
55   mc_time = xbt_new0(double, simix_process_maxpid);
56
57   /* Initialize the data structures that must be persistent across every
58      iteration of the model-checker (in RAW memory) */
59   MC_SET_RAW_MEM;
60
61   /* Initialize statistics */
62   mc_stats = xbt_new0(s_mc_stats_t, 1);
63   mc_stats->state_size = 1;
64
65   /* Create exploration stack */
66   mc_stack_safety = xbt_fifo_new();
67
68   MC_UNSET_RAW_MEM;
69
70   MC_dpor_init();
71
72   MC_SET_RAW_MEM;
73   /* Save the initial state */
74   initial_snapshot = xbt_new0(s_mc_snapshot_t, 1);
75   MC_take_snapshot(initial_snapshot);
76   MC_UNSET_RAW_MEM;
77
78 }
79
80
81 static void MC_init_liveness(xbt_automaton_t a){
82
83   XBT_DEBUG("Start init mc");
84   
85   mc_time = xbt_new0(double, simix_process_maxpid);
86
87   /* Initialize the data structures that must be persistent across every
88      iteration of the model-checker (in RAW memory) */
89
90   MC_SET_RAW_MEM;
91
92   /* Initialize statistics */
93   mc_stats_pair = xbt_new0(s_mc_stats_pair_t, 1);
94
95   XBT_DEBUG("Creating stack");
96
97   /* Create exploration stack */
98   mc_stack_liveness = xbt_fifo_new();
99
100   MC_UNSET_RAW_MEM;
101
102   automaton = a;
103
104   MC_ddfs_init();
105
106   
107 }
108
109
110 void MC_modelcheck(void)
111 {
112   MC_init_safety();
113   MC_dpor();
114   MC_exit();
115 }
116
117
118
119 void MC_modelcheck_liveness(xbt_automaton_t a){
120   MC_init_liveness(a);
121   MC_exit_liveness();
122 }
123
124 void MC_exit_liveness(void)
125 {
126   MC_print_statistics_pairs(mc_stats_pair);
127   //xbt_free(mc_time);
128   //MC_memory_exit();
129   exit(0);
130 }
131
132
133 void MC_exit(void)
134 {
135   MC_print_statistics(mc_stats);
136   xbt_free(mc_time);
137   MC_memory_exit();
138 }
139
140
141 int MC_random(int min, int max)
142 {
143   /*FIXME: return mc_current_state->executed_transition->random.value;*/
144   return 0;
145 }
146
147 /**
148  * \brief Schedules all the process that are ready to run
149  */
150 void MC_wait_for_requests(void)
151 {
152   smx_process_t process;
153   smx_simcall_t req;
154   unsigned int iter;
155
156   while (!xbt_dynar_is_empty(simix_global->process_to_run)) {
157     SIMIX_process_runall();
158     xbt_dynar_foreach(simix_global->process_that_ran, iter, process) {
159       req = &process->simcall;
160       if (req->call != SIMCALL_NONE && !MC_request_is_visible(req))
161         SIMIX_simcall_pre(req, 0);
162     }
163   }
164 }
165
166 int MC_deadlock_check()
167 {
168   int deadlock = FALSE;
169   smx_process_t process;
170   if(xbt_swag_size(simix_global->process_list)){
171     deadlock = TRUE;
172     xbt_swag_foreach(process, simix_global->process_list){
173       if(process->simcall.call != SIMCALL_NONE
174          && MC_request_is_enabled(&process->simcall)){
175         deadlock = FALSE;
176         break;
177       }
178     }
179   }
180   return deadlock;
181 }
182
183 /**
184  * \brief Re-executes from the state at position start all the transitions indicated by
185  *        a given model-checker stack.
186  * \param stack The stack with the transitions to execute.
187  * \param start Start index to begin the re-execution.
188  */
189 void MC_replay(xbt_fifo_t stack, int start)
190 {
191   int value, i = 1;
192   char *req_str;
193   smx_simcall_t req = NULL, saved_req = NULL;
194   xbt_fifo_item_t item, start_item;
195   mc_state_t state;
196
197   XBT_DEBUG("**** Begin Replay ****");
198
199   if(start == -1){
200     /* Restore the initial state */
201     MC_restore_snapshot(initial_snapshot);
202     /* At the moment of taking the snapshot the raw heap was set, so restoring
203      * it will set it back again, we have to unset it to continue  */
204     MC_UNSET_RAW_MEM;
205   }
206
207   start_item = xbt_fifo_get_last_item(stack);
208   if(start != -1){
209     while (i != start){
210       start_item = xbt_fifo_get_prev_item(start_item);
211       i++;
212     }
213   }
214
215   /* Traverse the stack from the state at position start and re-execute the transitions */
216   for (item = start_item;
217        item != xbt_fifo_get_first_item(stack);
218        item = xbt_fifo_get_prev_item(item)) {
219
220     state = (mc_state_t) xbt_fifo_get_item_content(item);
221     saved_req = MC_state_get_executed_request(state, &value);
222    
223     if(saved_req){
224       /* because we got a copy of the executed request, we have to fetch the  
225          real one, pointed by the request field of the issuer process */
226       req = &saved_req->issuer->simcall;
227
228       /* Debug information */
229       if(XBT_LOG_ISENABLED(mc_global, xbt_log_priority_debug)){
230         req_str = MC_request_to_string(req, value);
231         XBT_DEBUG("Replay: %s (%p)", req_str, state);
232         xbt_free(req_str);
233       }
234     }
235          
236     SIMIX_simcall_pre(req, value);
237     MC_wait_for_requests();
238          
239     /* Update statistics */
240     mc_stats->visited_states++;
241     mc_stats->executed_transitions++;
242   }
243   XBT_DEBUG("**** End Replay ****");
244 }
245
246 void MC_replay_liveness(xbt_fifo_t stack, int all_stack)
247 {
248   int value;
249   char *req_str;
250   smx_simcall_t req = NULL, saved_req = NULL;
251   xbt_fifo_item_t item;
252   mc_state_t state;
253   mc_pair_stateless_t pair;
254   int depth = 1;
255
256   XBT_DEBUG("**** Begin Replay ****");
257
258   /* Restore the initial state */
259   MC_restore_snapshot(initial_snapshot_liveness);
260   /* At the moment of taking the snapshot the raw heap was set, so restoring
261    * it will set it back again, we have to unset it to continue  */
262   MC_UNSET_RAW_MEM;
263
264   if(all_stack){
265
266     item = xbt_fifo_get_last_item(stack);
267
268     while(depth <= xbt_fifo_size(stack)){
269
270       pair = (mc_pair_stateless_t) xbt_fifo_get_item_content(item);
271       state = (mc_state_t) pair->graph_state;
272
273       if(pair->requests > 0){
274    
275         saved_req = MC_state_get_executed_request(state, &value);
276         //XBT_DEBUG("SavedReq->call %u", saved_req->call);
277       
278         if(saved_req != NULL){
279           /* because we got a copy of the executed request, we have to fetch the  
280              real one, pointed by the request field of the issuer process */
281           req = &saved_req->issuer->simcall;
282           //XBT_DEBUG("Req->call %u", req->call);
283   
284           /* Debug information */
285           if(XBT_LOG_ISENABLED(mc_global, xbt_log_priority_debug)){
286             req_str = MC_request_to_string(req, value);
287             XBT_DEBUG("Replay (depth = %d) : %s (%p)", depth, req_str, state);
288             xbt_free(req_str);
289           }
290   
291         }
292  
293         SIMIX_simcall_pre(req, value);
294         MC_wait_for_requests();
295       }
296
297       depth++;
298     
299       /* Update statistics */
300       mc_stats_pair->visited_pairs++;
301
302       item = xbt_fifo_get_prev_item(item);
303     }
304
305   }else{
306
307     /* Traverse the stack from the initial state and re-execute the transitions */
308     for (item = xbt_fifo_get_last_item(stack);
309          item != xbt_fifo_get_first_item(stack);
310          item = xbt_fifo_get_prev_item(item)) {
311
312       pair = (mc_pair_stateless_t) xbt_fifo_get_item_content(item);
313       state = (mc_state_t) pair->graph_state;
314
315       if(pair->requests > 0){
316    
317         saved_req = MC_state_get_executed_request(state, &value);
318         //XBT_DEBUG("SavedReq->call %u", saved_req->call);
319       
320         if(saved_req != NULL){
321           /* because we got a copy of the executed request, we have to fetch the  
322              real one, pointed by the request field of the issuer process */
323           req = &saved_req->issuer->simcall;
324           //XBT_DEBUG("Req->call %u", req->call);
325   
326           /* Debug information */
327           if(XBT_LOG_ISENABLED(mc_global, xbt_log_priority_debug)){
328             req_str = MC_request_to_string(req, value);
329             XBT_DEBUG("Replay (depth = %d) : %s (%p)", depth, req_str, state);
330             xbt_free(req_str);
331           }
332   
333         }
334  
335         SIMIX_simcall_pre(req, value);
336         MC_wait_for_requests();
337       }
338
339       depth++;
340     
341       /* Update statistics */
342       mc_stats_pair->visited_pairs++;
343     }
344   }  
345
346   XBT_DEBUG("**** End Replay ****");
347 }
348
349 /**
350  * \brief Dumps the contents of a model-checker's stack and shows the actual
351  *        execution trace
352  * \param stack The stack to dump
353  */
354 void MC_dump_stack_safety(xbt_fifo_t stack)
355 {
356
357   MC_show_stack_safety(stack);
358
359   if(!_surf_do_mc_checkpoint){
360
361     mc_state_t state;
362
363     MC_SET_RAW_MEM;
364     while ((state = (mc_state_t) xbt_fifo_pop(stack)) != NULL)
365       MC_state_delete(state);
366     MC_UNSET_RAW_MEM;
367
368   }
369 }
370
371
372 void MC_show_stack_safety(xbt_fifo_t stack)
373 {
374   int value;
375   mc_state_t state;
376   xbt_fifo_item_t item;
377   smx_simcall_t req;
378   char *req_str = NULL;
379   
380   for (item = xbt_fifo_get_last_item(stack);
381        (item ? (state = (mc_state_t) (xbt_fifo_get_item_content(item)))
382         : (NULL)); item = xbt_fifo_get_prev_item(item)) {
383     req = MC_state_get_executed_request(state, &value);
384     if(req){
385       req_str = MC_request_to_string(req, value);
386       XBT_INFO("%s", req_str);
387       xbt_free(req_str);
388     }
389   }
390 }
391
392 void MC_show_deadlock(smx_simcall_t req)
393 {
394   /*char *req_str = NULL;*/
395   XBT_INFO("**************************");
396   XBT_INFO("*** DEAD-LOCK DETECTED ***");
397   XBT_INFO("**************************");
398   XBT_INFO("Locked request:");
399   /*req_str = MC_request_to_string(req);
400     XBT_INFO("%s", req_str);
401     xbt_free(req_str);*/
402   XBT_INFO("Counter-example execution trace:");
403   MC_dump_stack_safety(mc_stack_safety);
404 }
405
406
407 void MC_show_stack_liveness(xbt_fifo_t stack){
408   int value;
409   mc_pair_stateless_t pair;
410   xbt_fifo_item_t item;
411   smx_simcall_t req;
412   char *req_str = NULL;
413   
414   for (item = xbt_fifo_get_last_item(stack);
415        (item ? (pair = (mc_pair_stateless_t) (xbt_fifo_get_item_content(item)))
416         : (NULL)); item = xbt_fifo_get_prev_item(item)) {
417     req = MC_state_get_executed_request(pair->graph_state, &value);
418     if(req){
419       if(pair->requests>0){
420         req_str = MC_request_to_string(req, value);
421         XBT_INFO("%s", req_str);
422         xbt_free(req_str);
423       }else{
424         XBT_INFO("End of system requests but evolution in Büchi automaton");
425       }
426     }
427   }
428 }
429
430 void MC_dump_stack_liveness(xbt_fifo_t stack){
431   mc_pair_stateless_t pair;
432
433   MC_SET_RAW_MEM;
434   while ((pair = (mc_pair_stateless_t) xbt_fifo_pop(stack)) != NULL)
435     MC_pair_stateless_delete(pair);
436   MC_UNSET_RAW_MEM;
437 }
438
439
440 void MC_print_statistics(mc_stats_t stats)
441 {
442   //XBT_INFO("State space size ~= %lu", stats->state_size);
443   XBT_INFO("Expanded states = %lu", stats->expanded_states);
444   XBT_INFO("Visited states = %lu", stats->visited_states);
445   XBT_INFO("Executed transitions = %lu", stats->executed_transitions);
446   XBT_INFO("Expanded / Visited = %lf",
447            (double) stats->visited_states / stats->expanded_states);
448   /*XBT_INFO("Exploration coverage = %lf",
449     (double)stats->expanded_states / stats->state_size); */
450 }
451
452 void MC_print_statistics_pairs(mc_stats_pair_t stats)
453 {
454   XBT_INFO("Expanded pairs = %lu", stats->expanded_pairs);
455   XBT_INFO("Visited pairs = %lu", stats->visited_pairs);
456   //XBT_INFO("Executed transitions = %lu", stats->executed_transitions);
457   XBT_INFO("Expanded / Visited = %lf",
458            (double) stats->visited_pairs / stats->expanded_pairs);
459   /*XBT_INFO("Exploration coverage = %lf",
460     (double)stats->expanded_states / stats->state_size); */
461 }
462
463 void MC_assert(int prop)
464 {
465   if (MC_IS_ENABLED && !prop){
466     XBT_INFO("**************************");
467     XBT_INFO("*** PROPERTY NOT VALID ***");
468     XBT_INFO("**************************");
469     XBT_INFO("Counter-example execution trace:");
470     MC_dump_stack_safety(mc_stack_safety);
471     MC_print_statistics(mc_stats);
472     xbt_abort();
473   }
474 }
475
476 static void MC_assert_pair(int prop){
477   if (MC_IS_ENABLED && !prop) {
478     XBT_INFO("**************************");
479     XBT_INFO("*** PROPERTY NOT VALID ***");
480     XBT_INFO("**************************");
481     //XBT_INFO("Counter-example execution trace:");
482     MC_show_stack_liveness(mc_stack_liveness);
483     //MC_dump_snapshot_stack(mc_snapshot_stack);
484     MC_print_statistics_pairs(mc_stats_pair);
485     xbt_abort();
486   }
487 }
488
489 void MC_process_clock_add(smx_process_t process, double amount)
490 {
491   mc_time[process->pid] += amount;
492 }
493
494 double MC_process_clock_get(smx_process_t process)
495 {
496   if(mc_time)
497     return mc_time[process->pid];
498   else
499     return 0;
500 }
501
502 void MC_diff(void){
503
504   mc_snapshot_t sn = xbt_new0(s_mc_snapshot_t, 1);
505   MC_take_snapshot_liveness(sn);
506
507   int i;
508
509   XBT_INFO("Number of regions : %u", sn->num_reg);
510
511   for(i=0; i<sn->num_reg; i++){
512     
513     switch(sn->regions[i]->type){
514     case 0: /* heap */
515       XBT_INFO("Size of heap : %zu", sn->regions[i]->size);
516       mmalloc_display_info_heap(sn->regions[i]->data);
517       break;
518     case 1 : /* libsimgrid */
519       XBT_INFO("Size of libsimgrid : %zu", sn->regions[i]->size);
520       break;
521     case 2 : /* data program */
522       XBT_INFO("Size of data program : %zu", sn->regions[i]->size);
523       break;
524     case 3 : /* stack */
525       XBT_INFO("Size of stack : %zu", sn->regions[i]->size);
526       XBT_INFO("Start addr of stack : %p", sn->regions[i]->start_addr);
527       break;
528     }
529
530   }
531
532 }
533
534 xbt_automaton_t MC_create_automaton(const char *file){
535   MC_SET_RAW_MEM;
536   xbt_automaton_t a = xbt_create_automaton(file);
537   MC_UNSET_RAW_MEM;
538   return a;
539 }