Logo AND Algorithmique Numérique Distribuée

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