Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[mc] Move mc_global stuff into CommunicationDeterminismChecker
[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 /* 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 namespace simgrid {
114 namespace mc {
115
116 /**
117  * \brief Re-executes from the state at position start all the transitions indicated by
118  *        a given model-checker stack.
119  * \param stack The stack with the transitions to execute.
120  * \param start Start index to begin the re-execution.
121  */
122 void replay(std::list<std::unique_ptr<simgrid::mc::State>> const& stack)
123 {
124   XBT_DEBUG("**** Begin Replay ****");
125
126   /* Intermediate backtracking */
127   if(_sg_mc_checkpoint > 0 || _sg_mc_termination || _sg_mc_visited > 0) {
128     simgrid::mc::State* state = stack.back().get();
129     if (state->system_state) {
130       simgrid::mc::restore_snapshot(state->system_state);
131       if(_sg_mc_comms_determinism || _sg_mc_send_determinism) 
132         MC_restore_communications_pattern(state);
133       return;
134     }
135   }
136
137
138   /* Restore the initial state */
139   simgrid::mc::session->restoreInitialState();
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, simgrid::mc::PatternCommunicationList*)->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 req_num = state->transition.argument;
160     smx_simcall_t saved_req = &state->executed_req;
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, req_num, 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       mc_model_checker->handle_simcall(state->transition);
181       if (_sg_mc_comms_determinism || _sg_mc_send_determinism)
182         MC_handle_comm_pattern(call, req, req_num, nullptr, 1);
183       mc_model_checker->wait_for_requests();
184
185       count++;
186     }
187
188     /* Update statistics */
189     mc_model_checker->visited_states++;
190     mc_model_checker->executed_transitions++;
191
192   }
193
194   XBT_DEBUG("**** End Replay ****");
195 }
196
197 }
198 }
199
200 void MC_show_deadlock(void)
201 {
202   XBT_INFO("**************************");
203   XBT_INFO("*** DEAD-LOCK DETECTED ***");
204   XBT_INFO("**************************");
205   XBT_INFO("Counter-example execution trace:");
206   for (auto& s : mc_model_checker->getChecker()->getTextualTrace())
207     XBT_INFO("%s", s.c_str());
208   simgrid::mc::session->logState();
209 }
210
211 void MC_automaton_load(const char *file)
212 {
213   if (simgrid::mc::property_automaton == nullptr)
214     simgrid::mc::property_automaton = xbt_automaton_new();
215
216   xbt_automaton_load(simgrid::mc::property_automaton, file);
217 }
218
219 namespace simgrid {
220 namespace mc {
221
222 void dumpStack(FILE* file, unw_cursor_t cursor)
223 {
224   int nframe = 0;
225   char buffer[100];
226
227   unw_word_t off;
228   do {
229     const char * name = !unw_get_proc_name(&cursor, buffer, 100, &off) ? buffer : "?";
230
231     int status;
232
233     // Demangle C++ names:
234     char* realname = abi::__cxa_demangle(name, 0, 0, &status);
235
236 #if defined(__x86_64__)
237     unw_word_t rip = 0;
238     unw_word_t rsp = 0;
239     unw_get_reg(&cursor, UNW_X86_64_RIP, &rip);
240     unw_get_reg(&cursor, UNW_X86_64_RSP, &rsp);
241     fprintf(file, "  %i: %s (RIP=0x%" PRIx64 " RSP=0x%" PRIx64 ")\n",
242       nframe, realname ? realname : name, (std::uint64_t) rip, (std::uint64_t) rsp);
243 #else
244     fprintf(file, "  %i: %s\n", nframe, realname ? realname : name);
245 #endif
246
247     free(realname);
248     ++nframe;
249   } while(unw_step(&cursor));
250 }
251
252 }
253 }
254
255 static void MC_dump_stacks(FILE* file)
256 {
257   int nstack = 0;
258   for (auto const& stack : mc_model_checker->process().stack_areas()) {
259     fprintf(file, "Stack %i:\n", nstack++);
260
261     simgrid::mc::UnwindContext context;
262     unw_context_t raw_context =
263       mc_model_checker->process().read<unw_context_t>(
264         simgrid::mc::remote((unw_context_t *)stack.context));
265     context.initialize(&mc_model_checker->process(), &raw_context);
266
267     unw_cursor_t cursor = context.cursor();
268     simgrid::mc::dumpStack(file, cursor);
269   }
270 }
271 #endif
272
273 double MC_process_clock_get(smx_process_t process)
274 {
275   if (simgrid::mc::processes_time.empty())
276     return 0;
277   if (process != nullptr)
278     return simgrid::mc::processes_time[process->pid];
279   return -1;
280 }
281
282 void MC_process_clock_add(smx_process_t process, double amount)
283 {
284   simgrid::mc::processes_time[process->pid] += amount;
285 }