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");
27 bool is_replay_active = false;
28 typedef std::vector<std::string> ReplayAction;
30 bool replay_is_active()
32 return is_replay_active;
43 explicit ReplayReader(const char* filename)
45 filename_ = xbt_strdup(filename);
46 fs = new std::ifstream(filename, std::ifstream::in);
53 bool get(ReplayAction* action);
56 bool ReplayReader::get(ReplayAction* action)
58 std::getline(*fs, line);
60 XBT_DEBUG("got from trace: %s", line.c_str());
63 if (line.length() > 0 && line.find("#") == std::string::npos) {
64 boost::split(*action, line, boost::is_any_of(" \t"), boost::token_compress_on);
70 return this->get(action);
73 std::ifstream* action_fs = nullptr;
77 std::unordered_map<std::string, action_fun> xbt_action_funs;
79 xbt_dict_t xbt_action_queues = nullptr;
81 bool is_replay_active = false;
83 static simgrid::xbt::ReplayAction* action_get_action(char* name);
87 * \brief Registers a function to handle a kind of action
89 * Registers a function to handle a kind of action
90 * This table is then used by \ref xbt_replay_action_runner
92 * The argument of the function is the line describing the action, fields separated by spaces.
94 * \param action_name the reference name of the action.
95 * \param function prototype given by the type: void...(const char** action)
97 void xbt_replay_action_register(const char *action_name, action_fun function)
99 if (!is_replay_active) // If the user registers a function before the start
100 _xbt_replay_action_init();
101 xbt_action_funs.insert({std::string(action_name), function});
104 void _xbt_replay_action_init()
106 if (!is_replay_active) {
107 xbt_action_queues = xbt_dict_new_homogeneous(nullptr);
108 is_replay_active = true;
112 void _xbt_replay_action_exit()
114 xbt_dict_free(&xbt_action_queues);
115 xbt_action_queues = nullptr;
119 * \ingroup XBT_replay
120 * \brief function used internally to actually run the replay
125 int xbt_replay_action_runner(int argc, char *argv[])
127 if (simgrid::xbt::action_fs) { // A unique trace file
129 simgrid::xbt::ReplayAction* evt = action_get_action(argv[0]);
133 char** args = new char*[evt->size() + 1];
135 for (auto arg : *evt) {
136 args[i] = xbt_strdup(arg.c_str());
140 action_fun function = xbt_action_funs.at(evt->at(1));
146 xbt_die("Replay error :\n %s", e.what());
148 for (unsigned int j = 0; j < evt->size(); j++)
153 } else { // Should have got my trace file in argument
154 simgrid::xbt::ReplayAction* evt = new simgrid::xbt::ReplayAction();
155 xbt_assert(argc >= 2,
156 "No '%s' agent function provided, no simulation-wide trace file provided, "
157 "and no process-wide trace file provided in deployment file. Aborting.", argv[0]
159 simgrid::xbt::ReplayReader* reader = new simgrid::xbt::ReplayReader(argv[1]);
160 while (reader->get(evt)) {
161 if (evt->at(0).compare(argv[0]) == 0) {
162 char** args = new char*[evt->size() + 1];
164 for (auto arg : *evt) {
165 args[i] = xbt_strdup(arg.c_str());
169 action_fun function = xbt_action_funs.at(evt->at(1));
173 for (unsigned int j = 0; j < evt->size(); j++)
177 xbt_die("Replay error on line %d of file %s :\n %s", reader->linenum, reader->filename_, e.what());
179 for (unsigned int j = 0; j < evt->size(); j++)
183 XBT_WARN("%s:%d: Ignore trace element not for me", reader->filename_, reader->linenum);
193 static simgrid::xbt::ReplayAction* action_get_action(char* name)
195 simgrid::xbt::ReplayAction* action;
197 std::queue<simgrid::xbt::ReplayAction*>* myqueue =
198 (std::queue<simgrid::xbt::ReplayAction*>*)xbt_dict_get_or_null(xbt_action_queues, name);
199 if (myqueue == nullptr || myqueue->empty()) { // nothing stored for me. Read the file further
200 if (simgrid::xbt::action_fs == nullptr) { // File closed now. There's nothing more to read. I'm out of here
203 // Read lines until I reach something for me (which breaks in loop body) or end of file reached
204 while (!simgrid::xbt::action_fs->eof()) {
205 std::string action_line;
206 std::getline(*simgrid::xbt::action_fs, action_line);
207 // cleanup and split the string I just read
208 boost::trim(action_line);
209 XBT_DEBUG("got from trace: %s", action_line.c_str());
210 if (action_line.length() > 0 && action_line.find("#") == std::string::npos) {
211 /* we cannot split in place here because we parse&store several lines for the colleagues... */
212 action = new simgrid::xbt::ReplayAction();
213 boost::split(*action, action_line, boost::is_any_of(" \t"), boost::token_compress_on);
215 // if it's for me, I'm done
216 std::string evtname = action->front();
217 if (evtname.compare(name) == 0) {
220 // Else, I have to store it for the relevant colleague
221 std::queue<simgrid::xbt::ReplayAction*>* otherqueue =
222 (std::queue<simgrid::xbt::ReplayAction*>*)xbt_dict_get_or_null(xbt_action_queues, evtname.c_str());
223 if (otherqueue == nullptr) { // Damn. Create the queue of that guy
224 otherqueue = new std::queue<simgrid::xbt::ReplayAction*>();
225 xbt_dict_set(xbt_action_queues, evtname.c_str(), otherqueue, nullptr);
227 otherqueue->push(action);
231 // end of file reached while searching in vain for more work
233 // Get something from my queue and return it
234 action = myqueue->front();
239 // All my actions in the file are done and either I or a colleague closed the file. Let's cleanup before leaving.
241 if (myqueue != nullptr) {
243 xbt_dict_remove(xbt_action_queues, name);