1 /* Copyright (c) 2010-2022. The SimGrid Team. All rights reserved. */
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. */
6 #include "simgrid/Exception.hpp"
8 #include "xbt/replay.hpp"
10 #include <boost/algorithm/string.hpp>
12 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(replay,xbt,"Replay trace reader");
17 static std::ifstream action_fs;
19 std::unordered_map<std::string, action_fun> action_funs;
20 static std::unordered_map<std::string, std::queue<ReplayAction*>> action_queues;
22 static void read_and_trim_line(std::ifstream& fs, std::string* line)
25 std::getline(fs, *line);
27 } while (not fs.eof() && (line->length() == 0 || line->front() == '#'));
28 XBT_DEBUG("got from trace: %s", line->c_str());
36 explicit ReplayReader(const char* filename) : fs(filename, std::ifstream::in)
38 XBT_VERB("Prepare to replay file '%s'", filename);
39 xbt_assert(fs.is_open(), "Cannot read replay file '%s'", filename);
41 ReplayReader(const ReplayReader&) = delete;
42 ReplayReader& operator=(const ReplayReader&) = delete;
43 bool get(ReplayAction* action);
46 bool ReplayReader::get(ReplayAction* action)
48 read_and_trim_line(fs, &line);
50 boost::split(*action, line, boost::is_any_of(" \t"), boost::token_compress_on);
54 static ReplayAction* get_action(const char* name)
56 if (auto queue_elt = action_queues.find(std::string(name)); queue_elt != action_queues.end()) {
57 if (auto& my_queue = queue_elt->second; not my_queue.empty()) {
58 // Get something from my queue and return it
59 ReplayAction* action = my_queue.front();
65 // Nothing stored for me. Read the file further
66 // Read lines until I reach something for me (which breaks in loop body) or end of file reached
68 std::string action_line;
69 read_and_trim_line(action_fs, &action_line);
72 /* we cannot split in place here because we parse&store several lines for the colleagues... */
73 auto* action = new ReplayAction();
74 boost::split(*action, action_line, boost::is_any_of(" \t"), boost::token_compress_on);
76 // if it's for me, I'm done
77 std::string evtname = action->front();
81 // else, I have to store it for the relevant colleague
82 action_queues[evtname].push(action);
84 // end of file reached while searching in vain for more work
89 static void handle_action(ReplayAction& action)
91 XBT_DEBUG("%s replays a %s action", action.at(0).c_str(), action.at(1).c_str());
94 function = action_funs.at(action.at(1));
95 } catch (const std::out_of_range&) {
96 xbt_die("Replay Error: action %s is unknown, please register it properly in the replay engine", action.at(1).c_str());
100 } catch (const Exception&) {
107 * @ingroup XBT_replay
108 * @brief function used internally to actually run the replay
110 int replay_runner(const char* actor_name, const char* trace_filename)
112 std::string actor_name_string(actor_name);
113 if (simgrid::xbt::action_fs.is_open()) { // <A unique trace file
114 xbt_assert(trace_filename == nullptr,
115 "Passing nullptr to replay_runner() means that you want to use a shared trace, but you did not provide "
116 "any. Please use xbt_replay_set_tracefile().");
118 simgrid::xbt::ReplayAction* evt = simgrid::xbt::get_action(actor_name);
121 simgrid::xbt::handle_action(*evt);
124 action_queues.erase(actor_name_string);
125 } else { // Should have got my trace file in argument
126 xbt_assert(trace_filename != nullptr,
127 "Trace replay cannot mix shared and unshared traces for now. Please don't set a shared tracefile with "
128 "xbt_replay_set_tracefile() if you use actor-specific trace files using the second parameter of "
130 simgrid::xbt::ReplayAction evt;
131 simgrid::xbt::ReplayReader reader(trace_filename);
132 while (reader.get(&evt)) {
133 if (evt.front().compare(actor_name) == 0) {
134 simgrid::xbt::handle_action(evt);
136 XBT_WARN("Ignore trace element not for me (target='%s', I am '%s')", evt.front().c_str(), actor_name);
147 * @ingroup XBT_replay
148 * @brief Registers a function to handle a kind of action
150 * Registers a function to handle a kind of action
151 * This table is then used by @ref simgrid::xbt::replay_runner
153 * The argument of the function is the line describing the action, fields separated by spaces.
155 * @param action_name the reference name of the action.
156 * @param function prototype given by the type: void...(const char** action)
158 void xbt_replay_action_register(const char* action_name, const action_fun& function)
160 simgrid::xbt::action_funs[std::string(action_name)] = function;
164 * @ingroup XBT_replay
165 * @brief Get the function that was previously registered to handle a kind of action
167 * This can be useful if you want to override and extend an existing action.
169 action_fun xbt_replay_action_get(const char* action_name)
171 return simgrid::xbt::action_funs.at(std::string(action_name));
174 void xbt_replay_set_tracefile(const std::string& filename)
176 xbt_assert(not simgrid::xbt::action_fs.is_open(), "Tracefile already set");
177 simgrid::xbt::action_fs.open(filename, std::ifstream::in);
178 xbt_assert(simgrid::xbt::action_fs.is_open(), "Failed to open file: %s", filename.c_str());