Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
mc: snake_case the checkers
[simgrid.git] / src / mc / mc_global.cpp
1 /* Copyright (c) 2008-2019. 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/Client.hpp"
20 #include "src/mc/sosp/mc_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
75 /*******************************  Core of MC *******************************/
76 /**************************************************************************/
77
78 void MC_run()
79 {
80   simgrid::mc::processes_time.resize(SIMIX_process_get_maxpid());
81   MC_ignore_heap(simgrid::mc::processes_time.data(),
82     simgrid::mc::processes_time.size() * sizeof(simgrid::mc::processes_time[0]));
83   simgrid::mc::Client::get()->mainLoop();
84 }
85
86 void MC_show_deadlock()
87 {
88   XBT_INFO("**************************");
89   XBT_INFO("*** DEAD-LOCK DETECTED ***");
90   XBT_INFO("**************************");
91   XBT_INFO("Counter-example execution trace:");
92   for (auto const& s : mc_model_checker->getChecker()->get_textual_trace())
93     XBT_INFO("  %s", s.c_str());
94   simgrid::mc::dumpRecordPath();
95   simgrid::mc::session->log_state();
96 }
97
98 void MC_automaton_load(const char *file)
99 {
100   if (simgrid::mc::property_automaton == nullptr)
101     simgrid::mc::property_automaton = xbt_automaton_new();
102
103   xbt_automaton_load(simgrid::mc::property_automaton, file);
104 }
105
106 namespace simgrid {
107 namespace mc {
108
109 void dumpStack(FILE* file, unw_cursor_t&& cursor)
110 {
111   int nframe = 0;
112   char buffer[100];
113
114   unw_word_t off;
115   do {
116     const char* name = not unw_get_proc_name(&cursor, buffer, 100, &off) ? buffer : "?";
117     // Unmangle C++ names:
118     auto realname = simgrid::xbt::demangle(name);
119
120 #if defined(__x86_64__)
121     unw_word_t rip = 0;
122     unw_word_t rsp = 0;
123     unw_get_reg(&cursor, UNW_X86_64_RIP, &rip);
124     unw_get_reg(&cursor, UNW_X86_64_RSP, &rsp);
125     fprintf(file, "  %i: %s (RIP=0x%" PRIx64 " RSP=0x%" PRIx64 ")\n", nframe, realname.get(), (std::uint64_t)rip,
126             (std::uint64_t)rsp);
127 #else
128     fprintf(file, "  %i: %s\n", nframe, realname.get());
129 #endif
130
131     ++nframe;
132   } while(unw_step(&cursor));
133 }
134
135 }
136 }
137
138 static void MC_dump_stacks(FILE* file)
139 {
140   int nstack = 0;
141   for (auto const& stack : mc_model_checker->process().stack_areas()) {
142     fprintf(file, "Stack %i:\n", nstack);
143     nstack++;
144
145     simgrid::mc::UnwindContext context;
146     unw_context_t raw_context =
147       (unw_context_t) mc_model_checker->process().read<unw_context_t>(
148         simgrid::mc::remote((unw_context_t *)stack.context));
149     context.initialize(&mc_model_checker->process(), &raw_context);
150
151     unw_cursor_t cursor = context.cursor();
152     simgrid::mc::dumpStack(file, std::move(cursor));
153   }
154 }
155 #endif
156
157 double MC_process_clock_get(smx_actor_t process)
158 {
159   if (simgrid::mc::processes_time.empty())
160     return 0;
161   if (process != nullptr)
162     return simgrid::mc::processes_time[process->get_pid()];
163   return -1;
164 }
165
166 void MC_process_clock_add(smx_actor_t process, double amount)
167 {
168   simgrid::mc::processes_time[process->get_pid()] += amount;
169 }