Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
3b6f904ec41a23e1c6c6cae9f4e874f1593b27c3
[simgrid.git] / src / mc / mc_global.cpp
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 <assert.h>
8 #include <string.h>
9 #include <stdint.h>
10
11 #include "mc_base.h"
12
13 #ifndef _XBT_WIN32
14 #include <unistd.h>
15 #include <sys/wait.h>
16 #include <sys/time.h>
17 #endif
18
19 #include "simgrid/sg_config.h"
20 #include "../surf/surf_private.h"
21 #include "../simix/smx_private.h"
22 #include "xbt/fifo.h"
23 #include "xbt/automaton.h"
24 #include "xbt/dict.h"
25 #include "mc_record.h"
26
27 #ifdef HAVE_MC
28 #include "mc_server.h"
29 #include <libunwind.h>
30 #include <xbt/mmalloc.h>
31 #include "../xbt/mmalloc/mmprivate.h"
32 #include "mc_object_info.h"
33 #include "mc_comm_pattern.h"
34 #include "mc_request.h"
35 #include "mc_safety.h"
36 #include "mc_memory_map.h"
37 #include "mc_snapshot.h"
38 #include "mc_liveness.h"
39 #include "mc_private.h"
40 #include "mc_unw.h"
41 #include "mc_smx.h"
42 #include "mcer_ignore.h"
43 #endif
44 #include "mc_record.h"
45 #include "mc_protocol.h"
46 #include "mc_client.h"
47
48 extern "C" {
49
50 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_global, mc,
51                                 "Logging specific to MC (global)");
52
53 e_mc_mode_t mc_mode;
54
55 double *mc_time = NULL;
56
57 #ifdef HAVE_MC
58 int user_max_depth_reached = 0;
59
60 /* MC global data structures */
61 mc_state_t mc_current_state = NULL;
62 char mc_replay_mode = FALSE;
63
64 __thread mc_comparison_times_t mc_comp_times = NULL;
65 __thread double mc_snapshot_comparison_time;
66 mc_stats_t mc_stats = NULL;
67 mc_global_t initial_global_state = NULL;
68 xbt_fifo_t mc_stack = NULL;
69
70 /* Liveness */
71 xbt_automaton_t _mc_property_automaton = NULL;
72
73 /* Dot output */
74 FILE *dot_output = NULL;
75 const char *colors[13];
76
77
78 /*******************************  Initialisation of MC *******************************/
79 /*********************************************************************************/
80
81 static void MC_init_dot_output()
82 {                               /* FIXME : more colors */
83
84   colors[0] = "blue";
85   colors[1] = "red";
86   colors[2] = "green3";
87   colors[3] = "goldenrod";
88   colors[4] = "brown";
89   colors[5] = "purple";
90   colors[6] = "magenta";
91   colors[7] = "turquoise4";
92   colors[8] = "gray25";
93   colors[9] = "forestgreen";
94   colors[10] = "hotpink";
95   colors[11] = "lightblue";
96   colors[12] = "tan";
97
98   dot_output = fopen(_sg_mc_dot_output_file, "w");
99
100   if (dot_output == NULL) {
101     perror("Error open dot output file");
102     xbt_abort();
103   }
104
105   fprintf(dot_output,
106           "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");
107
108 }
109
110 #ifdef HAVE_MC
111 void MC_init()
112 {
113   mc_time = xbt_new0(double, simix_process_maxpid);
114
115   if (_sg_mc_visited > 0 || _sg_mc_liveness  || _sg_mc_termination || mc_mode == MC_MODE_SERVER) {
116     /* Those requests are handled on the client side and propagated by message
117      * to the server: */
118
119     MC_ignore_heap(mc_time, simix_process_maxpid * sizeof(double));
120
121     smx_process_t process;
122     xbt_swag_foreach(process, simix_global->process_list) {
123       MC_ignore_heap(&(process->process_hookup), sizeof(process->process_hookup));
124     }
125   }
126 }
127
128 void MC_init_model_checker(pid_t pid, int socket)
129 {
130   mc_model_checker = new simgrid::mc::ModelChecker(pid, socket);
131
132   mc_comp_times = xbt_new0(s_mc_comparison_times_t, 1);
133
134   /* Initialize statistics */
135   mc_stats = xbt_new0(s_mc_stats_t, 1);
136   mc_stats->state_size = 1;
137
138   if ((_sg_mc_dot_output_file != NULL) && (_sg_mc_dot_output_file[0] != '\0'))
139     MC_init_dot_output();
140
141   /* Init parmap */
142   //parmap = xbt_parmap_mc_new(xbt_os_get_numcores(), XBT_PARMAP_DEFAULT);
143
144   /* Ignore some variables from xbt/ex.h used by exception e for stacks comparison */
145   MC_ignore_local_variable("e", "*");
146   MC_ignore_local_variable("__ex_cleanup", "*");
147   MC_ignore_local_variable("__ex_mctx_en", "*");
148   MC_ignore_local_variable("__ex_mctx_me", "*");
149   MC_ignore_local_variable("__xbt_ex_ctx_ptr", "*");
150   MC_ignore_local_variable("_log_ev", "*");
151   MC_ignore_local_variable("_throw_ctx", "*");
152   MC_ignore_local_variable("ctx", "*");
153
154   MC_ignore_local_variable("self", "simcall_BODY_mc_snapshot");
155   MC_ignore_local_variable("next_cont"
156     "ext", "smx_ctx_sysv_suspend_serial");
157   MC_ignore_local_variable("i", "smx_ctx_sysv_suspend_serial");
158
159   /* Ignore local variable about time used for tracing */
160   MC_ignore_local_variable("start_time", "*");
161
162   /* Static variable used for tracing */
163   MCer_ignore_global_variable("counter");
164
165   /* SIMIX */
166   MCer_ignore_global_variable("smx_total_comms");
167 }
168 #endif
169
170 /*******************************  Core of MC *******************************/
171 /**************************************************************************/
172
173 void MC_run()
174 {
175   mc_mode = MC_MODE_CLIENT;
176   MC_init();
177   MC_client_main_loop();
178 }
179
180 void MC_exit(void)
181 {
182   xbt_free(mc_time);
183
184   MC_memory_exit();
185   //xbt_abort();
186 }
187
188 #ifdef HAVE_MC
189 int MC_deadlock_check()
190 {
191   if (mc_mode == MC_MODE_SERVER) {
192     int res;
193     if ((res = MC_protocol_send_simple_message(mc_model_checker->process().socket,
194       MC_MESSAGE_DEADLOCK_CHECK)))
195       xbt_die("Could not check deadlock state");
196     s_mc_int_message_t message;
197     ssize_t s = MC_receive_message(mc_model_checker->process().socket, &message, sizeof(message), 0);
198     if (s == -1)
199       xbt_die("Could not receive message");
200     else if (s != sizeof(message) || message.type != MC_MESSAGE_DEADLOCK_CHECK_REPLY) {
201       xbt_die("%s received unexpected message %s (%i, size=%i) "
202         "expected MC_MESSAGE_DEADLOCK_CHECK_REPLY (%i, size=%i)",
203         MC_mode_name(mc_mode),
204         MC_message_type_name(message.type), (int) message.type, (int) s,
205         (int) MC_MESSAGE_DEADLOCK_CHECK_REPLY, (int) sizeof(message)
206         );
207     }
208     else
209       return message.value;
210   }
211
212   int deadlock = FALSE;
213   smx_process_t process;
214   if (xbt_swag_size(simix_global->process_list)) {
215     deadlock = TRUE;
216     xbt_swag_foreach(process, simix_global->process_list) {
217       if (MC_process_is_enabled(process)) {
218         deadlock = FALSE;
219         break;
220       }
221     }
222   }
223   return deadlock;
224 }
225 #endif
226
227 /**
228  * \brief Re-executes from the state at position start all the transitions indicated by
229  *        a given model-checker stack.
230  * \param stack The stack with the transitions to execute.
231  * \param start Start index to begin the re-execution.
232  */
233 void MC_replay(xbt_fifo_t stack)
234 {
235   int value, count = 1;
236   char *req_str;
237   smx_simcall_t req = NULL, saved_req = NULL;
238   xbt_fifo_item_t item, start_item;
239   mc_state_t state;
240   
241   XBT_DEBUG("**** Begin Replay ****");
242
243   /* Intermediate backtracking */
244   if(_sg_mc_checkpoint > 0 || _sg_mc_termination || _sg_mc_visited > 0) {
245     start_item = xbt_fifo_get_first_item(stack);
246     state = (mc_state_t)xbt_fifo_get_item_content(start_item);
247     if(state->system_state){
248       MC_restore_snapshot(state->system_state);
249       if(_sg_mc_comms_determinism || _sg_mc_send_determinism) 
250         MC_restore_communications_pattern(state);
251       return;
252     }
253   }
254
255
256   /* Restore the initial state */
257   MC_restore_snapshot(initial_global_state->snapshot);
258   /* At the moment of taking the snapshot the raw heap was set, so restoring
259    * it will set it back again, we have to unset it to continue  */
260
261   start_item = xbt_fifo_get_last_item(stack);
262
263   if (_sg_mc_comms_determinism || _sg_mc_send_determinism) {
264     // int n = xbt_dynar_length(incomplete_communications_pattern);
265     unsigned n = MC_smx_get_maxpid();
266     assert(n == xbt_dynar_length(incomplete_communications_pattern));
267     assert(n == xbt_dynar_length(initial_communications_pattern));
268     for (unsigned j=0; j < n ; j++) {
269       xbt_dynar_reset((xbt_dynar_t)xbt_dynar_get_as(incomplete_communications_pattern, j, xbt_dynar_t));
270       xbt_dynar_get_as(initial_communications_pattern, j, mc_list_comm_pattern_t)->index_comm = 0;
271     }
272   }
273
274   /* Traverse the stack from the state at position start and re-execute the transitions */
275   for (item = start_item;
276        item != xbt_fifo_get_first_item(stack);
277        item = xbt_fifo_get_prev_item(item)) {
278
279     state = (mc_state_t) xbt_fifo_get_item_content(item);
280     saved_req = MC_state_get_executed_request(state, &value);
281     
282     if (saved_req) {
283       /* because we got a copy of the executed request, we have to fetch the  
284          real one, pointed by the request field of the issuer process */
285
286       const smx_process_t issuer = MC_smx_simcall_get_issuer(saved_req);
287       req = &issuer->simcall;
288
289       /* Debug information */
290       if (XBT_LOG_ISENABLED(mc_global, xbt_log_priority_debug)) {
291         req_str = MC_request_to_string(req, value, MC_REQUEST_SIMIX);
292         XBT_DEBUG("Replay: %s (%p)", req_str, state);
293         xbt_free(req_str);
294       }
295
296       /* TODO : handle test and testany simcalls */
297       e_mc_call_type_t call = MC_CALL_TYPE_NONE;
298       if (_sg_mc_comms_determinism || _sg_mc_send_determinism)
299         call = MC_get_call_type(req);
300
301       MC_simcall_handle(req, value);
302
303       if (_sg_mc_comms_determinism || _sg_mc_send_determinism)
304         MC_handle_comm_pattern(call, req, value, NULL, 1);
305
306       MC_wait_for_requests();
307
308       count++;
309     }
310
311     /* Update statistics */
312     mc_stats->visited_states++;
313     mc_stats->executed_transitions++;
314
315   }
316
317   XBT_DEBUG("**** End Replay ****");
318 }
319
320 void MC_replay_liveness(xbt_fifo_t stack)
321 {
322   xbt_fifo_item_t item;
323   mc_pair_t pair = NULL;
324   mc_state_t state = NULL;
325   smx_simcall_t req = NULL, saved_req = NULL;
326   int value, depth = 1;
327   char *req_str;
328
329   XBT_DEBUG("**** Begin Replay ****");
330
331   /* Intermediate backtracking */
332   if(_sg_mc_checkpoint > 0) {
333     item = xbt_fifo_get_first_item(stack);
334     pair = (mc_pair_t) xbt_fifo_get_item_content(item);
335     if(pair->graph_state->system_state){
336       MC_restore_snapshot(pair->graph_state->system_state);
337       return;
338     }
339   }
340
341   /* Restore the initial state */
342   MC_restore_snapshot(initial_global_state->snapshot);
343
344     /* Traverse the stack from the initial state and re-execute the transitions */
345     for (item = xbt_fifo_get_last_item(stack);
346          item != xbt_fifo_get_first_item(stack);
347          item = xbt_fifo_get_prev_item(item)) {
348
349       pair = (mc_pair_t) xbt_fifo_get_item_content(item);
350
351       state = (mc_state_t) pair->graph_state;
352
353       if (pair->exploration_started) {
354
355         saved_req = MC_state_get_executed_request(state, &value);
356
357         if (saved_req != NULL) {
358           /* because we got a copy of the executed request, we have to fetch the
359              real one, pointed by the request field of the issuer process */
360           const smx_process_t issuer = MC_smx_simcall_get_issuer(saved_req);
361           req = &issuer->simcall;
362
363           /* Debug information */
364           if (XBT_LOG_ISENABLED(mc_global, xbt_log_priority_debug)) {
365             req_str = MC_request_to_string(req, value, MC_REQUEST_SIMIX);
366             XBT_DEBUG("Replay (depth = %d) : %s (%p)", depth, req_str, state);
367             xbt_free(req_str);
368           }
369
370         }
371
372         MC_simcall_handle(req, value);
373         MC_wait_for_requests();
374       }
375
376       /* Update statistics */
377       mc_stats->visited_pairs++;
378       mc_stats->executed_transitions++;
379
380       depth++;
381       
382     }
383
384   XBT_DEBUG("**** End Replay ****");
385 }
386
387 /**
388  * \brief Dumps the contents of a model-checker's stack and shows the actual
389  *        execution trace
390  * \param stack The stack to dump
391  */
392 void MC_dump_stack_safety(xbt_fifo_t stack)
393 {
394   MC_show_stack_safety(stack);
395   
396   mc_state_t state;
397
398   while ((state = (mc_state_t) xbt_fifo_pop(stack)) != NULL)
399     MC_state_delete(state, !state->in_visited_states ? 1 : 0);
400 }
401
402
403 void MC_show_stack_safety(xbt_fifo_t stack)
404 {
405   int value;
406   mc_state_t state;
407   xbt_fifo_item_t item;
408   smx_simcall_t req;
409   char *req_str = NULL;
410
411   for (item = xbt_fifo_get_last_item(stack);
412        (item ? (state = (mc_state_t) (xbt_fifo_get_item_content(item)))
413         : (NULL)); item = xbt_fifo_get_prev_item(item)) {
414     req = MC_state_get_executed_request(state, &value);
415     if (req) {
416       req_str = MC_request_to_string(req, value, MC_REQUEST_EXECUTED);
417       XBT_INFO("%s", req_str);
418       xbt_free(req_str);
419     }
420   }
421 }
422
423 void MC_show_deadlock(smx_simcall_t req)
424 {
425   /*char *req_str = NULL; */
426   XBT_INFO("**************************");
427   XBT_INFO("*** DEAD-LOCK DETECTED ***");
428   XBT_INFO("**************************");
429   XBT_INFO("Locked request:");
430   /*req_str = MC_request_to_string(req);
431      XBT_INFO("%s", req_str);
432      xbt_free(req_str); */
433   XBT_INFO("Counter-example execution trace:");
434   MC_dump_stack_safety(mc_stack);
435   MC_print_statistics(mc_stats);
436 }
437
438 void MC_show_non_termination(void){
439   XBT_INFO("******************************************");
440   XBT_INFO("*** NON-PROGRESSIVE CYCLE DETECTED ***");
441   XBT_INFO("******************************************");
442   XBT_INFO("Counter-example execution trace:");
443   MC_dump_stack_safety(mc_stack);
444   MC_print_statistics(mc_stats);
445 }
446
447
448 void MC_show_stack_liveness(xbt_fifo_t stack)
449 {
450   int value;
451   mc_pair_t pair;
452   xbt_fifo_item_t item;
453   smx_simcall_t req;
454   char *req_str = NULL;
455
456   for (item = xbt_fifo_get_last_item(stack);
457        (item ? (pair = (mc_pair_t) (xbt_fifo_get_item_content(item))) : (NULL));
458        item = xbt_fifo_get_prev_item(item)) {
459     req = MC_state_get_executed_request(pair->graph_state, &value);
460     if (req && req->call != SIMCALL_NONE) {
461       req_str = MC_request_to_string(req, value, MC_REQUEST_EXECUTED);
462       XBT_INFO("%s", req_str);
463       xbt_free(req_str);
464     }
465   }
466 }
467
468
469 void MC_dump_stack_liveness(xbt_fifo_t stack)
470 {
471   mc_pair_t pair;
472   while ((pair = (mc_pair_t) xbt_fifo_pop(stack)) != NULL)
473     MC_pair_delete(pair);
474 }
475
476 void MC_print_statistics(mc_stats_t stats)
477 {
478   if(_sg_mc_comms_determinism) {
479     if (!initial_global_state->recv_deterministic && initial_global_state->send_deterministic){
480       XBT_INFO("******************************************************");
481       XBT_INFO("**** Only-send-deterministic communication pattern ****");
482       XBT_INFO("******************************************************");
483       XBT_INFO("%s", initial_global_state->recv_diff);
484     }else if(!initial_global_state->send_deterministic && initial_global_state->recv_deterministic) {
485       XBT_INFO("******************************************************");
486       XBT_INFO("**** Only-recv-deterministic communication pattern ****");
487       XBT_INFO("******************************************************");
488       XBT_INFO("%s", initial_global_state->send_diff);
489     }
490   }
491
492   if (stats->expanded_pairs == 0) {
493     XBT_INFO("Expanded states = %lu", stats->expanded_states);
494     XBT_INFO("Visited states = %lu", stats->visited_states);
495   } else {
496     XBT_INFO("Expanded pairs = %lu", stats->expanded_pairs);
497     XBT_INFO("Visited pairs = %lu", stats->visited_pairs);
498   }
499   XBT_INFO("Executed transitions = %lu", stats->executed_transitions);
500   if ((_sg_mc_dot_output_file != NULL) && (_sg_mc_dot_output_file[0] != '\0')) {
501     fprintf(dot_output, "}\n");
502     fclose(dot_output);
503   }
504   if (initial_global_state != NULL && (_sg_mc_comms_determinism || _sg_mc_send_determinism)) {
505     XBT_INFO("Send-deterministic : %s", !initial_global_state->send_deterministic ? "No" : "Yes");
506     if (_sg_mc_comms_determinism)
507       XBT_INFO("Recv-deterministic : %s", !initial_global_state->recv_deterministic ? "No" : "Yes");
508   }
509 }
510
511 void MC_automaton_load(const char *file)
512 {
513   if (_mc_property_automaton == NULL)
514     _mc_property_automaton = xbt_automaton_new();
515
516   xbt_automaton_load(_mc_property_automaton, file);
517 }
518
519 static void register_symbol(xbt_automaton_propositional_symbol_t symbol)
520 {
521   if (mc_mode != MC_MODE_CLIENT)
522     return;
523   s_mc_register_symbol_message_t message;
524   message.type = MC_MESSAGE_REGISTER_SYMBOL;
525   const char* name = xbt_automaton_propositional_symbol_get_name(symbol);
526   if (strlen(name) + 1 > sizeof(message.name))
527     xbt_die("Symbol is too long");
528   strncpy(message.name, name, sizeof(message.name));
529   message.callback = xbt_automaton_propositional_symbol_get_callback(symbol);
530   message.data = xbt_automaton_propositional_symbol_get_data(symbol);
531   MC_client_send_message(&message, sizeof(message));
532 }
533
534 void MC_automaton_new_propositional_symbol(const char *id, int(*fct)(void))
535 {
536   if (_mc_property_automaton == NULL)
537     _mc_property_automaton = xbt_automaton_new();
538
539   xbt_automaton_propositional_symbol_t symbol = xbt_automaton_propositional_symbol_new(_mc_property_automaton, id, fct);
540   register_symbol(symbol);
541 }
542
543 void MC_automaton_new_propositional_symbol_pointer(const char *id, int* value)
544 {
545   if (_mc_property_automaton == NULL)
546     _mc_property_automaton = xbt_automaton_new();
547   xbt_automaton_propositional_symbol_t symbol = xbt_automaton_propositional_symbol_new_pointer(_mc_property_automaton, id, value);
548   register_symbol(symbol);
549 }
550
551 void MC_automaton_new_propositional_symbol_callback(const char* id,
552   xbt_automaton_propositional_symbol_callback_type callback,
553   void* data, xbt_automaton_propositional_symbol_free_function_type free_function)
554 {
555   if (_mc_property_automaton == NULL)
556     _mc_property_automaton = xbt_automaton_new();
557   xbt_automaton_propositional_symbol_t symbol = xbt_automaton_propositional_symbol_new_callback(
558     _mc_property_automaton, id, callback, data, free_function);
559   register_symbol(symbol);
560 }
561
562 // TODO, fix cross-process access (this function is not used)
563 void MC_dump_stacks(FILE* file)
564 {
565   int nstack = 0;
566   stack_region_t current_stack;
567   unsigned cursor;
568   xbt_dynar_foreach(stacks_areas, cursor, current_stack) {
569     unw_context_t * context = (unw_context_t *)current_stack->context;
570     fprintf(file, "Stack %i:\n", nstack);
571
572     int nframe = 0;
573     char buffer[100];
574     unw_cursor_t c;
575     unw_init_local (&c, context);
576     unw_word_t off;
577     do {
578       const char * name = !unw_get_proc_name(&c, buffer, 100, &off) ? buffer : "?";
579 #if defined(__x86_64__)
580       unw_word_t rip = 0;
581       unw_word_t rsp = 0;
582       unw_get_reg(&c, UNW_X86_64_RIP, &rip);
583       unw_get_reg(&c, UNW_X86_64_RSP, &rsp);
584       fprintf(file, "  %i: %s (RIP=0x%" PRIx64 " RSP=0x%" PRIx64 ")\n",
585         nframe, name, rip, rsp);
586 #else
587       fprintf(file, "  %i: %s\n", nframe, name);
588 #endif
589       ++nframe;
590     } while(unw_step(&c));
591
592     ++nstack;
593   }
594 }
595 #endif
596
597 double MC_process_clock_get(smx_process_t process)
598 {
599   if (mc_time) {
600     if (process != NULL)
601       return mc_time[process->pid];
602     else
603       return -1;
604   } else {
605     return 0;
606   }
607 }
608
609 void MC_process_clock_add(smx_process_t process, double amount)
610 {
611   mc_time[process->pid] += amount;
612 }
613
614 #ifdef HAVE_MC
615 void MC_report_assertion_error(void)
616 {
617   XBT_INFO("**************************");
618   XBT_INFO("*** PROPERTY NOT VALID ***");
619   XBT_INFO("**************************");
620   XBT_INFO("Counter-example execution trace:");
621   MC_record_dump_path(mc_stack);
622   MC_dump_stack_safety(mc_stack);
623   MC_print_statistics(mc_stats);
624 }
625
626 void MC_invalidate_cache(void)
627 {
628   if (mc_model_checker)
629     mc_model_checker->process().cache_flags = 0;
630 }
631 #endif
632
633 }