Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[mc] Fix check on type name
[simgrid.git] / src / mc / mc_global.c
1 /* Copyright (c) 2008-2014. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 #include <unistd.h>
8 #include <sys/types.h>
9 #include <sys/wait.h>
10 #include <sys/time.h>
11 #include <sys/mman.h>
12 #include <libgen.h>
13
14 #include "simgrid/sg_config.h"
15 #include "../surf/surf_private.h"
16 #include "../simix/smx_private.h"
17 #include "../xbt/mmalloc/mmprivate.h"
18 #include "xbt/fifo.h"
19 #include "mc_private.h"
20 #include "xbt/automaton.h"
21 #include "xbt/dict.h"
22
23 XBT_LOG_NEW_CATEGORY(mc, "All MC categories");
24 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_global, mc,
25                                 "Logging specific to MC (global)");
26
27 /* Configuration support */
28 e_mc_reduce_t mc_reduce_kind = e_mc_reduce_unset;
29
30 int _sg_do_model_check = 0;
31 int _sg_mc_checkpoint = 0;
32 char *_sg_mc_property_file = NULL;
33 int _sg_mc_timeout = 0;
34 int _sg_mc_hash = 0;
35 int _sg_mc_max_depth = 1000;
36 int _sg_mc_visited = 0;
37 char *_sg_mc_dot_output_file = NULL;
38 int _sg_mc_comms_determinism = 0;
39 int _sg_mc_send_determinism = 0;
40 int _sg_mc_safety = 0;
41 int _sg_mc_liveness = 0;
42
43 int user_max_depth_reached = 0;
44
45 void _mc_cfg_cb_reduce(const char *name, int pos)
46 {
47   if (_sg_cfg_init_status && !_sg_do_model_check) {
48     xbt_die
49         ("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.");
50   }
51   char *val = xbt_cfg_get_string(_sg_cfg_set, name);
52   if (!strcasecmp(val, "none")) {
53     mc_reduce_kind = e_mc_reduce_none;
54   } else if (!strcasecmp(val, "dpor")) {
55     mc_reduce_kind = e_mc_reduce_dpor;
56   } else {
57     xbt_die("configuration option %s can only take 'none' or 'dpor' as a value",
58             name);
59   }
60 }
61
62 void _mc_cfg_cb_checkpoint(const char *name, int pos)
63 {
64   if (_sg_cfg_init_status && !_sg_do_model_check) {
65     xbt_die
66         ("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.");
67   }
68   _sg_mc_checkpoint = xbt_cfg_get_int(_sg_cfg_set, name);
69 }
70
71 void _mc_cfg_cb_property(const char *name, int pos)
72 {
73   if (_sg_cfg_init_status && !_sg_do_model_check) {
74     xbt_die
75         ("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.");
76   }
77   _sg_mc_property_file = xbt_cfg_get_string(_sg_cfg_set, name);
78 }
79
80 void _mc_cfg_cb_timeout(const char *name, int pos)
81 {
82   if (_sg_cfg_init_status && !_sg_do_model_check) {
83     xbt_die
84         ("You are specifying a value to enable/disable timeout for wait requests 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.");
85   }
86   _sg_mc_timeout = xbt_cfg_get_boolean(_sg_cfg_set, name);
87 }
88
89 void _mc_cfg_cb_hash(const char *name, int pos)
90 {
91   if (_sg_cfg_init_status && !_sg_do_model_check) {
92     xbt_die
93         ("You are specifying a value to enable/disable the use of global hash to speedup state comparaison, but model-checking was not activated at config time (through --cfg=model-check:1). This won't work, sorry.");
94   }
95   _sg_mc_hash = xbt_cfg_get_boolean(_sg_cfg_set, name);
96 }
97
98 void _mc_cfg_cb_max_depth(const char *name, int pos)
99 {
100   if (_sg_cfg_init_status && !_sg_do_model_check) {
101     xbt_die
102         ("You are specifying a max depth 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.");
103   }
104   _sg_mc_max_depth = xbt_cfg_get_int(_sg_cfg_set, name);
105 }
106
107 void _mc_cfg_cb_visited(const char *name, int pos)
108 {
109   if (_sg_cfg_init_status && !_sg_do_model_check) {
110     xbt_die
111         ("You are specifying a number of stored visited states 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.");
112   }
113   _sg_mc_visited = xbt_cfg_get_int(_sg_cfg_set, name);
114 }
115
116 void _mc_cfg_cb_dot_output(const char *name, int pos)
117 {
118   if (_sg_cfg_init_status && !_sg_do_model_check) {
119     xbt_die
120         ("You are specifying a file name for a dot output of graph state 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.");
121   }
122   _sg_mc_dot_output_file = xbt_cfg_get_string(_sg_cfg_set, name);
123 }
124
125 void _mc_cfg_cb_comms_determinism(const char *name, int pos)
126 {
127   if (_sg_cfg_init_status && !_sg_do_model_check) {
128     xbt_die
129         ("You are specifying a value to enable/disable the detection of determinism in the communications schemes 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.");
130   }
131   _sg_mc_comms_determinism = xbt_cfg_get_boolean(_sg_cfg_set, name);
132 }
133
134 void _mc_cfg_cb_send_determinism(const char *name, int pos)
135 {
136   if (_sg_cfg_init_status && !_sg_do_model_check) {
137     xbt_die
138         ("You are specifying a value to enable/disable the detection of send-determinism in the communications schemes 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.");
139   }
140   _sg_mc_send_determinism = xbt_cfg_get_boolean(_sg_cfg_set, name);
141 }
142
143 /* MC global data structures */
144 mc_state_t mc_current_state = NULL;
145 char mc_replay_mode = FALSE;
146 double *mc_time = NULL;
147 __thread mc_comparison_times_t mc_comp_times = NULL;
148 __thread double mc_snapshot_comparison_time;
149 mc_stats_t mc_stats = NULL;
150 mc_global_t initial_global_state = NULL;
151 xbt_fifo_t mc_stack = NULL;
152
153 /* Liveness */
154 xbt_automaton_t _mc_property_automaton = NULL;
155
156 /* Variables */
157 mc_object_info_t mc_libsimgrid_info = NULL;
158 mc_object_info_t mc_binary_info = NULL;
159
160 mc_object_info_t mc_object_infos[2] = { NULL, NULL };
161
162 size_t mc_object_infos_size = 2;
163
164 /* Dot output */
165 FILE *dot_output = NULL;
166 const char *colors[13];
167
168
169 /*******************************  Initialisation of MC *******************************/
170 /*********************************************************************************/
171
172 static void MC_init_dot_output()
173 {                               /* FIXME : more colors */
174
175   colors[0] = "blue";
176   colors[1] = "red";
177   colors[2] = "green3";
178   colors[3] = "goldenrod";
179   colors[4] = "brown";
180   colors[5] = "purple";
181   colors[6] = "magenta";
182   colors[7] = "turquoise4";
183   colors[8] = "gray25";
184   colors[9] = "forestgreen";
185   colors[10] = "hotpink";
186   colors[11] = "lightblue";
187   colors[12] = "tan";
188
189   dot_output = fopen(_sg_mc_dot_output_file, "w");
190
191   if (dot_output == NULL) {
192     perror("Error open dot output file");
193     xbt_abort();
194   }
195
196   fprintf(dot_output,
197           "digraph graphname{\n fixedsize=true; rankdir=TB; ranksep=.25; edge [fontsize=12]; node [fontsize=10, shape=circle,width=.5 ]; graph [resolution=20, fontsize=10];\n");
198
199 }
200
201 static void MC_init_debug_info(void)
202 {
203   XBT_INFO("Get debug information ...");
204
205   memory_map_t maps = MC_get_memory_map();
206
207   /* Get local variables for state equality detection */
208   mc_binary_info = MC_find_object_info(maps, xbt_binary_name, 1);
209   mc_object_infos[0] = mc_binary_info;
210
211   mc_libsimgrid_info = MC_find_object_info(maps, libsimgrid_path, 0);
212   mc_object_infos[1] = mc_libsimgrid_info;
213
214   // Use information of the other objects:
215   MC_post_process_object_info(mc_binary_info);
216   MC_post_process_object_info(mc_libsimgrid_info);
217
218   MC_free_memory_map(maps);
219   XBT_INFO("Get debug information done !");
220 }
221
222
223 void MC_init()
224 {
225
226   int raw_mem_set = (mmalloc_get_current_heap() == mc_heap);
227
228   mc_time = xbt_new0(double, simix_process_maxpid);
229
230   /* Initialize the data structures that must be persistent across every
231      iteration of the model-checker (in RAW memory) */
232
233   MC_SET_MC_HEAP;
234
235   mc_comp_times = xbt_new0(s_mc_comparison_times_t, 1);
236
237   /* Initialize statistics */
238   mc_stats = xbt_new0(s_mc_stats_t, 1);
239   mc_stats->state_size = 1;
240
241   MC_init_memory_map_info();
242   MC_init_debug_info();         /* FIXME : get debug information only if liveness verification or visited state reduction */
243
244   if ((_sg_mc_dot_output_file != NULL) && (_sg_mc_dot_output_file[0] != '\0'))
245     MC_init_dot_output();
246
247   /* Init parmap */
248   //parmap = xbt_parmap_mc_new(xbt_os_get_numcores(), XBT_PARMAP_DEFAULT);
249
250   MC_SET_STD_HEAP;
251
252   if (_sg_mc_visited > 0 || _sg_mc_liveness) {
253     /* Ignore some variables from xbt/ex.h used by exception e for stacks comparison */
254     MC_ignore_local_variable("e", "*");
255     MC_ignore_local_variable("__ex_cleanup", "*");
256     MC_ignore_local_variable("__ex_mctx_en", "*");
257     MC_ignore_local_variable("__ex_mctx_me", "*");
258     MC_ignore_local_variable("__xbt_ex_ctx_ptr", "*");
259     MC_ignore_local_variable("_log_ev", "*");
260     MC_ignore_local_variable("_throw_ctx", "*");
261     MC_ignore_local_variable("ctx", "*");
262
263     MC_ignore_local_variable("self", "simcall_BODY_mc_snapshot");
264     MC_ignore_local_variable("next_context", "smx_ctx_sysv_suspend_serial");
265     MC_ignore_local_variable("i", "smx_ctx_sysv_suspend_serial");
266
267     /* Ignore local variable about time used for tracing */
268     MC_ignore_local_variable("start_time", "*");
269
270     MC_ignore_global_variable("compared_pointers");
271     MC_ignore_global_variable("mc_comp_times");
272     MC_ignore_global_variable("mc_snapshot_comparison_time");
273     MC_ignore_global_variable("mc_time");
274     MC_ignore_global_variable("smpi_current_rank");
275     MC_ignore_global_variable("counter");       /* Static variable used for tracing */
276     MC_ignore_global_variable("maestro_stack_start");
277     MC_ignore_global_variable("maestro_stack_end");
278     MC_ignore_global_variable("smx_total_comms");
279
280     MC_ignore_heap(mc_time, simix_process_maxpid * sizeof(double));
281
282     smx_process_t process;
283     xbt_swag_foreach(process, simix_global->process_list) {
284       MC_ignore_heap(&(process->process_hookup),
285                      sizeof(process->process_hookup));
286                      }
287   }
288
289   if (raw_mem_set)
290     MC_SET_MC_HEAP;
291
292 }
293
294 /*******************************  Core of MC *******************************/
295 /**************************************************************************/
296
297 static void MC_modelcheck_comm_determinism_init(void)
298 {
299
300   int mc_mem_set = (mmalloc_get_current_heap() == mc_heap);
301
302   MC_init();
303
304   if (!mc_mem_set)
305     MC_SET_MC_HEAP;
306
307   /* Create exploration stack */
308   mc_stack = xbt_fifo_new();
309
310   MC_SET_STD_HEAP;
311
312   MC_pre_modelcheck_comm_determinism();
313
314   MC_SET_MC_HEAP;
315   initial_global_state = xbt_new0(s_mc_global_t, 1);
316   initial_global_state->snapshot = MC_take_snapshot(0);
317   initial_global_state->initial_communications_pattern_done = 0;
318   initial_global_state->comm_deterministic = 1;
319   initial_global_state->send_deterministic = 1;
320   MC_SET_STD_HEAP;
321
322   MC_modelcheck_comm_determinism();
323
324   if(mc_mem_set)
325     MC_SET_MC_HEAP;
326
327 }
328
329 static void MC_modelcheck_safety_init(void)
330 {
331   int mc_mem_set = (mmalloc_get_current_heap() == mc_heap);
332
333   _sg_mc_safety = 1;
334
335   MC_init();
336
337   if (!mc_mem_set)
338     MC_SET_MC_HEAP;
339
340   /* Create exploration stack */
341   mc_stack = xbt_fifo_new();
342
343   MC_SET_STD_HEAP;
344
345   MC_pre_modelcheck_safety();
346
347   MC_SET_MC_HEAP;
348   /* Save the initial state */
349   initial_global_state = xbt_new0(s_mc_global_t, 1);
350   initial_global_state->snapshot = MC_take_snapshot(0);
351   MC_SET_STD_HEAP;
352
353   MC_modelcheck_safety();
354
355   if (mc_mem_set)
356     MC_SET_MC_HEAP;
357
358   xbt_abort();
359   //MC_exit();
360 }
361
362 static void MC_modelcheck_liveness_init()
363 {
364
365   int mc_mem_set = (mmalloc_get_current_heap() == mc_heap);
366
367   _sg_mc_liveness = 1;
368
369   MC_init();
370
371   if (!mc_mem_set)
372     MC_SET_MC_HEAP;
373
374   /* Create exploration stack */
375   mc_stack = xbt_fifo_new();
376
377   /* Create the initial state */
378   initial_global_state = xbt_new0(s_mc_global_t, 1);
379
380   MC_SET_STD_HEAP;
381
382   MC_pre_modelcheck_liveness();
383
384   /* We're done */
385   MC_print_statistics(mc_stats);
386   xbt_free(mc_time);
387
388   if (mc_mem_set)
389     MC_SET_MC_HEAP;
390
391 }
392
393 void MC_do_the_modelcheck_for_real()
394 {
395
396   if (_sg_mc_comms_determinism || _sg_mc_send_determinism) {
397     XBT_INFO("Check communication determinism");
398     MC_modelcheck_comm_determinism_init();
399   } else if (!_sg_mc_property_file || _sg_mc_property_file[0] == '\0') {
400     if (mc_reduce_kind == e_mc_reduce_unset)
401       mc_reduce_kind = e_mc_reduce_dpor;
402     XBT_INFO("Check a safety property");
403     MC_modelcheck_safety_init();
404   } else {
405     if (mc_reduce_kind == e_mc_reduce_unset)
406       mc_reduce_kind = e_mc_reduce_none;
407     XBT_INFO("Check the liveness property %s", _sg_mc_property_file);
408     MC_automaton_load(_sg_mc_property_file);
409     MC_modelcheck_liveness_init();
410   }
411 }
412
413
414 void MC_exit(void)
415 {
416   xbt_free(mc_time);
417
418   MC_memory_exit();
419   //xbt_abort();
420 }
421
422 int SIMIX_pre_mc_random(smx_simcall_t simcall, int min, int max)
423 {
424
425   return simcall->mc_value;
426 }
427
428
429 int MC_random(int min, int max)
430 {
431   /*FIXME: return mc_current_state->executed_transition->random.value; */
432   return simcall_mc_random(min, max);
433 }
434
435 /**
436  * \brief Schedules all the process that are ready to run
437  */
438 void MC_wait_for_requests(void)
439 {
440   smx_process_t process;
441   smx_simcall_t req;
442   unsigned int iter;
443
444   while (!xbt_dynar_is_empty(simix_global->process_to_run)) {
445     SIMIX_process_runall();
446     xbt_dynar_foreach(simix_global->process_that_ran, iter, process) {
447       req = &process->simcall;
448       if (req->call != SIMCALL_NONE && !MC_request_is_visible(req))
449         SIMIX_simcall_pre(req, 0);
450     }
451   }
452 }
453
454 int MC_deadlock_check()
455 {
456   int deadlock = FALSE;
457   smx_process_t process;
458   if (xbt_swag_size(simix_global->process_list)) {
459     deadlock = TRUE;
460     xbt_swag_foreach(process, simix_global->process_list) {
461       if (process->simcall.call != SIMCALL_NONE
462           && MC_request_is_enabled(&process->simcall)) {
463         deadlock = FALSE;
464         break;
465       }
466     }
467   }
468   return deadlock;
469 }
470
471 /**
472  * \brief Re-executes from the state at position start all the transitions indicated by
473  *        a given model-checker stack.
474  * \param stack The stack with the transitions to execute.
475  * \param start Start index to begin the re-execution.
476  */
477 void MC_replay(xbt_fifo_t stack, int start)
478 {
479   int raw_mem = (mmalloc_get_current_heap() == mc_heap);
480
481   int value, i = 1, count = 1;
482   char *req_str;
483   smx_simcall_t req = NULL, saved_req = NULL;
484   xbt_fifo_item_t item, start_item;
485   mc_state_t state;
486   smx_process_t process = NULL;
487   int comm_pattern = 0;
488   smx_action_t current_comm;
489
490   XBT_DEBUG("**** Begin Replay ****");
491
492   if (start == -1) {
493     /* Restore the initial state */
494     MC_restore_snapshot(initial_global_state->snapshot);
495     /* At the moment of taking the snapshot the raw heap was set, so restoring
496      * it will set it back again, we have to unset it to continue  */
497     MC_SET_STD_HEAP;
498   }
499
500   start_item = xbt_fifo_get_last_item(stack);
501   if (start != -1) {
502     while (i != start) {
503       start_item = xbt_fifo_get_prev_item(start_item);
504       i++;
505     }
506   }
507
508   MC_SET_MC_HEAP;
509
510   if ((mc_reduce_kind == e_mc_reduce_dpor) && !_sg_mc_comms_determinism && !_sg_mc_send_determinism) {
511     xbt_dict_reset(first_enabled_state);
512     xbt_swag_foreach(process, simix_global->process_list) {
513       if (MC_process_is_enabled(process)) {
514         char *key = bprintf("%lu", process->pid);
515         char *data = bprintf("%d", count);
516         xbt_dict_set(first_enabled_state, key, data, NULL);
517         xbt_free(key);
518       }
519     }
520   }
521
522   if (_sg_mc_comms_determinism || _sg_mc_send_determinism) {
523     xbt_dynar_reset(communications_pattern);
524     xbt_dynar_reset(incomplete_communications_pattern);
525   }
526
527   MC_SET_STD_HEAP;
528
529
530   /* Traverse the stack from the state at position start and re-execute the transitions */
531   for (item = start_item;
532        item != xbt_fifo_get_first_item(stack);
533        item = xbt_fifo_get_prev_item(item)) {
534
535     state = (mc_state_t) xbt_fifo_get_item_content(item);
536     saved_req = MC_state_get_executed_request(state, &value);
537
538     if ((mc_reduce_kind == e_mc_reduce_dpor) && !_sg_mc_comms_determinism && !_sg_mc_send_determinism) {
539       MC_SET_MC_HEAP;
540       char *key = bprintf("%lu", saved_req->issuer->pid);
541       xbt_dict_remove(first_enabled_state, key);
542       xbt_free(key);
543       MC_SET_STD_HEAP;
544     }
545
546     if (saved_req) {
547       /* because we got a copy of the executed request, we have to fetch the  
548          real one, pointed by the request field of the issuer process */
549       req = &saved_req->issuer->simcall;
550
551       /* Debug information */
552       if (XBT_LOG_ISENABLED(mc_global, xbt_log_priority_debug)) {
553         req_str = MC_request_to_string(req, value);
554         XBT_DEBUG("Replay: %s (%p)", req_str, state);
555         xbt_free(req_str);
556       }
557
558       /* TODO : handle test and testany simcalls */
559       if (_sg_mc_comms_determinism || _sg_mc_send_determinism) {
560         if (req->call == SIMCALL_COMM_ISEND)
561           comm_pattern = 1;
562         else if (req->call == SIMCALL_COMM_IRECV)
563           comm_pattern = 2;
564         else if (req->call == SIMCALL_COMM_WAIT)
565           comm_pattern = 3;
566         else if (req->call == SIMCALL_COMM_WAITANY)
567           comm_pattern = 4;
568       }
569
570       SIMIX_simcall_pre(req, value);
571
572       if (_sg_mc_comms_determinism || _sg_mc_send_determinism) {
573         MC_SET_MC_HEAP;
574         if (comm_pattern == 1 || comm_pattern == 2) {
575           get_comm_pattern(communications_pattern, req, comm_pattern);
576         } else if (comm_pattern == 3 /* || comm_pattern == 4 */ ) {
577           current_comm = simcall_comm_wait__get__comm(req);
578           if (current_comm->comm.refcount == 1) {       /* First wait only must be considered */
579             complete_comm_pattern(communications_pattern, current_comm);
580           }
581         } else if (comm_pattern == 4 /* || comm_pattern == 4 */ ) {
582           current_comm =
583               xbt_dynar_get_as(simcall_comm_waitany__get__comms(req), value,
584                                smx_action_t);
585           if (current_comm->comm.refcount == 1) {       /* First wait only must be considered */
586             complete_comm_pattern(communications_pattern, current_comm);
587           }
588         }
589         MC_SET_STD_HEAP;
590         comm_pattern = 0;
591       }
592
593
594       MC_wait_for_requests();
595
596       count++;
597
598       if ((mc_reduce_kind == e_mc_reduce_dpor) && !_sg_mc_comms_determinism && !_sg_mc_send_determinism) {
599         MC_SET_MC_HEAP;
600         /* Insert in dict all enabled processes */
601         xbt_swag_foreach(process, simix_global->process_list) {
602           if (MC_process_is_enabled(process) ) {
603             char *key = bprintf("%lu", process->pid);
604             if (xbt_dict_get_or_null(first_enabled_state, key) == NULL) {
605               char *data = bprintf("%d", count);
606               xbt_dict_set(first_enabled_state, key, data, NULL);
607             }
608             xbt_free(key);
609           }
610         }
611         MC_SET_STD_HEAP;
612       }
613     }
614
615     /* Update statistics */
616     mc_stats->visited_states++;
617     mc_stats->executed_transitions++;
618
619   }
620
621   XBT_DEBUG("**** End Replay ****");
622
623   if (raw_mem)
624     MC_SET_MC_HEAP;
625   else
626     MC_SET_STD_HEAP;
627
628
629 }
630
631 void MC_replay_liveness(xbt_fifo_t stack, int all_stack)
632 {
633
634   initial_global_state->raw_mem_set = (mmalloc_get_current_heap() == mc_heap);
635
636   int value;
637   char *req_str;
638   smx_simcall_t req = NULL, saved_req = NULL;
639   xbt_fifo_item_t item;
640   mc_state_t state;
641   mc_pair_t pair;
642   int depth = 1;
643
644   XBT_DEBUG("**** Begin Replay ****");
645
646   /* Restore the initial state */
647   MC_restore_snapshot(initial_global_state->snapshot);
648
649   /* At the moment of taking the snapshot the raw heap was set, so restoring
650    * it will set it back again, we have to unset it to continue  */
651   if (!initial_global_state->raw_mem_set)
652     MC_SET_STD_HEAP;
653
654   if (all_stack) {
655
656     item = xbt_fifo_get_last_item(stack);
657
658     while (depth <= xbt_fifo_size(stack)) {
659
660       pair = (mc_pair_t) xbt_fifo_get_item_content(item);
661       state = (mc_state_t) pair->graph_state;
662
663       if (pair->requests > 0) {
664
665         saved_req = MC_state_get_executed_request(state, &value);
666         //XBT_DEBUG("SavedReq->call %u", saved_req->call);
667
668         if (saved_req != NULL) {
669           /* because we got a copy of the executed request, we have to fetch the  
670              real one, pointed by the request field of the issuer process */
671           req = &saved_req->issuer->simcall;
672           //XBT_DEBUG("Req->call %u", req->call);
673
674           /* Debug information */
675           if (XBT_LOG_ISENABLED(mc_global, xbt_log_priority_debug)) {
676             req_str = MC_request_to_string(req, value);
677             XBT_DEBUG("Replay (depth = %d) : %s (%p)", depth, req_str, state);
678             xbt_free(req_str);
679           }
680
681         }
682
683         SIMIX_simcall_pre(req, value);
684         MC_wait_for_requests();
685       }
686
687       depth++;
688
689       /* Update statistics */
690       mc_stats->visited_pairs++;
691       mc_stats->executed_transitions++;
692
693       item = xbt_fifo_get_prev_item(item);
694     }
695
696   } else {
697
698     /* Traverse the stack from the initial state and re-execute the transitions */
699     for (item = xbt_fifo_get_last_item(stack);
700          item != xbt_fifo_get_first_item(stack);
701          item = xbt_fifo_get_prev_item(item)) {
702
703       pair = (mc_pair_t) xbt_fifo_get_item_content(item);
704       state = (mc_state_t) pair->graph_state;
705
706       if (pair->requests > 0) {
707
708         saved_req = MC_state_get_executed_request(state, &value);
709         //XBT_DEBUG("SavedReq->call %u", saved_req->call);
710
711         if (saved_req != NULL) {
712           /* because we got a copy of the executed request, we have to fetch the  
713              real one, pointed by the request field of the issuer process */
714           req = &saved_req->issuer->simcall;
715           //XBT_DEBUG("Req->call %u", req->call);
716
717           /* Debug information */
718           if (XBT_LOG_ISENABLED(mc_global, xbt_log_priority_debug)) {
719             req_str = MC_request_to_string(req, value);
720             XBT_DEBUG("Replay (depth = %d) : %s (%p)", depth, req_str, state);
721             xbt_free(req_str);
722           }
723
724         }
725
726         SIMIX_simcall_pre(req, value);
727         MC_wait_for_requests();
728       }
729
730       depth++;
731
732       /* Update statistics */
733       mc_stats->visited_pairs++;
734       mc_stats->executed_transitions++;
735     }
736   }
737
738   XBT_DEBUG("**** End Replay ****");
739
740   if (initial_global_state->raw_mem_set)
741     MC_SET_MC_HEAP;
742   else
743     MC_SET_STD_HEAP;
744
745 }
746
747 /**
748  * \brief Dumps the contents of a model-checker's stack and shows the actual
749  *        execution trace
750  * \param stack The stack to dump
751  */
752 void MC_dump_stack_safety(xbt_fifo_t stack)
753 {
754
755   int raw_mem_set = (mmalloc_get_current_heap() == mc_heap);
756
757   MC_show_stack_safety(stack);
758
759   if (!_sg_mc_checkpoint) {
760
761     mc_state_t state;
762
763     MC_SET_MC_HEAP;
764     while ((state = (mc_state_t) xbt_fifo_pop(stack)) != NULL)
765       MC_state_delete(state);
766     MC_SET_STD_HEAP;
767
768   }
769
770   if (raw_mem_set)
771     MC_SET_MC_HEAP;
772   else
773     MC_SET_STD_HEAP;
774
775 }
776
777
778 void MC_show_stack_safety(xbt_fifo_t stack)
779 {
780
781   int raw_mem_set = (mmalloc_get_current_heap() == mc_heap);
782
783   MC_SET_MC_HEAP;
784
785   int value;
786   mc_state_t state;
787   xbt_fifo_item_t item;
788   smx_simcall_t req;
789   char *req_str = NULL;
790
791   for (item = xbt_fifo_get_last_item(stack);
792        (item ? (state = (mc_state_t) (xbt_fifo_get_item_content(item)))
793         : (NULL)); item = xbt_fifo_get_prev_item(item)) {
794     req = MC_state_get_executed_request(state, &value);
795     if (req) {
796       req_str = MC_request_to_string(req, value);
797       XBT_INFO("%s", req_str);
798       xbt_free(req_str);
799     }
800   }
801
802   if (!raw_mem_set)
803     MC_SET_STD_HEAP;
804 }
805
806 void MC_show_deadlock(smx_simcall_t req)
807 {
808   /*char *req_str = NULL; */
809   XBT_INFO("**************************");
810   XBT_INFO("*** DEAD-LOCK DETECTED ***");
811   XBT_INFO("**************************");
812   XBT_INFO("Locked request:");
813   /*req_str = MC_request_to_string(req);
814      XBT_INFO("%s", req_str);
815      xbt_free(req_str); */
816   XBT_INFO("Counter-example execution trace:");
817   MC_dump_stack_safety(mc_stack);
818   MC_print_statistics(mc_stats);
819 }
820
821
822 void MC_show_stack_liveness(xbt_fifo_t stack)
823 {
824   int value;
825   mc_pair_t pair;
826   xbt_fifo_item_t item;
827   smx_simcall_t req;
828   char *req_str = NULL;
829
830   for (item = xbt_fifo_get_last_item(stack);
831        (item ? (pair = (mc_pair_t) (xbt_fifo_get_item_content(item)))
832         : (NULL)); item = xbt_fifo_get_prev_item(item)) {
833     req = MC_state_get_executed_request(pair->graph_state, &value);
834     if (req) {
835       if (pair->requests > 0) {
836         req_str = MC_request_to_string(req, value);
837         XBT_INFO("%s", req_str);
838         xbt_free(req_str);
839       } else {
840         XBT_INFO("End of system requests but evolution in Büchi automaton");
841       }
842     }
843   }
844 }
845
846 void MC_dump_stack_liveness(xbt_fifo_t stack)
847 {
848
849   int raw_mem_set = (mmalloc_get_current_heap() == mc_heap);
850
851   mc_pair_t pair;
852
853   MC_SET_MC_HEAP;
854   while ((pair = (mc_pair_t) xbt_fifo_pop(stack)) != NULL)
855     MC_pair_delete(pair);
856   MC_SET_STD_HEAP;
857
858   if (raw_mem_set)
859     MC_SET_MC_HEAP;
860
861 }
862
863
864 void MC_print_statistics(mc_stats_t stats)
865 {
866   if (stats->expanded_pairs == 0) {
867     XBT_INFO("Expanded states = %lu", stats->expanded_states);
868     XBT_INFO("Visited states = %lu", stats->visited_states);
869   } else {
870     XBT_INFO("Expanded pairs = %lu", stats->expanded_pairs);
871     XBT_INFO("Visited pairs = %lu", stats->visited_pairs);
872   }
873   XBT_INFO("Executed transitions = %lu", stats->executed_transitions);
874   MC_SET_MC_HEAP;
875   if ((_sg_mc_dot_output_file != NULL) && (_sg_mc_dot_output_file[0] != '\0')) {
876     fprintf(dot_output, "}\n");
877     fclose(dot_output);
878   }
879   if (initial_global_state != NULL) {
880     if (_sg_mc_comms_determinism)
881       XBT_INFO("Communication-deterministic : %s",
882                !initial_global_state->comm_deterministic ? "No" : "Yes");
883     if (_sg_mc_send_determinism)
884       XBT_INFO("Send-deterministic : %s",
885                !initial_global_state->send_deterministic ? "No" : "Yes");
886   }
887   MC_SET_STD_HEAP;
888 }
889
890 void MC_assert(int prop)
891 {
892   if (MC_is_active() && !prop) {
893     XBT_INFO("**************************");
894     XBT_INFO("*** PROPERTY NOT VALID ***");
895     XBT_INFO("**************************");
896     XBT_INFO("Counter-example execution trace:");
897     MC_dump_stack_safety(mc_stack);
898     MC_print_statistics(mc_stats);
899     xbt_abort();
900   }
901 }
902
903 void MC_cut(void)
904 {
905   user_max_depth_reached = 1;
906 }
907
908 void MC_process_clock_add(smx_process_t process, double amount)
909 {
910   mc_time[process->pid] += amount;
911 }
912
913 double MC_process_clock_get(smx_process_t process)
914 {
915   if (mc_time) {
916     if (process != NULL)
917       return mc_time[process->pid];
918     else
919       return -1;
920   } else {
921     return 0;
922   }
923 }
924
925 void MC_automaton_load(const char *file)
926 {
927
928   int raw_mem_set = (mmalloc_get_current_heap() == mc_heap);
929
930   MC_SET_MC_HEAP;
931
932   if (_mc_property_automaton == NULL)
933     _mc_property_automaton = xbt_automaton_new();
934
935   xbt_automaton_load(_mc_property_automaton, file);
936
937   MC_SET_STD_HEAP;
938
939   if (raw_mem_set)
940     MC_SET_MC_HEAP;
941
942 }
943
944 void MC_automaton_new_propositional_symbol(const char *id, void *fct)
945 {
946
947   int raw_mem_set = (mmalloc_get_current_heap() == mc_heap);
948
949   MC_SET_MC_HEAP;
950
951   if (_mc_property_automaton == NULL)
952     _mc_property_automaton = xbt_automaton_new();
953
954   xbt_automaton_propositional_symbol_new(_mc_property_automaton, id, fct);
955
956   MC_SET_STD_HEAP;
957
958   if (raw_mem_set)
959     MC_SET_MC_HEAP;
960
961 }