Logo AND Algorithmique Numérique Distribuée

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