Logo AND Algorithmique Numérique Distribuée

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