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"
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 xbt_action_funs = NULL;
30 xbt_dict_t xbt_action_queues = NULL;
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(xbt_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(xbt_action_funs, lowername);
134 /** @brief Initializes the replay mechanism, and returns true if (and only if) it was necessary
136 * It returns false if it was already done by another process.
138 int _xbt_replay_action_init(void)
142 is_replay_active = 1;
143 xbt_action_funs = xbt_dict_new_homogeneous(NULL);
144 xbt_action_queues = xbt_dict_new_homogeneous(NULL);
148 void _xbt_replay_action_exit(void)
150 xbt_dict_free(&xbt_action_queues);
151 xbt_dict_free(&xbt_action_funs);
153 xbt_action_queues = NULL;
154 xbt_action_funs = NULL;
159 * \ingroup XBT_replay
160 * \brief function used internally to actually run the replay
165 int xbt_replay_action_runner(int argc, char *argv[])
169 if (xbt_action_fp) { // A unique trace file
171 while ((evt = action_get_action(argv[0]))) {
172 char* lowername = str_tolower (evt[1]);
173 action_fun function =
174 (action_fun)xbt_dict_get(xbt_action_funs, lowername);
177 function((const char **)evt);
181 xbt_die("Replay error :\n %s"
184 for (i=0;evt[i]!= NULL;i++)
188 } else { // Should have got my trace file in argument
190 xbt_assert(argc >= 2,
191 "No '%s' agent function provided, no simulation-wide trace file provided, "
192 "and no process-wide trace file provided in deployment file. Aborting.",
195 xbt_replay_reader_t reader = xbt_replay_reader_new(argv[1]);
196 while ((evt=xbt_replay_reader_get(reader))) {
197 if (!strcmp(argv[0],evt[0])) {
198 char* lowername = str_tolower (evt[1]);
199 action_fun function = (action_fun)xbt_dict_get(xbt_action_funs, lowername);
206 xbt_die("Replay error on line %d of file %s :\n %s"
207 , reader->linenum,reader->filename, e.msg);
210 XBT_WARN("%s: Ignore trace element not for me",
211 xbt_replay_reader_position(reader));
215 xbt_replay_reader_free(&reader);
221 static char **action_get_action(char *name)
223 xbt_dynar_t evt = NULL;
224 char *evtname = NULL;
226 xbt_dynar_t myqueue = xbt_dict_get_or_null(xbt_action_queues, name);
227 if (myqueue == NULL || xbt_dynar_is_empty(myqueue)) { // nothing stored for me. Read the file further
229 if (xbt_action_fp == NULL) { // File closed now. There's nothing more to read. I'm out of here
232 // Read lines until I reach something for me (which breaks in loop body)
233 // or end of file reached
234 while (xbt_getline(&action_line, &action_len, xbt_action_fp) != -1) {
235 // cleanup and split the string I just read
236 char *comment = strchr(action_line, '#');
239 xbt_str_trim(action_line, NULL);
240 if (action_line[0] == '\0')
242 /* we cannot split in place here because we parse&store several lines for
243 * the colleagues... */
244 evt = xbt_str_split_quoted(action_line);
246 // if it's for me, I'm done
247 evtname = xbt_dynar_get_as(evt, 0, char *);
248 if (!strcasecmp(name, evtname)) {
249 return xbt_dynar_to_array(evt);
251 // Else, I have to store it for the relevant colleague
252 xbt_dynar_t otherqueue =
253 xbt_dict_get_or_null(xbt_action_queues, evtname);
254 if (otherqueue == NULL) { // Damn. Create the queue of that guy
256 xbt_dynar_new(sizeof(xbt_dynar_t), xbt_dynar_free_voidp);
257 xbt_dict_set(xbt_action_queues, evtname, otherqueue, NULL);
259 xbt_dynar_push(otherqueue, &evt);
262 goto todo_done; // end of file reached while searching in vain for more work
264 // Get something from my queue and return it
265 xbt_dynar_shift(myqueue, &evt);
266 return xbt_dynar_to_array(evt);
270 // I did all my actions for me in the file (either I closed the file, or a colleague did)
271 // Let's cleanup before leaving
273 if (myqueue != NULL) {
274 xbt_dynar_free(&myqueue);
275 xbt_dict_remove(xbt_action_queues, name);