Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[sonar] Convert integer literals to bool literals.
[simgrid.git] / src / mc / mc_global.cpp
1 /* Copyright (c) 2008-2020. The SimGrid Team. All rights reserved.          */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 #include "mc/mc.h"
7 #include "src/kernel/actor/ActorImpl.hpp"
8
9 #if SIMGRID_HAVE_MC
10 #include "src/mc/Session.hpp"
11 #include "src/mc/checker/Checker.hpp"
12 #include "src/mc/inspect/mc_unw.hpp"
13 #include "src/mc/mc_comm_pattern.hpp"
14 #include "src/mc/mc_config.hpp"
15 #include "src/mc/mc_private.hpp"
16 #include "src/mc/mc_request.hpp"
17 #include "src/mc/mc_safety.hpp"
18 #include "src/mc/mc_smx.hpp"
19 #include "src/mc/remote/AppSide.hpp"
20 #include "src/mc/sosp/Snapshot.hpp"
21 #include "xbt/backtrace.hpp"
22
23 #include <libunwind.h>
24 #endif
25
26 #ifndef _WIN32
27 #include <sys/time.h>
28 #include <sys/wait.h>
29 #include <unistd.h>
30 #endif
31
32 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_global, mc, "Logging specific to MC (global)");
33
34 namespace simgrid {
35 namespace mc {
36
37 std::vector<double> processes_time;
38
39 }
40 }
41
42 #if SIMGRID_HAVE_MC
43
44 /* Liveness */
45
46 namespace simgrid {
47 namespace mc {
48
49 xbt_automaton_t property_automaton = nullptr;
50
51 }
52 }
53
54 /* Dot output */
55 FILE *dot_output = nullptr;
56
57
58 /*******************************  Initialization of MC *******************************/
59 /*********************************************************************************/
60
61 void MC_init_dot_output()
62 {
63   dot_output = fopen(_sg_mc_dot_output_file.get().c_str(), "w");
64
65   if (dot_output == nullptr) {
66     perror("Error open dot output file");
67     xbt_abort();
68   }
69
70   fprintf(dot_output,
71           "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");
72 }
73
74 /*******************************  Core of MC *******************************/
75 /**************************************************************************/
76
77 void MC_run()
78 {
79   simgrid::mc::processes_time.resize(simgrid::kernel::actor::get_maxpid());
80   MC_ignore_heap(simgrid::mc::processes_time.data(),
81     simgrid::mc::processes_time.size() * sizeof(simgrid::mc::processes_time[0]));
82   simgrid::mc::AppSide::get()->main_loop();
83 }
84
85 void MC_show_deadlock()
86 {
87   XBT_INFO("**************************");
88   XBT_INFO("*** DEADLOCK DETECTED ***");
89   XBT_INFO("**************************");
90   XBT_INFO("Counter-example execution trace:");
91   for (auto const& s : mc_model_checker->getChecker()->get_textual_trace())
92     XBT_INFO("  %s", s.c_str());
93   simgrid::mc::dumpRecordPath();
94   simgrid::mc::session->log_state();
95 }
96
97 void MC_automaton_load(const char *file)
98 {
99   if (simgrid::mc::property_automaton == nullptr)
100     simgrid::mc::property_automaton = xbt_automaton_new();
101
102   xbt_automaton_load(simgrid::mc::property_automaton, file);
103 }
104
105 namespace simgrid {
106 namespace mc {
107
108 void dumpStack(FILE* file, unw_cursor_t* cursor)
109 {
110   int nframe = 0;
111   char buffer[100];
112
113   unw_word_t off;
114   do {
115     const char* name = not unw_get_proc_name(cursor, buffer, 100, &off) ? buffer : "?";
116     // Unmangle C++ names:
117     auto realname = simgrid::xbt::demangle(name);
118
119 #if defined(__x86_64__)
120     unw_word_t rip = 0;
121     unw_word_t rsp = 0;
122     unw_get_reg(cursor, UNW_X86_64_RIP, &rip);
123     unw_get_reg(cursor, UNW_X86_64_RSP, &rsp);
124     fprintf(file, "  %i: %s (RIP=0x%" PRIx64 " RSP=0x%" PRIx64 ")\n", nframe, realname.get(), (std::uint64_t)rip,
125             (std::uint64_t)rsp);
126 #else
127     fprintf(file, "  %i: %s\n", nframe, realname.get());
128 #endif
129
130     ++nframe;
131   } while (unw_step(cursor));
132 }
133
134 }
135 }
136 #endif
137
138 double MC_process_clock_get(const simgrid::kernel::actor::ActorImpl* process)
139 {
140   if (simgrid::mc::processes_time.empty())
141     return 0;
142   if (process != nullptr)
143     return simgrid::mc::processes_time[process->get_pid()];
144   return -1;
145 }
146
147 void MC_process_clock_add(const simgrid::kernel::actor::ActorImpl* process, double amount)
148 {
149   simgrid::mc::processes_time[process->get_pid()] += amount;
150 }