Logo AND Algorithmique Numérique Distribuée

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