Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[mc] Move code in simgrid::mc
[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   xbt_assert(res->fp != NULL, "Cannot open %s: %s", filename, strerror(errno));
59   res->filename = xbt_strdup(filename);
60   return res;
61 }
62
63 const char **xbt_replay_reader_get(xbt_replay_reader_t reader)
64 {
65   ssize_t read;
66   xbt_dynar_t d;
67   read = xbt_getline(&reader->line, &reader->line_len, reader->fp);
68   //XBT_INFO("got from trace: %s",reader->line);
69   reader->linenum++;
70   if (read==-1)
71     return NULL; /* end of file */
72   char *comment = strchr(reader->line, '#');
73   if (comment != NULL)
74     *comment = '\0';
75   xbt_str_trim(reader->line, NULL);
76   if (reader->line[0] == '\0')
77     return xbt_replay_reader_get(reader); /* Get next line */
78
79   d=xbt_str_split_quoted_in_place(reader->line);
80   if (xbt_dynar_is_empty(d)) {
81     xbt_dynar_free(&d);
82     return xbt_replay_reader_get(reader); /* Get next line */
83   }
84   return xbt_dynar_to_array(d);
85 }
86
87 void xbt_replay_reader_free(xbt_replay_reader_t *reader)
88 {
89   free((*reader)->filename);
90   free((*reader)->position);
91   fclose((*reader)->fp);
92   free((*reader)->line);
93   free(*reader);
94   *reader=NULL;
95 }
96
97 /**
98  * \ingroup XBT_replay
99  * \brief Registers a function to handle a kind of action
100  *
101  * Registers a function to handle a kind of action
102  * This table is then used by \ref xbt_replay_action_runner
103  *
104  * The argument of the function is the line describing the action, splitted on spaces with xbt_str_split_quoted()
105  *
106  * \param action_name the reference name of the action.
107  * \param function prototype given by the type: void...(xbt_dynar_t action)
108  */
109 void xbt_replay_action_register(const char *action_name, action_fun function)
110 {
111   char* lowername = str_tolower (action_name);
112   xbt_dict_set(xbt_action_funs, lowername, function, NULL);
113   xbt_free(lowername);
114 }
115
116 /** @brief Initializes the replay mechanism, and returns true if (and only if) it was necessary
117  *
118  * It returns false if it was already done by another process.
119  */
120 int _xbt_replay_action_init(void)
121 {
122   if (xbt_action_funs)
123     return 0;
124   is_replay_active = 1;
125   xbt_action_funs = xbt_dict_new_homogeneous(NULL);
126   xbt_action_queues = xbt_dict_new_homogeneous(NULL);
127   return 1;
128 }
129
130 void _xbt_replay_action_exit(void)
131 {
132   xbt_dict_free(&xbt_action_queues);
133   xbt_dict_free(&xbt_action_funs);
134   free(action_line);
135   xbt_action_queues = NULL;
136   xbt_action_funs = NULL;
137   action_line = NULL;
138 }
139
140 /**
141  * \ingroup XBT_replay
142  * \brief function used internally to actually run the replay
143
144  * \param argc argc .
145  * \param argv argv
146  */
147 int xbt_replay_action_runner(int argc, char *argv[])
148 {
149   int i;
150   xbt_ex_t e;
151   if (xbt_action_fp) {              // A unique trace file
152     char **evt;
153     while ((evt = action_get_action(argv[0]))) {
154       char* lowername = str_tolower (evt[1]);
155       action_fun function =
156         (action_fun)xbt_dict_get(xbt_action_funs, lowername);
157       xbt_free(lowername);
158       TRY{
159         function((const char **)evt);
160       }
161       CATCH(e) {
162         free(evt);
163         xbt_die("Replay error :\n %s", e.msg);
164       }
165       for (i=0;evt[i]!= NULL;i++)
166         free(evt[i]);
167       free(evt);
168     }
169   } else {                      // Should have got my trace file in argument
170     const char **evt;
171     xbt_assert(argc >= 2,
172                 "No '%s' agent function provided, no simulation-wide trace file provided, "
173                 "and no process-wide trace file provided in deployment file. Aborting.",
174                 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         }
185         CATCH(e) {
186           free(evt);
187           xbt_die("Replay error on line %d of file %s :\n %s" , reader->linenum,reader->filename, e.msg);
188         }
189       } else {
190         XBT_WARN("%s:%d: Ignore trace element not for me", reader->filename, reader->linenum);
191       }
192       free(evt);
193     }
194     xbt_replay_reader_free(&reader);
195   }
196   return 0;
197 }
198
199
200 static char **action_get_action(char *name)
201 {
202   xbt_dynar_t evt = NULL;
203   char *evtname = NULL;
204
205   xbt_dynar_t myqueue = xbt_dict_get_or_null(xbt_action_queues, name);
206   if (myqueue == NULL || xbt_dynar_is_empty(myqueue)) {      // nothing stored for me. Read the file further
207
208     if (xbt_action_fp == NULL) {    // File closed now. There's nothing more to read. I'm out of here
209       goto todo_done;
210     }
211     // Read lines until I reach something for me (which breaks in loop body)
212     // or end of file reached
213     while (xbt_getline(&action_line, &action_len, xbt_action_fp) != -1) {
214       // cleanup and split the string I just read
215       char *comment = strchr(action_line, '#');
216       if (comment != NULL)
217         *comment = '\0';
218       xbt_str_trim(action_line, NULL);
219       if (action_line[0] == '\0')
220         continue;
221       /* we cannot split in place here because we parse&store several lines for
222        * the colleagues... */
223       evt = xbt_str_split_quoted(action_line);
224
225       // if it's for me, I'm done
226       evtname = xbt_dynar_get_as(evt, 0, char *);
227       if (!strcasecmp(name, evtname)) {
228         return xbt_dynar_to_array(evt);
229       } else {
230         // Else, I have to store it for the relevant colleague
231         xbt_dynar_t otherqueue =
232             xbt_dict_get_or_null(xbt_action_queues, evtname);
233         if (otherqueue == NULL) {       // Damn. Create the queue of that guy
234           otherqueue =
235               xbt_dynar_new(sizeof(xbt_dynar_t), xbt_dynar_free_voidp);
236           xbt_dict_set(xbt_action_queues, evtname, otherqueue, NULL);
237         }
238         xbt_dynar_push(otherqueue, &evt);
239       }
240     }
241     goto todo_done;             // end of file reached while searching in vain for more work
242   } else {
243     // Get something from my queue and return it
244     xbt_dynar_shift(myqueue, &evt);
245     return xbt_dynar_to_array(evt);
246   }
247
248
249   // I did all my actions for me in the file (either I closed the file, or a colleague did)
250   // Let's cleanup before leaving
251 todo_done:
252   if (myqueue != NULL) {
253     xbt_dynar_free(&myqueue);
254     xbt_dict_remove(xbt_action_queues, name);
255   }
256   return NULL;
257 }