Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
c04b5f3e1969bd94b79fd95e3547640089f6117b
[simgrid.git] / src / mc / mc_global.cpp
1 /* Copyright (c) 2008-2017. 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 <cinttypes>
7 #include <cassert>
8 #include <cstddef>
9 #include <cstdint>
10
11 #include <cxxabi.h>
12
13 #include <vector>
14
15 #include "xbt/automaton.h"
16 #include "xbt/backtrace.hpp"
17 #include "xbt/dynar.h"
18 #include "xbt/swag.h"
19
20 #include "mc_base.h"
21
22 #include "mc/mc.h"
23
24 #ifndef _WIN32
25 #include <unistd.h>
26 #include <sys/wait.h>
27 #include <sys/time.h>
28 #endif
29
30 #include "src/simix/ActorImpl.hpp"
31
32 #if SIMGRID_HAVE_MC
33 #include "src/mc/checker/Checker.hpp"
34 #include "src/mc/mc_comm_pattern.hpp"
35 #include "src/mc/mc_private.hpp"
36 #include "src/mc/mc_request.hpp"
37 #include "src/mc/mc_safety.hpp"
38 #include "src/mc/mc_smx.hpp"
39 #include "src/mc/mc_snapshot.hpp"
40 #include "src/mc/mc_unw.hpp"
41 #include <libunwind.h>
42 #endif
43
44 #include "src/mc/Transition.hpp"
45 #include "src/mc/mc_record.hpp"
46 #include "src/mc/remote/Client.hpp"
47 #include "src/mc/remote/mc_protocol.h"
48
49 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_global, mc, "Logging specific to MC (global)");
50
51 extern std::string _sg_mc_dot_output_file;
52
53 namespace simgrid {
54 namespace mc {
55
56 std::vector<double> processes_time;
57
58 }
59 }
60
61 #if SIMGRID_HAVE_MC
62
63 /* Liveness */
64
65 namespace simgrid {
66 namespace mc {
67
68 xbt_automaton_t property_automaton = nullptr;
69
70 }
71 }
72
73 /* Dot output */
74 FILE *dot_output = nullptr;
75
76
77 /*******************************  Initialization of MC *******************************/
78 /*********************************************************************************/
79
80 void MC_init_dot_output()
81 {
82   dot_output = fopen(_sg_mc_dot_output_file.c_str(), "w");
83
84   if (dot_output == nullptr) {
85     perror("Error open dot output file");
86     xbt_abort();
87   }
88
89   fprintf(dot_output,
90           "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");
91
92 }
93
94 /*******************************  Core of MC *******************************/
95 /**************************************************************************/
96
97 void MC_run()
98 {
99   simgrid::mc::processes_time.resize(SIMIX_process_get_maxpid());
100   MC_ignore_heap(simgrid::mc::processes_time.data(),
101     simgrid::mc::processes_time.size() * sizeof(simgrid::mc::processes_time[0]));
102   simgrid::mc::Client::get()->mainLoop();
103   simgrid::mc::processes_time.clear();
104 }
105
106 void MC_show_deadlock()
107 {
108   XBT_INFO("**************************");
109   XBT_INFO("*** DEAD-LOCK DETECTED ***");
110   XBT_INFO("**************************");
111   XBT_INFO("Counter-example execution trace:");
112   for (auto const& s : mc_model_checker->getChecker()->getTextualTrace())
113     XBT_INFO("%s", s.c_str());
114   simgrid::mc::session->logState();
115 }
116
117 void MC_automaton_load(const char *file)
118 {
119   if (simgrid::mc::property_automaton == nullptr)
120     simgrid::mc::property_automaton = xbt_automaton_new();
121
122   xbt_automaton_load(simgrid::mc::property_automaton, file);
123 }
124
125 namespace simgrid {
126 namespace mc {
127
128 void dumpStack(FILE* file, unw_cursor_t cursor)
129 {
130   int nframe = 0;
131   char buffer[100];
132
133   unw_word_t off;
134   do {
135     const char* name = not unw_get_proc_name(&cursor, buffer, 100, &off) ? buffer : "?";
136     // Unmangle C++ names:
137     auto realname = simgrid::xbt::demangle(name);
138
139 #if defined(__x86_64__)
140     unw_word_t rip = 0;
141     unw_word_t rsp = 0;
142     unw_get_reg(&cursor, UNW_X86_64_RIP, &rip);
143     unw_get_reg(&cursor, UNW_X86_64_RSP, &rsp);
144     fprintf(file, "  %i: %s (RIP=0x%" PRIx64 " RSP=0x%" PRIx64 ")\n", nframe, realname.get(), (std::uint64_t)rip,
145             (std::uint64_t)rsp);
146 #else
147     fprintf(file, "  %i: %s\n", nframe, realname.get());
148 #endif
149
150     ++nframe;
151   } while(unw_step(&cursor));
152 }
153
154 }
155 }
156
157 static void MC_dump_stacks(FILE* file)
158 {
159   int nstack = 0;
160   for (auto const& stack : mc_model_checker->process().stack_areas()) {
161     fprintf(file, "Stack %i:\n", nstack);
162     nstack++;
163
164     simgrid::mc::UnwindContext context;
165     unw_context_t raw_context =
166       (unw_context_t) mc_model_checker->process().read<unw_context_t>(
167         simgrid::mc::remote((unw_context_t *)stack.context));
168     context.initialize(&mc_model_checker->process(), &raw_context);
169
170     unw_cursor_t cursor = context.cursor();
171     simgrid::mc::dumpStack(file, cursor);
172   }
173 }
174 #endif
175
176 double MC_process_clock_get(smx_actor_t process)
177 {
178   if (simgrid::mc::processes_time.empty())
179     return 0;
180   if (process != nullptr)
181     return simgrid::mc::processes_time[process->pid];
182   return -1;
183 }
184
185 void MC_process_clock_add(smx_actor_t process, double amount)
186 {
187   simgrid::mc::processes_time[process->pid] += amount;
188 }