Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
simplify the s4u version of actions-comm to use s4u
[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 "src/internal_config.h"
8 #include "xbt/ex.hpp"
9 #include "xbt/log.h"
10 #include "xbt/replay.hpp"
11 #include "xbt/str.h"
12 #include "xbt/sysdep.h"
13
14 #include <boost/algorithm/string.hpp>
15 #include <ctype.h>
16 #include <errno.h>
17 #include <fstream>
18 #include <queue>
19 #include <unordered_map>
20 #include <wchar.h>
21
22 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(replay,xbt,"Replay trace reader");
23
24 namespace simgrid {
25 namespace xbt {
26
27 bool is_replay_active = false;
28 typedef std::vector<std::string> ReplayAction;
29
30 bool replay_is_active()
31 {
32   return is_replay_active;
33 }
34
35 class ReplayReader {
36   std::ifstream* fs;
37   std::string line;
38
39 public:
40   char* filename_;
41   int linenum = 0;
42
43   explicit ReplayReader(const char* filename)
44   {
45     filename_ = xbt_strdup(filename);
46     fs        = new std::ifstream(filename, std::ifstream::in);
47   }
48   ~ReplayReader()
49   {
50     free(filename_);
51     delete fs;
52   }
53   bool get(ReplayAction* action);
54 };
55
56 bool ReplayReader::get(ReplayAction* action)
57 {
58   std::getline(*fs, line);
59   boost::trim(line);
60   XBT_DEBUG("got from trace: %s", line.c_str());
61   linenum++;
62
63   if (line.length() > 0 && line.find("#") == std::string::npos) {
64     boost::split(*action, line, boost::is_any_of(" \t"), boost::token_compress_on);
65     return !fs->eof();
66   } else {
67     if (fs->eof())
68       return false;
69     else
70       return this->get(action);
71   }
72 }
73 std::ifstream* action_fs = nullptr;
74 }
75 }
76
77 std::unordered_map<std::string, action_fun> xbt_action_funs;
78
79 xbt_dict_t xbt_action_queues = nullptr;
80
81 bool is_replay_active = false;
82
83 static simgrid::xbt::ReplayAction* action_get_action(char* name);
84
85 /**
86  * \ingroup XBT_replay
87  * \brief Registers a function to handle a kind of action
88  *
89  * Registers a function to handle a kind of action
90  * This table is then used by \ref xbt_replay_action_runner
91  *
92  * The argument of the function is the line describing the action, fields separated by spaces.
93  *
94  * \param action_name the reference name of the action.
95  * \param function prototype given by the type: void...(const char** action)
96  */
97 void xbt_replay_action_register(const char *action_name, action_fun function)
98 {
99   if (!is_replay_active) // If the user registers a function before the start
100     _xbt_replay_action_init();
101   xbt_action_funs.insert({std::string(action_name), function});
102 }
103
104 void _xbt_replay_action_init()
105 {
106   if (!is_replay_active) {
107     xbt_action_queues = xbt_dict_new_homogeneous(nullptr);
108     is_replay_active  = true;
109   }
110 }
111
112 void _xbt_replay_action_exit()
113 {
114   xbt_dict_free(&xbt_action_queues);
115   xbt_action_queues = nullptr;
116 }
117
118 /**
119  * \ingroup XBT_replay
120  * \brief function used internally to actually run the replay
121
122  * \param argc argc .
123  * \param argv argv
124  */
125 int xbt_replay_action_runner(int argc, char *argv[])
126 {
127   if (simgrid::xbt::action_fs) { // A unique trace file
128     while (true) {
129       simgrid::xbt::ReplayAction* evt = action_get_action(argv[0]);
130       if (evt == nullptr)
131         break;
132
133       char** args = new char*[evt->size() + 1];
134       int i       = 0;
135       for (auto arg : *evt) {
136         args[i] = xbt_strdup(arg.c_str());
137         i++;
138       }
139       args[i]             = nullptr;
140       action_fun function = xbt_action_funs.at(evt->at(1));
141
142       try {
143         function(args);
144       }
145       catch(xbt_ex& e) {
146         xbt_die("Replay error :\n %s", e.what());
147       }
148       for (unsigned int j = 0; j < evt->size(); j++)
149         xbt_free(args[j]);
150       delete[] args;
151       delete evt;
152     }
153   } else {                      // Should have got my trace file in argument
154     simgrid::xbt::ReplayAction* evt = new simgrid::xbt::ReplayAction();
155     xbt_assert(argc >= 2,
156                 "No '%s' agent function provided, no simulation-wide trace file provided, "
157                 "and no process-wide trace file provided in deployment file. Aborting.", argv[0]
158         );
159     simgrid::xbt::ReplayReader* reader = new simgrid::xbt::ReplayReader(argv[1]);
160     while (reader->get(evt)) {
161       if (evt->at(0).compare(argv[0]) == 0) {
162         char** args = new char*[evt->size() + 1];
163         int i       = 0;
164         for (auto arg : *evt) {
165           args[i] = xbt_strdup(arg.c_str());
166           i++;
167         }
168         args[i]             = nullptr;
169         action_fun function = xbt_action_funs.at(evt->at(1));
170         try {
171           function(args);
172         } catch(xbt_ex& e) {
173           for (unsigned int j = 0; j < evt->size(); j++)
174             xbt_free(args[j]);
175           delete[] args;
176           evt->clear();
177           xbt_die("Replay error on line %d of file %s :\n %s", reader->linenum, reader->filename_, e.what());
178         }
179         for (unsigned int j = 0; j < evt->size(); j++)
180           xbt_free(args[j]);
181         delete[] args;
182       } else {
183         XBT_WARN("%s:%d: Ignore trace element not for me", reader->filename_, reader->linenum);
184       }
185       evt->clear();
186     }
187     delete evt;
188     delete reader;
189   }
190   return 0;
191 }
192
193 static simgrid::xbt::ReplayAction* action_get_action(char* name)
194 {
195   simgrid::xbt::ReplayAction* action;
196
197   std::queue<simgrid::xbt::ReplayAction*>* myqueue =
198       (std::queue<simgrid::xbt::ReplayAction*>*)xbt_dict_get_or_null(xbt_action_queues, name);
199   if (myqueue == nullptr || myqueue->empty()) { // nothing stored for me. Read the file further
200     if (simgrid::xbt::action_fs == nullptr) {   // File closed now. There's nothing more to read. I'm out of here
201       goto todo_done;
202     }
203     // Read lines until I reach something for me (which breaks in loop body) or end of file reached
204     while (!simgrid::xbt::action_fs->eof()) {
205       std::string action_line;
206       std::getline(*simgrid::xbt::action_fs, action_line);
207       // cleanup and split the string I just read
208       boost::trim(action_line);
209       XBT_DEBUG("got from trace: %s", action_line.c_str());
210       if (action_line.length() > 0 && action_line.find("#") == std::string::npos) {
211         /* we cannot split in place here because we parse&store several lines for the colleagues... */
212         action = new simgrid::xbt::ReplayAction();
213         boost::split(*action, action_line, boost::is_any_of(" \t"), boost::token_compress_on);
214
215         // if it's for me, I'm done
216         std::string evtname = action->front();
217         if (evtname.compare(name) == 0) {
218           return action;
219         } else {
220           // Else, I have to store it for the relevant colleague
221           std::queue<simgrid::xbt::ReplayAction*>* otherqueue =
222               (std::queue<simgrid::xbt::ReplayAction*>*)xbt_dict_get_or_null(xbt_action_queues, evtname.c_str());
223           if (otherqueue == nullptr) { // Damn. Create the queue of that guy
224             otherqueue = new std::queue<simgrid::xbt::ReplayAction*>();
225             xbt_dict_set(xbt_action_queues, evtname.c_str(), otherqueue, nullptr);
226           }
227           otherqueue->push(action);
228         }
229       }
230     }
231     // end of file reached while searching in vain for more work
232   } else {
233     // Get something from my queue and return it
234     action = myqueue->front();
235     myqueue->pop();
236     return action;
237   }
238
239 // All my actions in the file are done and either I or a colleague closed the file. Let's cleanup before leaving.
240 todo_done:
241   if (myqueue != nullptr) {
242     delete myqueue;
243     xbt_dict_remove(xbt_action_queues, name);
244   }
245   return nullptr;
246 }