Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
further tidy the includes in MC
[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 #include "src/mc/Session.hpp"
9 #include "src/mc/mc_config.hpp"
10 #include "src/mc/remote/Client.hpp"
11 #include "xbt/backtrace.hpp"
12
13 #if SIMGRID_HAVE_MC
14 #include "src/mc/checker/Checker.hpp"
15 #include "src/mc/inspect/mc_unw.hpp"
16 #include "src/mc/mc_comm_pattern.hpp"
17 #include "src/mc/mc_private.hpp"
18 #include "src/mc/mc_request.hpp"
19 #include "src/mc/mc_safety.hpp"
20 #include "src/mc/mc_smx.hpp"
21 #include "src/mc/sosp/mc_snapshot.hpp"
22 #include <libunwind.h>
23 #endif
24
25 #ifndef _WIN32
26 #include <sys/time.h>
27 #include <sys/wait.h>
28 #include <unistd.h>
29 #endif
30
31 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_global, mc, "Logging specific to MC (global)");
32
33 namespace simgrid {
34 namespace mc {
35
36 std::vector<double> processes_time;
37
38 }
39 }
40
41 #if SIMGRID_HAVE_MC
42
43 /* Liveness */
44
45 namespace simgrid {
46 namespace mc {
47
48 xbt_automaton_t property_automaton = nullptr;
49
50 }
51 }
52
53 /* Dot output */
54 FILE *dot_output = nullptr;
55
56
57 /*******************************  Initialization of MC *******************************/
58 /*********************************************************************************/
59
60 void MC_init_dot_output()
61 {
62   dot_output = fopen(_sg_mc_dot_output_file.get().c_str(), "w");
63
64   if (dot_output == nullptr) {
65     perror("Error open dot output file");
66     xbt_abort();
67   }
68
69   fprintf(dot_output,
70           "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");
71
72 }
73
74 /*******************************  Core of MC *******************************/
75 /**************************************************************************/
76
77 void MC_run()
78 {
79   simgrid::mc::processes_time.resize(SIMIX_process_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::Client::get()->mainLoop();
83 }
84
85 void MC_show_deadlock()
86 {
87   XBT_INFO("**************************");
88   XBT_INFO("*** DEAD-LOCK DETECTED ***");
89   XBT_INFO("**************************");
90   XBT_INFO("Counter-example execution trace:");
91   for (auto const& s : mc_model_checker->getChecker()->getTextualTrace())
92     XBT_INFO("  %s", s.c_str());
93   simgrid::mc::dumpRecordPath();
94   simgrid::mc::session->logState();
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
137 static void MC_dump_stacks(FILE* file)
138 {
139   int nstack = 0;
140   for (auto const& stack : mc_model_checker->process().stack_areas()) {
141     fprintf(file, "Stack %i:\n", nstack);
142     nstack++;
143
144     simgrid::mc::UnwindContext context;
145     unw_context_t raw_context =
146       (unw_context_t) mc_model_checker->process().read<unw_context_t>(
147         simgrid::mc::remote((unw_context_t *)stack.context));
148     context.initialize(&mc_model_checker->process(), &raw_context);
149
150     unw_cursor_t cursor = context.cursor();
151     simgrid::mc::dumpStack(file, std::move(cursor));
152   }
153 }
154 #endif
155
156 double MC_process_clock_get(smx_actor_t process)
157 {
158   if (simgrid::mc::processes_time.empty())
159     return 0;
160   if (process != nullptr)
161     return simgrid::mc::processes_time[process->get_pid()];
162   return -1;
163 }
164
165 void MC_process_clock_add(smx_actor_t process, double amount)
166 {
167   simgrid::mc::processes_time[process->get_pid()] += amount;
168 }