Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
MC: apply some sonar advices
[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/api/RemoteApp.hpp"
11 #include "src/mc/explo/Exploration.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/remote/AppSide.hpp"
16 #include "src/mc/sosp/Snapshot.hpp"
17
18 #include <array>
19 #include <boost/core/demangle.hpp>
20 #include <cerrno>
21 #include <cstring>
22 #include <libunwind.h>
23 #endif
24
25 #ifndef _WIN32
26 #include <sys/time.h>
27 #include <sys/wait.h>
28 #include <unistd.h>
29 #endif
30
31 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_global, mc, "Logging specific to MC (global)");
32
33 namespace simgrid::mc {
34
35 std::vector<double> processes_time;
36
37 }
38
39 #if SIMGRID_HAVE_MC
40
41 namespace simgrid::mc {
42
43 /*******************************  Core of MC *******************************/
44 /**************************************************************************/
45 void dumpStack(FILE* file, unw_cursor_t* cursor)
46 {
47   int nframe = 0;
48   std::array<char, 100> buffer;
49
50   unw_word_t off;
51   do {
52     const char* name = not unw_get_proc_name(cursor, buffer.data(), buffer.size(), &off) ? buffer.data() : "?";
53     // Unmangle C++ names:
54     std::string realname = boost::core::demangle(name);
55
56 #if defined(__x86_64__)
57     unw_word_t rip = 0;
58     unw_word_t rsp = 0;
59     unw_get_reg(cursor, UNW_X86_64_RIP, &rip);
60     unw_get_reg(cursor, UNW_X86_64_RSP, &rsp);
61     fprintf(file, "  %i: %s (RIP=0x%" PRIx64 " RSP=0x%" PRIx64 ")\n", nframe, realname.c_str(), (std::uint64_t)rip,
62             (std::uint64_t)rsp);
63 #else
64     fprintf(file, "  %i: %s\n", nframe, realname.c_str());
65 #endif
66
67     ++nframe;
68   } while (unw_step(cursor));
69 }
70
71 } // namespace simgrid::mc
72 #endif
73
74 double MC_process_clock_get(const simgrid::kernel::actor::ActorImpl* process)
75 {
76   if (simgrid::mc::processes_time.empty())
77     return 0;
78   if (process == nullptr)
79     return -1;
80   return simgrid::mc::processes_time.at(process->get_pid());
81 }
82
83 void MC_process_clock_add(const simgrid::kernel::actor::ActorImpl* process, double amount)
84 {
85   simgrid::mc::processes_time.at(process->get_pid()) += amount;
86 }