Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Remove fixme comment.
[simgrid.git] / src / xbt / xbt_replay.c
1 /* Copyright (c) 2010. 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 #include "simgrid_config.h" //For getline, keep that include first
7
8 #include "internal_config.h"
9 #include <errno.h>
10 #include "xbt/sysdep.h"
11 #include "xbt/log.h"
12 #include "xbt/str.h"
13 #include "xbt/replay.h"
14
15 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(replay,xbt,"Replay trace reader");
16
17 typedef struct s_replay_reader {
18   FILE *fp;
19   char *line;
20   size_t line_len;
21   char *position; /* stable storage */
22   char *filename; int linenum;
23 } s_xbt_replay_reader_t;
24
25 FILE *action_fp;
26
27 xbt_dict_t action_funs;
28 xbt_dict_t action_queues;
29
30 static char *action_line = NULL;
31 static size_t action_len = 0;
32
33 static const char **action_get_action(char *name);
34
35 xbt_replay_reader_t xbt_replay_reader_new(const char *filename)
36 {
37   xbt_replay_reader_t res = xbt_new0(s_xbt_replay_reader_t,1);
38   res->fp = fopen(filename, "r");
39   xbt_assert(res->fp != NULL, "Cannot open %s: %s", filename,
40       strerror(errno));
41   res->filename = xbt_strdup(filename);
42   return res;
43 }
44
45 const char *xbt_replay_reader_position(xbt_replay_reader_t reader)
46 {
47   free(reader->position);
48   reader->position = bprintf("%s:%d",reader->filename,reader->linenum);
49   return reader->position;
50 }
51
52 const char **xbt_replay_reader_get(xbt_replay_reader_t reader)
53 {
54   ssize_t read;
55   xbt_dynar_t d;
56   read = getline(&reader->line, &reader->line_len, reader->fp);
57   //XBT_INFO("got from trace: %s",reader->line);
58   reader->linenum++;
59   if (read==-1)
60     return NULL; /* end of file */
61   char *comment = strchr(reader->line, '#');
62   if (comment != NULL)
63     *comment = '\0';
64   xbt_str_trim(reader->line, NULL);
65   if (reader->line[0] == '\0')
66     return xbt_replay_reader_get(reader); /* Get next line */
67
68   d=xbt_str_split_quoted_in_place(reader->line);
69   if (xbt_dynar_is_empty(d)) {
70     xbt_dynar_free(&d);
71     return xbt_replay_reader_get(reader); /* Get next line */
72   }
73   return xbt_dynar_to_array(d);
74 }
75
76 void xbt_replay_reader_free(xbt_replay_reader_t *reader)
77 {
78   free((*reader)->filename);
79   free((*reader)->position);
80   fclose((*reader)->fp);
81   free((*reader)->line);
82   free(*reader);
83   *reader=NULL;
84 }
85
86 /**
87  * \ingroup XBT_replay
88  * \brief Registers a function to handle a kind of action
89  *
90  * Registers a function to handle a kind of action
91  * This table is then used by \ref xbt_replay_action_runner
92  *
93  * The argument of the function is the line describing the action, splitted on spaces with xbt_str_split_quoted()
94  *
95  * \param action_name the reference name of the action.
96  * \param function prototype given by the type: void...(xbt_dynar_t action)
97  */
98 void xbt_replay_action_register(const char *action_name, action_fun function)
99 {
100   xbt_dict_set(action_funs, action_name, function, NULL);
101 }
102
103 /** \ingroup XBT_replay
104  * \brief Unregisters a function, which handled a kind of action
105  *
106  * \param action_name the reference name of the action.
107  */
108 void xbt_replay_action_unregister(const char *action_name)
109 {
110   xbt_dict_remove(action_funs, action_name);
111 }
112
113 void _xbt_replay_action_init(void)
114 {
115   action_funs = xbt_dict_new_homogeneous(NULL);
116   action_queues = xbt_dict_new_homogeneous(NULL);
117 }
118
119 void _xbt_replay_action_exit(void)
120 {
121   xbt_dict_free(&action_queues);
122   xbt_dict_free(&action_funs);
123   free(action_line);
124 }
125
126 /**
127  * \ingroup XBT_replay
128  * \brief TODO
129
130  * \param argc argc .
131  * \param argv argv
132  */
133 int xbt_replay_action_runner(int argc, char *argv[])
134 {
135   const char **evt;
136   if (action_fp) {              // A unique trace file
137
138     while ((evt = action_get_action(argv[0]))) {
139       action_fun function =
140         (action_fun)xbt_dict_get(action_funs, evt[1]);
141       function(evt);
142       free(evt);
143     }
144   } else {                      // Should have got my trace file in argument
145     xbt_assert(argc >= 2,
146                 "No '%s' agent function provided, no simulation-wide trace file provided, "
147                 "and no process-wide trace file provided in deployment file. Aborting.",
148                 argv[0]
149         );
150     xbt_replay_reader_t reader = xbt_replay_reader_new(argv[1]);
151     while ((evt=xbt_replay_reader_get(reader))) {
152       if (!strcmp(argv[0],evt[0])) {
153         action_fun function =
154           (action_fun)xbt_dict_get(action_funs, evt[1]);
155         function(evt);
156         free(evt);
157       } else {
158         XBT_WARN("%s: Ignore trace element not for me",
159               xbt_replay_reader_position(reader));
160       }
161     }
162     xbt_replay_reader_free(&reader);
163   }
164   return 0;
165 }
166
167
168 static const char **action_get_action(char *name)
169 {
170   xbt_dynar_t evt = NULL;
171   char *evtname = NULL;
172
173   xbt_dynar_t myqueue = xbt_dict_get_or_null(action_queues, name);
174   if (myqueue == NULL || xbt_dynar_is_empty(myqueue)) {      // nothing stored for me. Read the file further
175
176     if (action_fp == NULL) {    // File closed now. There's nothing more to read. I'm out of here
177       goto todo_done;
178     }
179     // Read lines until I reach something for me (which breaks in loop body)
180     // or end of file reached
181     while (getline(&action_line, &action_len, action_fp) != -1) {
182       // cleanup and split the string I just read
183       char *comment = strchr(action_line, '#');
184       if (comment != NULL)
185         *comment = '\0';
186       xbt_str_trim(action_line, NULL);
187       if (action_line[0] == '\0')
188         continue;
189       /* we cannot split in place here because we parse&store several lines for
190        * the colleagues... */
191       evt = xbt_str_split_quoted(action_line);
192
193       // if it's for me, I'm done
194       evtname = xbt_dynar_get_as(evt, 0, char *);
195       if (!strcmp(name, evtname)) {
196         return xbt_dynar_to_array(evt);
197       } else {
198         // Else, I have to store it for the relevant colleague
199         xbt_dynar_t otherqueue =
200             xbt_dict_get_or_null(action_queues, evtname);
201         if (otherqueue == NULL) {       // Damn. Create the queue of that guy
202           otherqueue =
203               xbt_dynar_new(sizeof(xbt_dynar_t), xbt_dynar_free_voidp);
204           xbt_dict_set(action_queues, evtname, otherqueue, NULL);
205         }
206         xbt_dynar_push(otherqueue, &evt);
207       }
208     }
209     goto todo_done;             // end of file reached while searching in vain for more work
210   } else {
211     // Get something from my queue and return it
212     xbt_dynar_shift(myqueue, &evt);
213     return xbt_dynar_to_array(evt);
214   }
215
216
217   // I did all my actions for me in the file (either I closed the file, or a colleague did)
218   // Let's cleanup before leaving
219 todo_done:
220   if (myqueue != NULL) {
221     xbt_dynar_free(&myqueue);
222     xbt_dict_remove(action_queues, name);
223   }
224   return NULL;
225 }