Logo AND Algorithmique Numérique Distribuée

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