Logo AND Algorithmique Numérique Distribuée

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