Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
dd6761cee213ae29945c79b607bdcdd97b19a2c9
[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 start state 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;
192   char *req_str;
193   smx_simcall_t req = NULL, saved_req = NULL;
194   xbt_fifo_item_t item;
195   mc_state_t state;
196
197   XBT_DEBUG("**** Begin Replay ****");
198
199   /* Restore the initial state */
200   MC_restore_snapshot(initial_snapshot);
201   /* At the moment of taking the snapshot the raw heap was set, so restoring
202    * it will set it back again, we have to unset it to continue  */
203   MC_UNSET_RAW_MEM;
204
205   /* Traverse the stack from the initial state and re-execute the transitions */
206   for (item = xbt_fifo_get_last_item(stack);
207        item != xbt_fifo_get_first_item(stack);
208        item = xbt_fifo_get_prev_item(item)) {
209
210     state = (mc_state_t) xbt_fifo_get_item_content(item);
211     saved_req = MC_state_get_executed_request(state, &value);
212    
213     if(saved_req){
214       /* because we got a copy of the executed request, we have to fetch the  
215          real one, pointed by the request field of the issuer process */
216       req = &saved_req->issuer->simcall;
217
218       /* Debug information */
219       if(XBT_LOG_ISENABLED(mc_global, xbt_log_priority_debug)){
220         req_str = MC_request_to_string(req, value);
221         XBT_DEBUG("Replay: %s (%p)", req_str, state);
222         xbt_free(req_str);
223       }
224     }
225          
226     SIMIX_simcall_pre(req, value);
227     MC_wait_for_requests();
228          
229     /* Update statistics */
230     mc_stats->visited_states++;
231     mc_stats->executed_transitions++;
232   }
233   XBT_DEBUG("**** End Replay ****");
234 }
235
236 void MC_replay_liveness(xbt_fifo_t stack, int all_stack)
237 {
238   int value;
239   char *req_str;
240   smx_simcall_t req = NULL, saved_req = NULL;
241   xbt_fifo_item_t item;
242   mc_state_t state;
243   mc_pair_stateless_t pair;
244   int depth = 1;
245
246   XBT_DEBUG("**** Begin Replay ****");
247
248   /* Restore the initial state */
249   MC_restore_snapshot(initial_snapshot_liveness);
250   /* At the moment of taking the snapshot the raw heap was set, so restoring
251    * it will set it back again, we have to unset it to continue  */
252   MC_UNSET_RAW_MEM;
253
254   if(all_stack){
255
256     item = xbt_fifo_get_last_item(stack);
257
258     while(depth <= xbt_fifo_size(stack)){
259
260       pair = (mc_pair_stateless_t) xbt_fifo_get_item_content(item);
261       state = (mc_state_t) pair->graph_state;
262
263       if(pair->requests > 0){
264    
265         saved_req = MC_state_get_executed_request(state, &value);
266         //XBT_DEBUG("SavedReq->call %u", saved_req->call);
267       
268         if(saved_req != NULL){
269           /* because we got a copy of the executed request, we have to fetch the  
270              real one, pointed by the request field of the issuer process */
271           req = &saved_req->issuer->simcall;
272           //XBT_DEBUG("Req->call %u", req->call);
273   
274           /* Debug information */
275           if(XBT_LOG_ISENABLED(mc_global, xbt_log_priority_debug)){
276             req_str = MC_request_to_string(req, value);
277             XBT_DEBUG("Replay (depth = %d) : %s (%p)", depth, req_str, state);
278             xbt_free(req_str);
279           }
280   
281         }
282  
283         SIMIX_simcall_pre(req, value);
284         MC_wait_for_requests();
285       }
286
287       depth++;
288     
289       /* Update statistics */
290       mc_stats_pair->visited_pairs++;
291
292       item = xbt_fifo_get_prev_item(item);
293     }
294
295   }else{
296
297     /* Traverse the stack from the initial state and re-execute the transitions */
298     for (item = xbt_fifo_get_last_item(stack);
299          item != xbt_fifo_get_first_item(stack);
300          item = xbt_fifo_get_prev_item(item)) {
301
302       pair = (mc_pair_stateless_t) xbt_fifo_get_item_content(item);
303       state = (mc_state_t) pair->graph_state;
304
305       if(pair->requests > 0){
306    
307         saved_req = MC_state_get_executed_request(state, &value);
308         //XBT_DEBUG("SavedReq->call %u", saved_req->call);
309       
310         if(saved_req != NULL){
311           /* because we got a copy of the executed request, we have to fetch the  
312              real one, pointed by the request field of the issuer process */
313           req = &saved_req->issuer->simcall;
314           //XBT_DEBUG("Req->call %u", req->call);
315   
316           /* Debug information */
317           if(XBT_LOG_ISENABLED(mc_global, xbt_log_priority_debug)){
318             req_str = MC_request_to_string(req, value);
319             XBT_DEBUG("Replay (depth = %d) : %s (%p)", depth, req_str, state);
320             xbt_free(req_str);
321           }
322   
323         }
324  
325         SIMIX_simcall_pre(req, value);
326         MC_wait_for_requests();
327       }
328
329       depth++;
330     
331       /* Update statistics */
332       mc_stats_pair->visited_pairs++;
333     }
334   }  
335
336   XBT_DEBUG("**** End Replay ****");
337 }
338
339 /**
340  * \brief Dumps the contents of a model-checker's stack and shows the actual
341  *        execution trace
342  * \param stack The stack to dump
343  */
344 void MC_dump_stack_safety(xbt_fifo_t stack)
345 {
346
347   MC_show_stack_safety(stack);
348
349   if(!_surf_do_mc_checkpoint){
350
351     mc_state_t state;
352
353     MC_SET_RAW_MEM;
354     while ((state = (mc_state_t) xbt_fifo_pop(stack)) != NULL)
355       MC_state_delete(state);
356     MC_UNSET_RAW_MEM;
357
358   }
359 }
360
361
362 void MC_show_stack_safety(xbt_fifo_t stack)
363 {
364   int value;
365   mc_state_t state;
366   xbt_fifo_item_t item;
367   smx_simcall_t req;
368   char *req_str = NULL;
369   
370   for (item = xbt_fifo_get_last_item(stack);
371        (item ? (state = (mc_state_t) (xbt_fifo_get_item_content(item)))
372         : (NULL)); item = xbt_fifo_get_prev_item(item)) {
373     req = MC_state_get_executed_request(state, &value);
374     if(req){
375       req_str = MC_request_to_string(req, value);
376       XBT_INFO("%s", req_str);
377       xbt_free(req_str);
378     }
379   }
380 }
381
382 void MC_show_deadlock(smx_simcall_t req)
383 {
384   /*char *req_str = NULL;*/
385   XBT_INFO("**************************");
386   XBT_INFO("*** DEAD-LOCK DETECTED ***");
387   XBT_INFO("**************************");
388   XBT_INFO("Locked request:");
389   /*req_str = MC_request_to_string(req);
390     XBT_INFO("%s", req_str);
391     xbt_free(req_str);*/
392   XBT_INFO("Counter-example execution trace:");
393   MC_dump_stack_safety(mc_stack_safety);
394 }
395
396
397 void MC_show_stack_liveness(xbt_fifo_t stack){
398   int value;
399   mc_pair_stateless_t pair;
400   xbt_fifo_item_t item;
401   smx_simcall_t req;
402   char *req_str = NULL;
403   
404   for (item = xbt_fifo_get_last_item(stack);
405        (item ? (pair = (mc_pair_stateless_t) (xbt_fifo_get_item_content(item)))
406         : (NULL)); item = xbt_fifo_get_prev_item(item)) {
407     req = MC_state_get_executed_request(pair->graph_state, &value);
408     if(req){
409       if(pair->requests>0){
410         req_str = MC_request_to_string(req, value);
411         XBT_INFO("%s", req_str);
412         xbt_free(req_str);
413       }else{
414         XBT_INFO("End of system requests but evolution in Büchi automaton");
415       }
416     }
417   }
418 }
419
420 void MC_dump_stack_liveness(xbt_fifo_t stack){
421   mc_pair_stateless_t pair;
422
423   MC_SET_RAW_MEM;
424   while ((pair = (mc_pair_stateless_t) xbt_fifo_pop(stack)) != NULL)
425     MC_pair_stateless_delete(pair);
426   MC_UNSET_RAW_MEM;
427 }
428
429
430 void MC_print_statistics(mc_stats_t stats)
431 {
432   //XBT_INFO("State space size ~= %lu", stats->state_size);
433   XBT_INFO("Expanded states = %lu", stats->expanded_states);
434   XBT_INFO("Visited states = %lu", stats->visited_states);
435   XBT_INFO("Executed transitions = %lu", stats->executed_transitions);
436   XBT_INFO("Expanded / Visited = %lf",
437            (double) stats->visited_states / stats->expanded_states);
438   /*XBT_INFO("Exploration coverage = %lf",
439     (double)stats->expanded_states / stats->state_size); */
440 }
441
442 void MC_print_statistics_pairs(mc_stats_pair_t stats)
443 {
444   XBT_INFO("Expanded pairs = %lu", stats->expanded_pairs);
445   XBT_INFO("Visited pairs = %lu", stats->visited_pairs);
446   //XBT_INFO("Executed transitions = %lu", stats->executed_transitions);
447   XBT_INFO("Expanded / Visited = %lf",
448            (double) stats->visited_pairs / stats->expanded_pairs);
449   /*XBT_INFO("Exploration coverage = %lf",
450     (double)stats->expanded_states / stats->state_size); */
451 }
452
453 void MC_assert(int prop)
454 {
455   if (MC_IS_ENABLED && !prop){
456     XBT_INFO("**************************");
457     XBT_INFO("*** PROPERTY NOT VALID ***");
458     XBT_INFO("**************************");
459     XBT_INFO("Counter-example execution trace:");
460     MC_dump_stack_safety(mc_stack_safety);
461     MC_print_statistics(mc_stats);
462     xbt_abort();
463   }
464 }
465
466 static void MC_assert_pair(int prop){
467   if (MC_IS_ENABLED && !prop) {
468     XBT_INFO("**************************");
469     XBT_INFO("*** PROPERTY NOT VALID ***");
470     XBT_INFO("**************************");
471     //XBT_INFO("Counter-example execution trace:");
472     MC_show_stack_liveness(mc_stack_liveness);
473     //MC_dump_snapshot_stack(mc_snapshot_stack);
474     MC_print_statistics_pairs(mc_stats_pair);
475     xbt_abort();
476   }
477 }
478
479 void MC_process_clock_add(smx_process_t process, double amount)
480 {
481   mc_time[process->pid] += amount;
482 }
483
484 double MC_process_clock_get(smx_process_t process)
485 {
486   if(mc_time)
487     return mc_time[process->pid];
488   else
489     return 0;
490 }
491
492 void MC_diff(void){
493
494   mc_snapshot_t sn = xbt_new0(s_mc_snapshot_t, 1);
495   MC_take_snapshot_liveness(sn);
496
497   int i;
498
499   XBT_INFO("Number of regions : %u", sn->num_reg);
500
501   for(i=0; i<sn->num_reg; i++){
502     
503     switch(sn->regions[i]->type){
504     case 0: /* heap */
505       XBT_INFO("Size of heap : %zu", sn->regions[i]->size);
506       mmalloc_display_info_heap(sn->regions[i]->data);
507       break;
508     case 1 : /* libsimgrid */
509       XBT_INFO("Size of libsimgrid : %zu", sn->regions[i]->size);
510       break;
511     case 2 : /* data program */
512       XBT_INFO("Size of data program : %zu", sn->regions[i]->size);
513       break;
514     case 3 : /* stack */
515       XBT_INFO("Size of stack : %zu", sn->regions[i]->size);
516       XBT_INFO("Start addr of stack : %p", sn->regions[i]->start_addr);
517       break;
518     }
519
520   }
521
522 }
523
524 xbt_automaton_t MC_create_automaton(const char *file){
525   MC_SET_RAW_MEM;
526   xbt_automaton_t a = xbt_create_automaton(file);
527   MC_UNSET_RAW_MEM;
528   return a;
529 }