Logo AND Algorithmique Numérique Distribuée

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