Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
9af925bd2fafccd19d65a31cdd7858e70a47ec12
[simgrid.git] / src / xbt / xbt_replay.cpp
1 /* Copyright (c) 2010, 2012-2015. 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 "src/internal_config.h"
9 #include "xbt/sysdep.h"
10 #include "xbt/log.h"
11 #include "xbt/str.h"
12 #include "xbt/file.h"
13 #include "xbt/replay.h"
14
15 #include <errno.h>
16 #include <ctype.h>
17 #include <wchar.h>
18
19 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(replay,xbt,"Replay trace reader");
20
21 typedef struct s_replay_reader {
22   FILE *fp;
23   char *line;
24   size_t line_len;
25   char *position; /* stable storage */
26   char *filename; 
27   int linenum;
28 } s_xbt_replay_reader_t;
29
30 FILE *xbt_action_fp;
31
32 xbt_dict_t xbt_action_funs = nullptr;
33 xbt_dict_t xbt_action_queues = nullptr;
34
35 static char *action_line = nullptr;
36 static size_t action_len = 0;
37
38 int is_replay_active = 0 ;
39
40 static char **action_get_action(char *name);
41
42 static char *str_tolower (const char *str)
43 {
44   char *ret = xbt_strdup (str);
45   int n     = strlen(ret);
46   for (int i = 0; i < n; i++)
47     ret[i] = tolower (str[i]);
48   return ret;
49 }
50
51 int _xbt_replay_is_active(){
52   return is_replay_active;
53 }
54
55 xbt_replay_reader_t xbt_replay_reader_new(const char *filename)
56 {
57   xbt_replay_reader_t res = xbt_new0(s_xbt_replay_reader_t,1);
58   res->fp = fopen(filename, "r");
59   xbt_assert(res->fp != nullptr, "Cannot open %s: %s", filename, strerror(errno));
60   res->filename = xbt_strdup(filename);
61   return res;
62 }
63
64 const char **xbt_replay_reader_get(xbt_replay_reader_t reader)
65 {
66   ssize_t read = xbt_getline(&reader->line, &reader->line_len, reader->fp);
67   XBT_DEBUG("got from trace: %s", reader->line);
68   reader->linenum++;
69   if (read==-1)
70     return nullptr; /* end of file */
71   char *comment = strchr(reader->line, '#');
72   if (comment != nullptr)
73     *comment = '\0';
74   xbt_str_trim(reader->line, nullptr);
75   if (reader->line[0] == '\0')
76     return xbt_replay_reader_get(reader); /* Get next line */
77
78   xbt_dynar_t d = xbt_str_split_quoted_in_place(reader->line);
79   if (xbt_dynar_is_empty(d)) {
80     xbt_dynar_free(&d);
81     return xbt_replay_reader_get(reader); /* Get next line */
82   }
83   return (const char**) xbt_dynar_to_array(d);
84 }
85
86 void xbt_replay_reader_free(xbt_replay_reader_t *reader)
87 {
88   free((*reader)->filename);
89   free((*reader)->position);
90   fclose((*reader)->fp);
91   free((*reader)->line);
92   free(*reader);
93   *reader=nullptr;
94 }
95
96 /**
97  * \ingroup XBT_replay
98  * \brief Registers a function to handle a kind of action
99  *
100  * Registers a function to handle a kind of action
101  * This table is then used by \ref xbt_replay_action_runner
102  *
103  * The argument of the function is the line describing the action, splitted on spaces with xbt_str_split_quoted()
104  *
105  * \param action_name the reference name of the action.
106  * \param function prototype given by the type: void...(xbt_dynar_t action)
107  */
108 void xbt_replay_action_register(const char *action_name, action_fun function)
109 {
110   if (xbt_action_funs == nullptr) // If the user registers a function before the start
111     _xbt_replay_action_init();
112
113   char* lowername = str_tolower (action_name);
114   xbt_dict_set(xbt_action_funs, lowername, (void*) function, nullptr);
115   xbt_free(lowername);
116 }
117
118 /** @brief Initializes the replay mechanism, and returns true if (and only if) it was necessary
119  *
120  * It returns false if it was already done by another process.
121  */
122 int _xbt_replay_action_init()
123 {
124   if (xbt_action_funs)
125     return 0;
126   is_replay_active = 1;
127   xbt_action_funs = xbt_dict_new_homogeneous(nullptr);
128   xbt_action_queues = xbt_dict_new_homogeneous(nullptr);
129   return 1;
130 }
131
132 void _xbt_replay_action_exit()
133 {
134   xbt_dict_free(&xbt_action_queues);
135   xbt_dict_free(&xbt_action_funs);
136   free(action_line);
137   xbt_action_queues = nullptr;
138   xbt_action_funs = nullptr;
139   action_line = nullptr;
140 }
141
142 /**
143  * \ingroup XBT_replay
144  * \brief function used internally to actually run the replay
145
146  * \param argc argc .
147  * \param argv argv
148  */
149 int xbt_replay_action_runner(int argc, char *argv[])
150 {
151   if (xbt_action_fp) {              // A unique trace file
152     while (true) {
153       char **evt = action_get_action(argv[0]);
154       if (evt == nullptr)
155         break;
156
157       char* lowername = str_tolower (evt[1]);
158       action_fun function = (action_fun)xbt_dict_get(xbt_action_funs, lowername);
159       xbt_free(lowername);
160       try {
161         function((const char **)evt);
162       }
163       catch(xbt_ex& e) {
164         xbt_die("Replay error :\n %s", e.what());
165       }
166       for (int i=0;evt[i]!= nullptr;i++)
167         free(evt[i]);
168       free(evt);
169     }
170   } else {                      // Should have got my trace file in argument
171     const char **evt;
172     xbt_assert(argc >= 2,
173                 "No '%s' agent function provided, no simulation-wide trace file provided, "
174                 "and no process-wide trace file provided in deployment file. Aborting.", argv[0]
175         );
176     xbt_replay_reader_t reader = xbt_replay_reader_new(argv[1]);
177     while ((evt=xbt_replay_reader_get(reader))) {
178       if (!strcmp(argv[0],evt[0])) {
179         char* lowername = str_tolower (evt[1]);
180         action_fun function = (action_fun)xbt_dict_get(xbt_action_funs, lowername);
181         xbt_free(lowername);
182         try {
183           function(evt);
184         } catch(xbt_ex& e) {
185           free(evt);
186           xbt_die("Replay error on line %d of file %s :\n %s" , reader->linenum,reader->filename, e.what());
187         }
188       } else {
189         XBT_WARN("%s:%d: Ignore trace element not for me", reader->filename, reader->linenum);
190       }
191       free(evt);
192     }
193     xbt_replay_reader_free(&reader);
194   }
195   return 0;
196 }
197
198 static char **action_get_action(char *name)
199 {
200   xbt_dynar_t evt = nullptr;
201   char *evtname = nullptr;
202
203   xbt_dynar_t myqueue = (xbt_dynar_t) xbt_dict_get_or_null(xbt_action_queues, name);
204   if (myqueue == nullptr || xbt_dynar_is_empty(myqueue)) {      // nothing stored for me. Read the file further
205     if (xbt_action_fp == nullptr) {    // File closed now. There's nothing more to read. I'm out of here
206       goto todo_done;
207     }
208     // Read lines until I reach something for me (which breaks in loop body)
209     // or end of file reached
210     while (xbt_getline(&action_line, &action_len, xbt_action_fp) != -1) {
211       // cleanup and split the string I just read
212       char *comment = strchr(action_line, '#');
213       if (comment != nullptr)
214         *comment = '\0';
215       xbt_str_trim(action_line, nullptr);
216       if (action_line[0] == '\0')
217         continue;
218       /* we cannot split in place here because we parse&store several lines for
219        * the colleagues... */
220       evt = xbt_str_split_quoted(action_line);
221
222       // if it's for me, I'm done
223       evtname = xbt_dynar_get_as(evt, 0, char *);
224       if (!strcasecmp(name, evtname)) {
225         return (char**) xbt_dynar_to_array(evt);
226       } else {
227         // Else, I have to store it for the relevant colleague
228         xbt_dynar_t otherqueue =
229             (xbt_dynar_t) xbt_dict_get_or_null(xbt_action_queues, evtname);
230         if (otherqueue == nullptr) {       // Damn. Create the queue of that guy
231           otherqueue = xbt_dynar_new(sizeof(xbt_dynar_t), xbt_dynar_free_voidp);
232           xbt_dict_set(xbt_action_queues, evtname, otherqueue, nullptr);
233         }
234         xbt_dynar_push(otherqueue, &evt);
235       }
236     }
237     // end of file reached while searching in vain for more work
238   } else {
239     // Get something from my queue and return it
240     xbt_dynar_shift(myqueue, &evt);
241     return (char**) xbt_dynar_to_array(evt);
242   }
243
244   // I did all my actions for me in the file (either I closed the file, or a colleague did)
245   // Let's cleanup before leaving
246 todo_done:
247   if (myqueue != nullptr) {
248     xbt_dynar_free(&myqueue);
249     xbt_dict_remove(xbt_action_queues, name);
250   }
251   return nullptr;
252 }