Logo AND Algorithmique Numérique Distribuée

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