Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
51c4074fed40983440ccb178263421a34f0815ff
[simgrid.git] / src / mc / mc_global.cpp
1 /* Copyright (c) 2008-2022. 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
9 #if SIMGRID_HAVE_MC
10 #include "src/mc/Session.hpp"
11 #include "src/mc/checker/Checker.hpp"
12 #include "src/mc/inspect/mc_unw.hpp"
13 #include "src/mc/mc_config.hpp"
14 #include "src/mc/mc_private.hpp"
15 #include "src/mc/mc_safety.hpp"
16 #include "src/mc/remote/AppSide.hpp"
17 #include "src/mc/sosp/Snapshot.hpp"
18
19 #include <array>
20 #include <boost/core/demangle.hpp>
21 #include <cerrno>
22 #include <cstring>
23 #include <libunwind.h>
24 #endif
25
26 #ifndef _WIN32
27 #include <sys/time.h>
28 #include <sys/wait.h>
29 #include <unistd.h>
30 #endif
31
32 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_global, mc, "Logging specific to MC (global)");
33
34 namespace simgrid {
35 namespace mc {
36
37 std::vector<double> processes_time;
38
39 }
40 }
41
42 #if SIMGRID_HAVE_MC
43
44 /* Liveness */
45
46 namespace simgrid {
47 namespace mc {
48
49 xbt_automaton_t property_automaton = nullptr;
50
51 }
52 }
53
54 /* Dot output */
55 FILE *dot_output = nullptr;
56
57
58 /*******************************  Initialization of MC *******************************/
59 /*********************************************************************************/
60
61 void MC_init_dot_output()
62 {
63   dot_output = fopen(_sg_mc_dot_output_file.get().c_str(), "w");
64   xbt_assert(dot_output != nullptr, "Error open dot output file: %s", strerror(errno));
65
66   fprintf(dot_output,
67           "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");
68 }
69
70 /*******************************  Core of MC *******************************/
71 /**************************************************************************/
72
73 void MC_run()
74 {
75   simgrid::mc::processes_time.resize(simgrid::kernel::actor::get_maxpid());
76   MC_ignore_heap(simgrid::mc::processes_time.data(),
77     simgrid::mc::processes_time.size() * sizeof(simgrid::mc::processes_time[0]));
78   simgrid::mc::AppSide::get()->main_loop();
79 }
80
81 namespace simgrid {
82 namespace mc {
83
84 void dumpStack(FILE* file, unw_cursor_t* cursor)
85 {
86   int nframe = 0;
87   std::array<char, 100> buffer;
88
89   unw_word_t off;
90   do {
91     const char* name = not unw_get_proc_name(cursor, buffer.data(), buffer.size(), &off) ? buffer.data() : "?";
92     // Unmangle C++ names:
93     std::string realname = boost::core::demangle(name);
94
95 #if defined(__x86_64__)
96     unw_word_t rip = 0;
97     unw_word_t rsp = 0;
98     unw_get_reg(cursor, UNW_X86_64_RIP, &rip);
99     unw_get_reg(cursor, UNW_X86_64_RSP, &rsp);
100     fprintf(file, "  %i: %s (RIP=0x%" PRIx64 " RSP=0x%" PRIx64 ")\n", nframe, realname.c_str(), (std::uint64_t)rip,
101             (std::uint64_t)rsp);
102 #else
103     fprintf(file, "  %i: %s\n", nframe, realname.c_str());
104 #endif
105
106     ++nframe;
107   } while (unw_step(cursor));
108 }
109
110 }
111 }
112 #endif
113
114 double MC_process_clock_get(const simgrid::kernel::actor::ActorImpl* process)
115 {
116   if (simgrid::mc::processes_time.empty())
117     return 0;
118   if (process != nullptr)
119     return simgrid::mc::processes_time[process->get_pid()];
120   return -1;
121 }
122
123 void MC_process_clock_add(const simgrid::kernel::actor::ActorImpl* process, double amount)
124 {
125   simgrid::mc::processes_time[process->get_pid()] += amount;
126 }