Logo AND Algorithmique Numérique Distribuée

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