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. */
24 #include "src/simix/smx_process_private.h"
27 #include <libunwind.h>
28 #include "src/mc/mc_comm_pattern.h"
29 #include "src/mc/mc_request.h"
30 #include "src/mc/mc_safety.h"
31 #include "src/mc/mc_snapshot.h"
32 #include "src/mc/mc_liveness.h"
33 #include "src/mc/mc_private.h"
34 #include "src/mc/mc_unw.h"
35 #include "src/mc/mc_smx.h"
38 #include "src/mc/mc_record.h"
39 #include "src/mc/mc_protocol.h"
40 #include "src/mc/Client.hpp"
42 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_global, mc, "Logging specific to MC (global)");
49 std::vector<double> processes_time;
55 int user_max_depth_reached = 0;
57 /* MC global data structures */
58 mc_state_t mc_current_state = nullptr;
59 char mc_replay_mode = false;
61 mc_stats_t mc_stats = nullptr;
62 mc_global_t initial_global_state = nullptr;
63 xbt_fifo_t mc_stack = nullptr;
70 xbt_automaton_t property_automaton = nullptr;
76 FILE *dot_output = nullptr;
79 /******************************* Initialisation of MC *******************************/
80 /*********************************************************************************/
82 void MC_init_dot_output()
84 dot_output = fopen(_sg_mc_dot_output_file, "w");
86 if (dot_output == nullptr) {
87 perror("Error open dot output file");
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");
99 simgrid::mc::processes_time.resize(simix_process_maxpid);
101 if (_sg_mc_visited > 0 || _sg_mc_liveness || _sg_mc_termination || mc_mode == MC_MODE_SERVER) {
102 /* Those requests are handled on the client side and propagated by message
105 MC_ignore_heap(simgrid::mc::processes_time.data(),
106 simix_process_maxpid * sizeof(double));
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));
116 /******************************* Core of MC *******************************/
117 /**************************************************************************/
121 mc_mode = MC_MODE_CLIENT;
123 simgrid::mc::Client::get()->mainLoop();
128 simgrid::mc::processes_time.clear();
134 * \brief Re-executes from the state at position start all the transitions indicated by
135 * a given model-checker stack.
136 * \param stack The stack with the transitions to execute.
137 * \param start Start index to begin the re-execution.
139 void MC_replay(xbt_fifo_t stack)
141 int value, count = 1;
143 smx_simcall_t req = nullptr, saved_req = NULL;
144 xbt_fifo_item_t item, start_item;
147 XBT_DEBUG("**** Begin Replay ****");
149 /* Intermediate backtracking */
150 if(_sg_mc_checkpoint > 0 || _sg_mc_termination || _sg_mc_visited > 0) {
151 start_item = xbt_fifo_get_first_item(stack);
152 state = (mc_state_t)xbt_fifo_get_item_content(start_item);
153 if(state->system_state){
154 simgrid::mc::restore_snapshot(state->system_state);
155 if(_sg_mc_comms_determinism || _sg_mc_send_determinism)
156 MC_restore_communications_pattern(state);
162 /* Restore the initial state */
163 simgrid::mc::restore_snapshot(initial_global_state->snapshot);
164 /* At the moment of taking the snapshot the raw heap was set, so restoring
165 * it will set it back again, we have to unset it to continue */
167 start_item = xbt_fifo_get_last_item(stack);
169 if (_sg_mc_comms_determinism || _sg_mc_send_determinism) {
170 // int n = xbt_dynar_length(incomplete_communications_pattern);
171 unsigned n = MC_smx_get_maxpid();
172 assert(n == xbt_dynar_length(incomplete_communications_pattern));
173 assert(n == xbt_dynar_length(initial_communications_pattern));
174 for (unsigned j=0; j < n ; j++) {
175 xbt_dynar_reset((xbt_dynar_t)xbt_dynar_get_as(incomplete_communications_pattern, j, xbt_dynar_t));
176 xbt_dynar_get_as(initial_communications_pattern, j, mc_list_comm_pattern_t)->index_comm = 0;
180 /* Traverse the stack from the state at position start and re-execute the transitions */
181 for (item = start_item;
182 item != xbt_fifo_get_first_item(stack);
183 item = xbt_fifo_get_prev_item(item)) {
185 state = (mc_state_t) xbt_fifo_get_item_content(item);
186 saved_req = MC_state_get_executed_request(state, &value);
189 /* because we got a copy of the executed request, we have to fetch the
190 real one, pointed by the request field of the issuer process */
192 const smx_process_t issuer = MC_smx_simcall_get_issuer(saved_req);
193 req = &issuer->simcall;
195 /* Debug information */
196 if (XBT_LOG_ISENABLED(mc_global, xbt_log_priority_debug)) {
197 req_str = simgrid::mc::request_to_string(req, value, simgrid::mc::RequestType::simix);
198 XBT_DEBUG("Replay: %s (%p)", req_str, state);
202 /* TODO : handle test and testany simcalls */
203 e_mc_call_type_t call = MC_CALL_TYPE_NONE;
204 if (_sg_mc_comms_determinism || _sg_mc_send_determinism)
205 call = MC_get_call_type(req);
207 simgrid::mc::handle_simcall(req, value);
209 if (_sg_mc_comms_determinism || _sg_mc_send_determinism)
210 MC_handle_comm_pattern(call, req, value, nullptr, 1);
212 mc_model_checker->wait_for_requests();
217 /* Update statistics */
218 mc_stats->visited_states++;
219 mc_stats->executed_transitions++;
223 XBT_DEBUG("**** End Replay ****");
227 * \brief Dumps the contents of a model-checker's stack and shows the actual
229 * \param stack The stack to dump
231 void MC_dump_stack_safety(xbt_fifo_t stack)
233 MC_show_stack_safety(stack);
237 while ((state = (mc_state_t) xbt_fifo_pop(stack)) != nullptr)
238 MC_state_delete(state, !state->in_visited_states ? 1 : 0);
242 void MC_show_stack_safety(xbt_fifo_t stack)
246 xbt_fifo_item_t item;
248 char *req_str = nullptr;
250 for (item = xbt_fifo_get_last_item(stack);
251 item; item = xbt_fifo_get_prev_item(item)) {
252 state = (mc_state_t)xbt_fifo_get_item_content(item);
253 req = MC_state_get_executed_request(state, &value);
255 req_str = simgrid::mc::request_to_string(req, value, simgrid::mc::RequestType::executed);
256 XBT_INFO("%s", req_str);
262 void MC_show_deadlock(smx_simcall_t req)
264 /*char *req_str = nullptr; */
265 XBT_INFO("**************************");
266 XBT_INFO("*** DEAD-LOCK DETECTED ***");
267 XBT_INFO("**************************");
268 XBT_INFO("Locked request:");
269 /*req_str = simgrid::mc::request_to_string(req);
270 XBT_INFO("%s", req_str);
271 xbt_free(req_str); */
272 XBT_INFO("Counter-example execution trace:");
273 MC_dump_stack_safety(mc_stack);
274 MC_print_statistics(mc_stats);
277 void MC_show_non_termination(void){
278 XBT_INFO("******************************************");
279 XBT_INFO("*** NON-PROGRESSIVE CYCLE DETECTED ***");
280 XBT_INFO("******************************************");
281 XBT_INFO("Counter-example execution trace:");
282 MC_dump_stack_safety(mc_stack);
283 MC_print_statistics(mc_stats);
286 void MC_print_statistics(mc_stats_t stats)
288 if(_sg_mc_comms_determinism) {
289 if (!initial_global_state->recv_deterministic && initial_global_state->send_deterministic){
290 XBT_INFO("******************************************************");
291 XBT_INFO("**** Only-send-deterministic communication pattern ****");
292 XBT_INFO("******************************************************");
293 XBT_INFO("%s", initial_global_state->recv_diff);
294 }else if(!initial_global_state->send_deterministic && initial_global_state->recv_deterministic) {
295 XBT_INFO("******************************************************");
296 XBT_INFO("**** Only-recv-deterministic communication pattern ****");
297 XBT_INFO("******************************************************");
298 XBT_INFO("%s", initial_global_state->send_diff);
302 if (stats->expanded_pairs == 0) {
303 XBT_INFO("Expanded states = %lu", stats->expanded_states);
304 XBT_INFO("Visited states = %lu", stats->visited_states);
306 XBT_INFO("Expanded pairs = %lu", stats->expanded_pairs);
307 XBT_INFO("Visited pairs = %lu", stats->visited_pairs);
309 XBT_INFO("Executed transitions = %lu", stats->executed_transitions);
310 if ((_sg_mc_dot_output_file != nullptr) && (_sg_mc_dot_output_file[0] != '\0')) {
311 fprintf(dot_output, "}\n");
314 if (initial_global_state != nullptr && (_sg_mc_comms_determinism || _sg_mc_send_determinism)) {
315 XBT_INFO("Send-deterministic : %s", !initial_global_state->send_deterministic ? "No" : "Yes");
316 if (_sg_mc_comms_determinism)
317 XBT_INFO("Recv-deterministic : %s", !initial_global_state->recv_deterministic ? "No" : "Yes");
319 if (getenv("SIMGRID_MC_SYSTEM_STATISTICS")){
320 int ret=system("free");
321 if(ret!=0)XBT_WARN("system call did not return 0, but %d",ret);
325 void MC_automaton_load(const char *file)
327 if (simgrid::mc::property_automaton == nullptr)
328 simgrid::mc::property_automaton = xbt_automaton_new();
330 xbt_automaton_load(simgrid::mc::property_automaton, file);
333 // TODO, fix cross-process access (this function is not used)
334 static void MC_dump_stacks(FILE* file)
337 for (auto const& stack : mc_model_checker->process().stack_areas()) {
339 xbt_die("Fix cross-process access to the context");
340 unw_context_t * context = (unw_context_t *)stack.context;
341 fprintf(file, "Stack %i:\n", nstack);
346 unw_init_local (&c, context);
349 const char * name = !unw_get_proc_name(&c, buffer, 100, &off) ? buffer : "?";
350 #if defined(__x86_64__)
353 unw_get_reg(&c, UNW_X86_64_RIP, &rip);
354 unw_get_reg(&c, UNW_X86_64_RSP, &rsp);
355 fprintf(file, " %i: %s (RIP=0x%" PRIx64 " RSP=0x%" PRIx64 ")\n",
356 nframe, name, (std::uint64_t) rip, (std::uint64_t) rsp);
358 fprintf(file, " %i: %s\n", nframe, name);
361 } while(unw_step(&c));
368 double MC_process_clock_get(smx_process_t process)
370 if (simgrid::mc::processes_time.empty())
372 if (process != nullptr)
373 return simgrid::mc::processes_time[process->pid];
377 void MC_process_clock_add(smx_process_t process, double amount)
379 simgrid::mc::processes_time[process->pid] += amount;
383 void MC_report_assertion_error(void)
385 XBT_INFO("**************************");
386 XBT_INFO("*** PROPERTY NOT VALID ***");
387 XBT_INFO("**************************");
388 XBT_INFO("Counter-example execution trace:");
389 MC_record_dump_path(mc_stack);
390 MC_dump_stack_safety(mc_stack);
391 MC_print_statistics(mc_stats);
394 void MC_report_crash(int status)
396 XBT_INFO("**************************");
397 XBT_INFO("** CRASH IN THE PROGRAM **");
398 XBT_INFO("**************************");
399 if (WIFSIGNALED(status))
400 XBT_INFO("From signal: %s", strsignal(WTERMSIG(status)));
401 else if (WIFEXITED(status))
402 XBT_INFO("From exit: %i", WEXITSTATUS(status));
403 if (WCOREDUMP(status))
404 XBT_INFO("A core dump was generated by the system.");
406 XBT_INFO("No core dump was generated by the system.");
407 XBT_INFO("Counter-example execution trace:");
408 MC_record_dump_path(mc_stack);
409 MC_dump_stack_safety(mc_stack);
410 MC_print_statistics(mc_stats);