Logo AND Algorithmique Numérique Distribuée

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