Logo AND Algorithmique Numérique Distribuée

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