Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
model-checker : MUTEX_UNLOCK is invisible for MC
[simgrid.git] / src / xbt / xbt_replay.c
1 /* Copyright (c) 2010, 2012-2014. 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 *action_fp;
28
29 xbt_dict_t action_funs;
30 xbt_dict_t action_queues;
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(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(action_funs, lowername);
131   xbt_free(lowername);
132 }
133
134 void _xbt_replay_action_init(void)
135 {
136   is_replay_active = 1;
137   action_funs = xbt_dict_new_homogeneous(NULL);
138   action_queues = xbt_dict_new_homogeneous(NULL);
139 }
140
141 void _xbt_replay_action_exit(void)
142 {
143   xbt_dict_free(&action_queues);
144   xbt_dict_free(&action_funs);
145   free(action_line);
146 }
147
148 /**
149  * \ingroup XBT_replay
150  * \brief function used internally to actually run the replay
151
152  * \param argc argc .
153  * \param argv argv
154  */
155 int xbt_replay_action_runner(int argc, char *argv[])
156 {
157   int i;
158   xbt_ex_t e;
159   if (action_fp) {              // A unique trace file
160     char **evt;
161     while ((evt = action_get_action(argv[0]))) {
162       char* lowername = str_tolower (evt[1]);
163       action_fun function =
164         (action_fun)xbt_dict_get(action_funs, lowername);
165       xbt_free(lowername);
166       TRY{
167         function((const char **)evt);
168       }
169       CATCH(e) {
170         free(evt);
171         xbt_die("Replay error :\n %s"
172                   , e.msg);
173       }
174       for (i=0;evt[i]!= NULL;i++)
175         free(evt[i]);
176       free(evt);
177     }
178   } else {                      // Should have got my trace file in argument
179     const char **evt;
180     xbt_assert(argc >= 2,
181                 "No '%s' agent function provided, no simulation-wide trace file provided, "
182                 "and no process-wide trace file provided in deployment file. Aborting.",
183                 argv[0]
184         );
185     xbt_replay_reader_t reader = xbt_replay_reader_new(argv[1]);
186     while ((evt=xbt_replay_reader_get(reader))) {
187       if (!strcmp(argv[0],evt[0])) {
188         char* lowername = str_tolower (evt[1]);
189         action_fun function = (action_fun)xbt_dict_get(action_funs, lowername);
190         xbt_free(lowername);
191         TRY{
192           function(evt);
193         }
194         CATCH(e) {
195           free(evt);
196           xbt_die("Replay error on line %d of file %s :\n %s"
197                      , reader->linenum,reader->filename, e.msg);               
198         }
199       } else {
200         XBT_WARN("%s: Ignore trace element not for me",
201               xbt_replay_reader_position(reader));
202       }
203       free(evt);
204     }
205     xbt_replay_reader_free(&reader);
206   }
207   return 0;
208 }
209
210
211 static char **action_get_action(char *name)
212 {
213   xbt_dynar_t evt = NULL;
214   char *evtname = NULL;
215
216   xbt_dynar_t myqueue = xbt_dict_get_or_null(action_queues, name);
217   if (myqueue == NULL || xbt_dynar_is_empty(myqueue)) {      // nothing stored for me. Read the file further
218
219     if (action_fp == NULL) {    // File closed now. There's nothing more to read. I'm out of here
220       goto todo_done;
221     }
222     // Read lines until I reach something for me (which breaks in loop body)
223     // or end of file reached
224     while (xbt_getline(&action_line, &action_len, action_fp) != -1) {
225       // cleanup and split the string I just read
226       char *comment = strchr(action_line, '#');
227       if (comment != NULL)
228         *comment = '\0';
229       xbt_str_trim(action_line, NULL);
230       if (action_line[0] == '\0')
231         continue;
232       /* we cannot split in place here because we parse&store several lines for
233        * the colleagues... */
234       evt = xbt_str_split_quoted(action_line);
235
236       // if it's for me, I'm done
237       evtname = xbt_dynar_get_as(evt, 0, char *);
238       if (!strcasecmp(name, evtname)) {
239         return xbt_dynar_to_array(evt);
240       } else {
241         // Else, I have to store it for the relevant colleague
242         xbt_dynar_t otherqueue =
243             xbt_dict_get_or_null(action_queues, evtname);
244         if (otherqueue == NULL) {       // Damn. Create the queue of that guy
245           otherqueue =
246               xbt_dynar_new(sizeof(xbt_dynar_t), xbt_dynar_free_voidp);
247           xbt_dict_set(action_queues, evtname, otherqueue, NULL);
248         }
249         xbt_dynar_push(otherqueue, &evt);
250       }
251     }
252     goto todo_done;             // end of file reached while searching in vain for more work
253   } else {
254     // Get something from my queue and return it
255     xbt_dynar_shift(myqueue, &evt);
256     return xbt_dynar_to_array(evt);
257   }
258
259
260   // I did all my actions for me in the file (either I closed the file, or a colleague did)
261   // Let's cleanup before leaving
262 todo_done:
263   if (myqueue != NULL) {
264     xbt_dynar_free(&myqueue);
265     xbt_dict_remove(action_queues, name);
266   }
267   return NULL;
268 }