1 /* Copyright (c) 2010, 2012-2015. 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"
13 #include "xbt/replay.h"
17 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(replay,xbt,"Replay trace reader");
19 typedef struct s_replay_reader {
23 char *position; /* stable storage */
26 } s_xbt_replay_reader_t;
30 xbt_dict_t xbt_action_funs = NULL;
31 xbt_dict_t xbt_action_queues = NULL;
33 static char *action_line = NULL;
34 static size_t action_len = 0;
36 int is_replay_active = 0 ;
38 static char **action_get_action(char *name);
40 static char *str_tolower (const char *str)
42 char *ret = xbt_strdup (str);
43 int i, n = strlen (ret);
44 for (i = 0; i < n; i++)
45 ret[i] = tolower (str[i]);
49 int _xbt_replay_is_active(void){
50 return is_replay_active;
53 xbt_replay_reader_t xbt_replay_reader_new(const char *filename)
55 xbt_replay_reader_t res = xbt_new0(s_xbt_replay_reader_t,1);
56 res->fp = fopen(filename, "r");
58 xbt_die("Cannot open %s: %s", filename, strerror(errno));
59 res->filename = xbt_strdup(filename);
63 const char *xbt_replay_reader_position(xbt_replay_reader_t reader)
65 free(reader->position);
66 reader->position = bprintf("%s:%d",reader->filename,reader->linenum);
67 return reader->position;
70 const char **xbt_replay_reader_get(xbt_replay_reader_t reader)
74 read = xbt_getline(&reader->line, &reader->line_len, reader->fp);
75 //XBT_INFO("got from trace: %s",reader->line);
78 return NULL; /* end of file */
79 char *comment = strchr(reader->line, '#');
82 xbt_str_trim(reader->line, NULL);
83 if (reader->line[0] == '\0')
84 return xbt_replay_reader_get(reader); /* Get next line */
86 d=xbt_str_split_quoted_in_place(reader->line);
87 if (xbt_dynar_is_empty(d)) {
89 return xbt_replay_reader_get(reader); /* Get next line */
91 return xbt_dynar_to_array(d);
94 void xbt_replay_reader_free(xbt_replay_reader_t *reader)
96 free((*reader)->filename);
97 free((*reader)->position);
98 fclose((*reader)->fp);
99 free((*reader)->line);
105 * \ingroup XBT_replay
106 * \brief Registers a function to handle a kind of action
108 * Registers a function to handle a kind of action
109 * This table is then used by \ref xbt_replay_action_runner
111 * The argument of the function is the line describing the action, splitted on spaces with xbt_str_split_quoted()
113 * \param action_name the reference name of the action.
114 * \param function prototype given by the type: void...(xbt_dynar_t action)
116 void xbt_replay_action_register(const char *action_name, action_fun function)
118 char* lowername = str_tolower (action_name);
119 xbt_dict_set(xbt_action_funs, lowername, function, NULL);
123 /** \ingroup XBT_replay
124 * \brief Unregisters a function, which handled a kind of action
126 * \param action_name the reference name of the action.
128 void xbt_replay_action_unregister(const char *action_name)
130 char* lowername = str_tolower (action_name);
131 xbt_dict_remove(xbt_action_funs, lowername);
135 /** @brief Initializes the replay mechanism, and returns true if (and only if) it was necessary
137 * It returns false if it was already done by another process.
139 int _xbt_replay_action_init(void)
143 is_replay_active = 1;
144 xbt_action_funs = xbt_dict_new_homogeneous(NULL);
145 xbt_action_queues = xbt_dict_new_homogeneous(NULL);
149 void _xbt_replay_action_exit(void)
151 xbt_dict_free(&xbt_action_queues);
152 xbt_dict_free(&xbt_action_funs);
154 xbt_action_queues = NULL;
155 xbt_action_funs = NULL;
160 * \ingroup XBT_replay
161 * \brief function used internally to actually run the replay
166 int xbt_replay_action_runner(int argc, char *argv[])
170 if (xbt_action_fp) { // A unique trace file
172 while ((evt = action_get_action(argv[0]))) {
173 char* lowername = str_tolower (evt[1]);
174 action_fun function =
175 (action_fun)xbt_dict_get(xbt_action_funs, lowername);
178 function((const char **)evt);
182 xbt_die("Replay error :\n %s"
185 for (i=0;evt[i]!= NULL;i++)
189 } else { // Should have got my trace file in argument
191 xbt_assert(argc >= 2,
192 "No '%s' agent function provided, no simulation-wide trace file provided, "
193 "and no process-wide trace file provided in deployment file. Aborting.",
196 xbt_replay_reader_t reader = xbt_replay_reader_new(argv[1]);
197 while ((evt=xbt_replay_reader_get(reader))) {
198 if (!strcmp(argv[0],evt[0])) {
199 char* lowername = str_tolower (evt[1]);
200 action_fun function = (action_fun)xbt_dict_get(xbt_action_funs, lowername);
207 xbt_die("Replay error on line %d of file %s :\n %s"
208 , reader->linenum,reader->filename, e.msg);
211 XBT_WARN("%s: Ignore trace element not for me",
212 xbt_replay_reader_position(reader));
216 xbt_replay_reader_free(&reader);
222 static char **action_get_action(char *name)
224 xbt_dynar_t evt = NULL;
225 char *evtname = NULL;
227 xbt_dynar_t myqueue = xbt_dict_get_or_null(xbt_action_queues, name);
228 if (myqueue == NULL || xbt_dynar_is_empty(myqueue)) { // nothing stored for me. Read the file further
230 if (xbt_action_fp == NULL) { // File closed now. There's nothing more to read. I'm out of here
233 // Read lines until I reach something for me (which breaks in loop body)
234 // or end of file reached
235 while (xbt_getline(&action_line, &action_len, xbt_action_fp) != -1) {
236 // cleanup and split the string I just read
237 char *comment = strchr(action_line, '#');
240 xbt_str_trim(action_line, NULL);
241 if (action_line[0] == '\0')
243 /* we cannot split in place here because we parse&store several lines for
244 * the colleagues... */
245 evt = xbt_str_split_quoted(action_line);
247 // if it's for me, I'm done
248 evtname = xbt_dynar_get_as(evt, 0, char *);
249 if (!strcasecmp(name, evtname)) {
250 return xbt_dynar_to_array(evt);
252 // Else, I have to store it for the relevant colleague
253 xbt_dynar_t otherqueue =
254 xbt_dict_get_or_null(xbt_action_queues, evtname);
255 if (otherqueue == NULL) { // Damn. Create the queue of that guy
257 xbt_dynar_new(sizeof(xbt_dynar_t), xbt_dynar_free_voidp);
258 xbt_dict_set(xbt_action_queues, evtname, otherqueue, NULL);
260 xbt_dynar_push(otherqueue, &evt);
263 goto todo_done; // end of file reached while searching in vain for more work
265 // Get something from my queue and return it
266 xbt_dynar_shift(myqueue, &evt);
267 return xbt_dynar_to_array(evt);
271 // I did all my actions for me in the file (either I closed the file, or a colleague did)
272 // Let's cleanup before leaving
274 if (myqueue != NULL) {
275 xbt_dynar_free(&myqueue);
276 xbt_dict_remove(xbt_action_queues, name);