Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
3b35eade53b1273bbd33cd27292a09c5c4f79e1e
[simgrid.git] / src / mc / mc_record.cpp
1 /* Copyright (c) 2014-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 "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   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_DEBUG("Executing %ld$%i: %s", transition->aid_, transition->times_considered_,
36               simcall->observer_->to_string().c_str());
37     if (not mc::actor_is_enabled(actor))
38       simgrid::kernel::EngineImpl::get_instance()->display_all_actor_status();
39
40     xbt_assert(simgrid::mc::actor_is_enabled(actor), "Actor %s (simcall %s) is not enabled.", actor->get_cname(),
41                simcall->get_cname());
42
43     // Execute the request:
44     simcall->issuer_->simcall_handle(transition->times_considered_);
45     simgrid::mc::execute_actors();
46   }
47
48   const auto& actor_list = engine->get_actor_list();
49   if (actor_list.empty()) {
50     XBT_INFO("The replay of the trace is complete. The application is terminating.");
51   } else if (std::none_of(begin(actor_list), end(actor_list),
52                           [](const auto& kv) { return mc::actor_is_enabled(kv.second); })) {
53     XBT_INFO("The replay of the trace is complete. DEADLOCK detected.");
54     engine->display_all_actor_status();
55   } else {
56     XBT_INFO("The replay of the trace is complete. The application could run further.");
57   }
58 }
59
60 void simgrid::mc::RecordTrace::replay(const std::string& path_string)
61 {
62   simgrid::mc::processes_time.resize(kernel::actor::ActorImpl::get_maxpid());
63   simgrid::mc::RecordTrace trace(path_string.c_str());
64   trace.replay();
65   for (auto* item : trace.transitions_)
66     delete item;
67   simgrid::mc::processes_time.clear();
68 }
69
70 simgrid::mc::RecordTrace::RecordTrace(const char* data)
71 {
72   XBT_INFO("path=%s", data);
73   if (data == nullptr || data[0] == '\0')
74     throw std::invalid_argument("Could not parse record path");
75
76   const char* current = data;
77   while (*current) {
78     long aid;
79     int times_considered = 0;
80
81     if (int count = sscanf(current, "%ld/%d", &aid, &times_considered); count != 2 && count != 1)
82       throw std::invalid_argument("Could not parse record path");
83     push_back(new simgrid::mc::Transition(simgrid::mc::Transition::Type::UNKNOWN, aid, times_considered));
84
85     // Find next chunk:
86     const char* end = std::strchr(current, ';');
87     if(end == nullptr)
88       break;
89     else
90       current = end + 1;
91   }
92 }
93
94 #if SIMGRID_HAVE_MC
95
96 std::string simgrid::mc::RecordTrace::to_string() const
97 {
98   std::ostringstream stream;
99   for (auto i = transitions_.begin(); i != transitions_.end(); ++i) {
100     if (i != transitions_.begin())
101       stream << ';';
102     stream << (*i)->aid_;
103     if ((*i)->times_considered_ > 0)
104       stream << '/' << (*i)->times_considered_;
105   }
106   return stream.str();
107 }
108
109 #endif
110
111 } // namespace simgrid::mc