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"
47 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_global, mc,
48 "Logging specific to MC (global)");
57 std::vector<double> processes_time;
63 int user_max_depth_reached = 0;
65 /* MC global data structures */
66 mc_state_t mc_current_state = nullptr;
67 char mc_replay_mode = FALSE;
69 mc_stats_t mc_stats = nullptr;
70 mc_global_t initial_global_state = nullptr;
71 xbt_fifo_t mc_stack = nullptr;
78 xbt_automaton_t property_automaton = nullptr;
84 FILE *dot_output = nullptr;
85 const char *colors[13];
88 /******************************* Initialisation of MC *******************************/
89 /*********************************************************************************/
91 void MC_init_dot_output()
92 { /* FIXME : more colors */
97 colors[3] = "goldenrod";
100 colors[6] = "magenta";
101 colors[7] = "turquoise4";
102 colors[8] = "gray25";
103 colors[9] = "forestgreen";
104 colors[10] = "hotpink";
105 colors[11] = "lightblue";
108 dot_output = fopen(_sg_mc_dot_output_file, "w");
110 if (dot_output == nullptr) {
111 perror("Error open dot output file");
116 "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");
123 simgrid::mc::processes_time.resize(simix_process_maxpid);
125 if (_sg_mc_visited > 0 || _sg_mc_liveness || _sg_mc_termination || mc_mode == MC_MODE_SERVER) {
126 /* Those requests are handled on the client side and propagated by message
129 MC_ignore_heap(simgrid::mc::processes_time.data(),
130 simix_process_maxpid * sizeof(double));
132 smx_process_t process;
133 xbt_swag_foreach(process, simix_global->process_list)
134 MC_ignore_heap(&(process->process_hookup), sizeof(process->process_hookup));
140 /******************************* Core of MC *******************************/
141 /**************************************************************************/
145 mc_mode = MC_MODE_CLIENT;
147 MC_client_main_loop();
152 simgrid::mc::processes_time.clear();
158 int MC_deadlock_check()
160 if (mc_mode == MC_MODE_SERVER) {
162 if ((res = mc_model_checker->process().send_message(MC_MESSAGE_DEADLOCK_CHECK)))
163 xbt_die("Could not check deadlock state");
164 s_mc_int_message_t message;
165 ssize_t s = mc_model_checker->process().receive_message(message);
167 xbt_die("Could not receive message");
168 if (s != sizeof(message) || message.type != MC_MESSAGE_DEADLOCK_CHECK_REPLY)
169 xbt_die("%s received unexpected message %s (%i, size=%i) "
170 "expected MC_MESSAGE_DEADLOCK_CHECK_REPLY (%i, size=%i)",
171 MC_mode_name(mc_mode),
172 MC_message_type_name(message.type), (int) message.type, (int) s,
173 (int) MC_MESSAGE_DEADLOCK_CHECK_REPLY, (int) sizeof(message)
175 return message.value;
178 int deadlock = FALSE;
179 smx_process_t process;
180 if (xbt_swag_size(simix_global->process_list)) {
182 xbt_swag_foreach(process, simix_global->process_list)
183 if (simgrid::mc::process_is_enabled(process)) {
193 * \brief Re-executes from the state at position start all the transitions indicated by
194 * a given model-checker stack.
195 * \param stack The stack with the transitions to execute.
196 * \param start Start index to begin the re-execution.
198 void MC_replay(xbt_fifo_t stack)
200 int value, count = 1;
202 smx_simcall_t req = nullptr, saved_req = NULL;
203 xbt_fifo_item_t item, start_item;
206 XBT_DEBUG("**** Begin Replay ****");
208 /* Intermediate backtracking */
209 if(_sg_mc_checkpoint > 0 || _sg_mc_termination || _sg_mc_visited > 0) {
210 start_item = xbt_fifo_get_first_item(stack);
211 state = (mc_state_t)xbt_fifo_get_item_content(start_item);
212 if(state->system_state){
213 simgrid::mc::restore_snapshot(state->system_state);
214 if(_sg_mc_comms_determinism || _sg_mc_send_determinism)
215 MC_restore_communications_pattern(state);
221 /* Restore the initial state */
222 simgrid::mc::restore_snapshot(initial_global_state->snapshot);
223 /* At the moment of taking the snapshot the raw heap was set, so restoring
224 * it will set it back again, we have to unset it to continue */
226 start_item = xbt_fifo_get_last_item(stack);
228 if (_sg_mc_comms_determinism || _sg_mc_send_determinism) {
229 // int n = xbt_dynar_length(incomplete_communications_pattern);
230 unsigned n = MC_smx_get_maxpid();
231 assert(n == xbt_dynar_length(incomplete_communications_pattern));
232 assert(n == xbt_dynar_length(initial_communications_pattern));
233 for (unsigned j=0; j < n ; j++) {
234 xbt_dynar_reset((xbt_dynar_t)xbt_dynar_get_as(incomplete_communications_pattern, j, xbt_dynar_t));
235 xbt_dynar_get_as(initial_communications_pattern, j, mc_list_comm_pattern_t)->index_comm = 0;
239 /* Traverse the stack from the state at position start and re-execute the transitions */
240 for (item = start_item;
241 item != xbt_fifo_get_first_item(stack);
242 item = xbt_fifo_get_prev_item(item)) {
244 state = (mc_state_t) xbt_fifo_get_item_content(item);
245 saved_req = MC_state_get_executed_request(state, &value);
248 /* because we got a copy of the executed request, we have to fetch the
249 real one, pointed by the request field of the issuer process */
251 const smx_process_t issuer = MC_smx_simcall_get_issuer(saved_req);
252 req = &issuer->simcall;
254 /* Debug information */
255 if (XBT_LOG_ISENABLED(mc_global, xbt_log_priority_debug)) {
256 req_str = simgrid::mc::request_to_string(req, value, simgrid::mc::RequestType::simix);
257 XBT_DEBUG("Replay: %s (%p)", req_str, state);
261 /* TODO : handle test and testany simcalls */
262 e_mc_call_type_t call = MC_CALL_TYPE_NONE;
263 if (_sg_mc_comms_determinism || _sg_mc_send_determinism)
264 call = MC_get_call_type(req);
266 simgrid::mc::handle_simcall(req, value);
268 if (_sg_mc_comms_determinism || _sg_mc_send_determinism)
269 MC_handle_comm_pattern(call, req, value, nullptr, 1);
271 mc_model_checker->wait_for_requests();
276 /* Update statistics */
277 mc_stats->visited_states++;
278 mc_stats->executed_transitions++;
282 XBT_DEBUG("**** End Replay ****");
285 void MC_replay_liveness(xbt_fifo_t stack)
287 xbt_fifo_item_t item;
288 simgrid::mc::Pair* pair = nullptr;
289 mc_state_t state = nullptr;
290 smx_simcall_t req = nullptr, saved_req = NULL;
291 int value, depth = 1;
294 XBT_DEBUG("**** Begin Replay ****");
296 /* Intermediate backtracking */
297 if(_sg_mc_checkpoint > 0) {
298 item = xbt_fifo_get_first_item(stack);
299 pair = (simgrid::mc::Pair*) xbt_fifo_get_item_content(item);
300 if(pair->graph_state->system_state){
301 simgrid::mc::restore_snapshot(pair->graph_state->system_state);
306 /* Restore the initial state */
307 simgrid::mc::restore_snapshot(initial_global_state->snapshot);
309 /* Traverse the stack from the initial state and re-execute the transitions */
310 for (item = xbt_fifo_get_last_item(stack);
311 item != xbt_fifo_get_first_item(stack);
312 item = xbt_fifo_get_prev_item(item)) {
314 pair = (simgrid::mc::Pair*) xbt_fifo_get_item_content(item);
316 state = (mc_state_t) pair->graph_state;
318 if (pair->exploration_started) {
320 saved_req = MC_state_get_executed_request(state, &value);
322 if (saved_req != nullptr) {
323 /* because we got a copy of the executed request, we have to fetch the
324 real one, pointed by the request field of the issuer process */
325 const smx_process_t issuer = MC_smx_simcall_get_issuer(saved_req);
326 req = &issuer->simcall;
328 /* Debug information */
329 if (XBT_LOG_ISENABLED(mc_global, xbt_log_priority_debug)) {
330 req_str = simgrid::mc::request_to_string(req, value, simgrid::mc::RequestType::simix);
331 XBT_DEBUG("Replay (depth = %d) : %s (%p)", depth, req_str, state);
337 simgrid::mc::handle_simcall(req, value);
338 mc_model_checker->wait_for_requests();
341 /* Update statistics */
342 mc_stats->visited_pairs++;
343 mc_stats->executed_transitions++;
349 XBT_DEBUG("**** End Replay ****");
353 * \brief Dumps the contents of a model-checker's stack and shows the actual
355 * \param stack The stack to dump
357 void MC_dump_stack_safety(xbt_fifo_t stack)
359 MC_show_stack_safety(stack);
363 while ((state = (mc_state_t) xbt_fifo_pop(stack)) != nullptr)
364 MC_state_delete(state, !state->in_visited_states ? 1 : 0);
368 void MC_show_stack_safety(xbt_fifo_t stack)
372 xbt_fifo_item_t item;
374 char *req_str = nullptr;
376 for (item = xbt_fifo_get_last_item(stack);
377 item; item = xbt_fifo_get_prev_item(item)) {
378 state = (mc_state_t)xbt_fifo_get_item_content(item);
379 req = MC_state_get_executed_request(state, &value);
381 req_str = simgrid::mc::request_to_string(req, value, simgrid::mc::RequestType::executed);
382 XBT_INFO("%s", req_str);
388 void MC_show_deadlock(smx_simcall_t req)
390 /*char *req_str = nullptr; */
391 XBT_INFO("**************************");
392 XBT_INFO("*** DEAD-LOCK DETECTED ***");
393 XBT_INFO("**************************");
394 XBT_INFO("Locked request:");
395 /*req_str = simgrid::mc::request_to_string(req);
396 XBT_INFO("%s", req_str);
397 xbt_free(req_str); */
398 XBT_INFO("Counter-example execution trace:");
399 MC_dump_stack_safety(mc_stack);
400 MC_print_statistics(mc_stats);
403 void MC_show_non_termination(void){
404 XBT_INFO("******************************************");
405 XBT_INFO("*** NON-PROGRESSIVE CYCLE DETECTED ***");
406 XBT_INFO("******************************************");
407 XBT_INFO("Counter-example execution trace:");
408 MC_dump_stack_safety(mc_stack);
409 MC_print_statistics(mc_stats);
415 void show_stack_liveness(xbt_fifo_t stack)
418 simgrid::mc::Pair* pair;
419 xbt_fifo_item_t item;
421 char *req_str = nullptr;
423 for (item = xbt_fifo_get_last_item(stack);
424 item; item = xbt_fifo_get_prev_item(item)) {
425 pair = (simgrid::mc::Pair*) xbt_fifo_get_item_content(item);
426 req = MC_state_get_executed_request(pair->graph_state, &value);
427 if (req && req->call != SIMCALL_NONE) {
428 req_str = simgrid::mc::request_to_string(req, value, simgrid::mc::RequestType::executed);
429 XBT_INFO("%s", req_str);
435 void dump_stack_liveness(xbt_fifo_t stack)
437 simgrid::mc::Pair* pair;
438 while ((pair = (simgrid::mc::Pair*) xbt_fifo_pop(stack)) != nullptr)
439 simgrid::mc::pair_delete(pair);
445 void MC_print_statistics(mc_stats_t stats)
447 if(_sg_mc_comms_determinism) {
448 if (!initial_global_state->recv_deterministic && initial_global_state->send_deterministic){
449 XBT_INFO("******************************************************");
450 XBT_INFO("**** Only-send-deterministic communication pattern ****");
451 XBT_INFO("******************************************************");
452 XBT_INFO("%s", initial_global_state->recv_diff);
453 }else if(!initial_global_state->send_deterministic && initial_global_state->recv_deterministic) {
454 XBT_INFO("******************************************************");
455 XBT_INFO("**** Only-recv-deterministic communication pattern ****");
456 XBT_INFO("******************************************************");
457 XBT_INFO("%s", initial_global_state->send_diff);
461 if (stats->expanded_pairs == 0) {
462 XBT_INFO("Expanded states = %lu", stats->expanded_states);
463 XBT_INFO("Visited states = %lu", stats->visited_states);
465 XBT_INFO("Expanded pairs = %lu", stats->expanded_pairs);
466 XBT_INFO("Visited pairs = %lu", stats->visited_pairs);
468 XBT_INFO("Executed transitions = %lu", stats->executed_transitions);
469 if ((_sg_mc_dot_output_file != nullptr) && (_sg_mc_dot_output_file[0] != '\0')) {
470 fprintf(dot_output, "}\n");
473 if (initial_global_state != nullptr && (_sg_mc_comms_determinism || _sg_mc_send_determinism)) {
474 XBT_INFO("Send-deterministic : %s", !initial_global_state->send_deterministic ? "No" : "Yes");
475 if (_sg_mc_comms_determinism)
476 XBT_INFO("Recv-deterministic : %s", !initial_global_state->recv_deterministic ? "No" : "Yes");
478 if (getenv("SIMGRID_MC_SYSTEM_STATISTICS")){
479 int ret=system("free");
480 if(ret!=0)XBT_WARN("system call did not return 0, but %d",ret);
484 void MC_automaton_load(const char *file)
486 if (simgrid::mc::property_automaton == nullptr)
487 simgrid::mc::property_automaton = xbt_automaton_new();
489 xbt_automaton_load(simgrid::mc::property_automaton, file);
492 // TODO, fix cross-process access (this function is not used)
493 static void MC_dump_stacks(FILE* file)
496 for (auto const& stack : mc_model_checker->process().stack_areas()) {
498 xbt_die("Fix cross-process access to the context");
499 unw_context_t * context = (unw_context_t *)stack.context;
500 fprintf(file, "Stack %i:\n", nstack);
505 unw_init_local (&c, context);
508 const char * name = !unw_get_proc_name(&c, buffer, 100, &off) ? buffer : "?";
509 #if defined(__x86_64__)
512 unw_get_reg(&c, UNW_X86_64_RIP, &rip);
513 unw_get_reg(&c, UNW_X86_64_RSP, &rsp);
514 fprintf(file, " %i: %s (RIP=0x%" PRIx64 " RSP=0x%" PRIx64 ")\n",
515 nframe, name, (std::uint64_t) rip, (std::uint64_t) rsp);
517 fprintf(file, " %i: %s\n", nframe, name);
520 } while(unw_step(&c));
527 double MC_process_clock_get(smx_process_t process)
529 if (simgrid::mc::processes_time.empty())
531 if (process != nullptr)
532 return simgrid::mc::processes_time[process->pid];
536 void MC_process_clock_add(smx_process_t process, double amount)
538 simgrid::mc::processes_time[process->pid] += amount;
542 void MC_report_assertion_error(void)
544 XBT_INFO("**************************");
545 XBT_INFO("*** PROPERTY NOT VALID ***");
546 XBT_INFO("**************************");
547 XBT_INFO("Counter-example execution trace:");
548 MC_record_dump_path(mc_stack);
549 MC_dump_stack_safety(mc_stack);
550 MC_print_statistics(mc_stats);
553 void MC_report_crash(int status)
555 XBT_INFO("**************************");
556 XBT_INFO("** CRASH IN THE PROGRAM **");
557 XBT_INFO("**************************");
558 if (WIFSIGNALED(status))
559 XBT_INFO("From signal: %s", strsignal(WTERMSIG(status)));
560 else if (WIFEXITED(status))
561 XBT_INFO("From exit: %i", WEXITSTATUS(status));
562 if (WCOREDUMP(status))
563 XBT_INFO("A core dump was generated by the system.");
565 XBT_INFO("No core dump was generated by the system.");
566 XBT_INFO("Counter-example execution trace:");
567 MC_record_dump_path(mc_stack);
568 MC_dump_stack_safety(mc_stack);
569 MC_print_statistics(mc_stats);