1 /* Copyright (c) 2010, 2012-2015, 2017. The SimGrid Team.
2 * All rights reserved. */
4 /* This program is free software; you can redistribute it and/or modify it
5 * under the terms of the license (GNU LGPL) which comes with this package. */
9 #include "xbt/replay.hpp"
11 #include <boost/algorithm/string.hpp>
13 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(replay,xbt,"Replay trace reader");
18 std::ifstream* action_fs = nullptr;
19 std::unordered_map<std::string, action_fun> action_funs;
21 static void read_and_trim_line(std::ifstream* fs, std::string* line)
23 std::getline(*fs, *line);
25 XBT_DEBUG("got from trace: %s", line->c_str());
33 explicit ReplayReader(const char* filename)
35 fs = new std::ifstream(filename, std::ifstream::in);
41 bool get(ReplayAction* action);
44 bool ReplayReader::get(ReplayAction* action)
46 read_and_trim_line(fs, &line);
48 if (line.length() > 0 && line.find("#") == std::string::npos) {
49 boost::split(*action, line, boost::is_any_of(" \t"), boost::token_compress_on);
55 return this->get(action);
59 static ReplayAction* get_action(char* name)
63 std::queue<ReplayAction*>* myqueue = nullptr;
64 if (action_queues.find(std::string(name)) != action_queues.end())
65 myqueue = action_queues.at(std::string(name));
66 if (myqueue == nullptr || myqueue->empty()) { // Nothing stored for me. Read the file further
67 if (action_fs == nullptr) { // File closed now. There's nothing more to read. I'm out of here
70 // Read lines until I reach something for me (which breaks in loop body) or end of file reached
71 while (!action_fs->eof()) {
72 std::string action_line;
73 read_and_trim_line(action_fs, &action_line);
74 if (action_line.length() > 0 && action_line.find("#") == std::string::npos) {
75 /* we cannot split in place here because we parse&store several lines for the colleagues... */
76 action = new ReplayAction();
77 boost::split(*action, action_line, boost::is_any_of(" \t"), boost::token_compress_on);
79 // if it's for me, I'm done
80 std::string evtname = action->front();
81 if (evtname.compare(name) == 0) {
84 // Else, I have to store it for the relevant colleague
85 std::queue<ReplayAction*>* otherqueue = nullptr;
86 if (action_queues.find(evtname) != action_queues.end())
87 otherqueue = action_queues.at(evtname);
88 if (otherqueue == nullptr) { // Damn. Create the queue of that guy
89 otherqueue = new std::queue<ReplayAction*>();
90 action_queues.insert({evtname, otherqueue});
92 otherqueue->push(action);
96 // end of file reached while searching in vain for more work
98 // Get something from my queue and return it
99 action = myqueue->front();
104 // All my actions in the file are done and either I or a colleague closed the file. Let's cleanup before leaving.
106 if (myqueue != nullptr) {
108 action_queues.erase(std::string(name));
113 static void handle_action(ReplayAction* action)
115 XBT_DEBUG("%s replays a %s action", action->at(0).c_str(), action->at(1).c_str());
116 char** c_action = new char*[action->size() + 1];
117 action_fun function = action_funs.at(action->at(1));
119 for (auto arg : *action) {
120 c_action[i] = xbt_strdup(arg.c_str());
123 c_action[i] = nullptr;
126 } catch (xbt_ex& e) {
127 for (unsigned int j = 0; j < action->size(); j++)
128 xbt_free(c_action[j]);
131 xbt_die("Replay error:\n %s", e.what());
133 for (unsigned int j = 0; j < action->size(); j++)
134 xbt_free(c_action[j]);
139 * \ingroup XBT_replay
140 * \brief function used internally to actually run the replay
145 int replay_runner(int argc, char* argv[])
147 if (simgrid::xbt::action_fs) { // A unique trace file
149 simgrid::xbt::ReplayAction* evt = simgrid::xbt::get_action(argv[0]);
152 simgrid::xbt::handle_action(evt);
155 } else { // Should have got my trace file in argument
156 simgrid::xbt::ReplayAction* evt = new simgrid::xbt::ReplayAction();
157 xbt_assert(argc >= 2, "No '%s' agent function provided, no simulation-wide trace file provided, "
158 "and no process-wide trace file provided in deployment file. Aborting.",
160 simgrid::xbt::ReplayReader* reader = new simgrid::xbt::ReplayReader(argv[1]);
161 while (reader->get(evt)) {
162 if (evt->at(0).compare(argv[0]) == 0) {
163 simgrid::xbt::handle_action(evt);
165 XBT_WARN("Ignore trace element ... not for me");
178 * \ingroup XBT_replay
179 * \brief Registers a function to handle a kind of action
181 * Registers a function to handle a kind of action
182 * This table is then used by \ref xbt_replay_action_runner
184 * The argument of the function is the line describing the action, fields separated by spaces.
186 * \param action_name the reference name of the action.
187 * \param function prototype given by the type: void...(const char** action)
189 void xbt_replay_action_register(const char* action_name, action_fun function)
191 simgrid::xbt::action_funs.insert({std::string(action_name), function});