1 /* Copyright (c) 2010-2018. 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 std::ifstream* action_fs = nullptr;
18 std::unordered_map<std::string, action_fun> action_funs;
19 static std::unordered_map<std::string, std::queue<ReplayAction*>*> action_queues;
21 static void read_and_trim_line(std::ifstream* fs, std::string* line)
24 std::getline(*fs, *line);
26 } while (not fs->eof() && (line->length() == 0 || line->front() == '#'));
27 XBT_DEBUG("got from trace: %s", line->c_str());
35 explicit ReplayReader(const char* filename)
37 XBT_VERB("Prepare to replay file '%s'", filename);
38 fs = new std::ifstream(filename, std::ifstream::in);
39 xbt_assert(fs->is_open(), "Cannot read replay file '%s'", filename);
41 ReplayReader(const ReplayReader&) = delete;
42 ReplayReader& operator=(const ReplayReader&) = delete;
47 bool get(ReplayAction* action);
50 bool ReplayReader::get(ReplayAction* action)
52 read_and_trim_line(fs, &line);
54 boost::split(*action, line, boost::is_any_of(" \t"), boost::token_compress_on);
58 static ReplayAction* get_action(const char* name)
62 std::queue<ReplayAction*>* myqueue = nullptr;
63 if (action_queues.find(std::string(name)) != action_queues.end())
64 myqueue = action_queues.at(std::string(name));
65 if (myqueue == nullptr || myqueue->empty()) { // 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 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();
78 if (evtname.compare(name) == 0) {
81 // Else, I have to store it for the relevant colleague
82 std::queue<ReplayAction*>* otherqueue = nullptr;
83 auto act = action_queues.find(evtname);
84 if (act != action_queues.end()) {
85 otherqueue = act->second;
86 } else { // Damn. Create the queue of that guy
87 otherqueue = new std::queue<ReplayAction*>();
88 action_queues.insert({evtname, otherqueue});
90 otherqueue->push(action);
93 // end of file reached while searching in vain for more work
95 // Get something from my queue and return it
96 action = myqueue->front();
104 static void handle_action(ReplayAction& action)
106 XBT_DEBUG("%s replays a %s action", action.at(0).c_str(), action.at(1).c_str());
107 action_fun function = action_funs.at(action.at(1));
110 } catch (xbt_ex& e) {
112 xbt_die("Replay error:\n %s", e.what());
117 * @ingroup XBT_replay
118 * @brief function used internally to actually run the replay
120 int replay_runner(const char* actor_name, const char* trace_filename)
122 std::string actor_name_string(actor_name);
123 if (simgrid::xbt::action_fs) { // <A unique trace file
125 simgrid::xbt::ReplayAction* evt = simgrid::xbt::get_action(actor_name);
128 simgrid::xbt::handle_action(*evt);
131 if (action_queues.find(actor_name_string) != action_queues.end()) {
132 std::queue<ReplayAction*>* myqueue = action_queues.at(actor_name_string);
134 action_queues.erase(actor_name_string);
136 } else { // Should have got my trace file in argument
137 xbt_assert(trace_filename != nullptr);
138 simgrid::xbt::ReplayAction evt;
139 simgrid::xbt::ReplayReader reader(trace_filename);
140 while (reader.get(&evt)) {
141 if (evt.front().compare(actor_name) == 0) {
142 simgrid::xbt::handle_action(evt);
144 XBT_WARN("Ignore trace element not for me (target='%s', I am '%s')", evt.front().c_str(), actor_name);
155 * @ingroup XBT_replay
156 * @brief Registers a function to handle a kind of action
158 * Registers a function to handle a kind of action
159 * This table is then used by @ref simgrid::xbt::replay_runner
161 * The argument of the function is the line describing the action, fields separated by spaces.
163 * @param action_name the reference name of the action.
164 * @param function prototype given by the type: void...(const char** action)
166 void xbt_replay_action_register(const char* action_name, action_fun function)
168 simgrid::xbt::action_funs[std::string(action_name)] = function;
172 * @ingroup XBT_replay
173 * @brief Get the function that was previously registered to handle a kind of action
175 * This can be useful if you want to override and extend an existing action.
177 action_fun xbt_replay_action_get(const char* action_name)
179 return simgrid::xbt::action_funs.at(std::string(action_name));