Logo AND Algorithmique Numérique Distribuée

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