Logo AND Algorithmique Numérique Distribuée

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