Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Deprecate legacy xbt_ex; kill xbt_errcat_t.
[simgrid.git] / src / xbt / xbt_replay.cpp
1 /* Copyright (c) 2010-2019. 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 std::ifstream* action_fs = nullptr;
18 std::unordered_map<std::string, action_fun> action_funs;
19 static std::unordered_map<std::string, std::queue<ReplayAction*>*> action_queues;
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) : fs(filename, std::ifstream::in)
36   {
37     XBT_VERB("Prepare to replay file '%s'", filename);
38     xbt_assert(fs.is_open(), "Cannot read replay file '%s'", filename);
39   }
40   ReplayReader(const ReplayReader&) = delete;
41   ReplayReader& operator=(const ReplayReader&) = delete;
42   bool get(ReplayAction* action);
43 };
44
45 bool ReplayReader::get(ReplayAction* action)
46 {
47   read_and_trim_line(fs, &line);
48
49   boost::split(*action, line, boost::is_any_of(" \t"), boost::token_compress_on);
50   return not fs.eof();
51 }
52
53 static ReplayAction* get_action(const char* name)
54 {
55   ReplayAction* action;
56
57   std::queue<ReplayAction*>* myqueue = nullptr;
58   if (action_queues.find(std::string(name)) != action_queues.end())
59     myqueue = action_queues.at(std::string(name));
60   if (myqueue == nullptr || myqueue->empty()) { // Nothing stored for me. Read the file further
61     // Read lines until I reach something for me (which breaks in loop body) or end of file reached
62     while (true) {
63       std::string action_line;
64       read_and_trim_line(*action_fs, &action_line);
65       if (action_fs->eof())
66         break;
67       /* we cannot split in place here because we parse&store several lines for the colleagues... */
68       action = new ReplayAction();
69       boost::split(*action, action_line, boost::is_any_of(" \t"), boost::token_compress_on);
70
71       // if it's for me, I'm done
72       std::string evtname = action->front();
73       if (evtname.compare(name) == 0) {
74         return action;
75       } else {
76         // Else, I have to store it for the relevant colleague
77         std::queue<ReplayAction*>* otherqueue = nullptr;
78         auto act                              = action_queues.find(evtname);
79         if (act != action_queues.end()) {
80           otherqueue = act->second;
81         } else { // Damn. Create the queue of that guy
82           otherqueue = new std::queue<ReplayAction*>();
83           action_queues.insert({evtname, otherqueue});
84         }
85         otherqueue->push(action);
86       }
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& e) {
106     action.clear();
107     xbt_die("Replay error:\n %s", e.what());
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) { // <A unique trace file
119     while (true) {
120       simgrid::xbt::ReplayAction* evt = simgrid::xbt::get_action(actor_name);
121       if (!evt)
122         break;
123       simgrid::xbt::handle_action(*evt);
124       delete evt;
125     }
126     if (action_queues.find(actor_name_string) != action_queues.end()) {
127       std::queue<ReplayAction*>* myqueue = action_queues.at(actor_name_string);
128       delete myqueue;
129       action_queues.erase(actor_name_string);
130     }
131   } else { // Should have got my trace file in argument
132     xbt_assert(trace_filename != nullptr);
133     simgrid::xbt::ReplayAction evt;
134     simgrid::xbt::ReplayReader reader(trace_filename);
135     while (reader.get(&evt)) {
136       if (evt.front().compare(actor_name) == 0) {
137         simgrid::xbt::handle_action(evt);
138       } else {
139         XBT_WARN("Ignore trace element not for me (target='%s', I am '%s')", evt.front().c_str(), actor_name);
140       }
141       evt.clear();
142     }
143   }
144   return 0;
145 }
146 }
147 }
148
149 /**
150  * @ingroup XBT_replay
151  * @brief Registers a function to handle a kind of action
152  *
153  * Registers a function to handle a kind of action
154  * This table is then used by @ref simgrid::xbt::replay_runner
155  *
156  * The argument of the function is the line describing the action, fields separated by spaces.
157  *
158  * @param action_name the reference name of the action.
159  * @param function prototype given by the type: void...(const char** action)
160  */
161 void xbt_replay_action_register(const char* action_name, const action_fun& function)
162 {
163   simgrid::xbt::action_funs[std::string(action_name)] = function;
164 }
165
166 /**
167  * @ingroup XBT_replay
168  * @brief Get the function that was previously registered to handle a kind of action
169  *
170  * This can be useful if you want to override and extend an existing action.
171  */
172 action_fun xbt_replay_action_get(const char* action_name)
173 {
174   return simgrid::xbt::action_funs.at(std::string(action_name));
175 }