1 /* Copyright (c) 2010, 2012-2014. The SimGrid Team.
2 * All rights reserved. */
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. */
7 #include "internal_config.h"
9 #include "xbt/sysdep.h"
12 #include "xbt/replay.h"
16 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(replay,xbt,"Replay trace reader");
18 typedef struct s_replay_reader {
22 char *position; /* stable storage */
25 } s_xbt_replay_reader_t;
29 xbt_dict_t action_funs;
30 xbt_dict_t action_queues;
32 static char *action_line = NULL;
33 static size_t action_len = 0;
35 int is_replay_active = 0 ;
37 static char **action_get_action(char *name);
39 static char *str_tolower (const char *str)
41 char *ret = xbt_strdup (str);
42 int i, n = strlen (ret);
43 for (i = 0; i < n; i++)
44 ret[i] = tolower (str[i]);
48 int _xbt_replay_is_active(void){
49 return is_replay_active;
52 xbt_replay_reader_t xbt_replay_reader_new(const char *filename)
54 xbt_replay_reader_t res = xbt_new0(s_xbt_replay_reader_t,1);
55 res->fp = fopen(filename, "r");
57 xbt_die("Cannot open %s: %s", filename, strerror(errno));
58 res->filename = xbt_strdup(filename);
62 const char *xbt_replay_reader_position(xbt_replay_reader_t reader)
64 free(reader->position);
65 reader->position = bprintf("%s:%d",reader->filename,reader->linenum);
66 return reader->position;
69 const char **xbt_replay_reader_get(xbt_replay_reader_t reader)
73 read = xbt_getline(&reader->line, &reader->line_len, reader->fp);
74 //XBT_INFO("got from trace: %s",reader->line);
77 return NULL; /* end of file */
78 char *comment = strchr(reader->line, '#');
81 xbt_str_trim(reader->line, NULL);
82 if (reader->line[0] == '\0')
83 return xbt_replay_reader_get(reader); /* Get next line */
85 d=xbt_str_split_quoted_in_place(reader->line);
86 if (xbt_dynar_is_empty(d)) {
88 return xbt_replay_reader_get(reader); /* Get next line */
90 return xbt_dynar_to_array(d);
93 void xbt_replay_reader_free(xbt_replay_reader_t *reader)
95 free((*reader)->filename);
96 free((*reader)->position);
97 fclose((*reader)->fp);
98 free((*reader)->line);
104 * \ingroup XBT_replay
105 * \brief Registers a function to handle a kind of action
107 * Registers a function to handle a kind of action
108 * This table is then used by \ref xbt_replay_action_runner
110 * The argument of the function is the line describing the action, splitted on spaces with xbt_str_split_quoted()
112 * \param action_name the reference name of the action.
113 * \param function prototype given by the type: void...(xbt_dynar_t action)
115 void xbt_replay_action_register(const char *action_name, action_fun function)
117 char* lowername = str_tolower (action_name);
118 xbt_dict_set(action_funs, lowername, function, NULL);
122 /** \ingroup XBT_replay
123 * \brief Unregisters a function, which handled a kind of action
125 * \param action_name the reference name of the action.
127 void xbt_replay_action_unregister(const char *action_name)
129 char* lowername = str_tolower (action_name);
130 xbt_dict_remove(action_funs, lowername);
134 void _xbt_replay_action_init(void)
136 is_replay_active = 1;
137 action_funs = xbt_dict_new_homogeneous(NULL);
138 action_queues = xbt_dict_new_homogeneous(NULL);
141 void _xbt_replay_action_exit(void)
143 xbt_dict_free(&action_queues);
144 xbt_dict_free(&action_funs);
149 * \ingroup XBT_replay
150 * \brief function used internally to actually run the replay
155 int xbt_replay_action_runner(int argc, char *argv[])
159 if (action_fp) { // A unique trace file
161 while ((evt = action_get_action(argv[0]))) {
162 char* lowername = str_tolower (evt[1]);
163 action_fun function =
164 (action_fun)xbt_dict_get(action_funs, lowername);
167 function((const char **)evt);
171 xbt_die("Replay error :\n %s"
174 for (i=0;evt[i]!= NULL;i++)
178 } else { // Should have got my trace file in argument
180 xbt_assert(argc >= 2,
181 "No '%s' agent function provided, no simulation-wide trace file provided, "
182 "and no process-wide trace file provided in deployment file. Aborting.",
185 xbt_replay_reader_t reader = xbt_replay_reader_new(argv[1]);
186 while ((evt=xbt_replay_reader_get(reader))) {
187 if (!strcmp(argv[0],evt[0])) {
188 char* lowername = str_tolower (evt[1]);
189 action_fun function = (action_fun)xbt_dict_get(action_funs, lowername);
196 xbt_die("Replay error on line %d of file %s :\n %s"
197 , reader->linenum,reader->filename, e.msg);
200 XBT_WARN("%s: Ignore trace element not for me",
201 xbt_replay_reader_position(reader));
205 xbt_replay_reader_free(&reader);
211 static char **action_get_action(char *name)
213 xbt_dynar_t evt = NULL;
214 char *evtname = NULL;
216 xbt_dynar_t myqueue = xbt_dict_get_or_null(action_queues, name);
217 if (myqueue == NULL || xbt_dynar_is_empty(myqueue)) { // nothing stored for me. Read the file further
219 if (action_fp == NULL) { // File closed now. There's nothing more to read. I'm out of here
222 // Read lines until I reach something for me (which breaks in loop body)
223 // or end of file reached
224 while (xbt_getline(&action_line, &action_len, action_fp) != -1) {
225 // cleanup and split the string I just read
226 char *comment = strchr(action_line, '#');
229 xbt_str_trim(action_line, NULL);
230 if (action_line[0] == '\0')
232 /* we cannot split in place here because we parse&store several lines for
233 * the colleagues... */
234 evt = xbt_str_split_quoted(action_line);
236 // if it's for me, I'm done
237 evtname = xbt_dynar_get_as(evt, 0, char *);
238 if (!strcasecmp(name, evtname)) {
239 return xbt_dynar_to_array(evt);
241 // Else, I have to store it for the relevant colleague
242 xbt_dynar_t otherqueue =
243 xbt_dict_get_or_null(action_queues, evtname);
244 if (otherqueue == NULL) { // Damn. Create the queue of that guy
246 xbt_dynar_new(sizeof(xbt_dynar_t), xbt_dynar_free_voidp);
247 xbt_dict_set(action_queues, evtname, otherqueue, NULL);
249 xbt_dynar_push(otherqueue, &evt);
252 goto todo_done; // end of file reached while searching in vain for more work
254 // Get something from my queue and return it
255 xbt_dynar_shift(myqueue, &evt);
256 return xbt_dynar_to_array(evt);
260 // I did all my actions for me in the file (either I closed the file, or a colleague did)
261 // Let's cleanup before leaving
263 if (myqueue != NULL) {
264 xbt_dynar_free(&myqueue);
265 xbt_dict_remove(action_queues, name);