Logo AND Algorithmique Numérique Distribuée

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