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. */
7 #include "src/internal_config.h"
10 #include "xbt/replay.hpp"
12 #include "xbt/sysdep.h"
14 #include <boost/algorithm/string.hpp>
19 #include <unordered_map>
22 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(replay,xbt,"Replay trace reader");
24 xbt_dict_t xbt_action_queues = nullptr;
25 bool is_replay_active = false;
30 std::ifstream* action_fs = nullptr;
31 std::unordered_map<std::string, action_fun> action_funs;
32 typedef std::vector<std::string> ReplayAction;
34 static void read_and_trim_line(std::ifstream* fs, std::string* line)
36 std::getline(*fs, *line);
38 XBT_DEBUG("got from trace: %s", line->c_str());
49 explicit ReplayReader(const char* filename)
51 filename_ = xbt_strdup(filename);
52 fs = new std::ifstream(filename, std::ifstream::in);
59 bool get(ReplayAction* action);
62 bool ReplayReader::get(ReplayAction* action)
64 read_and_trim_line(fs, &line);
67 if (line.length() > 0 && line.find("#") == std::string::npos) {
68 boost::split(*action, line, boost::is_any_of(" \t"), boost::token_compress_on);
74 return this->get(action);
80 if (!is_replay_active) {
81 xbt_action_queues = xbt_dict_new_homogeneous(nullptr);
82 is_replay_active = true;
88 xbt_dict_free(&xbt_action_queues);
89 xbt_action_queues = nullptr;
92 bool replay_is_active()
94 return is_replay_active;
97 static ReplayAction* get_action(char* name)
101 std::queue<ReplayAction*>* myqueue =
102 static_cast<std::queue<ReplayAction*>*>(xbt_dict_get_or_null(xbt_action_queues, name));
103 if (myqueue == nullptr || myqueue->empty()) { // Nothing stored for me. Read the file further
104 if (action_fs == nullptr) { // File closed now. There's nothing more to read. I'm out of here
107 // Read lines until I reach something for me (which breaks in loop body) or end of file reached
108 while (!action_fs->eof()) {
109 std::string action_line;
110 read_and_trim_line(action_fs, &action_line);
111 if (action_line.length() > 0 && action_line.find("#") == std::string::npos) {
112 /* we cannot split in place here because we parse&store several lines for the colleagues... */
113 action = new ReplayAction();
114 boost::split(*action, action_line, boost::is_any_of(" \t"), boost::token_compress_on);
116 // if it's for me, I'm done
117 std::string evtname = action->front();
118 if (evtname.compare(name) == 0) {
121 // Else, I have to store it for the relevant colleague
122 std::queue<ReplayAction*>* otherqueue =
123 static_cast<std::queue<ReplayAction*>*>(xbt_dict_get_or_null(xbt_action_queues, evtname.c_str()));
124 if (otherqueue == nullptr) { // Damn. Create the queue of that guy
125 otherqueue = new std::queue<ReplayAction*>();
126 xbt_dict_set(xbt_action_queues, evtname.c_str(), otherqueue, nullptr);
128 otherqueue->push(action);
132 // end of file reached while searching in vain for more work
134 // Get something from my queue and return it
135 action = myqueue->front();
140 // All my actions in the file are done and either I or a colleague closed the file. Let's cleanup before leaving.
142 if (myqueue != nullptr) {
144 xbt_dict_remove(xbt_action_queues, name);
149 static void handle_action(ReplayAction* action)
151 XBT_DEBUG("%s replays a %s action", action->at(0).c_str(), action->at(1).c_str());
152 char** c_action = new char*[action->size() + 1];
153 action_fun function = action_funs.at(action->at(1));
155 for (auto arg : *action) {
156 c_action[i] = xbt_strdup(arg.c_str());
159 c_action[i] = nullptr;
162 } catch (xbt_ex& e) {
163 for (unsigned int j = 0; j < action->size(); j++)
164 xbt_free(c_action[j]);
167 xbt_die("Replay error:\n %s", e.what());
169 for (unsigned int j = 0; j < action->size(); j++)
170 xbt_free(c_action[j]);
175 * \ingroup XBT_replay
176 * \brief function used internally to actually run the replay
181 int replay_runner(int argc, char* argv[])
183 if (simgrid::xbt::action_fs) { // A unique trace file
185 simgrid::xbt::ReplayAction* evt = simgrid::xbt::get_action(argv[0]);
188 simgrid::xbt::handle_action(evt);
191 } else { // Should have got my trace file in argument
192 simgrid::xbt::ReplayAction* evt = new simgrid::xbt::ReplayAction();
193 xbt_assert(argc >= 2, "No '%s' agent function provided, no simulation-wide trace file provided, "
194 "and no process-wide trace file provided in deployment file. Aborting.",
196 simgrid::xbt::ReplayReader* reader = new simgrid::xbt::ReplayReader(argv[1]);
197 while (reader->get(evt)) {
198 if (evt->at(0).compare(argv[0]) == 0) {
199 simgrid::xbt::handle_action(evt);
201 XBT_WARN("%s:%d: Ignore trace element not for me", reader->filename_, reader->linenum);
214 * \ingroup XBT_replay
215 * \brief Registers a function to handle a kind of action
217 * Registers a function to handle a kind of action
218 * This table is then used by \ref xbt_replay_action_runner
220 * The argument of the function is the line describing the action, fields separated by spaces.
222 * \param action_name the reference name of the action.
223 * \param function prototype given by the type: void...(const char** action)
225 void xbt_replay_action_register(const char* action_name, action_fun function)
227 if (!is_replay_active) // If the user registers a function before the start
228 simgrid::xbt::replay_init();
229 simgrid::xbt::action_funs.insert({std::string(action_name), function});