Logo AND Algorithmique Numérique Distribuée

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