Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Throw unimplemented.
[simgrid.git] / src / xbt / xbt_replay.c
1 /* Copyright (c) 2010, 2012-2013. 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; int linenum;
24 } s_xbt_replay_reader_t;
25
26 FILE *action_fp;
27
28 xbt_dict_t action_funs;
29 xbt_dict_t action_queues;
30
31 static char *action_line = NULL;
32 static size_t action_len = 0;
33
34 static char **action_get_action(char *name);
35
36 static char *str_tolower (const char *str)
37 {
38   char *ret = xbt_strdup (str);
39   int i, n = strlen (ret);
40   for (i = 0; i < n; i++)
41     ret[i] = tolower (str[i]);
42   return ret;
43 }
44
45
46 xbt_replay_reader_t xbt_replay_reader_new(const char *filename)
47 {
48   xbt_replay_reader_t res = xbt_new0(s_xbt_replay_reader_t,1);
49   res->fp = fopen(filename, "r");
50   xbt_assert(res->fp != NULL, "Cannot open %s: %s", filename,
51       strerror(errno));
52   res->filename = xbt_strdup(filename);
53   return res;
54 }
55
56 const char *xbt_replay_reader_position(xbt_replay_reader_t reader)
57 {
58   free(reader->position);
59   reader->position = bprintf("%s:%d",reader->filename,reader->linenum);
60   return reader->position;
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(action_funs, lowername, function, NULL);
113   xbt_free(lowername);
114 }
115
116 /** \ingroup XBT_replay
117  * \brief Unregisters a function, which handled a kind of action
118  *
119  * \param action_name the reference name of the action.
120  */
121 void xbt_replay_action_unregister(const char *action_name)
122 {
123   char* lowername = str_tolower (action_name);
124   xbt_dict_remove(action_funs, lowername);
125   xbt_free(lowername);
126 }
127
128 void _xbt_replay_action_init(void)
129 {
130   action_funs = xbt_dict_new_homogeneous(NULL);
131   action_queues = xbt_dict_new_homogeneous(NULL);
132 }
133
134 void _xbt_replay_action_exit(void)
135 {
136   xbt_dict_free(&action_queues);
137   xbt_dict_free(&action_funs);
138   free(action_line);
139 }
140
141 /**
142  * \ingroup XBT_replay
143  * \brief TODO
144
145  * \param argc argc .
146  * \param argv argv
147  */
148 int xbt_replay_action_runner(int argc, char *argv[])
149 {
150   int i;
151   if (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(action_funs, lowername);
157       xbt_free(lowername);
158       function((const char **)evt);
159       for (i=0;evt[i]!= NULL;i++)
160         free(evt[i]);
161       free(evt);
162     }
163   } else {                      // Should have got my trace file in argument
164     const char **evt;
165     xbt_assert(argc >= 2,
166                 "No '%s' agent function provided, no simulation-wide trace file provided, "
167                 "and no process-wide trace file provided in deployment file. Aborting.",
168                 argv[0]
169         );
170     xbt_replay_reader_t reader = xbt_replay_reader_new(argv[1]);
171     while ((evt=xbt_replay_reader_get(reader))) {
172       if (!strcmp(argv[0],evt[0])) {
173         char* lowername = str_tolower (evt[1]);
174         action_fun function = (action_fun)xbt_dict_get(action_funs, lowername);
175         xbt_free(lowername);
176         function(evt);
177       } else {
178         XBT_WARN("%s: Ignore trace element not for me",
179               xbt_replay_reader_position(reader));
180       }
181       free(evt);
182     }
183     xbt_replay_reader_free(&reader);
184   }
185   return 0;
186 }
187
188
189 static char **action_get_action(char *name)
190 {
191   xbt_dynar_t evt = NULL;
192   char *evtname = NULL;
193
194   xbt_dynar_t myqueue = xbt_dict_get_or_null(action_queues, name);
195   if (myqueue == NULL || xbt_dynar_is_empty(myqueue)) {      // nothing stored for me. Read the file further
196
197     if (action_fp == NULL) {    // File closed now. There's nothing more to read. I'm out of here
198       goto todo_done;
199     }
200     // Read lines until I reach something for me (which breaks in loop body)
201     // or end of file reached
202     while (xbt_getline(&action_line, &action_len, action_fp) != -1) {
203       // cleanup and split the string I just read
204       char *comment = strchr(action_line, '#');
205       if (comment != NULL)
206         *comment = '\0';
207       xbt_str_trim(action_line, NULL);
208       if (action_line[0] == '\0')
209         continue;
210       /* we cannot split in place here because we parse&store several lines for
211        * the colleagues... */
212       evt = xbt_str_split_quoted(action_line);
213
214       // if it's for me, I'm done
215       evtname = xbt_dynar_get_as(evt, 0, char *);
216       if (!strcasecmp(name, evtname)) {
217         return xbt_dynar_to_array(evt);
218       } else {
219         // Else, I have to store it for the relevant colleague
220         xbt_dynar_t otherqueue =
221             xbt_dict_get_or_null(action_queues, evtname);
222         if (otherqueue == NULL) {       // Damn. Create the queue of that guy
223           otherqueue =
224               xbt_dynar_new(sizeof(xbt_dynar_t), xbt_dynar_free_voidp);
225           xbt_dict_set(action_queues, evtname, otherqueue, NULL);
226         }
227         xbt_dynar_push(otherqueue, &evt);
228       }
229     }
230     goto todo_done;             // end of file reached while searching in vain for more work
231   } else {
232     // Get something from my queue and return it
233     xbt_dynar_shift(myqueue, &evt);
234     return xbt_dynar_to_array(evt);
235   }
236
237
238   // I did all my actions for me in the file (either I closed the file, or a colleague did)
239   // Let's cleanup before leaving
240 todo_done:
241   if (myqueue != NULL) {
242     xbt_dynar_free(&myqueue);
243     xbt_dict_remove(action_queues, name);
244   }
245   return NULL;
246 }