Logo AND Algorithmique Numérique Distribuée

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