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 std::unordered_map<std::string, action_fun> xbt_action_funs;
25 xbt_dict_t xbt_action_queues = nullptr;
26 bool is_replay_active = false;
31 typedef std::vector<std::string> ReplayAction;
33 static void read_and_trim_line(std::ifstream* fs, std::string* line)
35 std::getline(*fs, *line);
37 XBT_DEBUG("got from trace: %s", line->c_str());
40 static void handle_action(ReplayAction* action)
42 XBT_DEBUG("%s replays a %s action", action->at(0).c_str(), action->at(1).c_str());
43 char** c_action = new char*[action->size() + 1];
44 action_fun function = xbt_action_funs.at(action->at(1));
46 for (auto arg : *action) {
47 c_action[i] = xbt_strdup(arg.c_str());
50 c_action[i] = nullptr;
54 for (unsigned int j = 0; j < action->size(); j++)
55 xbt_free(c_action[j]);
58 xbt_die("Replay error:\n %s", e.what());
60 for (unsigned int j = 0; j < action->size(); j++)
61 xbt_free(c_action[j]);
65 bool replay_is_active()
67 return is_replay_active;
78 explicit ReplayReader(const char* filename)
80 filename_ = xbt_strdup(filename);
81 fs = new std::ifstream(filename, std::ifstream::in);
88 bool get(ReplayAction* action);
91 bool ReplayReader::get(ReplayAction* action)
93 read_and_trim_line(fs, &line);
96 if (line.length() > 0 && line.find("#") == std::string::npos) {
97 boost::split(*action, line, boost::is_any_of(" \t"), boost::token_compress_on);
103 return this->get(action);
106 std::ifstream* action_fs = nullptr;
110 static simgrid::xbt::ReplayAction* action_get_action(char* name);
112 * \ingroup XBT_replay
113 * \brief Registers a function to handle a kind of action
115 * Registers a function to handle a kind of action
116 * This table is then used by \ref xbt_replay_action_runner
118 * The argument of the function is the line describing the action, fields separated by spaces.
120 * \param action_name the reference name of the action.
121 * \param function prototype given by the type: void...(const char** action)
123 void xbt_replay_action_register(const char *action_name, action_fun function)
125 if (!is_replay_active) // If the user registers a function before the start
126 _xbt_replay_action_init();
127 xbt_action_funs.insert({std::string(action_name), function});
130 void _xbt_replay_action_init()
132 if (!is_replay_active) {
133 xbt_action_queues = xbt_dict_new_homogeneous(nullptr);
134 is_replay_active = true;
138 void _xbt_replay_action_exit()
140 xbt_dict_free(&xbt_action_queues);
141 xbt_action_queues = nullptr;
145 * \ingroup XBT_replay
146 * \brief function used internally to actually run the replay
151 int xbt_replay_action_runner(int argc, char *argv[])
153 if (simgrid::xbt::action_fs) { // A unique trace file
155 simgrid::xbt::ReplayAction* evt = action_get_action(argv[0]);
158 simgrid::xbt::handle_action(evt);
161 } else { // Should have got my trace file in argument
162 simgrid::xbt::ReplayAction* evt = new simgrid::xbt::ReplayAction();
163 xbt_assert(argc >= 2,
164 "No '%s' agent function provided, no simulation-wide trace file provided, "
165 "and no process-wide trace file provided in deployment file. Aborting.", argv[0]
167 simgrid::xbt::ReplayReader* reader = new simgrid::xbt::ReplayReader(argv[1]);
168 while (reader->get(evt)) {
169 if (evt->at(0).compare(argv[0]) == 0) {
170 simgrid::xbt::handle_action(evt);
172 XBT_WARN("%s:%d: Ignore trace element not for me", reader->filename_, reader->linenum);
182 static simgrid::xbt::ReplayAction* action_get_action(char* name)
184 simgrid::xbt::ReplayAction* action;
186 std::queue<simgrid::xbt::ReplayAction*>* myqueue =
187 (std::queue<simgrid::xbt::ReplayAction*>*)xbt_dict_get_or_null(xbt_action_queues, name);
188 if (myqueue == nullptr || myqueue->empty()) { // nothing stored for me. Read the file further
189 if (simgrid::xbt::action_fs == nullptr) { // File closed now. There's nothing more to read. I'm out of here
192 // Read lines until I reach something for me (which breaks in loop body) or end of file reached
193 while (!simgrid::xbt::action_fs->eof()) {
194 std::string action_line;
195 simgrid::xbt::read_and_trim_line(simgrid::xbt::action_fs, &action_line);
196 if (action_line.length() > 0 && action_line.find("#") == std::string::npos) {
197 /* we cannot split in place here because we parse&store several lines for the colleagues... */
198 action = new simgrid::xbt::ReplayAction();
199 boost::split(*action, action_line, boost::is_any_of(" \t"), boost::token_compress_on);
201 // if it's for me, I'm done
202 std::string evtname = action->front();
203 if (evtname.compare(name) == 0) {
206 // Else, I have to store it for the relevant colleague
207 std::queue<simgrid::xbt::ReplayAction*>* otherqueue =
208 (std::queue<simgrid::xbt::ReplayAction*>*)xbt_dict_get_or_null(xbt_action_queues, evtname.c_str());
209 if (otherqueue == nullptr) { // Damn. Create the queue of that guy
210 otherqueue = new std::queue<simgrid::xbt::ReplayAction*>();
211 xbt_dict_set(xbt_action_queues, evtname.c_str(), otherqueue, nullptr);
213 otherqueue->push(action);
217 // end of file reached while searching in vain for more work
219 // Get something from my queue and return it
220 action = myqueue->front();
225 // All my actions in the file are done and either I or a colleague closed the file. Let's cleanup before leaving.
227 if (myqueue != nullptr) {
229 xbt_dict_remove(xbt_action_queues, name);