Logo AND Algorithmique Numérique Distribuée

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