Logo AND Algorithmique Numérique Distribuée

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