Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Unused exception parameter 'e'.
[simgrid.git] / src / xbt / xbt_replay.cpp
1 /* Copyright (c) 2010-2021. The SimGrid Team. All rights reserved.          */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 #include "simgrid/Exception.hpp"
7 #include "xbt/log.h"
8 #include "xbt/replay.hpp"
9
10 #include <boost/algorithm/string.hpp>
11
12 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(replay,xbt,"Replay trace reader");
13
14 namespace simgrid {
15 namespace xbt {
16
17 static std::ifstream action_fs;
18
19 std::unordered_map<std::string, action_fun> action_funs;
20 static std::unordered_map<std::string, std::queue<ReplayAction*>*> action_queues;
21
22 static void read_and_trim_line(std::ifstream& fs, std::string* line)
23 {
24   do {
25     std::getline(fs, *line);
26     boost::trim(*line);
27   } while (not fs.eof() && (line->length() == 0 || line->front() == '#'));
28   XBT_DEBUG("got from trace: %s", line->c_str());
29 }
30
31 class ReplayReader {
32   std::ifstream fs;
33   std::string line;
34
35 public:
36   explicit ReplayReader(const char* filename) : fs(filename, std::ifstream::in)
37   {
38     XBT_VERB("Prepare to replay file '%s'", filename);
39     xbt_assert(fs.is_open(), "Cannot read replay file '%s'", filename);
40   }
41   ReplayReader(const ReplayReader&) = delete;
42   ReplayReader& operator=(const ReplayReader&) = delete;
43   bool get(ReplayAction* action);
44 };
45
46 bool ReplayReader::get(ReplayAction* action)
47 {
48   read_and_trim_line(fs, &line);
49
50   boost::split(*action, line, boost::is_any_of(" \t"), boost::token_compress_on);
51   return not fs.eof();
52 }
53
54 static ReplayAction* get_action(const char* name)
55 {
56   ReplayAction* action;
57
58   std::queue<ReplayAction*>* myqueue = nullptr;
59   if (action_queues.find(std::string(name)) != action_queues.end())
60     myqueue = action_queues.at(std::string(name));
61   if (myqueue == nullptr || myqueue->empty()) { // Nothing stored for me. Read the file further
62     // Read lines until I reach something for me (which breaks in loop body) or end of file reached
63     while (true) {
64       std::string action_line;
65       read_and_trim_line(action_fs, &action_line);
66       if (action_fs.eof())
67         break;
68       /* we cannot split in place here because we parse&store several lines for the colleagues... */
69       action = new ReplayAction();
70       boost::split(*action, action_line, boost::is_any_of(" \t"), boost::token_compress_on);
71
72       // if it's for me, I'm done
73       std::string evtname = action->front();
74       if (evtname.compare(name) == 0)
75         return action;
76
77       // Else, I have to store it for the relevant colleague
78       std::queue<ReplayAction*>* otherqueue = nullptr;
79       auto act                              = action_queues.find(evtname);
80       if (act != action_queues.end()) {
81         otherqueue = act->second;
82       } else { // Damn. Create the queue of that guy
83         otherqueue = new std::queue<ReplayAction*>();
84         action_queues.insert({evtname, otherqueue});
85       }
86       otherqueue->push(action);
87     }
88     // end of file reached while searching in vain for more work
89   } else {
90     // Get something from my queue and return it
91     action = myqueue->front();
92     myqueue->pop();
93     return action;
94   }
95
96   return nullptr;
97 }
98
99 static void handle_action(ReplayAction& action)
100 {
101   XBT_DEBUG("%s replays a %s action", action.at(0).c_str(), action.at(1).c_str());
102   action_fun function = action_funs.at(action.at(1));
103   try {
104     function(action);
105   } catch (const Exception&) {
106     action.clear();
107     throw;
108   }
109 }
110
111 /**
112  * @ingroup XBT_replay
113  * @brief function used internally to actually run the replay
114  */
115 int replay_runner(const char* actor_name, const char* trace_filename)
116 {
117   std::string actor_name_string(actor_name);
118   if (simgrid::xbt::action_fs.is_open()) { // <A unique trace file
119     xbt_assert(trace_filename == nullptr,
120                "Passing nullptr to replay_runner() means that you want to use a shared trace, but you did not provide "
121                "any. Please use xbt_replay_set_tracefile().");
122     while (true) {
123       simgrid::xbt::ReplayAction* evt = simgrid::xbt::get_action(actor_name);
124       if (not evt)
125         break;
126       simgrid::xbt::handle_action(*evt);
127       delete evt;
128     }
129     if (action_queues.find(actor_name_string) != action_queues.end()) {
130       delete action_queues.at(actor_name_string);
131       action_queues.erase(actor_name_string);
132     }
133   } else { // Should have got my trace file in argument
134     xbt_assert(trace_filename != nullptr,
135                "Trace replay cannot mix shared and unshared traces for now. Please don't set a shared tracefile with "
136                "xbt_replay_set_tracefile() if you use actor-specific trace files using the second parameter of "
137                "replay_runner().");
138     simgrid::xbt::ReplayAction evt;
139     simgrid::xbt::ReplayReader reader(trace_filename);
140     while (reader.get(&evt)) {
141       if (evt.front().compare(actor_name) == 0) {
142         simgrid::xbt::handle_action(evt);
143       } else {
144         XBT_WARN("Ignore trace element not for me (target='%s', I am '%s')", evt.front().c_str(), actor_name);
145       }
146       evt.clear();
147     }
148   }
149   return 0;
150 }
151 }
152 }
153
154 /**
155  * @ingroup XBT_replay
156  * @brief Registers a function to handle a kind of action
157  *
158  * Registers a function to handle a kind of action
159  * This table is then used by @ref simgrid::xbt::replay_runner
160  *
161  * The argument of the function is the line describing the action, fields separated by spaces.
162  *
163  * @param action_name the reference name of the action.
164  * @param function prototype given by the type: void...(const char** action)
165  */
166 void xbt_replay_action_register(const char* action_name, const action_fun& function)
167 {
168   simgrid::xbt::action_funs[std::string(action_name)] = function;
169 }
170
171 /**
172  * @ingroup XBT_replay
173  * @brief Get the function that was previously registered to handle a kind of action
174  *
175  * This can be useful if you want to override and extend an existing action.
176  */
177 action_fun xbt_replay_action_get(const char* action_name)
178 {
179   return simgrid::xbt::action_funs.at(std::string(action_name));
180 }
181
182 void xbt_replay_set_tracefile(const std::string& filename)
183 {
184   xbt_assert(not simgrid::xbt::action_fs.is_open(), "Tracefile already set");
185   simgrid::xbt::action_fs.open(filename, std::ifstream::in);
186   xbt_assert(simgrid::xbt::action_fs.is_open(), "Failed to open file: %s", filename.c_str());
187 }