Logo AND Algorithmique Numérique Distribuée

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