Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
e074d8e53696bd5108be6322526102e1d8a8f8cb
[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 {
22 namespace mc {
23
24 void RecordTrace::replay() const
25 {
26   simgrid::mc::execute_actors();
27
28   for (const simgrid::mc::Transition* transition : transitions_) {
29     XBT_DEBUG("Executing %ld$%i", transition->aid_, transition->times_considered_);
30
31     // Choose a request:
32     kernel::actor::ActorImpl* actor = kernel::EngineImpl::get_instance()->get_actor_by_pid(transition->aid_);
33     xbt_assert(actor != nullptr, "Unexpected actor (id:%ld).", transition->aid_);
34     const kernel::actor::Simcall* simcall = &(actor->simcall_);
35     xbt_assert(simcall->call_ != kernel::actor::Simcall::Type::NONE, "No simcall for process %ld.", transition->aid_);
36     xbt_assert(simgrid::mc::request_is_visible(simcall) && simgrid::mc::actor_is_enabled(actor), "Unexpected simcall.");
37
38     // Execute the request:
39     simcall->issuer_->simcall_handle(transition->times_considered_);
40     simgrid::mc::execute_actors();
41   }
42 }
43
44 void simgrid::mc::RecordTrace::replay(const std::string& path_string)
45 {
46   simgrid::mc::processes_time.resize(kernel::actor::ActorImpl::get_maxpid());
47   simgrid::mc::RecordTrace trace(path_string.c_str());
48   trace.replay();
49   for (auto* item : trace.transitions_)
50     delete item;
51   simgrid::mc::processes_time.clear();
52 }
53
54 simgrid::mc::RecordTrace::RecordTrace(const char* data)
55 {
56   XBT_INFO("path=%s", data);
57   if (data == nullptr || data[0] == '\0')
58     throw std::invalid_argument("Could not parse record path");
59
60   const char* current = data;
61   while (*current) {
62     long aid;
63     int times_considered;
64
65     if (int count = sscanf(current, "%ld/%d", &aid, &times_considered); count != 2 && count != 1)
66       throw std::invalid_argument("Could not parse record path");
67     push_back(new simgrid::mc::Transition(simgrid::mc::Transition::Type::UNKNOWN, aid, times_considered));
68
69     // Find next chunk:
70     const char* end = std::strchr(current, ';');
71     if(end == nullptr)
72       break;
73     else
74       current = end + 1;
75   }
76 }
77
78 #if SIMGRID_HAVE_MC
79
80 std::string simgrid::mc::RecordTrace::to_string() const
81 {
82   std::ostringstream stream;
83   for (auto i = transitions_.begin(); i != transitions_.end(); ++i) {
84     if (i != transitions_.begin())
85       stream << ';';
86     stream << (*i)->aid_;
87     if ((*i)->times_considered_ > 0)
88       stream << '/' << (*i)->times_considered_;
89   }
90   return stream.str();
91 }
92
93 #endif
94
95 }
96 }