Logo AND Algorithmique Numérique Distribuée

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