Logo AND Algorithmique Numérique Distribuée

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