Logo AND Algorithmique Numérique Distribuée

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