Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'udpor-phase6' into 'master'
[simgrid.git] / src / mc / mc_record.cpp
1 /* Copyright (c) 2014-2023. 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 "src/mc/mc_record.hpp"
7 #include "src/kernel/EngineImpl.hpp"
8 #include "src/kernel/activity/CommImpl.hpp"
9 #include "src/mc/mc_base.hpp"
10 #include "src/mc/mc_replay.hpp"
11 #include "src/mc/transition/Transition.hpp"
12
13 #if SIMGRID_HAVE_MC
14 #include "src/mc/api/State.hpp"
15 #include "src/mc/explo/Exploration.hpp"
16 #include "src/mc/mc_private.hpp"
17 #endif
18
19 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_record, mc, "Logging specific to MC record/replay facility");
20
21 namespace simgrid::mc {
22
23 void RecordTrace::replay() const
24 {
25   simgrid::mc::execute_actors();
26   auto* engine = kernel::EngineImpl::get_instance();
27
28   int frame_count = 1;
29   if (xbt_log_no_loc)
30     XBT_INFO("The backtrace of each transition will not be shown because of --log=no_loc");
31   else
32     simgrid_mc_replay_show_backtraces = true;
33
34   for (const simgrid::mc::Transition* transition : transitions_) {
35     kernel::actor::ActorImpl* actor = engine->get_actor_by_pid(transition->aid_);
36     xbt_assert(actor != nullptr, "Unexpected actor (id:%ld).", transition->aid_);
37     const kernel::actor::Simcall* simcall = &(actor->simcall_);
38     xbt_assert(simgrid::mc::request_is_visible(simcall), "Simcall %s of actor %s is not visible.", simcall->get_cname(),
39                actor->get_cname());
40
41     XBT_INFO("***********************************************************************************");
42     XBT_INFO("* Path chunk #%d '%ld/%i' Actor %s(pid:%ld): %s", frame_count++, transition->aid_,
43              transition->times_considered_, simcall->issuer_->get_cname(), simcall->issuer_->get_pid(),
44              simcall->observer_->to_string().c_str());
45     XBT_INFO("***********************************************************************************");
46     if (not mc::actor_is_enabled(actor))
47       simgrid::kernel::EngineImpl::get_instance()->display_all_actor_status();
48
49     xbt_assert(simgrid::mc::actor_is_enabled(actor), "Actor %s (simcall %s) is not enabled.", actor->get_cname(),
50                simcall->get_cname());
51
52     // Execute the request:
53     simcall->issuer_->simcall_handle(transition->times_considered_);
54     simgrid::mc::execute_actors();
55   }
56
57   const auto& actor_list = engine->get_actor_list();
58   if (actor_list.empty()) {
59     XBT_INFO("The replay of the trace is complete. The application is terminating.");
60   } else if (std::none_of(begin(actor_list), end(actor_list),
61                           [](const auto& kv) { return mc::actor_is_enabled(kv.second); })) {
62     XBT_INFO("The replay of the trace is complete. DEADLOCK detected.");
63     engine->display_all_actor_status();
64   } else {
65     XBT_INFO("The replay of the trace is complete. The application could run further.");
66   }
67 }
68
69 void simgrid::mc::RecordTrace::replay(const std::string& path_string)
70 {
71   simgrid::mc::processes_time.resize(kernel::actor::ActorImpl::get_maxpid());
72   simgrid::mc::RecordTrace trace(path_string.c_str());
73   trace.replay();
74   for (auto* item : trace.transitions_)
75     delete item;
76   simgrid::mc::processes_time.clear();
77 }
78
79 simgrid::mc::RecordTrace::RecordTrace(const char* data)
80 {
81   XBT_INFO("path=%s", data);
82   if (data == nullptr || data[0] == '\0')
83     throw std::invalid_argument("Could not parse record path");
84
85   const char* current = data;
86   while (*current) {
87     long aid;
88     int times_considered = 0;
89
90     if (int count = sscanf(current, "%ld/%d", &aid, &times_considered); count != 2 && count != 1)
91       throw std::invalid_argument("Could not parse record path");
92     push_back(new simgrid::mc::Transition(simgrid::mc::Transition::Type::UNKNOWN, aid, times_considered));
93
94     // Find next chunk:
95     const char* end = std::strchr(current, ';');
96     if(end == nullptr)
97       break;
98     else
99       current = end + 1;
100   }
101 }
102
103 #if SIMGRID_HAVE_MC
104
105 std::string simgrid::mc::RecordTrace::to_string() const
106 {
107   std::ostringstream stream;
108   for (auto i = transitions_.begin(); i != transitions_.end(); ++i) {
109     if (i != transitions_.begin())
110       stream << ';';
111     stream << (*i)->aid_;
112     if ((*i)->times_considered_ > 0)
113       stream << '/' << (*i)->times_considered_;
114   }
115   return stream.str();
116 }
117
118 #endif
119
120 } // namespace simgrid::mc