Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of git+ssh://scm.gforge.inria.fr//gitroot/simgrid/simgrid
[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 "src/internal_config.h"
8 #include "xbt/ex.hpp"
9 #include "xbt/log.h"
10 #include "xbt/replay.hpp"
11 #include "xbt/str.h"
12 #include "xbt/sysdep.h"
13
14 #include <boost/algorithm/string.hpp>
15 #include <ctype.h>
16 #include <errno.h>
17 #include <wchar.h>
18
19 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(replay,xbt,"Replay trace reader");
20
21 bool is_replay_active        = false;
22
23 namespace simgrid {
24 namespace xbt {
25
26 std::ifstream* action_fs = nullptr;
27 std::unordered_map<std::string, action_fun> action_funs;
28
29 static void read_and_trim_line(std::ifstream* fs, std::string* line)
30 {
31   std::getline(*fs, *line);
32   boost::trim(*line);
33   XBT_DEBUG("got from trace: %s", line->c_str());
34 }
35
36 class ReplayReader {
37   std::ifstream* fs;
38   std::string line;
39
40 public:
41   char* filename_;
42   int linenum = 0;
43
44   explicit ReplayReader(const char* filename)
45   {
46     filename_ = xbt_strdup(filename);
47     fs        = new std::ifstream(filename, std::ifstream::in);
48   }
49   ~ReplayReader()
50   {
51     free(filename_);
52     delete fs;
53   }
54   bool get(ReplayAction* action);
55 };
56
57 bool ReplayReader::get(ReplayAction* action)
58 {
59   read_and_trim_line(fs, &line);
60   linenum++;
61
62   if (line.length() > 0 && line.find("#") == std::string::npos) {
63     boost::split(*action, line, boost::is_any_of(" \t"), boost::token_compress_on);
64     return !fs->eof();
65   } else {
66     if (fs->eof())
67       return false;
68     else
69       return this->get(action);
70   }
71 }
72
73 void replay_init()
74 {
75   if (!is_replay_active) {
76     is_replay_active  = true;
77   }
78 }
79
80 void replay_exit()
81 {
82 }
83
84 bool replay_is_active()
85 {
86   return is_replay_active;
87 }
88
89 static ReplayAction* get_action(char* name)
90 {
91   ReplayAction* action;
92
93   std::queue<ReplayAction*>* myqueue = nullptr;
94   if (action_queues.find(std::string(name)) != action_queues.end())
95     myqueue = action_queues.at(std::string(name));
96   if (myqueue == nullptr || myqueue->empty()) { // Nothing stored for me. Read the file further
97     if (action_fs == nullptr) {                 // File closed now. There's nothing more to read. I'm out of here
98       goto todo_done;
99     }
100     // Read lines until I reach something for me (which breaks in loop body) or end of file reached
101     while (!action_fs->eof()) {
102       std::string action_line;
103       read_and_trim_line(action_fs, &action_line);
104       if (action_line.length() > 0 && action_line.find("#") == std::string::npos) {
105         /* we cannot split in place here because we parse&store several lines for the colleagues... */
106         action = new ReplayAction();
107         boost::split(*action, action_line, boost::is_any_of(" \t"), boost::token_compress_on);
108
109         // if it's for me, I'm done
110         std::string evtname = action->front();
111         if (evtname.compare(name) == 0) {
112           return action;
113         } else {
114           // Else, I have to store it for the relevant colleague
115           std::queue<ReplayAction*>* otherqueue = nullptr;
116           if (action_queues.find(evtname) != action_queues.end())
117             otherqueue = action_queues.at(evtname);
118           if (otherqueue == nullptr) { // Damn. Create the queue of that guy
119             otherqueue = new std::queue<ReplayAction*>();
120             action_queues.insert({evtname, otherqueue});
121           }
122           otherqueue->push(action);
123         }
124       }
125     }
126     // end of file reached while searching in vain for more work
127   } else {
128     // Get something from my queue and return it
129     action = myqueue->front();
130     myqueue->pop();
131     return action;
132   }
133
134 // All my actions in the file are done and either I or a colleague closed the file. Let's cleanup before leaving.
135 todo_done:
136   if (myqueue != nullptr) {
137     delete myqueue;
138     action_queues.erase(std::string(name));
139   }
140   return nullptr;
141 }
142
143 static void handle_action(ReplayAction* action)
144 {
145   XBT_DEBUG("%s replays a %s action", action->at(0).c_str(), action->at(1).c_str());
146   char** c_action     = new char*[action->size() + 1];
147   action_fun function = action_funs.at(action->at(1));
148   int i               = 0;
149   for (auto arg : *action) {
150     c_action[i] = xbt_strdup(arg.c_str());
151     i++;
152   }
153   c_action[i] = nullptr;
154   try {
155     function(c_action);
156   } catch (xbt_ex& e) {
157     for (unsigned int j = 0; j < action->size(); j++)
158       xbt_free(c_action[j]);
159     delete[] c_action;
160     action->clear();
161     xbt_die("Replay error:\n %s", e.what());
162   }
163   for (unsigned int j = 0; j < action->size(); j++)
164     xbt_free(c_action[j]);
165   delete[] c_action;
166 }
167
168 /**
169  * \ingroup XBT_replay
170  * \brief function used internally to actually run the replay
171
172  * \param argc argc .
173  * \param argv argv
174  */
175 int replay_runner(int argc, char* argv[])
176 {
177   if (simgrid::xbt::action_fs) { // A unique trace file
178     while (true) {
179       simgrid::xbt::ReplayAction* evt = simgrid::xbt::get_action(argv[0]);
180       if (evt == nullptr)
181         break;
182       simgrid::xbt::handle_action(evt);
183       delete evt;
184     }
185   } else { // Should have got my trace file in argument
186     simgrid::xbt::ReplayAction* evt = new simgrid::xbt::ReplayAction();
187     xbt_assert(argc >= 2, "No '%s' agent function provided, no simulation-wide trace file provided, "
188                           "and no process-wide trace file provided in deployment file. Aborting.",
189                argv[0]);
190     simgrid::xbt::ReplayReader* reader = new simgrid::xbt::ReplayReader(argv[1]);
191     while (reader->get(evt)) {
192       if (evt->at(0).compare(argv[0]) == 0) {
193         simgrid::xbt::handle_action(evt);
194       } else {
195         XBT_WARN("%s:%d: Ignore trace element not for me", reader->filename_, reader->linenum);
196       }
197       evt->clear();
198     }
199     delete evt;
200     delete reader;
201   }
202   return 0;
203 }
204 }
205 }
206
207 /**
208  * \ingroup XBT_replay
209  * \brief Registers a function to handle a kind of action
210  *
211  * Registers a function to handle a kind of action
212  * This table is then used by \ref xbt_replay_action_runner
213  *
214  * The argument of the function is the line describing the action, fields separated by spaces.
215  *
216  * \param action_name the reference name of the action.
217  * \param function prototype given by the type: void...(const char** action)
218  */
219 void xbt_replay_action_register(const char* action_name, action_fun function)
220 {
221   if (!is_replay_active) // If the user registers a function before the start
222     simgrid::xbt::replay_init();
223   simgrid::xbt::action_funs.insert({std::string(action_name), function});
224 }