Logo AND Algorithmique Numérique Distribuée

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