Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add new entry in Release_Notes.
[simgrid.git] / src / xbt / xbt_replay.cpp
1 /* Copyright (c) 2010-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 "simgrid/Exception.hpp"
7 #include "xbt/log.h"
8 #include "xbt/replay.hpp"
9
10 #include <boost/algorithm/string.hpp>
11
12 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(replay,xbt,"Replay trace reader");
13
14 namespace simgrid::xbt {
15
16 static std::ifstream action_fs;
17
18 std::unordered_map<std::string, action_fun> action_funs;
19 static std::unordered_map<std::string, std::queue<std::unique_ptr<ReplayAction>>> action_queues;
20
21 static void read_and_trim_line(std::ifstream& fs, std::string* line)
22 {
23   do {
24     std::getline(fs, *line);
25     boost::trim(*line);
26   } while (not fs.eof() && (line->length() == 0 || line->front() == '#'));
27   XBT_DEBUG("got from trace: %s", line->c_str());
28 }
29
30 class ReplayReader {
31   std::ifstream fs;
32   std::string line;
33
34 public:
35   explicit ReplayReader(const char* filename) : fs(filename, std::ifstream::in)
36   {
37     XBT_VERB("Prepare to replay file '%s'", filename);
38     xbt_assert(fs.is_open(), "Cannot read replay file '%s'", filename);
39   }
40   ReplayReader(const ReplayReader&) = delete;
41   ReplayReader& operator=(const ReplayReader&) = delete;
42   bool get(ReplayAction* action);
43 };
44
45 bool ReplayReader::get(ReplayAction* action)
46 {
47   read_and_trim_line(fs, &line);
48
49   boost::split(*action, line, boost::is_any_of(" \t"), boost::token_compress_on);
50   return not fs.eof();
51 }
52
53 static std::unique_ptr<ReplayAction> get_action(const char* name)
54 {
55   if (auto queue_elt = action_queues.find(name); queue_elt != action_queues.end()) {
56     if (auto& my_queue = queue_elt->second; not my_queue.empty()) {
57       // Get something from my queue and return it
58       auto action = std::move(my_queue.front());
59       my_queue.pop();
60       return action;
61     }
62   }
63
64   // Nothing stored for me. Read the file further
65   // Read lines until I reach something for me (which breaks in loop body) or end of file reached
66   while (true) {
67     std::string action_line;
68     read_and_trim_line(action_fs, &action_line);
69     if (action_fs.eof())
70       break;
71     /* we cannot split in place here because we parse&store several lines for the colleagues... */
72     auto action = std::make_unique<ReplayAction>();
73     boost::split(*action, action_line, boost::is_any_of(" \t"), boost::token_compress_on);
74
75     // if it's for me, I'm done
76     std::string evtname = action->front();
77     if (evtname == name)
78       return action;
79
80     // else, I have to store it for the relevant colleague
81     action_queues[evtname].emplace(std::move(action));
82   }
83   // end of file reached while searching in vain for more work
84
85   return nullptr;
86 }
87
88 static void handle_action(ReplayAction& action)
89 {
90   XBT_DEBUG("%s replays a %s action", action.at(0).c_str(), action.at(1).c_str());
91   action_fun function;
92   try {
93     function = action_funs.at(action.at(1));
94   } catch (const std::out_of_range&) {
95     xbt_die("Replay Error: action %s is unknown, please register it properly in the replay engine",  action.at(1).c_str());
96   }
97   try {
98     function(action);
99   } catch (const Exception&) {
100     action.clear();
101     throw;
102   }
103 }
104
105 /**
106  * @ingroup XBT_replay
107  * @brief function used internally to actually run the replay
108  */
109 int replay_runner(const char* actor_name, const char* trace_filename)
110 {
111   std::string actor_name_string(actor_name);
112   if (simgrid::xbt::action_fs.is_open()) { // <A unique trace file
113     xbt_assert(trace_filename == nullptr,
114                "Passing nullptr to replay_runner() means that you want to use a shared trace, but you did not provide "
115                "any. Please use xbt_replay_set_tracefile().");
116     while (true) {
117       auto evt = simgrid::xbt::get_action(actor_name);
118       if (not evt)
119         break;
120       simgrid::xbt::handle_action(*evt);
121     }
122     action_queues.erase(actor_name_string);
123   } else { // Should have got my trace file in argument
124     xbt_assert(trace_filename != nullptr,
125                "Trace replay cannot mix shared and unshared traces for now. Please don't set a shared tracefile with "
126                "xbt_replay_set_tracefile() if you use actor-specific trace files using the second parameter of "
127                "replay_runner().");
128     simgrid::xbt::ReplayAction evt;
129     simgrid::xbt::ReplayReader reader(trace_filename);
130     while (reader.get(&evt)) {
131       if (evt.front() == actor_name) {
132         simgrid::xbt::handle_action(evt);
133       } else {
134         XBT_WARN("Ignore trace element not for me (target='%s', I am '%s')", evt.front().c_str(), actor_name);
135       }
136       evt.clear();
137     }
138   }
139   return 0;
140 }
141 } // namespace simgrid::xbt
142
143 /**
144  * @ingroup XBT_replay
145  * @brief Registers a function to handle a kind of action
146  *
147  * Registers a function to handle a kind of action
148  * This table is then used by @ref simgrid::xbt::replay_runner
149  *
150  * The argument of the function is the line describing the action, fields separated by spaces.
151  *
152  * @param action_name the reference name of the action.
153  * @param function prototype given by the type: void...(simgrid::xbt::ReplayAction& action)
154  */
155 void xbt_replay_action_register(const char* action_name, const action_fun& function)
156 {
157   simgrid::xbt::action_funs[action_name] = function;
158 }
159
160 /**
161  * @ingroup XBT_replay
162  * @brief Get the function that was previously registered to handle a kind of action
163  *
164  * This can be useful if you want to override and extend an existing action.
165  */
166 action_fun xbt_replay_action_get(const char* action_name)
167 {
168   return simgrid::xbt::action_funs.at(action_name);
169 }
170
171 void xbt_replay_set_tracefile(const std::string& filename)
172 {
173   xbt_assert(not simgrid::xbt::action_fs.is_open(), "Tracefile already set");
174   simgrid::xbt::action_fs.open(filename, std::ifstream::in);
175   xbt_assert(simgrid::xbt::action_fs.is_open(), "Failed to open file: %s", filename.c_str());
176 }