Logo AND Algorithmique Numérique Distribuée

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