Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
factor line reading and action handling
[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 <fstream>
18 #include <queue>
19 #include <unordered_map>
20 #include <wchar.h>
21
22 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(replay,xbt,"Replay trace reader");
23
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;
27
28 namespace simgrid {
29 namespace xbt {
30
31 typedef std::vector<std::string> ReplayAction;
32
33 static void read_and_trim_line(std::ifstream* fs, std::string* line)
34 {
35   std::getline(*fs, *line);
36   boost::trim(*line);
37   XBT_DEBUG("got from trace: %s", line->c_str());
38 }
39
40 static void handle_action(ReplayAction* action)
41 {
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));
45   int i = 0;
46   for (auto arg : *action) {
47     c_action[i] = xbt_strdup(arg.c_str());
48     i++;
49   }
50   c_action[i] = nullptr;
51   try {
52     function(c_action);
53   } catch (xbt_ex& e) {
54     for (unsigned int j = 0; j < action->size(); j++)
55       xbt_free(c_action[j]);
56     delete[] c_action;
57     action->clear();
58     xbt_die("Replay error:\n %s", e.what());
59   }
60   for (unsigned int j = 0; j < action->size(); j++)
61     xbt_free(c_action[j]);
62   delete[] c_action;
63 }
64
65 bool replay_is_active()
66 {
67   return is_replay_active;
68 }
69
70 class ReplayReader {
71   std::ifstream* fs;
72   std::string line;
73
74 public:
75   char* filename_;
76   int linenum = 0;
77
78   explicit ReplayReader(const char* filename)
79   {
80     filename_ = xbt_strdup(filename);
81     fs        = new std::ifstream(filename, std::ifstream::in);
82   }
83   ~ReplayReader()
84   {
85     free(filename_);
86     delete fs;
87   }
88   bool get(ReplayAction* action);
89 };
90
91 bool ReplayReader::get(ReplayAction* action)
92 {
93   read_and_trim_line(fs, &line);
94   linenum++;
95
96   if (line.length() > 0 && line.find("#") == std::string::npos) {
97     boost::split(*action, line, boost::is_any_of(" \t"), boost::token_compress_on);
98     return !fs->eof();
99   } else {
100     if (fs->eof())
101       return false;
102     else
103       return this->get(action);
104   }
105 }
106 std::ifstream* action_fs = nullptr;
107 }
108 }
109
110 static simgrid::xbt::ReplayAction* action_get_action(char* name);
111 /**
112  * \ingroup XBT_replay
113  * \brief Registers a function to handle a kind of action
114  *
115  * Registers a function to handle a kind of action
116  * This table is then used by \ref xbt_replay_action_runner
117  *
118  * The argument of the function is the line describing the action, fields separated by spaces.
119  *
120  * \param action_name the reference name of the action.
121  * \param function prototype given by the type: void...(const char** action)
122  */
123 void xbt_replay_action_register(const char *action_name, action_fun function)
124 {
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});
128 }
129
130 void _xbt_replay_action_init()
131 {
132   if (!is_replay_active) {
133     xbt_action_queues = xbt_dict_new_homogeneous(nullptr);
134     is_replay_active  = true;
135   }
136 }
137
138 void _xbt_replay_action_exit()
139 {
140   xbt_dict_free(&xbt_action_queues);
141   xbt_action_queues = nullptr;
142 }
143
144 /**
145  * \ingroup XBT_replay
146  * \brief function used internally to actually run the replay
147
148  * \param argc argc .
149  * \param argv argv
150  */
151 int xbt_replay_action_runner(int argc, char *argv[])
152 {
153   if (simgrid::xbt::action_fs) { // A unique trace file
154     while (true) {
155       simgrid::xbt::ReplayAction* evt = action_get_action(argv[0]);
156       if (evt == nullptr)
157         break;
158       simgrid::xbt::handle_action(evt);
159       delete evt;
160     }
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]
166         );
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);
171       } else {
172         XBT_WARN("%s:%d: Ignore trace element not for me", reader->filename_, reader->linenum);
173       }
174       evt->clear();
175     }
176     delete evt;
177     delete reader;
178   }
179   return 0;
180 }
181
182 static simgrid::xbt::ReplayAction* action_get_action(char* name)
183 {
184   simgrid::xbt::ReplayAction* action;
185
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
190       goto todo_done;
191     }
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);
200
201         // if it's for me, I'm done
202         std::string evtname = action->front();
203         if (evtname.compare(name) == 0) {
204           return action;
205         } else {
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);
212           }
213           otherqueue->push(action);
214         }
215       }
216     }
217     // end of file reached while searching in vain for more work
218   } else {
219     // Get something from my queue and return it
220     action = myqueue->front();
221     myqueue->pop();
222     return action;
223   }
224
225 // All my actions in the file are done and either I or a colleague closed the file. Let's cleanup before leaving.
226 todo_done:
227   if (myqueue != nullptr) {
228     delete myqueue;
229     xbt_dict_remove(xbt_action_queues, name);
230   }
231   return nullptr;
232 }