Logo AND Algorithmique Numérique Distribuée

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