Logo AND Algorithmique Numérique Distribuée

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