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/Client.hpp"
45 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_global, mc, "Logging specific to MC (global)");
52 std::vector<double> processes_time;
58 int user_max_depth_reached = 0;
60 /* MC global data structures */
61 mc_state_t mc_current_state = nullptr;
62 char mc_replay_mode = false;
64 mc_stats_t mc_stats = nullptr;
65 mc_global_t initial_global_state = nullptr;
66 xbt_fifo_t mc_stack = 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");
102 simgrid::mc::processes_time.resize(simix_process_maxpid);
104 if (_sg_mc_visited > 0 || _sg_mc_liveness || _sg_mc_termination || mc_mode == MC_MODE_SERVER) {
105 /* Those requests are handled on the client side and propagated by message
108 MC_ignore_heap(simgrid::mc::processes_time.data(),
109 simix_process_maxpid * sizeof(double));
111 smx_process_t process;
112 xbt_swag_foreach(process, simix_global->process_list)
113 MC_ignore_heap(&(process->process_hookup), sizeof(process->process_hookup));
119 /******************************* Core of MC *******************************/
120 /**************************************************************************/
124 mc_mode = MC_MODE_CLIENT;
126 simgrid::mc::Client::get()->mainLoop();
131 simgrid::mc::processes_time.clear();
137 * \brief Re-executes from the state at position start all the transitions indicated by
138 * a given model-checker stack.
139 * \param stack The stack with the transitions to execute.
140 * \param start Start index to begin the re-execution.
142 void MC_replay(xbt_fifo_t stack)
144 int value, count = 1;
146 smx_simcall_t req = nullptr, saved_req = NULL;
147 xbt_fifo_item_t item, start_item;
150 XBT_DEBUG("**** Begin Replay ****");
152 /* Intermediate backtracking */
153 if(_sg_mc_checkpoint > 0 || _sg_mc_termination || _sg_mc_visited > 0) {
154 start_item = xbt_fifo_get_first_item(stack);
155 state = (mc_state_t)xbt_fifo_get_item_content(start_item);
156 if(state->system_state){
157 simgrid::mc::restore_snapshot(state->system_state);
158 if(_sg_mc_comms_determinism || _sg_mc_send_determinism)
159 MC_restore_communications_pattern(state);
165 /* Restore the initial state */
166 simgrid::mc::restore_snapshot(initial_global_state->snapshot);
167 /* At the moment of taking the snapshot the raw heap was set, so restoring
168 * it will set it back again, we have to unset it to continue */
170 start_item = xbt_fifo_get_last_item(stack);
172 if (_sg_mc_comms_determinism || _sg_mc_send_determinism) {
173 // int n = xbt_dynar_length(incomplete_communications_pattern);
174 unsigned n = MC_smx_get_maxpid();
175 assert(n == xbt_dynar_length(incomplete_communications_pattern));
176 assert(n == xbt_dynar_length(initial_communications_pattern));
177 for (unsigned j=0; j < n ; j++) {
178 xbt_dynar_reset((xbt_dynar_t)xbt_dynar_get_as(incomplete_communications_pattern, j, xbt_dynar_t));
179 xbt_dynar_get_as(initial_communications_pattern, j, mc_list_comm_pattern_t)->index_comm = 0;
183 /* Traverse the stack from the state at position start and re-execute the transitions */
184 for (item = start_item;
185 item != xbt_fifo_get_first_item(stack);
186 item = xbt_fifo_get_prev_item(item)) {
188 state = (mc_state_t) xbt_fifo_get_item_content(item);
189 saved_req = MC_state_get_executed_request(state, &value);
192 /* because we got a copy of the executed request, we have to fetch the
193 real one, pointed by the request field of the issuer process */
195 const smx_process_t issuer = MC_smx_simcall_get_issuer(saved_req);
196 req = &issuer->simcall;
198 /* Debug information */
199 if (XBT_LOG_ISENABLED(mc_global, xbt_log_priority_debug)) {
200 req_str = simgrid::mc::request_to_string(req, value, simgrid::mc::RequestType::simix);
201 XBT_DEBUG("Replay: %s (%p)", req_str, state);
205 /* TODO : handle test and testany simcalls */
206 e_mc_call_type_t call = MC_CALL_TYPE_NONE;
207 if (_sg_mc_comms_determinism || _sg_mc_send_determinism)
208 call = MC_get_call_type(req);
210 simgrid::mc::handle_simcall(req, value);
212 if (_sg_mc_comms_determinism || _sg_mc_send_determinism)
213 MC_handle_comm_pattern(call, req, value, nullptr, 1);
215 mc_model_checker->wait_for_requests();
220 /* Update statistics */
221 mc_stats->visited_states++;
222 mc_stats->executed_transitions++;
226 XBT_DEBUG("**** End Replay ****");
229 void MC_replay_liveness(xbt_fifo_t stack)
231 xbt_fifo_item_t item;
232 simgrid::mc::Pair* pair = nullptr;
233 mc_state_t state = nullptr;
234 smx_simcall_t req = nullptr, saved_req = NULL;
235 int value, depth = 1;
238 XBT_DEBUG("**** Begin Replay ****");
240 /* Intermediate backtracking */
241 if(_sg_mc_checkpoint > 0) {
242 item = xbt_fifo_get_first_item(stack);
243 pair = (simgrid::mc::Pair*) xbt_fifo_get_item_content(item);
244 if(pair->graph_state->system_state){
245 simgrid::mc::restore_snapshot(pair->graph_state->system_state);
250 /* Restore the initial state */
251 simgrid::mc::restore_snapshot(initial_global_state->snapshot);
253 /* Traverse the stack from the initial state and re-execute the transitions */
254 for (item = xbt_fifo_get_last_item(stack);
255 item != xbt_fifo_get_first_item(stack);
256 item = xbt_fifo_get_prev_item(item)) {
258 pair = (simgrid::mc::Pair*) xbt_fifo_get_item_content(item);
260 state = (mc_state_t) pair->graph_state;
262 if (pair->exploration_started) {
264 saved_req = MC_state_get_executed_request(state, &value);
266 if (saved_req != nullptr) {
267 /* because we got a copy of the executed request, we have to fetch the
268 real one, pointed by the request field of the issuer process */
269 const smx_process_t issuer = MC_smx_simcall_get_issuer(saved_req);
270 req = &issuer->simcall;
272 /* Debug information */
273 if (XBT_LOG_ISENABLED(mc_global, xbt_log_priority_debug)) {
274 req_str = simgrid::mc::request_to_string(req, value, simgrid::mc::RequestType::simix);
275 XBT_DEBUG("Replay (depth = %d) : %s (%p)", depth, req_str, state);
281 simgrid::mc::handle_simcall(req, value);
282 mc_model_checker->wait_for_requests();
285 /* Update statistics */
286 mc_stats->visited_pairs++;
287 mc_stats->executed_transitions++;
293 XBT_DEBUG("**** End Replay ****");
297 * \brief Dumps the contents of a model-checker's stack and shows the actual
299 * \param stack The stack to dump
301 void MC_dump_stack_safety(xbt_fifo_t stack)
303 MC_show_stack_safety(stack);
307 while ((state = (mc_state_t) xbt_fifo_pop(stack)) != nullptr)
308 MC_state_delete(state, !state->in_visited_states ? 1 : 0);
312 void MC_show_stack_safety(xbt_fifo_t stack)
316 xbt_fifo_item_t item;
318 char *req_str = nullptr;
320 for (item = xbt_fifo_get_last_item(stack);
321 item; item = xbt_fifo_get_prev_item(item)) {
322 state = (mc_state_t)xbt_fifo_get_item_content(item);
323 req = MC_state_get_executed_request(state, &value);
325 req_str = simgrid::mc::request_to_string(req, value, simgrid::mc::RequestType::executed);
326 XBT_INFO("%s", req_str);
332 void MC_show_deadlock(smx_simcall_t req)
334 /*char *req_str = nullptr; */
335 XBT_INFO("**************************");
336 XBT_INFO("*** DEAD-LOCK DETECTED ***");
337 XBT_INFO("**************************");
338 XBT_INFO("Locked request:");
339 /*req_str = simgrid::mc::request_to_string(req);
340 XBT_INFO("%s", req_str);
341 xbt_free(req_str); */
342 XBT_INFO("Counter-example execution trace:");
343 MC_dump_stack_safety(mc_stack);
344 MC_print_statistics(mc_stats);
347 void MC_show_non_termination(void){
348 XBT_INFO("******************************************");
349 XBT_INFO("*** NON-PROGRESSIVE CYCLE DETECTED ***");
350 XBT_INFO("******************************************");
351 XBT_INFO("Counter-example execution trace:");
352 MC_dump_stack_safety(mc_stack);
353 MC_print_statistics(mc_stats);
359 void show_stack_liveness(xbt_fifo_t stack)
362 simgrid::mc::Pair* pair;
363 xbt_fifo_item_t item;
365 char *req_str = nullptr;
367 for (item = xbt_fifo_get_last_item(stack);
368 item; item = xbt_fifo_get_prev_item(item)) {
369 pair = (simgrid::mc::Pair*) xbt_fifo_get_item_content(item);
370 req = MC_state_get_executed_request(pair->graph_state, &value);
371 if (req && req->call != SIMCALL_NONE) {
372 req_str = simgrid::mc::request_to_string(req, value, simgrid::mc::RequestType::executed);
373 XBT_INFO("%s", req_str);
379 void dump_stack_liveness(xbt_fifo_t stack)
381 simgrid::mc::Pair* pair;
382 while ((pair = (simgrid::mc::Pair*) xbt_fifo_pop(stack)) != nullptr)
389 void MC_print_statistics(mc_stats_t stats)
391 if(_sg_mc_comms_determinism) {
392 if (!initial_global_state->recv_deterministic && initial_global_state->send_deterministic){
393 XBT_INFO("******************************************************");
394 XBT_INFO("**** Only-send-deterministic communication pattern ****");
395 XBT_INFO("******************************************************");
396 XBT_INFO("%s", initial_global_state->recv_diff);
397 }else if(!initial_global_state->send_deterministic && initial_global_state->recv_deterministic) {
398 XBT_INFO("******************************************************");
399 XBT_INFO("**** Only-recv-deterministic communication pattern ****");
400 XBT_INFO("******************************************************");
401 XBT_INFO("%s", initial_global_state->send_diff);
405 if (stats->expanded_pairs == 0) {
406 XBT_INFO("Expanded states = %lu", stats->expanded_states);
407 XBT_INFO("Visited states = %lu", stats->visited_states);
409 XBT_INFO("Expanded pairs = %lu", stats->expanded_pairs);
410 XBT_INFO("Visited pairs = %lu", stats->visited_pairs);
412 XBT_INFO("Executed transitions = %lu", stats->executed_transitions);
413 if ((_sg_mc_dot_output_file != nullptr) && (_sg_mc_dot_output_file[0] != '\0')) {
414 fprintf(dot_output, "}\n");
417 if (initial_global_state != nullptr && (_sg_mc_comms_determinism || _sg_mc_send_determinism)) {
418 XBT_INFO("Send-deterministic : %s", !initial_global_state->send_deterministic ? "No" : "Yes");
419 if (_sg_mc_comms_determinism)
420 XBT_INFO("Recv-deterministic : %s", !initial_global_state->recv_deterministic ? "No" : "Yes");
422 if (getenv("SIMGRID_MC_SYSTEM_STATISTICS")){
423 int ret=system("free");
424 if(ret!=0)XBT_WARN("system call did not return 0, but %d",ret);
428 void MC_automaton_load(const char *file)
430 if (simgrid::mc::property_automaton == nullptr)
431 simgrid::mc::property_automaton = xbt_automaton_new();
433 xbt_automaton_load(simgrid::mc::property_automaton, file);
436 // TODO, fix cross-process access (this function is not used)
437 static void MC_dump_stacks(FILE* file)
440 for (auto const& stack : mc_model_checker->process().stack_areas()) {
442 xbt_die("Fix cross-process access to the context");
443 unw_context_t * context = (unw_context_t *)stack.context;
444 fprintf(file, "Stack %i:\n", nstack);
449 unw_init_local (&c, context);
452 const char * name = !unw_get_proc_name(&c, buffer, 100, &off) ? buffer : "?";
453 #if defined(__x86_64__)
456 unw_get_reg(&c, UNW_X86_64_RIP, &rip);
457 unw_get_reg(&c, UNW_X86_64_RSP, &rsp);
458 fprintf(file, " %i: %s (RIP=0x%" PRIx64 " RSP=0x%" PRIx64 ")\n",
459 nframe, name, (std::uint64_t) rip, (std::uint64_t) rsp);
461 fprintf(file, " %i: %s\n", nframe, name);
464 } while(unw_step(&c));
471 double MC_process_clock_get(smx_process_t process)
473 if (simgrid::mc::processes_time.empty())
475 if (process != nullptr)
476 return simgrid::mc::processes_time[process->pid];
480 void MC_process_clock_add(smx_process_t process, double amount)
482 simgrid::mc::processes_time[process->pid] += amount;
486 void MC_report_assertion_error(void)
488 XBT_INFO("**************************");
489 XBT_INFO("*** PROPERTY NOT VALID ***");
490 XBT_INFO("**************************");
491 XBT_INFO("Counter-example execution trace:");
492 MC_record_dump_path(mc_stack);
493 MC_dump_stack_safety(mc_stack);
494 MC_print_statistics(mc_stats);
497 void MC_report_crash(int status)
499 XBT_INFO("**************************");
500 XBT_INFO("** CRASH IN THE PROGRAM **");
501 XBT_INFO("**************************");
502 if (WIFSIGNALED(status))
503 XBT_INFO("From signal: %s", strsignal(WTERMSIG(status)));
504 else if (WIFEXITED(status))
505 XBT_INFO("From exit: %i", WEXITSTATUS(status));
506 if (WCOREDUMP(status))
507 XBT_INFO("A core dump was generated by the system.");
509 XBT_INFO("No core dump was generated by the system.");
510 XBT_INFO("Counter-example execution trace:");
511 MC_record_dump_path(mc_stack);
512 MC_dump_stack_safety(mc_stack);
513 MC_print_statistics(mc_stats);