Logo AND Algorithmique Numérique Distribuée

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