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. */
25 #include <xbt/automaton.h>
27 #include "src/simix/smx_process_private.h"
30 #include <libunwind.h>
31 #include "src/mc/mc_comm_pattern.h"
32 #include "src/mc/mc_request.h"
33 #include "src/mc/mc_safety.h"
34 #include "src/mc/mc_snapshot.h"
35 #include "src/mc/mc_liveness.h"
36 #include "src/mc/mc_private.h"
37 #include "src/mc/mc_unw.h"
38 #include "src/mc/mc_smx.h"
41 #include "src/mc/mc_record.h"
42 #include "src/mc/mc_protocol.h"
43 #include "src/mc/mc_client.h"
45 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_global, mc,
46 "Logging specific to MC (global)");
53 std::vector<double> processes_time;
59 int user_max_depth_reached = 0;
61 /* MC global data structures */
62 mc_state_t mc_current_state = nullptr;
63 char mc_replay_mode = false;
65 mc_stats_t mc_stats = nullptr;
66 mc_global_t initial_global_state = nullptr;
67 xbt_fifo_t mc_stack = nullptr;
74 xbt_automaton_t property_automaton = nullptr;
80 FILE *dot_output = nullptr;
81 const char *colors[13];
84 /******************************* Initialisation of MC *******************************/
85 /*********************************************************************************/
87 void MC_init_dot_output()
88 { /* FIXME : more colors */
93 colors[3] = "goldenrod";
96 colors[6] = "magenta";
97 colors[7] = "turquoise4";
99 colors[9] = "forestgreen";
100 colors[10] = "hotpink";
101 colors[11] = "lightblue";
104 dot_output = fopen(_sg_mc_dot_output_file, "w");
106 if (dot_output == nullptr) {
107 perror("Error open dot output file");
112 "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");
119 simgrid::mc::processes_time.resize(simix_process_maxpid);
121 if (_sg_mc_visited > 0 || _sg_mc_liveness || _sg_mc_termination || mc_mode == MC_MODE_SERVER) {
122 /* Those requests are handled on the client side and propagated by message
125 MC_ignore_heap(simgrid::mc::processes_time.data(),
126 simix_process_maxpid * sizeof(double));
128 smx_process_t process;
129 xbt_swag_foreach(process, simix_global->process_list)
130 MC_ignore_heap(&(process->process_hookup), sizeof(process->process_hookup));
136 /******************************* Core of MC *******************************/
137 /**************************************************************************/
141 mc_mode = MC_MODE_CLIENT;
143 MC_client_main_loop();
148 simgrid::mc::processes_time.clear();
154 int MC_deadlock_check()
156 if (mc_mode == MC_MODE_SERVER) {
158 if ((res = mc_model_checker->process().send_message(MC_MESSAGE_DEADLOCK_CHECK)))
159 xbt_die("Could not check deadlock state");
160 s_mc_int_message_t message;
161 ssize_t s = mc_model_checker->process().receive_message(message);
163 xbt_die("Could not receive message");
164 if (s != sizeof(message) || message.type != MC_MESSAGE_DEADLOCK_CHECK_REPLY)
165 xbt_die("%s received unexpected message %s (%i, size=%i) "
166 "expected MC_MESSAGE_DEADLOCK_CHECK_REPLY (%i, size=%i)",
167 MC_mode_name(mc_mode),
168 MC_message_type_name(message.type), (int) message.type, (int) s,
169 (int) MC_MESSAGE_DEADLOCK_CHECK_REPLY, (int) sizeof(message)
171 return message.value;
174 bool deadlock = false;
175 smx_process_t process;
176 if (xbt_swag_size(simix_global->process_list)) {
178 xbt_swag_foreach(process, simix_global->process_list)
179 if (simgrid::mc::process_is_enabled(process)) {
189 * \brief Re-executes from the state at position start all the transitions indicated by
190 * a given model-checker stack.
191 * \param stack The stack with the transitions to execute.
192 * \param start Start index to begin the re-execution.
194 void MC_replay(xbt_fifo_t stack)
196 int value, count = 1;
198 smx_simcall_t req = nullptr, saved_req = NULL;
199 xbt_fifo_item_t item, start_item;
202 XBT_DEBUG("**** Begin Replay ****");
204 /* Intermediate backtracking */
205 if(_sg_mc_checkpoint > 0 || _sg_mc_termination || _sg_mc_visited > 0) {
206 start_item = xbt_fifo_get_first_item(stack);
207 state = (mc_state_t)xbt_fifo_get_item_content(start_item);
208 if(state->system_state){
209 simgrid::mc::restore_snapshot(state->system_state);
210 if(_sg_mc_comms_determinism || _sg_mc_send_determinism)
211 MC_restore_communications_pattern(state);
217 /* Restore the initial state */
218 simgrid::mc::restore_snapshot(initial_global_state->snapshot);
219 /* At the moment of taking the snapshot the raw heap was set, so restoring
220 * it will set it back again, we have to unset it to continue */
222 start_item = xbt_fifo_get_last_item(stack);
224 if (_sg_mc_comms_determinism || _sg_mc_send_determinism) {
225 // int n = xbt_dynar_length(incomplete_communications_pattern);
226 unsigned n = MC_smx_get_maxpid();
227 assert(n == xbt_dynar_length(incomplete_communications_pattern));
228 assert(n == xbt_dynar_length(initial_communications_pattern));
229 for (unsigned j=0; j < n ; j++) {
230 xbt_dynar_reset((xbt_dynar_t)xbt_dynar_get_as(incomplete_communications_pattern, j, xbt_dynar_t));
231 xbt_dynar_get_as(initial_communications_pattern, j, mc_list_comm_pattern_t)->index_comm = 0;
235 /* Traverse the stack from the state at position start and re-execute the transitions */
236 for (item = start_item;
237 item != xbt_fifo_get_first_item(stack);
238 item = xbt_fifo_get_prev_item(item)) {
240 state = (mc_state_t) xbt_fifo_get_item_content(item);
241 saved_req = MC_state_get_executed_request(state, &value);
244 /* because we got a copy of the executed request, we have to fetch the
245 real one, pointed by the request field of the issuer process */
247 const smx_process_t issuer = MC_smx_simcall_get_issuer(saved_req);
248 req = &issuer->simcall;
250 /* Debug information */
251 if (XBT_LOG_ISENABLED(mc_global, xbt_log_priority_debug)) {
252 req_str = simgrid::mc::request_to_string(req, value, simgrid::mc::RequestType::simix);
253 XBT_DEBUG("Replay: %s (%p)", req_str, state);
257 /* TODO : handle test and testany simcalls */
258 e_mc_call_type_t call = MC_CALL_TYPE_NONE;
259 if (_sg_mc_comms_determinism || _sg_mc_send_determinism)
260 call = MC_get_call_type(req);
262 simgrid::mc::handle_simcall(req, value);
264 if (_sg_mc_comms_determinism || _sg_mc_send_determinism)
265 MC_handle_comm_pattern(call, req, value, nullptr, 1);
267 mc_model_checker->wait_for_requests();
272 /* Update statistics */
273 mc_stats->visited_states++;
274 mc_stats->executed_transitions++;
278 XBT_DEBUG("**** End Replay ****");
281 void MC_replay_liveness(xbt_fifo_t stack)
283 xbt_fifo_item_t item;
284 simgrid::mc::Pair* pair = nullptr;
285 mc_state_t state = nullptr;
286 smx_simcall_t req = nullptr, saved_req = NULL;
287 int value, depth = 1;
290 XBT_DEBUG("**** Begin Replay ****");
292 /* Intermediate backtracking */
293 if(_sg_mc_checkpoint > 0) {
294 item = xbt_fifo_get_first_item(stack);
295 pair = (simgrid::mc::Pair*) xbt_fifo_get_item_content(item);
296 if(pair->graph_state->system_state){
297 simgrid::mc::restore_snapshot(pair->graph_state->system_state);
302 /* Restore the initial state */
303 simgrid::mc::restore_snapshot(initial_global_state->snapshot);
305 /* Traverse the stack from the initial state and re-execute the transitions */
306 for (item = xbt_fifo_get_last_item(stack);
307 item != xbt_fifo_get_first_item(stack);
308 item = xbt_fifo_get_prev_item(item)) {
310 pair = (simgrid::mc::Pair*) xbt_fifo_get_item_content(item);
312 state = (mc_state_t) pair->graph_state;
314 if (pair->exploration_started) {
316 saved_req = MC_state_get_executed_request(state, &value);
318 if (saved_req != nullptr) {
319 /* because we got a copy of the executed request, we have to fetch the
320 real one, pointed by the request field of the issuer process */
321 const smx_process_t issuer = MC_smx_simcall_get_issuer(saved_req);
322 req = &issuer->simcall;
324 /* Debug information */
325 if (XBT_LOG_ISENABLED(mc_global, xbt_log_priority_debug)) {
326 req_str = simgrid::mc::request_to_string(req, value, simgrid::mc::RequestType::simix);
327 XBT_DEBUG("Replay (depth = %d) : %s (%p)", depth, req_str, state);
333 simgrid::mc::handle_simcall(req, value);
334 mc_model_checker->wait_for_requests();
337 /* Update statistics */
338 mc_stats->visited_pairs++;
339 mc_stats->executed_transitions++;
345 XBT_DEBUG("**** End Replay ****");
349 * \brief Dumps the contents of a model-checker's stack and shows the actual
351 * \param stack The stack to dump
353 void MC_dump_stack_safety(xbt_fifo_t stack)
355 MC_show_stack_safety(stack);
359 while ((state = (mc_state_t) xbt_fifo_pop(stack)) != nullptr)
360 MC_state_delete(state, !state->in_visited_states ? 1 : 0);
364 void MC_show_stack_safety(xbt_fifo_t stack)
368 xbt_fifo_item_t item;
370 char *req_str = nullptr;
372 for (item = xbt_fifo_get_last_item(stack);
373 item; item = xbt_fifo_get_prev_item(item)) {
374 state = (mc_state_t)xbt_fifo_get_item_content(item);
375 req = MC_state_get_executed_request(state, &value);
377 req_str = simgrid::mc::request_to_string(req, value, simgrid::mc::RequestType::executed);
378 XBT_INFO("%s", req_str);
384 void MC_show_deadlock(smx_simcall_t req)
386 /*char *req_str = nullptr; */
387 XBT_INFO("**************************");
388 XBT_INFO("*** DEAD-LOCK DETECTED ***");
389 XBT_INFO("**************************");
390 XBT_INFO("Locked request:");
391 /*req_str = simgrid::mc::request_to_string(req);
392 XBT_INFO("%s", req_str);
393 xbt_free(req_str); */
394 XBT_INFO("Counter-example execution trace:");
395 MC_dump_stack_safety(mc_stack);
396 MC_print_statistics(mc_stats);
399 void MC_show_non_termination(void){
400 XBT_INFO("******************************************");
401 XBT_INFO("*** NON-PROGRESSIVE CYCLE DETECTED ***");
402 XBT_INFO("******************************************");
403 XBT_INFO("Counter-example execution trace:");
404 MC_dump_stack_safety(mc_stack);
405 MC_print_statistics(mc_stats);
411 void show_stack_liveness(xbt_fifo_t stack)
414 simgrid::mc::Pair* pair;
415 xbt_fifo_item_t item;
417 char *req_str = nullptr;
419 for (item = xbt_fifo_get_last_item(stack);
420 item; item = xbt_fifo_get_prev_item(item)) {
421 pair = (simgrid::mc::Pair*) xbt_fifo_get_item_content(item);
422 req = MC_state_get_executed_request(pair->graph_state, &value);
423 if (req && req->call != SIMCALL_NONE) {
424 req_str = simgrid::mc::request_to_string(req, value, simgrid::mc::RequestType::executed);
425 XBT_INFO("%s", req_str);
431 void dump_stack_liveness(xbt_fifo_t stack)
433 simgrid::mc::Pair* pair;
434 while ((pair = (simgrid::mc::Pair*) xbt_fifo_pop(stack)) != nullptr)
441 void MC_print_statistics(mc_stats_t stats)
443 if(_sg_mc_comms_determinism) {
444 if (!initial_global_state->recv_deterministic && initial_global_state->send_deterministic){
445 XBT_INFO("******************************************************");
446 XBT_INFO("**** Only-send-deterministic communication pattern ****");
447 XBT_INFO("******************************************************");
448 XBT_INFO("%s", initial_global_state->recv_diff);
449 }else if(!initial_global_state->send_deterministic && initial_global_state->recv_deterministic) {
450 XBT_INFO("******************************************************");
451 XBT_INFO("**** Only-recv-deterministic communication pattern ****");
452 XBT_INFO("******************************************************");
453 XBT_INFO("%s", initial_global_state->send_diff);
457 if (stats->expanded_pairs == 0) {
458 XBT_INFO("Expanded states = %lu", stats->expanded_states);
459 XBT_INFO("Visited states = %lu", stats->visited_states);
461 XBT_INFO("Expanded pairs = %lu", stats->expanded_pairs);
462 XBT_INFO("Visited pairs = %lu", stats->visited_pairs);
464 XBT_INFO("Executed transitions = %lu", stats->executed_transitions);
465 if ((_sg_mc_dot_output_file != nullptr) && (_sg_mc_dot_output_file[0] != '\0')) {
466 fprintf(dot_output, "}\n");
469 if (initial_global_state != nullptr && (_sg_mc_comms_determinism || _sg_mc_send_determinism)) {
470 XBT_INFO("Send-deterministic : %s", !initial_global_state->send_deterministic ? "No" : "Yes");
471 if (_sg_mc_comms_determinism)
472 XBT_INFO("Recv-deterministic : %s", !initial_global_state->recv_deterministic ? "No" : "Yes");
474 if (getenv("SIMGRID_MC_SYSTEM_STATISTICS")){
475 int ret=system("free");
476 if(ret!=0)XBT_WARN("system call did not return 0, but %d",ret);
480 void MC_automaton_load(const char *file)
482 if (simgrid::mc::property_automaton == nullptr)
483 simgrid::mc::property_automaton = xbt_automaton_new();
485 xbt_automaton_load(simgrid::mc::property_automaton, file);
488 // TODO, fix cross-process access (this function is not used)
489 static void MC_dump_stacks(FILE* file)
492 for (auto const& stack : mc_model_checker->process().stack_areas()) {
494 xbt_die("Fix cross-process access to the context");
495 unw_context_t * context = (unw_context_t *)stack.context;
496 fprintf(file, "Stack %i:\n", nstack);
501 unw_init_local (&c, context);
504 const char * name = !unw_get_proc_name(&c, buffer, 100, &off) ? buffer : "?";
505 #if defined(__x86_64__)
508 unw_get_reg(&c, UNW_X86_64_RIP, &rip);
509 unw_get_reg(&c, UNW_X86_64_RSP, &rsp);
510 fprintf(file, " %i: %s (RIP=0x%" PRIx64 " RSP=0x%" PRIx64 ")\n",
511 nframe, name, (std::uint64_t) rip, (std::uint64_t) rsp);
513 fprintf(file, " %i: %s\n", nframe, name);
516 } while(unw_step(&c));
523 double MC_process_clock_get(smx_process_t process)
525 if (simgrid::mc::processes_time.empty())
527 if (process != nullptr)
528 return simgrid::mc::processes_time[process->pid];
532 void MC_process_clock_add(smx_process_t process, double amount)
534 simgrid::mc::processes_time[process->pid] += amount;
538 void MC_report_assertion_error(void)
540 XBT_INFO("**************************");
541 XBT_INFO("*** PROPERTY NOT VALID ***");
542 XBT_INFO("**************************");
543 XBT_INFO("Counter-example execution trace:");
544 MC_record_dump_path(mc_stack);
545 MC_dump_stack_safety(mc_stack);
546 MC_print_statistics(mc_stats);
549 void MC_report_crash(int status)
551 XBT_INFO("**************************");
552 XBT_INFO("** CRASH IN THE PROGRAM **");
553 XBT_INFO("**************************");
554 if (WIFSIGNALED(status))
555 XBT_INFO("From signal: %s", strsignal(WTERMSIG(status)));
556 else if (WIFEXITED(status))
557 XBT_INFO("From exit: %i", WEXITSTATUS(status));
558 if (WCOREDUMP(status))
559 XBT_INFO("A core dump was generated by the system.");
561 XBT_INFO("No core dump was generated by the system.");
562 XBT_INFO("Counter-example execution trace:");
563 MC_record_dump_path(mc_stack);
564 MC_dump_stack_safety(mc_stack);
565 MC_print_statistics(mc_stats);