1 /* Copyright (c) 2008-2015. The SimGrid Team.
2 * All rights reserved. */
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. */
16 #include <xbt/dynar.h>
17 #include <xbt/automaton.h>
30 #include "src/simix/smx_process_private.h"
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"
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"
49 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_global, mc, "Logging specific to MC (global)");
56 std::vector<double> processes_time;
63 /* MC global data structures */
64 simgrid::mc::State* mc_current_state = nullptr;
65 char mc_replay_mode = false;
72 std::unique_ptr<s_mc_global_t> initial_global_state = nullptr;
73 xbt_automaton_t property_automaton = nullptr;
79 FILE *dot_output = nullptr;
82 /******************************* Initialisation of MC *******************************/
83 /*********************************************************************************/
85 void MC_init_dot_output()
87 dot_output = fopen(_sg_mc_dot_output_file, "w");
89 if (dot_output == nullptr) {
90 perror("Error open dot output file");
95 "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");
99 /******************************* Core of MC *******************************/
100 /**************************************************************************/
104 simgrid::mc::processes_time.resize(simix_process_maxpid);
105 MC_ignore_heap(simgrid::mc::processes_time.data(),
106 simgrid::mc::processes_time.size() * sizeof(simgrid::mc::processes_time[0]));
107 smx_process_t process;
108 xbt_swag_foreach(process, simix_global->process_list)
109 MC_ignore_heap(&(process->process_hookup), sizeof(process->process_hookup));
110 simgrid::mc::Client::get()->mainLoop();
111 simgrid::mc::processes_time.clear();
118 * \brief Re-executes from the state at position start all the transitions indicated by
119 * a given model-checker stack.
120 * \param stack The stack with the transitions to execute.
121 * \param start Start index to begin the re-execution.
123 void replay(std::list<std::unique_ptr<simgrid::mc::State>> const& stack)
125 XBT_DEBUG("**** Begin Replay ****");
127 /* Intermediate backtracking */
128 if(_sg_mc_checkpoint > 0 || _sg_mc_termination || _sg_mc_visited > 0) {
129 simgrid::mc::State* state = stack.back().get();
130 if (state->system_state) {
131 simgrid::mc::restore_snapshot(state->system_state);
132 if(_sg_mc_comms_determinism || _sg_mc_send_determinism)
133 MC_restore_communications_pattern(state);
139 /* Restore the initial state */
140 simgrid::mc::restore_snapshot(simgrid::mc::initial_global_state->snapshot);
142 if (_sg_mc_comms_determinism || _sg_mc_send_determinism) {
143 // int n = xbt_dynar_length(incomplete_communications_pattern);
144 unsigned n = MC_smx_get_maxpid();
145 assert(n == xbt_dynar_length(incomplete_communications_pattern));
146 assert(n == xbt_dynar_length(initial_communications_pattern));
147 for (unsigned j=0; j < n ; j++) {
148 xbt_dynar_reset((xbt_dynar_t)xbt_dynar_get_as(incomplete_communications_pattern, j, xbt_dynar_t));
149 xbt_dynar_get_as(initial_communications_pattern, j, simgrid::mc::PatternCommunicationList*)->index_comm = 0;
155 /* Traverse the stack from the state at position start and re-execute the transitions */
156 for (std::unique_ptr<simgrid::mc::State> const& state : stack) {
157 if (state == stack.back())
160 int req_num = state->transition.argument;
161 smx_simcall_t saved_req = &state->executed_req;
164 /* because we got a copy of the executed request, we have to fetch the
165 real one, pointed by the request field of the issuer process */
167 const smx_process_t issuer = MC_smx_simcall_get_issuer(saved_req);
168 smx_simcall_t req = &issuer->simcall;
170 /* Debug information */
171 XBT_DEBUG("Replay: %s (%p)",
172 simgrid::mc::request_to_string(
173 req, req_num, simgrid::mc::RequestType::simix).c_str(),
176 /* TODO : handle test and testany simcalls */
177 e_mc_call_type_t call = MC_CALL_TYPE_NONE;
178 if (_sg_mc_comms_determinism || _sg_mc_send_determinism)
179 call = MC_get_call_type(req);
181 mc_model_checker->handle_simcall(state->transition);
182 if (_sg_mc_comms_determinism || _sg_mc_send_determinism)
183 MC_handle_comm_pattern(call, req, req_num, nullptr, 1);
184 mc_model_checker->wait_for_requests();
189 /* Update statistics */
190 mc_model_checker->visited_states++;
191 mc_model_checker->executed_transitions++;
195 XBT_DEBUG("**** End Replay ****");
201 void MC_show_deadlock(void)
203 XBT_INFO("**************************");
204 XBT_INFO("*** DEAD-LOCK DETECTED ***");
205 XBT_INFO("**************************");
206 XBT_INFO("Counter-example execution trace:");
207 for (auto& s : mc_model_checker->getChecker()->getTextualTrace())
208 XBT_INFO("%s", s.c_str());
209 simgrid::mc::session->logState();
212 void MC_automaton_load(const char *file)
214 if (simgrid::mc::property_automaton == nullptr)
215 simgrid::mc::property_automaton = xbt_automaton_new();
217 xbt_automaton_load(simgrid::mc::property_automaton, file);
223 void dumpStack(FILE* file, unw_cursor_t cursor)
230 const char * name = !unw_get_proc_name(&cursor, buffer, 100, &off) ? buffer : "?";
234 // Demangle C++ names:
235 char* realname = abi::__cxa_demangle(name, 0, 0, &status);
237 #if defined(__x86_64__)
240 unw_get_reg(&cursor, UNW_X86_64_RIP, &rip);
241 unw_get_reg(&cursor, UNW_X86_64_RSP, &rsp);
242 fprintf(file, " %i: %s (RIP=0x%" PRIx64 " RSP=0x%" PRIx64 ")\n",
243 nframe, realname ? realname : name, (std::uint64_t) rip, (std::uint64_t) rsp);
245 fprintf(file, " %i: %s\n", nframe, realname ? realname : name);
250 } while(unw_step(&cursor));
256 static void MC_dump_stacks(FILE* file)
259 for (auto const& stack : mc_model_checker->process().stack_areas()) {
260 fprintf(file, "Stack %i:\n", nstack++);
262 simgrid::mc::UnwindContext context;
263 unw_context_t raw_context =
264 mc_model_checker->process().read<unw_context_t>(
265 simgrid::mc::remote((unw_context_t *)stack.context));
266 context.initialize(&mc_model_checker->process(), &raw_context);
268 unw_cursor_t cursor = context.cursor();
269 simgrid::mc::dumpStack(file, cursor);
274 double MC_process_clock_get(smx_process_t process)
276 if (simgrid::mc::processes_time.empty())
278 if (process != nullptr)
279 return simgrid::mc::processes_time[process->pid];
283 void MC_process_clock_add(smx_process_t process, double amount)
285 simgrid::mc::processes_time[process->pid] += amount;