Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
model-checker : sort ignore list and dichotomic search
[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.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 /* Configuration support */
22 e_mc_reduce_t mc_reduce_kind=e_mc_reduce_unset;
23
24 extern int _surf_init_status;
25 void _mc_cfg_cb_reduce(const char *name, int pos) {
26   if (_surf_init_status && !_surf_do_model_check) {
27     xbt_die("You are specifying a reduction strategy after the initialization (through MSG_config?), but model-checking was not activated at config time (through --cfg=model-check:1). This won't work, sorry.");
28   }
29   char *val= xbt_cfg_get_string(_surf_cfg_set, name);
30   if (!strcasecmp(val,"none")) {
31     mc_reduce_kind = e_mc_reduce_none;
32   } else if (!strcasecmp(val,"dpor")) {
33     mc_reduce_kind = e_mc_reduce_dpor;
34   } else {
35     xbt_die("configuration option %s can only take 'none' or 'dpor' as a value",name);
36   }
37   xbt_cfg_set_int(_surf_cfg_set,"model-check",1);
38 }
39
40 void _mc_cfg_cb_checkpoint(const char *name, int pos) {
41   if (_surf_init_status && !_surf_do_model_check) {
42     xbt_die("You are specifying a checkpointing value after the initialization (through MSG_config?), but model-checking was not activated at config time (through --cfg=model-check:1). This won't work, sorry.");
43   }
44   _surf_mc_checkpoint = xbt_cfg_get_int(_surf_cfg_set, name);
45   xbt_cfg_set_int(_surf_cfg_set,"model-check",1);
46 }
47 void _mc_cfg_cb_property(const char *name, int pos) {
48   if (_surf_init_status && !_surf_do_model_check) {
49     xbt_die("You are specifying a property after the initialization (through MSG_config?), but model-checking was not activated at config time (through --cfg=model-check:1). This won't work, sorry.");
50   }
51   _surf_mc_property_file= xbt_cfg_get_string(_surf_cfg_set, name);
52   xbt_cfg_set_int(_surf_cfg_set,"model-check",1);
53 }
54
55
56 /* MC global data structures */
57
58 mc_state_t mc_current_state = NULL;
59 char mc_replay_mode = FALSE;
60 double *mc_time = NULL;
61 mc_snapshot_t initial_snapshot = NULL;
62 int raw_mem_set;
63
64 /* Safety */
65
66 xbt_fifo_t mc_stack_safety = NULL;
67 mc_stats_t mc_stats = NULL;
68
69 /* Liveness */
70
71 mc_stats_pair_t mc_stats_pair = NULL;
72 xbt_fifo_t mc_stack_liveness = NULL;
73 mc_snapshot_t initial_snapshot_liveness = NULL;
74 int compare;
75 extern xbt_dynar_t mmalloc_ignore;
76
77 xbt_automaton_t _mc_property_automaton = NULL;
78
79 static void MC_assert_pair(int prop);
80
81 void MC_do_the_modelcheck_for_real() {
82   if (!_surf_mc_property_file || _surf_mc_property_file[0]=='\0') {
83     if (mc_reduce_kind==e_mc_reduce_unset)
84       mc_reduce_kind=e_mc_reduce_dpor;
85
86     XBT_INFO("Check a safety property");
87     MC_modelcheck();
88
89   } else  {
90
91     if (mc_reduce_kind==e_mc_reduce_unset)
92       mc_reduce_kind=e_mc_reduce_none;
93
94     XBT_INFO("Check the liveness property %s",_surf_mc_property_file);
95     MC_automaton_load(_surf_mc_property_file);
96     MC_modelcheck_liveness();
97   }
98 }
99
100 /**
101  *  \brief Initialize the model-checker data structures
102  */
103 void MC_init_safety(void)
104 {
105
106   raw_mem_set = (mmalloc_get_current_heap() == raw_heap);
107
108   /* Check if MC is already initialized */
109   if (initial_snapshot)
110     return;
111
112   mc_time = xbt_new0(double, simix_process_maxpid);
113
114   /* Initialize the data structures that must be persistent across every
115      iteration of the model-checker (in RAW memory) */
116   
117   MC_SET_RAW_MEM;
118
119   /* Initialize statistics */
120   mc_stats = xbt_new0(s_mc_stats_t, 1);
121   mc_stats->state_size = 1;
122
123   /* Create exploration stack */
124   mc_stack_safety = xbt_fifo_new();
125
126   MC_UNSET_RAW_MEM;
127
128   MC_dpor_init();
129
130   MC_SET_RAW_MEM;
131   /* Save the initial state */
132   initial_snapshot = xbt_new0(s_mc_snapshot_t, 1);
133   MC_take_snapshot(initial_snapshot);
134   MC_UNSET_RAW_MEM;
135
136
137   if(raw_mem_set)
138     MC_SET_RAW_MEM;
139   else
140     MC_UNSET_RAW_MEM;
141   
142 }
143
144 void MC_compare(void){
145   compare = 1;
146 }
147
148
149 void MC_modelcheck(void)
150 {
151   MC_init_safety();
152   MC_dpor();
153   MC_exit();
154 }
155
156 void MC_modelcheck_liveness(){
157
158   raw_mem_set = (mmalloc_get_current_heap() == raw_heap);
159
160   /* init stuff */
161   XBT_DEBUG("Start init mc");
162   
163   mc_time = xbt_new0(double, simix_process_maxpid);
164
165   /* mc_time refers to clock for each process -> ignore it for heap comparison */
166   int i;
167   for(i = 0; i<simix_process_maxpid; i++)
168     MC_ignore(&(mc_time[i]), sizeof(double));
169   
170   compare = 0;
171
172   /* Initialize the data structures that must be persistent across every
173      iteration of the model-checker (in RAW memory) */
174
175   MC_SET_RAW_MEM;
176
177   /* Initialize statistics */
178   mc_stats_pair = xbt_new0(s_mc_stats_pair_t, 1);
179
180   XBT_DEBUG("Creating stack");
181
182   /* Create exploration stack */
183   mc_stack_liveness = xbt_fifo_new();
184
185   MC_UNSET_RAW_MEM;
186
187
188   MC_ddfs_init();
189
190   /* We're done */
191   MC_print_statistics_pairs(mc_stats_pair);
192   xbt_free(mc_time);
193   MC_memory_exit();
194   exit(0);
195 }
196
197
198 void MC_exit(void)
199 {
200   MC_print_statistics(mc_stats);
201   xbt_free(mc_time);
202   MC_memory_exit();
203 }
204
205
206 int MC_random(int min, int max)
207 {
208   /*FIXME: return mc_current_state->executed_transition->random.value;*/
209   return 0;
210 }
211
212 /**
213  * \brief Schedules all the process that are ready to run
214  */
215 void MC_wait_for_requests(void)
216 {
217   smx_process_t process;
218   smx_simcall_t req;
219   unsigned int iter;
220
221   while (!xbt_dynar_is_empty(simix_global->process_to_run)) {
222     SIMIX_process_runall();
223     xbt_dynar_foreach(simix_global->process_that_ran, iter, process) {
224       req = &process->simcall;
225       if (req->call != SIMCALL_NONE && !MC_request_is_visible(req))
226         SIMIX_simcall_pre(req, 0);
227     }
228   }
229 }
230
231 int MC_deadlock_check()
232 {
233   int deadlock = FALSE;
234   smx_process_t process;
235   if(xbt_swag_size(simix_global->process_list)){
236     deadlock = TRUE;
237     xbt_swag_foreach(process, simix_global->process_list){
238       if(process->simcall.call != SIMCALL_NONE
239          && MC_request_is_enabled(&process->simcall)){
240         deadlock = FALSE;
241         break;
242       }
243     }
244   }
245   return deadlock;
246 }
247
248 /**
249  * \brief Re-executes from the state at position start all the transitions indicated by
250  *        a given model-checker stack.
251  * \param stack The stack with the transitions to execute.
252  * \param start Start index to begin the re-execution.
253  */
254 void MC_replay(xbt_fifo_t stack, int start)
255 {
256   raw_mem_set = (mmalloc_get_current_heap() == raw_heap);
257
258   int value, i = 1;
259   char *req_str;
260   smx_simcall_t req = NULL, saved_req = NULL;
261   xbt_fifo_item_t item, start_item;
262   mc_state_t state;
263
264   XBT_DEBUG("**** Begin Replay ****");
265
266   if(start == -1){
267     /* Restore the initial state */
268     MC_restore_snapshot(initial_snapshot);
269     /* At the moment of taking the snapshot the raw heap was set, so restoring
270      * it will set it back again, we have to unset it to continue  */
271     MC_UNSET_RAW_MEM;
272   }
273
274   start_item = xbt_fifo_get_last_item(stack);
275   if(start != -1){
276     while (i != start){
277       start_item = xbt_fifo_get_prev_item(start_item);
278       i++;
279     }
280   }
281
282   /* Traverse the stack from the state at position start and re-execute the transitions */
283   for (item = start_item;
284        item != xbt_fifo_get_first_item(stack);
285        item = xbt_fifo_get_prev_item(item)) {
286
287     state = (mc_state_t) xbt_fifo_get_item_content(item);
288     saved_req = MC_state_get_executed_request(state, &value);
289    
290     if(saved_req){
291       /* because we got a copy of the executed request, we have to fetch the  
292          real one, pointed by the request field of the issuer process */
293       req = &saved_req->issuer->simcall;
294
295       /* Debug information */
296       if(XBT_LOG_ISENABLED(mc_global, xbt_log_priority_debug)){
297         req_str = MC_request_to_string(req, value);
298         XBT_DEBUG("Replay: %s (%p)", req_str, state);
299         xbt_free(req_str);
300       }
301     }
302          
303     SIMIX_simcall_pre(req, value);
304     MC_wait_for_requests();
305          
306     /* Update statistics */
307     mc_stats->visited_states++;
308     mc_stats->executed_transitions++;
309   }
310   XBT_DEBUG("**** End Replay ****");
311
312   if(raw_mem_set)
313     MC_SET_RAW_MEM;
314   else
315     MC_UNSET_RAW_MEM;
316   
317
318 }
319
320 void MC_replay_liveness(xbt_fifo_t stack, int all_stack)
321 {
322
323   raw_mem_set = (mmalloc_get_current_heap() == raw_heap);
324
325   int value;
326   char *req_str;
327   smx_simcall_t req = NULL, saved_req = NULL;
328   xbt_fifo_item_t item;
329   mc_state_t state;
330   mc_pair_stateless_t pair;
331   int depth = 1;
332
333   XBT_DEBUG("**** Begin Replay ****");
334
335   /* Restore the initial state */
336   MC_restore_snapshot(initial_snapshot_liveness);
337   /* At the moment of taking the snapshot the raw heap was set, so restoring
338    * it will set it back again, we have to unset it to continue  */
339   MC_UNSET_RAW_MEM;
340
341   if(all_stack){
342
343     item = xbt_fifo_get_last_item(stack);
344
345     while(depth <= xbt_fifo_size(stack)){
346
347       pair = (mc_pair_stateless_t) xbt_fifo_get_item_content(item);
348       state = (mc_state_t) pair->graph_state;
349
350       if(pair->requests > 0){
351    
352         saved_req = MC_state_get_executed_request(state, &value);
353         //XBT_DEBUG("SavedReq->call %u", saved_req->call);
354       
355         if(saved_req != NULL){
356           /* because we got a copy of the executed request, we have to fetch the  
357              real one, pointed by the request field of the issuer process */
358           req = &saved_req->issuer->simcall;
359           //XBT_DEBUG("Req->call %u", req->call);
360   
361           /* Debug information */
362           if(XBT_LOG_ISENABLED(mc_global, xbt_log_priority_debug)){
363             req_str = MC_request_to_string(req, value);
364             XBT_DEBUG("Replay (depth = %d) : %s (%p)", depth, req_str, state);
365             xbt_free(req_str);
366           }
367   
368         }
369  
370         SIMIX_simcall_pre(req, value);
371         MC_wait_for_requests();
372       }
373
374       depth++;
375     
376       /* Update statistics */
377       mc_stats_pair->visited_pairs++;
378
379       item = xbt_fifo_get_prev_item(item);
380     }
381
382   }else{
383
384     /* Traverse the stack from the initial state and re-execute the transitions */
385     for (item = xbt_fifo_get_last_item(stack);
386          item != xbt_fifo_get_first_item(stack);
387          item = xbt_fifo_get_prev_item(item)) {
388
389       pair = (mc_pair_stateless_t) xbt_fifo_get_item_content(item);
390       state = (mc_state_t) pair->graph_state;
391
392       if(pair->requests > 0){
393    
394         saved_req = MC_state_get_executed_request(state, &value);
395         //XBT_DEBUG("SavedReq->call %u", saved_req->call);
396       
397         if(saved_req != NULL){
398           /* because we got a copy of the executed request, we have to fetch the  
399              real one, pointed by the request field of the issuer process */
400           req = &saved_req->issuer->simcall;
401           //XBT_DEBUG("Req->call %u", req->call);
402   
403           /* Debug information */
404           if(XBT_LOG_ISENABLED(mc_global, xbt_log_priority_debug)){
405             req_str = MC_request_to_string(req, value);
406             XBT_DEBUG("Replay (depth = %d) : %s (%p)", depth, req_str, state);
407             xbt_free(req_str);
408           }
409   
410         }
411  
412         SIMIX_simcall_pre(req, value);
413         MC_wait_for_requests();
414       }
415
416       depth++;
417     
418       /* Update statistics */
419       mc_stats_pair->visited_pairs++;
420     }
421   }  
422
423   XBT_DEBUG("**** End Replay ****");
424
425   if(raw_mem_set)
426     MC_SET_RAW_MEM;
427   else
428     MC_UNSET_RAW_MEM;
429   
430 }
431
432 /**
433  * \brief Dumps the contents of a model-checker's stack and shows the actual
434  *        execution trace
435  * \param stack The stack to dump
436  */
437 void MC_dump_stack_safety(xbt_fifo_t stack)
438 {
439   
440   raw_mem_set = (mmalloc_get_current_heap() == raw_heap);
441
442   MC_show_stack_safety(stack);
443
444   if(!_surf_mc_checkpoint){
445
446     mc_state_t state;
447
448     MC_SET_RAW_MEM;
449     while ((state = (mc_state_t) xbt_fifo_pop(stack)) != NULL)
450       MC_state_delete(state);
451     MC_UNSET_RAW_MEM;
452
453   }
454
455   if(raw_mem_set)
456     MC_SET_RAW_MEM;
457   else
458     MC_UNSET_RAW_MEM;
459   
460 }
461
462
463 void MC_show_stack_safety(xbt_fifo_t stack)
464 {
465   int value;
466   mc_state_t state;
467   xbt_fifo_item_t item;
468   smx_simcall_t req;
469   char *req_str = NULL;
470   
471   for (item = xbt_fifo_get_last_item(stack);
472        (item ? (state = (mc_state_t) (xbt_fifo_get_item_content(item)))
473         : (NULL)); item = xbt_fifo_get_prev_item(item)) {
474     req = MC_state_get_executed_request(state, &value);
475     if(req){
476       req_str = MC_request_to_string(req, value);
477       XBT_INFO("%s", req_str);
478       xbt_free(req_str);
479     }
480   }
481 }
482
483 void MC_show_deadlock(smx_simcall_t req)
484 {
485   /*char *req_str = NULL;*/
486   XBT_INFO("**************************");
487   XBT_INFO("*** DEAD-LOCK DETECTED ***");
488   XBT_INFO("**************************");
489   XBT_INFO("Locked request:");
490   /*req_str = MC_request_to_string(req);
491     XBT_INFO("%s", req_str);
492     xbt_free(req_str);*/
493   XBT_INFO("Counter-example execution trace:");
494   MC_dump_stack_safety(mc_stack_safety);
495 }
496
497
498 void MC_show_stack_liveness(xbt_fifo_t stack){
499   int value;
500   mc_pair_stateless_t pair;
501   xbt_fifo_item_t item;
502   smx_simcall_t req;
503   char *req_str = NULL;
504   
505   for (item = xbt_fifo_get_last_item(stack);
506        (item ? (pair = (mc_pair_stateless_t) (xbt_fifo_get_item_content(item)))
507         : (NULL)); item = xbt_fifo_get_prev_item(item)) {
508     req = MC_state_get_executed_request(pair->graph_state, &value);
509     if(req){
510       if(pair->requests>0){
511         req_str = MC_request_to_string(req, value);
512         XBT_INFO("%s", req_str);
513         xbt_free(req_str);
514       }else{
515         XBT_INFO("End of system requests but evolution in Büchi automaton");
516       }
517     }
518   }
519 }
520
521 void MC_dump_stack_liveness(xbt_fifo_t stack){
522
523   raw_mem_set = (mmalloc_get_current_heap() == raw_heap);
524
525   mc_pair_stateless_t pair;
526
527   MC_SET_RAW_MEM;
528   while ((pair = (mc_pair_stateless_t) xbt_fifo_pop(stack)) != NULL)
529     MC_pair_stateless_delete(pair);
530   MC_UNSET_RAW_MEM;
531
532   if(raw_mem_set)
533     MC_SET_RAW_MEM;
534   else
535     MC_UNSET_RAW_MEM;
536
537 }
538
539
540 void MC_print_statistics(mc_stats_t stats)
541 {
542   //XBT_INFO("State space size ~= %lu", stats->state_size);
543   XBT_INFO("Expanded states = %lu", stats->expanded_states);
544   XBT_INFO("Visited states = %lu", stats->visited_states);
545   XBT_INFO("Executed transitions = %lu", stats->executed_transitions);
546   XBT_INFO("Expanded / Visited = %lf",
547            (double) stats->visited_states / stats->expanded_states);
548   /*XBT_INFO("Exploration coverage = %lf",
549     (double)stats->expanded_states / stats->state_size); */
550 }
551
552 void MC_print_statistics_pairs(mc_stats_pair_t stats)
553 {
554   XBT_INFO("Expanded pairs = %lu", stats->expanded_pairs);
555   XBT_INFO("Visited pairs = %lu", stats->visited_pairs);
556   //XBT_INFO("Executed transitions = %lu", stats->executed_transitions);
557   XBT_INFO("Expanded / Visited = %lf",
558            (double) stats->visited_pairs / stats->expanded_pairs);
559   /*XBT_INFO("Exploration coverage = %lf",
560     (double)stats->expanded_states / stats->state_size); */
561 }
562
563 void MC_assert(int prop)
564 {
565   if (MC_IS_ENABLED && !prop){
566     XBT_INFO("**************************");
567     XBT_INFO("*** PROPERTY NOT VALID ***");
568     XBT_INFO("**************************");
569     XBT_INFO("Counter-example execution trace:");
570     MC_dump_stack_safety(mc_stack_safety);
571     MC_print_statistics(mc_stats);
572     xbt_abort();
573   }
574 }
575
576 static void MC_assert_pair(int prop){
577   if (MC_IS_ENABLED && !prop) {
578     XBT_INFO("**************************");
579     XBT_INFO("*** PROPERTY NOT VALID ***");
580     XBT_INFO("**************************");
581     //XBT_INFO("Counter-example execution trace:");
582     MC_show_stack_liveness(mc_stack_liveness);
583     //MC_dump_snapshot_stack(mc_snapshot_stack);
584     MC_print_statistics_pairs(mc_stats_pair);
585     xbt_abort();
586   }
587 }
588
589 void MC_process_clock_add(smx_process_t process, double amount)
590 {
591   mc_time[process->pid] += amount;
592 }
593
594 double MC_process_clock_get(smx_process_t process)
595 {
596   if(mc_time)
597     return mc_time[process->pid];
598   else
599     return 0;
600 }
601
602 void MC_automaton_load(const char *file){
603
604   raw_mem_set = (mmalloc_get_current_heap() == raw_heap);
605
606   MC_SET_RAW_MEM;
607
608   if (_mc_property_automaton == NULL)
609     _mc_property_automaton = xbt_automaton_new();
610   
611   xbt_automaton_load(_mc_property_automaton,file);
612
613   MC_UNSET_RAW_MEM;
614
615   if(raw_mem_set)
616     MC_SET_RAW_MEM;
617   else
618     MC_UNSET_RAW_MEM;
619
620 }
621
622 void MC_automaton_new_propositional_symbol(const char* id, void* fct) {
623
624   raw_mem_set = (mmalloc_get_current_heap() == raw_heap);
625
626   MC_SET_RAW_MEM;
627
628   if (_mc_property_automaton == NULL)
629     _mc_property_automaton = xbt_automaton_new();
630
631   xbt_new_propositional_symbol(_mc_property_automaton,id,fct);
632
633   MC_UNSET_RAW_MEM;
634
635   if(raw_mem_set)
636     MC_SET_RAW_MEM;
637   else
638     MC_UNSET_RAW_MEM;
639   
640 }
641
642 void MC_ignore_init(){
643   MC_SET_RAW_MEM;
644   mmalloc_ignore = xbt_dynar_new(sizeof(mc_ignore_region_t), NULL);
645   MC_UNSET_RAW_MEM;
646 }
647
648 void MC_ignore(void *address, size_t size){
649
650   MC_SET_RAW_MEM;
651
652   mc_ignore_region_t region = NULL;
653   region = xbt_new0(s_mc_ignore_region_t, 1);
654   region->address = address;
655   region->size = size;
656
657   unsigned int cursor = 0;
658   mc_ignore_region_t current_region;
659   xbt_dynar_foreach(mmalloc_ignore, cursor, current_region){
660     if(current_region->address > address)
661       break;
662   }
663
664   xbt_dynar_insert_at(mmalloc_ignore, cursor, &region);
665
666   MC_UNSET_RAW_MEM;
667 }