Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'mc' into 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; 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 int is_replay_active = 0 ;
35
36 static char **action_get_action(char *name);
37
38 static char *str_tolower (const char *str)
39 {
40   char *ret = xbt_strdup (str);
41   int i, n = strlen (ret);
42   for (i = 0; i < n; i++)
43     ret[i] = tolower (str[i]);
44   return ret;
45 }
46
47 int _xbt_replay_is_active(void){
48   return is_replay_active;
49 }
50
51 xbt_replay_reader_t xbt_replay_reader_new(const char *filename)
52 {
53   xbt_replay_reader_t res = xbt_new0(s_xbt_replay_reader_t,1);
54   res->fp = fopen(filename, "r");
55   xbt_assert(res->fp != NULL, "Cannot open %s: %s", filename,
56       strerror(errno));
57   res->filename = xbt_strdup(filename);
58   return res;
59 }
60
61 const char *xbt_replay_reader_position(xbt_replay_reader_t reader)
62 {
63   free(reader->position);
64   reader->position = bprintf("%s:%d",reader->filename,reader->linenum);
65   return reader->position;
66 }
67
68 const char **xbt_replay_reader_get(xbt_replay_reader_t reader)
69 {
70   ssize_t read;
71   xbt_dynar_t d;
72   read = xbt_getline(&reader->line, &reader->line_len, reader->fp);
73   //XBT_INFO("got from trace: %s",reader->line);
74   reader->linenum++;
75   if (read==-1)
76     return NULL; /* end of file */
77   char *comment = strchr(reader->line, '#');
78   if (comment != NULL)
79     *comment = '\0';
80   xbt_str_trim(reader->line, NULL);
81   if (reader->line[0] == '\0')
82     return xbt_replay_reader_get(reader); /* Get next line */
83
84   d=xbt_str_split_quoted_in_place(reader->line);
85   if (xbt_dynar_is_empty(d)) {
86     xbt_dynar_free(&d);
87     return xbt_replay_reader_get(reader); /* Get next line */
88   }
89   return xbt_dynar_to_array(d);
90 }
91
92 void xbt_replay_reader_free(xbt_replay_reader_t *reader)
93 {
94   free((*reader)->filename);
95   free((*reader)->position);
96   fclose((*reader)->fp);
97   free((*reader)->line);
98   free(*reader);
99   *reader=NULL;
100 }
101
102 /**
103  * \ingroup XBT_replay
104  * \brief Registers a function to handle a kind of action
105  *
106  * Registers a function to handle a kind of action
107  * This table is then used by \ref xbt_replay_action_runner
108  *
109  * The argument of the function is the line describing the action, splitted on spaces with xbt_str_split_quoted()
110  *
111  * \param action_name the reference name of the action.
112  * \param function prototype given by the type: void...(xbt_dynar_t action)
113  */
114 void xbt_replay_action_register(const char *action_name, action_fun function)
115 {
116   char* lowername = str_tolower (action_name);
117   xbt_dict_set(action_funs, lowername, function, NULL);
118   xbt_free(lowername);
119 }
120
121 /** \ingroup XBT_replay
122  * \brief Unregisters a function, which handled a kind of action
123  *
124  * \param action_name the reference name of the action.
125  */
126 void xbt_replay_action_unregister(const char *action_name)
127 {
128   char* lowername = str_tolower (action_name);
129   xbt_dict_remove(action_funs, lowername);
130   xbt_free(lowername);
131 }
132
133 void _xbt_replay_action_init(void)
134 {
135   is_replay_active = 1;
136   action_funs = xbt_dict_new_homogeneous(NULL);
137   action_queues = xbt_dict_new_homogeneous(NULL);
138 }
139
140 void _xbt_replay_action_exit(void)
141 {
142   xbt_dict_free(&action_queues);
143   xbt_dict_free(&action_funs);
144   free(action_line);
145 }
146
147 /**
148  * \ingroup XBT_replay
149  * \brief TODO
150
151  * \param argc argc .
152  * \param argv argv
153  */
154 int xbt_replay_action_runner(int argc, char *argv[])
155 {
156   int i;
157   if (action_fp) {              // A unique trace file
158     char **evt;
159     while ((evt = action_get_action(argv[0]))) {
160       char* lowername = str_tolower (evt[1]);
161       action_fun function =
162         (action_fun)xbt_dict_get(action_funs, lowername);
163       xbt_free(lowername);
164       function((const char **)evt);
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(action_funs, lowername);
181         xbt_free(lowername);
182         function(evt);
183       } else {
184         XBT_WARN("%s: Ignore trace element not for me",
185               xbt_replay_reader_position(reader));
186       }
187       free(evt);
188     }
189     xbt_replay_reader_free(&reader);
190   }
191   return 0;
192 }
193
194
195 static char **action_get_action(char *name)
196 {
197   xbt_dynar_t evt = NULL;
198   char *evtname = NULL;
199
200   xbt_dynar_t myqueue = xbt_dict_get_or_null(action_queues, name);
201   if (myqueue == NULL || xbt_dynar_is_empty(myqueue)) {      // nothing stored for me. Read the file further
202
203     if (action_fp == NULL) {    // File closed now. There's nothing more to read. I'm out of here
204       goto todo_done;
205     }
206     // Read lines until I reach something for me (which breaks in loop body)
207     // or end of file reached
208     while (xbt_getline(&action_line, &action_len, action_fp) != -1) {
209       // cleanup and split the string I just read
210       char *comment = strchr(action_line, '#');
211       if (comment != NULL)
212         *comment = '\0';
213       xbt_str_trim(action_line, NULL);
214       if (action_line[0] == '\0')
215         continue;
216       /* we cannot split in place here because we parse&store several lines for
217        * the colleagues... */
218       evt = xbt_str_split_quoted(action_line);
219
220       // if it's for me, I'm done
221       evtname = xbt_dynar_get_as(evt, 0, char *);
222       if (!strcasecmp(name, evtname)) {
223         return xbt_dynar_to_array(evt);
224       } else {
225         // Else, I have to store it for the relevant colleague
226         xbt_dynar_t otherqueue =
227             xbt_dict_get_or_null(action_queues, evtname);
228         if (otherqueue == NULL) {       // Damn. Create the queue of that guy
229           otherqueue =
230               xbt_dynar_new(sizeof(xbt_dynar_t), xbt_dynar_free_voidp);
231           xbt_dict_set(action_queues, evtname, otherqueue, NULL);
232         }
233         xbt_dynar_push(otherqueue, &evt);
234       }
235     }
236     goto todo_done;             // end of file reached while searching in vain for more work
237   } else {
238     // Get something from my queue and return it
239     xbt_dynar_shift(myqueue, &evt);
240     return xbt_dynar_to_array(evt);
241   }
242
243
244   // I did all my actions for me in the file (either I closed the file, or a colleague did)
245   // Let's cleanup before leaving
246 todo_done:
247   if (myqueue != NULL) {
248     xbt_dynar_free(&myqueue);
249     xbt_dict_remove(action_queues, name);
250   }
251   return NULL;
252 }