Logo AND Algorithmique Numérique Distribuée

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