Logo AND Algorithmique Numérique Distribuée

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