Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
0ea7c5f8a63527d4a54ef7563847fcd5bd5262a8
[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 <string.h>
8
9 #include "mc_base.h"
10
11 #ifndef _XBT_WIN32
12 #include <unistd.h>
13 #include <sys/wait.h>
14 #include <sys/time.h>
15 #endif
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 <libunwind.h>
26 #include <xbt/mmalloc.h>
27
28 #include "../xbt/mmalloc/mmprivate.h"
29 #include "mc_object_info.h"
30 #include "mc_comm_pattern.h"
31 #include "mc_request.h"
32 #include "mc_safety.h"
33 #include "mc_memory_map.h"
34 #include "mc_snapshot.h"
35 #include "mc_liveness.h"
36 #include "mc_private.h"
37 #include "mc_unw.h"
38 #endif
39 #include "mc_record.h"
40 #include "mc_protocol.h"
41 #include "mc_client.h"
42
43 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_global, mc,
44                                 "Logging specific to MC (global)");
45
46 e_mc_mode_t mc_mode;
47
48 double *mc_time = NULL;
49
50 #ifdef HAVE_MC
51 int user_max_depth_reached = 0;
52
53 /* MC global data structures */
54 mc_state_t mc_current_state = NULL;
55 char mc_replay_mode = FALSE;
56
57 __thread mc_comparison_times_t mc_comp_times = NULL;
58 __thread double mc_snapshot_comparison_time;
59 mc_stats_t mc_stats = NULL;
60 mc_global_t initial_global_state = NULL;
61 xbt_fifo_t mc_stack = NULL;
62
63 /* Liveness */
64 xbt_automaton_t _mc_property_automaton = NULL;
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 void MC_init()
104 {
105   MC_init_pid(getpid(), -1);
106 }
107
108 void MC_init_pid(pid_t pid, int socket)
109 {
110   if (mc_mode == MC_MODE_NONE) {
111     if (getenv(MC_ENV_SOCKET_FD)) {
112       mc_mode = MC_MODE_CLIENT;
113     } else {
114       mc_mode = MC_MODE_STANDALONE;
115     }
116   }
117
118   mc_time = xbt_new0(double, simix_process_maxpid);
119
120   /* Initialize the data structures that must be persistent across every
121      iteration of the model-checker (in RAW memory) */
122
123   xbt_mheap_t heap = mmalloc_set_current_heap(mc_heap);
124
125   mc_model_checker = MC_model_checker_new(pid, socket);
126
127   mc_comp_times = xbt_new0(s_mc_comparison_times_t, 1);
128
129   /* Initialize statistics */
130   mc_stats = xbt_new0(s_mc_stats_t, 1);
131   mc_stats->state_size = 1;
132
133   if ((_sg_mc_dot_output_file != NULL) && (_sg_mc_dot_output_file[0] != '\0'))
134     MC_init_dot_output();
135
136   /* Init parmap */
137   //parmap = xbt_parmap_mc_new(xbt_os_get_numcores(), XBT_PARMAP_DEFAULT);
138
139   MC_SET_STD_HEAP;
140
141   if (_sg_mc_visited > 0 || _sg_mc_liveness) {
142     /* Ignore some variables from xbt/ex.h used by exception e for stacks comparison */
143     MC_ignore_local_variable("e", "*");
144     MC_ignore_local_variable("__ex_cleanup", "*");
145     MC_ignore_local_variable("__ex_mctx_en", "*");
146     MC_ignore_local_variable("__ex_mctx_me", "*");
147     MC_ignore_local_variable("__xbt_ex_ctx_ptr", "*");
148     MC_ignore_local_variable("_log_ev", "*");
149     MC_ignore_local_variable("_throw_ctx", "*");
150     MC_ignore_local_variable("ctx", "*");
151
152     MC_ignore_local_variable("self", "simcall_BODY_mc_snapshot");
153     MC_ignore_local_variable("next_cont"
154       "ext", "smx_ctx_sysv_suspend_serial");
155     MC_ignore_local_variable("i", "smx_ctx_sysv_suspend_serial");
156
157     /* Ignore local variable about time used for tracing */
158     MC_ignore_local_variable("start_time", "*");
159
160     /* Main MC state: */
161     MC_ignore_global_variable("mc_model_checker");
162     MC_ignore_global_variable("initial_communications_pattern");
163     MC_ignore_global_variable("incomplete_communications_pattern");
164     MC_ignore_global_variable("nb_comm_pattern");
165
166     /* MC __thread variables: */
167     MC_ignore_global_variable("mc_diff_info");
168     MC_ignore_global_variable("mc_comp_times");
169     MC_ignore_global_variable("mc_snapshot_comparison_time");
170
171     /* This MC state is used in MC replay as well: */
172     MC_ignore_global_variable("mc_time");
173
174     /* Static variable used for tracing */
175     MC_ignore_global_variable("counter");
176
177     /* SIMIX */
178     MC_ignore_global_variable("smx_total_comms");
179
180     if (mc_mode == MC_MODE_STANDALONE || mc_mode == MC_MODE_CLIENT) {
181       /* Those requests are handled on the client side and propagated by message
182        * to the server: */
183
184       MC_ignore_heap(mc_time, simix_process_maxpid * sizeof(double));
185
186       smx_process_t process;
187       xbt_swag_foreach(process, simix_global->process_list) {
188         MC_ignore_heap(&(process->process_hookup), sizeof(process->process_hookup));
189       }
190     }
191   }
192
193   mmalloc_set_current_heap(heap);
194
195   if (mc_mode == MC_MODE_CLIENT) {
196     // This will move somehwere else:
197     MC_client_handle_messages();
198   }
199
200 }
201
202 /*******************************  Core of MC *******************************/
203 /**************************************************************************/
204
205 static void MC_modelcheck_comm_determinism_init(void)
206 {
207   MC_init();
208
209   xbt_mheap_t heap = mmalloc_set_current_heap(mc_heap);
210
211   /* Create exploration stack */
212   mc_stack = xbt_fifo_new();
213
214   MC_SET_STD_HEAP;
215
216   MC_pre_modelcheck_comm_determinism();
217
218   MC_SET_MC_HEAP;
219   initial_global_state = xbt_new0(s_mc_global_t, 1);
220   initial_global_state->snapshot = MC_take_snapshot(0);
221   initial_global_state->initial_communications_pattern_done = 0;
222   initial_global_state->comm_deterministic = 1;
223   initial_global_state->send_deterministic = 1;
224   MC_SET_STD_HEAP;
225
226   MC_modelcheck_comm_determinism();
227
228   mmalloc_set_current_heap(heap);
229 }
230
231 static void MC_modelcheck_safety_init(void)
232 {
233   _sg_mc_safety = 1;
234
235   MC_init();
236
237   xbt_mheap_t heap = mmalloc_set_current_heap(mc_heap);
238
239   /* Create exploration stack */
240   mc_stack = xbt_fifo_new();
241
242   MC_SET_STD_HEAP;
243
244   MC_pre_modelcheck_safety();
245
246   MC_SET_MC_HEAP;
247   /* Save the initial state */
248   initial_global_state = xbt_new0(s_mc_global_t, 1);
249   initial_global_state->snapshot = MC_take_snapshot(0);
250   MC_SET_STD_HEAP;
251
252   MC_modelcheck_safety();
253
254   mmalloc_set_current_heap(heap);
255
256   xbt_abort();
257   //MC_exit();
258 }
259
260 static void MC_modelcheck_liveness_init()
261 {
262   _sg_mc_liveness = 1;
263
264   MC_init();
265
266   xbt_mheap_t heap = mmalloc_set_current_heap(mc_heap);
267
268   /* Create exploration stack */
269   mc_stack = xbt_fifo_new();
270
271   /* Create the initial state */
272   initial_global_state = xbt_new0(s_mc_global_t, 1);
273
274   MC_SET_STD_HEAP;
275
276   MC_pre_modelcheck_liveness();
277
278   /* We're done */
279   MC_print_statistics(mc_stats);
280   xbt_free(mc_time);
281
282   mmalloc_set_current_heap(heap);
283
284 }
285
286 void MC_do_the_modelcheck_for_real()
287 {
288
289   if (_sg_mc_comms_determinism || _sg_mc_send_determinism) {
290     XBT_INFO("Check communication determinism");
291     MC_modelcheck_comm_determinism_init();
292   } else if (!_sg_mc_property_file || _sg_mc_property_file[0] == '\0') {
293     if (mc_reduce_kind == e_mc_reduce_unset)
294       mc_reduce_kind = e_mc_reduce_dpor;
295     XBT_INFO("Check a safety property");
296     MC_modelcheck_safety_init();
297   } else {
298     if (mc_reduce_kind == e_mc_reduce_unset)
299       mc_reduce_kind = e_mc_reduce_none;
300     XBT_INFO("Check the liveness property %s", _sg_mc_property_file);
301     MC_automaton_load(_sg_mc_property_file);
302     MC_modelcheck_liveness_init();
303   }
304 }
305
306
307 void MC_exit(void)
308 {
309   xbt_free(mc_time);
310
311   MC_memory_exit();
312   //xbt_abort();
313 }
314
315 int MC_deadlock_check()
316 {
317   if (mc_mode == MC_MODE_SERVER) {
318     int res;
319     if ((res = MC_protocol_send_simple_message(mc_model_checker->process.socket,
320       MC_MESSAGE_DEADLOCK_CHECK)))
321       xbt_die("Could not check deadlock state");
322     s_mc_int_message_t message;
323     ssize_t s = MC_receive_message(mc_model_checker->process.socket, &message, sizeof(message));
324     if (s == -1)
325       xbt_die("Could not receive message");
326     else if (s != sizeof(message) || message.type != MC_MESSAGE_DEADLOCK_CHECK_REPLY) {
327       xbt_die("Unexpected message, expected MC_MESSAGE_DEADLOCK_CHECK_REPLY %i %i vs %i %i",
328         (int) s, (int) message.type, (int) sizeof(message), (int) MC_MESSAGE_DEADLOCK_CHECK_REPLY
329         );
330     }
331     else
332       return message.value;
333   }
334
335   int deadlock = FALSE;
336   smx_process_t process;
337   if (xbt_swag_size(simix_global->process_list)) {
338     deadlock = TRUE;
339     MC_EACH_SIMIX_PROCESS(process,
340       if (MC_process_is_enabled(process)) {
341         deadlock = FALSE;
342         break;
343       }
344     );
345   }
346   return deadlock;
347 }
348
349 void handle_comm_pattern(e_mc_call_type_t call_type, smx_simcall_t req, int value, xbt_dynar_t pattern, int backtracking) {
350
351   switch(call_type) {
352   case MC_CALL_TYPE_NONE:
353     break;
354   case MC_CALL_TYPE_SEND:
355   case MC_CALL_TYPE_RECV:
356     get_comm_pattern(pattern, req, call_type);
357     break;
358   case MC_CALL_TYPE_WAIT:
359   case MC_CALL_TYPE_WAITANY:
360     {
361       smx_synchro_t current_comm = NULL;
362       if (call_type == MC_CALL_TYPE_WAIT)
363         current_comm = simcall_comm_wait__get__comm(req);
364       else
365         current_comm = xbt_dynar_get_as(simcall_comm_waitany__get__comms(req), value, smx_synchro_t);
366       // First wait only must be considered:
367       if (current_comm->comm.refcount == 1)
368         complete_comm_pattern(pattern, current_comm, backtracking);
369       break;
370     }
371   default:
372     xbt_die("Unexpected call type %i", (int)call_type);
373   }
374   
375 }
376
377 static void MC_restore_communications_pattern(mc_state_t state) {
378   mc_list_comm_pattern_t list_process_comm;
379   unsigned int cursor, cursor2;
380   xbt_dynar_foreach(initial_communications_pattern, cursor, list_process_comm){
381     list_process_comm->index_comm = (int)xbt_dynar_get_as(state->index_comm, cursor, int);
382   }
383   mc_comm_pattern_t comm;
384   cursor = 0;
385   xbt_dynar_t initial_incomplete_process_comms, incomplete_process_comms;
386   for(int i=0; i<simix_process_maxpid; i++){
387     initial_incomplete_process_comms = xbt_dynar_get_as(incomplete_communications_pattern, i, xbt_dynar_t);
388     xbt_dynar_reset(initial_incomplete_process_comms);
389     incomplete_process_comms = xbt_dynar_get_as(state->incomplete_comm_pattern, i, xbt_dynar_t);
390     xbt_dynar_foreach(incomplete_process_comms, cursor2, comm) {
391       mc_comm_pattern_t copy_comm = xbt_new0(s_mc_comm_pattern_t, 1);
392       copy_comm->index = comm->index;
393       copy_comm->type = comm->type;
394       copy_comm->comm = comm->comm;
395       copy_comm->rdv = strdup(comm->rdv);
396       copy_comm->data_size = -1;
397       copy_comm->data = NULL;
398       if(comm->type == SIMIX_COMM_SEND){
399         copy_comm->src_proc = comm->src_proc;
400         copy_comm->src_host = comm->src_host;
401         if(comm->data != NULL){
402           copy_comm->data_size = comm->data_size;
403           copy_comm->data = xbt_malloc0(comm->data_size);
404           memcpy(copy_comm->data, comm->data, comm->data_size);
405         }
406       }else{
407         copy_comm->dst_proc = comm->dst_proc;
408         copy_comm->dst_host = comm->dst_host;
409       }
410       xbt_dynar_push(initial_incomplete_process_comms, &copy_comm);
411     }
412   }
413 }
414
415 /**
416  * \brief Re-executes from the state at position start all the transitions indicated by
417  *        a given model-checker stack.
418  * \param stack The stack with the transitions to execute.
419  * \param start Start index to begin the re-execution.
420  */
421 void MC_replay(xbt_fifo_t stack)
422 {
423   xbt_mheap_t heap = mmalloc_set_current_heap(mc_heap);
424
425   int value, count = 1, j;
426   char *req_str;
427   smx_simcall_t req = NULL, saved_req = NULL;
428   xbt_fifo_item_t item, start_item;
429   mc_state_t state;
430   
431   XBT_DEBUG("**** Begin Replay ****");
432
433   /* Intermediate backtracking */
434   if(_sg_mc_checkpoint > 0) {
435     start_item = xbt_fifo_get_first_item(stack);
436     state = (mc_state_t)xbt_fifo_get_item_content(start_item);
437     if(state->system_state){
438       MC_restore_snapshot(state->system_state);
439       if(_sg_mc_comms_determinism || _sg_mc_send_determinism) 
440         MC_restore_communications_pattern(state);
441       MC_SET_STD_HEAP;
442       return;
443     }
444   }
445
446
447   /* Restore the initial state */
448   MC_restore_snapshot(initial_global_state->snapshot);
449   /* At the moment of taking the snapshot the raw heap was set, so restoring
450    * it will set it back again, we have to unset it to continue  */
451   MC_SET_STD_HEAP;
452
453   start_item = xbt_fifo_get_last_item(stack);
454   
455   MC_SET_MC_HEAP;
456
457   if (_sg_mc_comms_determinism || _sg_mc_send_determinism) {
458     for (j=0; j<simix_process_maxpid; j++) {
459       xbt_dynar_reset((xbt_dynar_t)xbt_dynar_get_as(incomplete_communications_pattern, j, xbt_dynar_t));
460       ((mc_list_comm_pattern_t)xbt_dynar_get_as(initial_communications_pattern, j, mc_list_comm_pattern_t))->index_comm = 0;
461     }
462   }
463
464   MC_SET_STD_HEAP;
465
466   /* Traverse the stack from the state at position start and re-execute the transitions */
467   for (item = start_item;
468        item != xbt_fifo_get_first_item(stack);
469        item = xbt_fifo_get_prev_item(item)) {
470
471     state = (mc_state_t) xbt_fifo_get_item_content(item);
472     saved_req = MC_state_get_executed_request(state, &value);
473     
474     if (saved_req) {
475       /* because we got a copy of the executed request, we have to fetch the  
476          real one, pointed by the request field of the issuer process */
477
478       const smx_process_t issuer = MC_process_get_issuer(&mc_model_checker->process, saved_req);
479       req = &issuer->simcall;
480
481       /* Debug information */
482       if (XBT_LOG_ISENABLED(mc_global, xbt_log_priority_debug)) {
483         req_str = MC_request_to_string(req, value);
484         XBT_DEBUG("Replay: %s (%p)", req_str, state);
485         xbt_free(req_str);
486       }
487
488       /* TODO : handle test and testany simcalls */
489       e_mc_call_type_t call = MC_CALL_TYPE_NONE;
490       if (_sg_mc_comms_determinism || _sg_mc_send_determinism)
491         call = mc_get_call_type(req);
492
493       MC_simcall_handle(req, value);
494
495       MC_SET_MC_HEAP;
496       if (_sg_mc_comms_determinism || _sg_mc_send_determinism)
497         handle_comm_pattern(call, req, value, NULL, 1);
498       MC_SET_STD_HEAP;
499       
500       MC_wait_for_requests();
501
502       count++;
503     }
504
505     /* Update statistics */
506     mc_stats->visited_states++;
507     mc_stats->executed_transitions++;
508
509   }
510
511   XBT_DEBUG("**** End Replay ****");
512   mmalloc_set_current_heap(heap);
513 }
514
515 void MC_replay_liveness(xbt_fifo_t stack)
516 {
517
518   initial_global_state->raw_mem_set = (mmalloc_get_current_heap() == mc_heap);
519
520   xbt_fifo_item_t item;
521   mc_pair_t pair = NULL;
522   mc_state_t state = NULL;
523   smx_simcall_t req = NULL, saved_req = NULL;
524   int value, depth = 1;
525   char *req_str;
526
527   XBT_DEBUG("**** Begin Replay ****");
528
529   /* Intermediate backtracking */
530   if(_sg_mc_checkpoint > 0) {
531     item = xbt_fifo_get_first_item(stack);
532     pair = (mc_pair_t) xbt_fifo_get_item_content(item);
533     if(pair->graph_state->system_state){
534       MC_restore_snapshot(pair->graph_state->system_state);
535       MC_SET_STD_HEAP;
536       return;
537     }
538   }
539
540   /* Restore the initial state */
541   MC_restore_snapshot(initial_global_state->snapshot);
542
543   /* At the moment of taking the snapshot the raw heap was set, so restoring
544    * it will set it back again, we have to unset it to continue  */
545   if (!initial_global_state->raw_mem_set)
546     MC_SET_STD_HEAP;
547
548     /* Traverse the stack from the initial state and re-execute the transitions */
549     for (item = xbt_fifo_get_last_item(stack);
550          item != xbt_fifo_get_first_item(stack);
551          item = xbt_fifo_get_prev_item(item)) {
552
553       pair = (mc_pair_t) xbt_fifo_get_item_content(item);
554
555       state = (mc_state_t) pair->graph_state;
556
557       if (pair->exploration_started) {
558
559         saved_req = MC_state_get_executed_request(state, &value);
560
561         if (saved_req != NULL) {
562           /* because we got a copy of the executed request, we have to fetch the
563              real one, pointed by the request field of the issuer process */
564           const smx_process_t issuer = MC_process_get_issuer(&mc_model_checker->process, saved_req);
565           req = &issuer->simcall;
566
567           /* Debug information */
568           if (XBT_LOG_ISENABLED(mc_global, xbt_log_priority_debug)) {
569             req_str = MC_request_to_string(req, value);
570             XBT_DEBUG("Replay (depth = %d) : %s (%p)", depth, req_str, state);
571             xbt_free(req_str);
572           }
573
574         }
575
576         MC_simcall_handle(req, value);
577         MC_wait_for_requests();
578       }
579
580       /* Update statistics */
581       mc_stats->visited_pairs++;
582       mc_stats->executed_transitions++;
583
584       depth++;
585       
586     }
587
588   XBT_DEBUG("**** End Replay ****");
589
590   if (initial_global_state->raw_mem_set)
591     MC_SET_MC_HEAP;
592   else
593     MC_SET_STD_HEAP;
594
595 }
596
597 /**
598  * \brief Dumps the contents of a model-checker's stack and shows the actual
599  *        execution trace
600  * \param stack The stack to dump
601  */
602 void MC_dump_stack_safety(xbt_fifo_t stack)
603 {
604   xbt_mheap_t heap = mmalloc_set_current_heap(mc_heap);
605
606   MC_show_stack_safety(stack);
607   
608   mc_state_t state;
609   
610   MC_SET_MC_HEAP;
611   while ((state = (mc_state_t) xbt_fifo_pop(stack)) != NULL)
612     MC_state_delete(state, !state->in_visited_states ? 1 : 0);
613   mmalloc_set_current_heap(heap);
614 }
615
616
617 void MC_show_stack_safety(xbt_fifo_t stack)
618 {
619   xbt_mheap_t heap = mmalloc_set_current_heap(mc_heap);
620
621   int value;
622   mc_state_t state;
623   xbt_fifo_item_t item;
624   smx_simcall_t req;
625   char *req_str = NULL;
626
627   for (item = xbt_fifo_get_last_item(stack);
628        (item ? (state = (mc_state_t) (xbt_fifo_get_item_content(item)))
629         : (NULL)); item = xbt_fifo_get_prev_item(item)) {
630     req = MC_state_get_executed_request(state, &value);
631     if (req) {
632       req_str = MC_request_to_string(req, value);
633       XBT_INFO("%s", req_str);
634       xbt_free(req_str);
635     }
636   }
637
638   mmalloc_set_current_heap(heap);
639 }
640
641 void MC_show_deadlock(smx_simcall_t req)
642 {
643   /*char *req_str = NULL; */
644   XBT_INFO("**************************");
645   XBT_INFO("*** DEAD-LOCK DETECTED ***");
646   XBT_INFO("**************************");
647   XBT_INFO("Locked request:");
648   /*req_str = MC_request_to_string(req);
649      XBT_INFO("%s", req_str);
650      xbt_free(req_str); */
651   XBT_INFO("Counter-example execution trace:");
652   MC_dump_stack_safety(mc_stack);
653   MC_print_statistics(mc_stats);
654 }
655
656
657 void MC_show_stack_liveness(xbt_fifo_t stack)
658 {
659   int value;
660   mc_pair_t pair;
661   xbt_fifo_item_t item;
662   smx_simcall_t req;
663   char *req_str = NULL;
664
665   for (item = xbt_fifo_get_last_item(stack);
666        (item ? (pair = (mc_pair_t) (xbt_fifo_get_item_content(item))) : (NULL));
667        item = xbt_fifo_get_prev_item(item)) {
668     req = MC_state_get_executed_request(pair->graph_state, &value);
669     if (req && req->call != SIMCALL_NONE) {
670       req_str = MC_request_to_string(req, value);
671       XBT_INFO("%s", req_str);
672       xbt_free(req_str);
673     }
674   }
675 }
676
677
678 void MC_dump_stack_liveness(xbt_fifo_t stack)
679 {
680   xbt_mheap_t heap = mmalloc_set_current_heap(mc_heap);
681   mc_pair_t pair;
682   while ((pair = (mc_pair_t) xbt_fifo_pop(stack)) != NULL)
683     MC_pair_delete(pair);
684   mmalloc_set_current_heap(heap);
685 }
686
687
688 void MC_print_statistics(mc_stats_t stats)
689 {
690   if (stats->expanded_pairs == 0) {
691     XBT_INFO("Expanded states = %lu", stats->expanded_states);
692     XBT_INFO("Visited states = %lu", stats->visited_states);
693   } else {
694     XBT_INFO("Expanded pairs = %lu", stats->expanded_pairs);
695     XBT_INFO("Visited pairs = %lu", stats->visited_pairs);
696   }
697   XBT_INFO("Executed transitions = %lu", stats->executed_transitions);
698   xbt_mheap_t heap = mmalloc_set_current_heap(mc_heap);
699   if ((_sg_mc_dot_output_file != NULL) && (_sg_mc_dot_output_file[0] != '\0')) {
700     fprintf(dot_output, "}\n");
701     fclose(dot_output);
702   }
703   if (initial_global_state != NULL) {
704     if (_sg_mc_comms_determinism)
705       XBT_INFO("Communication-deterministic : %s", !initial_global_state->comm_deterministic ? "No" : "Yes");
706     if (_sg_mc_send_determinism)
707       XBT_INFO("Send-deterministic : %s", !initial_global_state->send_deterministic ? "No" : "Yes");
708   }
709   mmalloc_set_current_heap(heap);
710 }
711
712 void MC_automaton_load(const char *file)
713 {
714   xbt_mheap_t heap = mmalloc_set_current_heap(mc_heap);
715
716   if (_mc_property_automaton == NULL)
717     _mc_property_automaton = xbt_automaton_new();
718
719   xbt_automaton_load(_mc_property_automaton, file);
720   mmalloc_set_current_heap(heap);
721 }
722
723 static void register_symbol(xbt_automaton_propositional_symbol_t symbol)
724 {
725   if (mc_mode != MC_MODE_CLIENT)
726     return;
727   s_mc_register_symbol_message_t message;
728   message.type = MC_MESSAGE_REGISTER_SYMBOL;
729   const char* name = xbt_automaton_propositional_symbol_get_name(symbol);
730   if (strlen(name) + 1 > sizeof(message.name))
731     xbt_die("Symbol is too long");
732   strncpy(message.name, name, sizeof(message.name));
733   message.callback = xbt_automaton_propositional_symbol_get_callback(symbol);
734   message.data = xbt_automaton_propositional_symbol_get_data(symbol);
735   MC_client_send_message(&message, sizeof(message));
736 }
737
738 void MC_automaton_new_propositional_symbol(const char *id, int(*fct)(void))
739 {
740   xbt_mheap_t heap = mmalloc_set_current_heap(mc_heap);
741
742   if (_mc_property_automaton == NULL)
743     _mc_property_automaton = xbt_automaton_new();
744
745   xbt_automaton_propositional_symbol_t symbol = xbt_automaton_propositional_symbol_new(_mc_property_automaton, id, fct);
746   register_symbol(symbol);
747   mmalloc_set_current_heap(heap);
748 }
749
750 void MC_automaton_new_propositional_symbol_pointer(const char *id, int* value)
751 {
752   xbt_mheap_t heap = mmalloc_set_current_heap(mc_heap);
753   if (_mc_property_automaton == NULL)
754     _mc_property_automaton = xbt_automaton_new();
755   xbt_automaton_propositional_symbol_t symbol = xbt_automaton_propositional_symbol_new_pointer(_mc_property_automaton, id, value);
756   register_symbol(symbol);
757   mmalloc_set_current_heap(heap);
758 }
759
760 void MC_automaton_new_propositional_symbol_callback(const char* id,
761   xbt_automaton_propositional_symbol_callback_type callback,
762   void* data, xbt_automaton_propositional_symbol_free_function_type free_function)
763 {
764   xbt_mheap_t heap = mmalloc_set_current_heap(mc_heap);
765   if (_mc_property_automaton == NULL)
766     _mc_property_automaton = xbt_automaton_new();
767   xbt_automaton_propositional_symbol_t symbol = xbt_automaton_propositional_symbol_new_callback(
768     _mc_property_automaton, id, callback, data, free_function);
769   register_symbol(symbol);
770   mmalloc_set_current_heap(heap);
771 }
772
773 void MC_dump_stacks(FILE* file)
774 {
775   xbt_mheap_t heap = mmalloc_set_current_heap(mc_heap);
776
777   int nstack = 0;
778   stack_region_t current_stack;
779   unsigned cursor;
780   xbt_dynar_foreach(stacks_areas, cursor, current_stack) {
781     unw_context_t * context = (unw_context_t *)current_stack->context;
782     fprintf(file, "Stack %i:\n", nstack);
783
784     int nframe = 0;
785     char buffer[100];
786     unw_cursor_t c;
787     unw_init_local (&c, context);
788     unw_word_t off;
789     do {
790       const char * name = !unw_get_proc_name(&c, buffer, 100, &off) ? buffer : "?";
791       fprintf(file, "  %i: %s\n", nframe, name);
792       ++nframe;
793     } while(unw_step(&c));
794
795     ++nstack;
796   }
797   mmalloc_set_current_heap(heap);
798 }
799 #endif
800
801 double MC_process_clock_get(smx_process_t process)
802 {
803   if (mc_time) {
804     if (process != NULL)
805       return mc_time[process->pid];
806     else
807       return -1;
808   } else {
809     return 0;
810   }
811 }
812
813 void MC_process_clock_add(smx_process_t process, double amount)
814 {
815   mc_time[process->pid] += amount;
816 }