Logo AND Algorithmique Numérique Distribuée

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