Logo AND Algorithmique Numérique Distribuée

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