Logo AND Algorithmique Numérique Distribuée

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