Logo AND Algorithmique Numérique Distribuée

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