Logo AND Algorithmique Numérique Distribuée

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