Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
56169daff75c3461a7907414356e313e0d59c431
[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 <cxxabi.h>
13
14 #include <vector>
15
16 #include <xbt/dynar.h>
17 #include <xbt/automaton.h>
18 #include <xbt/swag.h>
19
20 #include "mc_base.h"
21
22 #include "mc/mc.h"
23
24 #ifndef _WIN32
25 #include <unistd.h>
26 #include <sys/wait.h>
27 #include <sys/time.h>
28 #endif
29
30 #include "src/simix/smx_process_private.h"
31
32 #if HAVE_MC
33 #include <libunwind.h>
34 #include "src/mc/mc_comm_pattern.h"
35 #include "src/mc/mc_request.h"
36 #include "src/mc/mc_safety.h"
37 #include "src/mc/mc_snapshot.h"
38 #include "src/mc/mc_private.h"
39 #include "src/mc/mc_unw.h"
40 #include "src/mc/mc_smx.h"
41 #include "src/mc/Checker.hpp"
42 #endif
43
44 #include "src/mc/mc_record.h"
45 #include "src/mc/mc_protocol.h"
46 #include "src/mc/Client.hpp"
47 #include "src/mc/Transition.hpp"
48
49 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_global, mc, "Logging specific to MC (global)");
50
51 e_mc_mode_t mc_mode;
52
53 namespace simgrid {
54 namespace mc {
55
56 std::vector<double> processes_time;
57
58 }
59 }
60
61 #if HAVE_MC
62
63 /* MC global data structures */
64 simgrid::mc::State* mc_current_state = nullptr;
65 char mc_replay_mode = false;
66
67 mc_stats_t mc_stats = nullptr;
68
69 /* Liveness */
70
71 namespace simgrid {
72 namespace mc {
73
74 std::unique_ptr<s_mc_global_t> initial_global_state = nullptr;
75 xbt_automaton_t property_automaton = nullptr;
76
77 }
78 }
79
80 /* Dot output */
81 FILE *dot_output = nullptr;
82
83
84 /*******************************  Initialisation of MC *******************************/
85 /*********************************************************************************/
86
87 void MC_init_dot_output()
88 {
89   dot_output = fopen(_sg_mc_dot_output_file, "w");
90
91   if (dot_output == nullptr) {
92     perror("Error open dot output file");
93     xbt_abort();
94   }
95
96   fprintf(dot_output,
97           "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");
98
99 }
100
101 /*******************************  Core of MC *******************************/
102 /**************************************************************************/
103
104 void MC_run()
105 {
106   simgrid::mc::processes_time.resize(simix_process_maxpid);
107   MC_ignore_heap(simgrid::mc::processes_time.data(),
108     simgrid::mc::processes_time.size() * sizeof(simgrid::mc::processes_time[0]));
109   smx_process_t process;
110   xbt_swag_foreach(process, simix_global->process_list)
111     MC_ignore_heap(&(process->process_hookup), sizeof(process->process_hookup));
112   simgrid::mc::Client::get()->mainLoop();
113   simgrid::mc::processes_time.clear();
114 }
115
116 namespace simgrid {
117 namespace mc {
118
119 /**
120  * \brief Re-executes from the state at position start all the transitions indicated by
121  *        a given model-checker stack.
122  * \param stack The stack with the transitions to execute.
123  * \param start Start index to begin the re-execution.
124  */
125 void replay(std::list<std::unique_ptr<simgrid::mc::State>> const& stack)
126 {
127   XBT_DEBUG("**** Begin Replay ****");
128
129   /* Intermediate backtracking */
130   if(_sg_mc_checkpoint > 0 || _sg_mc_termination || _sg_mc_visited > 0) {
131     simgrid::mc::State* state = stack.back().get();
132     if (state->system_state) {
133       simgrid::mc::restore_snapshot(state->system_state);
134       if(_sg_mc_comms_determinism || _sg_mc_send_determinism) 
135         MC_restore_communications_pattern(state);
136       return;
137     }
138   }
139
140
141   /* Restore the initial state */
142   simgrid::mc::restore_snapshot(simgrid::mc::initial_global_state->snapshot);
143   /* At the moment of taking the snapshot the raw heap was set, so restoring
144    * it will set it back again, we have to unset it to continue  */
145
146   if (_sg_mc_comms_determinism || _sg_mc_send_determinism) {
147     // int n = xbt_dynar_length(incomplete_communications_pattern);
148     unsigned n = MC_smx_get_maxpid();
149     assert(n == xbt_dynar_length(incomplete_communications_pattern));
150     assert(n == xbt_dynar_length(initial_communications_pattern));
151     for (unsigned j=0; j < n ; j++) {
152       xbt_dynar_reset((xbt_dynar_t)xbt_dynar_get_as(incomplete_communications_pattern, j, xbt_dynar_t));
153       xbt_dynar_get_as(initial_communications_pattern, j, simgrid::mc::PatternCommunicationList*)->index_comm = 0;
154     }
155   }
156
157   int count = 1;
158
159   /* Traverse the stack from the state at position start and re-execute the transitions */
160   for (std::unique_ptr<simgrid::mc::State> const& state : stack) {
161     if (state == stack.back())
162       break;
163
164     int req_num = state->transition.argument;
165     smx_simcall_t saved_req = &state->executed_req;
166     
167     if (saved_req) {
168       /* because we got a copy of the executed request, we have to fetch the  
169          real one, pointed by the request field of the issuer process */
170
171       const smx_process_t issuer = MC_smx_simcall_get_issuer(saved_req);
172       smx_simcall_t req = &issuer->simcall;
173
174       /* Debug information */
175       XBT_DEBUG("Replay: %s (%p)",
176         simgrid::mc::request_to_string(
177           req, req_num, simgrid::mc::RequestType::simix).c_str(),
178         state.get());
179
180       /* TODO : handle test and testany simcalls */
181       e_mc_call_type_t call = MC_CALL_TYPE_NONE;
182       if (_sg_mc_comms_determinism || _sg_mc_send_determinism)
183         call = MC_get_call_type(req);
184
185       mc_model_checker->handle_simcall(state->transition);
186       if (_sg_mc_comms_determinism || _sg_mc_send_determinism)
187         MC_handle_comm_pattern(call, req, req_num, nullptr, 1);
188       mc_model_checker->wait_for_requests();
189
190       count++;
191     }
192
193     /* Update statistics */
194     mc_stats->visited_states++;
195     mc_stats->executed_transitions++;
196
197   }
198
199   XBT_DEBUG("**** End Replay ****");
200 }
201
202 }
203 }
204
205 void MC_show_deadlock(void)
206 {
207   XBT_INFO("**************************");
208   XBT_INFO("*** DEAD-LOCK DETECTED ***");
209   XBT_INFO("**************************");
210   XBT_INFO("Counter-example execution trace:");
211   for (auto& s : mc_model_checker->getChecker()->getTextualTrace())
212     XBT_INFO("%s", s.c_str());
213   MC_print_statistics(mc_stats);
214 }
215
216 void MC_print_statistics(mc_stats_t stats)
217 {
218   if(_sg_mc_comms_determinism) {
219     if (!simgrid::mc::initial_global_state->recv_deterministic &&
220         simgrid::mc::initial_global_state->send_deterministic){
221       XBT_INFO("******************************************************");
222       XBT_INFO("**** Only-send-deterministic communication pattern ****");
223       XBT_INFO("******************************************************");
224       XBT_INFO("%s", simgrid::mc::initial_global_state->recv_diff);
225     }else if(!simgrid::mc::initial_global_state->send_deterministic &&
226         simgrid::mc::initial_global_state->recv_deterministic) {
227       XBT_INFO("******************************************************");
228       XBT_INFO("**** Only-recv-deterministic communication pattern ****");
229       XBT_INFO("******************************************************");
230       XBT_INFO("%s", simgrid::mc::initial_global_state->send_diff);
231     }
232   }
233
234   if (stats->expanded_pairs == 0) {
235     XBT_INFO("Expanded states = %lu", stats->expanded_states);
236     XBT_INFO("Visited states = %lu", stats->visited_states);
237   } else {
238     XBT_INFO("Expanded pairs = %lu", stats->expanded_pairs);
239     XBT_INFO("Visited pairs = %lu", stats->visited_pairs);
240   }
241   XBT_INFO("Executed transitions = %lu", stats->executed_transitions);
242   if ((_sg_mc_dot_output_file != nullptr) && (_sg_mc_dot_output_file[0] != '\0')) {
243     fprintf(dot_output, "}\n");
244     fclose(dot_output);
245   }
246   if (simgrid::mc::initial_global_state != nullptr
247       && (_sg_mc_comms_determinism || _sg_mc_send_determinism)) {
248     XBT_INFO("Send-deterministic : %s",
249       !simgrid::mc::initial_global_state->send_deterministic ? "No" : "Yes");
250     if (_sg_mc_comms_determinism)
251       XBT_INFO("Recv-deterministic : %s",
252         !simgrid::mc::initial_global_state->recv_deterministic ? "No" : "Yes");
253   }
254   if (getenv("SIMGRID_MC_SYSTEM_STATISTICS")){
255     int ret=system("free");
256     if(ret!=0)XBT_WARN("system call did not return 0, but %d",ret);
257   }
258 }
259
260 void MC_automaton_load(const char *file)
261 {
262   if (simgrid::mc::property_automaton == nullptr)
263     simgrid::mc::property_automaton = xbt_automaton_new();
264
265   xbt_automaton_load(simgrid::mc::property_automaton, file);
266 }
267
268 namespace simgrid {
269 namespace mc {
270
271 void dumpStack(FILE* file, unw_cursor_t cursor)
272 {
273   int nframe = 0;
274   char buffer[100];
275
276   unw_word_t off;
277   do {
278     const char * name = !unw_get_proc_name(&cursor, buffer, 100, &off) ? buffer : "?";
279
280     int status;
281
282     // Demangle C++ names:
283     char* realname = abi::__cxa_demangle(name, 0, 0, &status);
284
285 #if defined(__x86_64__)
286     unw_word_t rip = 0;
287     unw_word_t rsp = 0;
288     unw_get_reg(&cursor, UNW_X86_64_RIP, &rip);
289     unw_get_reg(&cursor, UNW_X86_64_RSP, &rsp);
290     fprintf(file, "  %i: %s (RIP=0x%" PRIx64 " RSP=0x%" PRIx64 ")\n",
291       nframe, realname ? realname : name, (std::uint64_t) rip, (std::uint64_t) rsp);
292 #else
293     fprintf(file, "  %i: %s\n", nframe, realname ? realname : name);
294 #endif
295
296     free(realname);
297     ++nframe;
298   } while(unw_step(&cursor));
299 }
300
301 }
302 }
303
304 static void MC_dump_stacks(FILE* file)
305 {
306   int nstack = 0;
307   for (auto const& stack : mc_model_checker->process().stack_areas()) {
308     fprintf(file, "Stack %i:\n", nstack++);
309
310     simgrid::mc::UnwindContext context;
311     unw_context_t raw_context =
312       mc_model_checker->process().read<unw_context_t>(
313         simgrid::mc::remote((unw_context_t *)stack.context));
314     context.initialize(&mc_model_checker->process(), &raw_context);
315
316     unw_cursor_t cursor = context.cursor();
317     simgrid::mc::dumpStack(file, cursor);
318   }
319 }
320 #endif
321
322 double MC_process_clock_get(smx_process_t process)
323 {
324   if (simgrid::mc::processes_time.empty())
325     return 0;
326   if (process != nullptr)
327     return simgrid::mc::processes_time[process->pid];
328   return -1;
329 }
330
331 void MC_process_clock_add(smx_process_t process, double amount)
332 {
333   simgrid::mc::processes_time[process->pid] += amount;
334 }
335
336 #if HAVE_MC
337 void MC_report_assertion_error(void)
338 {
339   XBT_INFO("**************************");
340   XBT_INFO("*** PROPERTY NOT VALID ***");
341   XBT_INFO("**************************");
342   XBT_INFO("Counter-example execution trace:");
343   simgrid::mc::dumpRecordPath();
344   for (auto& s : mc_model_checker->getChecker()->getTextualTrace())
345     XBT_INFO("%s", s.c_str());
346   MC_print_statistics(mc_stats);
347 }
348
349 void MC_report_crash(int status)
350 {
351   XBT_INFO("**************************");
352   XBT_INFO("** CRASH IN THE PROGRAM **");
353   XBT_INFO("**************************");
354   if (WIFSIGNALED(status))
355     XBT_INFO("From signal: %s", strsignal(WTERMSIG(status)));
356   else if (WIFEXITED(status))
357     XBT_INFO("From exit: %i", WEXITSTATUS(status));
358   if (WCOREDUMP(status))
359     XBT_INFO("A core dump was generated by the system.");
360   else
361     XBT_INFO("No core dump was generated by the system.");
362   XBT_INFO("Counter-example execution trace:");
363   simgrid::mc::dumpRecordPath();
364   for (auto& s : mc_model_checker->getChecker()->getTextualTrace())
365     XBT_INFO("%s", s.c_str());
366   MC_print_statistics(mc_stats);
367   XBT_INFO("Stack trace:");
368   mc_model_checker->process().dumpStack();
369 }
370
371 #endif