Logo AND Algorithmique Numérique Distribuée

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