Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
jedule: obey our coding standards
[simgrid.git] / src / xbt / xbt_replay.cpp
1 /* Copyright (c) 2010-2018. The SimGrid Team.
2  * All rights reserved.                                                     */
3
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. */
6
7 #include "xbt/ex.hpp"
8 #include "xbt/log.h"
9 #include "xbt/replay.hpp"
10
11 #include <boost/algorithm/string.hpp>
12
13 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(replay,xbt,"Replay trace reader");
14
15 namespace simgrid {
16 namespace xbt {
17
18 std::ifstream* action_fs = nullptr;
19 std::unordered_map<std::string, action_fun> action_funs;
20 static std::unordered_map<std::string, std::queue<ReplayAction*>*> action_queues;
21
22 static void read_and_trim_line(std::ifstream* fs, std::string* line)
23 {
24   do {
25     std::getline(*fs, *line);
26     boost::trim(*line);
27   } while (not fs->eof() && (line->length() == 0 || line->front() == '#'));
28   XBT_DEBUG("got from trace: %s", line->c_str());
29 }
30
31 class ReplayReader {
32   std::ifstream* fs;
33   std::string line;
34
35 public:
36   explicit ReplayReader(const char* filename)
37   {
38     XBT_VERB("Prepare to replay file '%s'", filename);
39     fs = new std::ifstream(filename, std::ifstream::in);
40     xbt_assert(fs->is_open(), "Cannot read replay file '%s'", filename);
41   }
42   ReplayReader(const ReplayReader&) = delete;
43   ReplayReader& operator=(const ReplayReader&) = delete;
44   ~ReplayReader()
45   {
46     delete fs;
47   }
48   bool get(ReplayAction* action);
49 };
50
51 bool ReplayReader::get(ReplayAction* action)
52 {
53   read_and_trim_line(fs, &line);
54
55   boost::split(*action, line, boost::is_any_of(" \t"), boost::token_compress_on);
56   return not fs->eof();
57 }
58
59 static ReplayAction* get_action(char* name)
60 {
61   ReplayAction* action;
62
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     // Read lines until I reach something for me (which breaks in loop body) or end of file reached
68     while (true) {
69       std::string action_line;
70       read_and_trim_line(action_fs, &action_line);
71       if (action_fs->eof())
72         break;
73       /* we cannot split in place here because we parse&store several lines for the colleagues... */
74       action = new ReplayAction();
75       boost::split(*action, action_line, boost::is_any_of(" \t"), boost::token_compress_on);
76
77       // if it's for me, I'm done
78       std::string evtname = action->front();
79       if (evtname.compare(name) == 0) {
80         return action;
81       } else {
82         // Else, I have to store it for the relevant colleague
83         std::queue<ReplayAction*>* otherqueue = nullptr;
84         auto act                              = action_queues.find(evtname);
85         if (act != action_queues.end()) {
86           otherqueue = act->second;
87         } else { // Damn. Create the queue of that guy
88           otherqueue = new std::queue<ReplayAction*>();
89           action_queues.insert({evtname, otherqueue});
90         }
91         otherqueue->push(action);
92       }
93     }
94     // end of file reached while searching in vain for more work
95   } else {
96     // Get something from my queue and return it
97     action = myqueue->front();
98     myqueue->pop();
99     return action;
100   }
101
102   return nullptr;
103 }
104
105 static void handle_action(ReplayAction& action)
106 {
107   XBT_DEBUG("%s replays a %s action", action.at(0).c_str(), action.at(1).c_str());
108   action_fun function = action_funs.at(action.at(1));
109   try {
110     function(action);
111   } catch (xbt_ex& e) {
112     action.clear();
113     xbt_die("Replay error:\n %s", e.what());
114   }
115 }
116
117 /**
118  * \ingroup XBT_replay
119  * \brief function used internally to actually run the replay
120  */
121 int replay_runner(int argc, char* argv[])
122 {
123   if (simgrid::xbt::action_fs) { // A unique trace file
124     while (true) {
125       simgrid::xbt::ReplayAction* evt = simgrid::xbt::get_action(argv[0]);
126       if (!evt)
127         break;
128       simgrid::xbt::handle_action(*evt);
129       delete evt;
130     }
131     if (action_queues.find(std::string(argv[0])) != action_queues.end()) {
132       std::queue<ReplayAction*>* myqueue = action_queues.at(std::string(argv[0]));
133       delete myqueue;
134       action_queues.erase(std::string(argv[0]));
135     }
136   } else { // Should have got my trace file in argument
137     simgrid::xbt::ReplayAction evt;
138     xbt_assert(argc >= 2, "No '%s' agent function provided, no simulation-wide trace file provided, "
139                           "and no process-wide trace file provided in deployment file. Aborting.",
140                argv[0]);
141     simgrid::xbt::ReplayReader reader(argv[1]);
142     while (reader.get(&evt)) {
143       if (evt.front().compare(argv[0]) == 0) {
144         simgrid::xbt::handle_action(evt);
145       } else {
146         XBT_WARN("Ignore trace element not for me");
147       }
148       evt.clear();
149     }
150   }
151   return 0;
152 }
153 }
154 }
155
156 /**
157  * \ingroup XBT_replay
158  * \brief Registers a function to handle a kind of action
159  *
160  * Registers a function to handle a kind of action
161  * This table is then used by \ref simgrid::xbt::replay_runner
162  *
163  * The argument of the function is the line describing the action, fields separated by spaces.
164  *
165  * \param action_name the reference name of the action.
166  * \param function prototype given by the type: void...(const char** action)
167  */
168 void xbt_replay_action_register(const char* action_name, action_fun function)
169 {
170   simgrid::xbt::action_funs[std::string(action_name)] = function;
171 }
172
173 /**
174  * \ingroup XBT_replay
175  * \brief Get the function that was previously registered to handle a kind of action
176  *
177  * This can be useful if you want to override and extend an existing action.
178  */
179 action_fun xbt_replay_action_get(const char* action_name)
180 {
181   return simgrid::xbt::action_funs.at(std::string(action_name));
182 }