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 "src/internal_config.h"
8 #include "xbt/sysdep.h"
12 #include "xbt/replay.h"
18 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(replay,xbt,"Replay trace reader");
20 typedef struct s_replay_reader {
24 char *position; /* stable storage */
27 } s_xbt_replay_reader_t;
31 xbt_dict_t xbt_action_funs = NULL;
32 xbt_dict_t xbt_action_queues = NULL;
34 static char *action_line = NULL;
35 static size_t action_len = 0;
37 int is_replay_active = 0 ;
39 static char **action_get_action(char *name);
41 static char *str_tolower (const char *str)
43 char *ret = xbt_strdup (str);
44 int i, n = strlen (ret);
45 for (i = 0; i < n; i++)
46 ret[i] = tolower (str[i]);
50 int _xbt_replay_is_active(void){
51 return is_replay_active;
54 xbt_replay_reader_t xbt_replay_reader_new(const char *filename)
56 xbt_replay_reader_t res = xbt_new0(s_xbt_replay_reader_t,1);
57 res->fp = fopen(filename, "r");
58 xbt_assert(res->fp != NULL, "Cannot open %s: %s", filename, strerror(errno));
59 res->filename = xbt_strdup(filename);
63 const char **xbt_replay_reader_get(xbt_replay_reader_t reader)
67 read = xbt_getline(&reader->line, &reader->line_len, reader->fp);
68 //XBT_INFO("got from trace: %s",reader->line);
71 return NULL; /* end of file */
72 char *comment = strchr(reader->line, '#');
75 xbt_str_trim(reader->line, NULL);
76 if (reader->line[0] == '\0')
77 return xbt_replay_reader_get(reader); /* Get next line */
79 d=xbt_str_split_quoted_in_place(reader->line);
80 if (xbt_dynar_is_empty(d)) {
82 return xbt_replay_reader_get(reader); /* Get next line */
84 return (const char**) xbt_dynar_to_array(d);
87 void xbt_replay_reader_free(xbt_replay_reader_t *reader)
89 free((*reader)->filename);
90 free((*reader)->position);
91 fclose((*reader)->fp);
92 free((*reader)->line);
99 * \brief Registers a function to handle a kind of action
101 * Registers a function to handle a kind of action
102 * This table is then used by \ref xbt_replay_action_runner
104 * The argument of the function is the line describing the action, splitted on spaces with xbt_str_split_quoted()
106 * \param action_name the reference name of the action.
107 * \param function prototype given by the type: void...(xbt_dynar_t action)
109 void xbt_replay_action_register(const char *action_name, action_fun function)
111 char* lowername = str_tolower (action_name);
112 xbt_dict_set(xbt_action_funs, lowername, (void*) function, NULL);
116 /** @brief Initializes the replay mechanism, and returns true if (and only if) it was necessary
118 * It returns false if it was already done by another process.
120 int _xbt_replay_action_init(void)
124 is_replay_active = 1;
125 xbt_action_funs = xbt_dict_new_homogeneous(NULL);
126 xbt_action_queues = xbt_dict_new_homogeneous(NULL);
130 void _xbt_replay_action_exit(void)
132 xbt_dict_free(&xbt_action_queues);
133 xbt_dict_free(&xbt_action_funs);
135 xbt_action_queues = NULL;
136 xbt_action_funs = NULL;
141 * \ingroup XBT_replay
142 * \brief function used internally to actually run the replay
147 int xbt_replay_action_runner(int argc, char *argv[])
150 if (xbt_action_fp) { // A unique trace file
152 char **evt = action_get_action(argv[0]);
156 char* lowername = str_tolower (evt[1]);
157 action_fun function = (action_fun)xbt_dict_get(xbt_action_funs, lowername);
160 function((const char **)evt);
163 xbt_die("Replay error :\n %s", e.what());
165 for (i=0;evt[i]!= NULL;i++)
169 } else { // Should have got my trace file in argument
171 xbt_assert(argc >= 2,
172 "No '%s' agent function provided, no simulation-wide trace file provided, "
173 "and no process-wide trace file provided in deployment file. Aborting.", argv[0]
175 xbt_replay_reader_t reader = xbt_replay_reader_new(argv[1]);
176 while ((evt=xbt_replay_reader_get(reader))) {
177 if (!strcmp(argv[0],evt[0])) {
178 char* lowername = str_tolower (evt[1]);
179 action_fun function = (action_fun)xbt_dict_get(xbt_action_funs, lowername);
185 xbt_die("Replay error on line %d of file %s :\n %s" , reader->linenum,reader->filename, e.what());
188 XBT_WARN("%s:%d: Ignore trace element not for me", reader->filename, reader->linenum);
192 xbt_replay_reader_free(&reader);
197 static char **action_get_action(char *name)
199 xbt_dynar_t evt = NULL;
200 char *evtname = NULL;
202 xbt_dynar_t myqueue = (xbt_dynar_t) xbt_dict_get_or_null(xbt_action_queues, name);
203 if (myqueue == NULL || xbt_dynar_is_empty(myqueue)) { // nothing stored for me. Read the file further
204 if (xbt_action_fp == NULL) { // File closed now. There's nothing more to read. I'm out of here
207 // Read lines until I reach something for me (which breaks in loop body)
208 // or end of file reached
209 while (xbt_getline(&action_line, &action_len, xbt_action_fp) != -1) {
210 // cleanup and split the string I just read
211 char *comment = strchr(action_line, '#');
214 xbt_str_trim(action_line, NULL);
215 if (action_line[0] == '\0')
217 /* we cannot split in place here because we parse&store several lines for
218 * the colleagues... */
219 evt = xbt_str_split_quoted(action_line);
221 // if it's for me, I'm done
222 evtname = xbt_dynar_get_as(evt, 0, char *);
223 if (!strcasecmp(name, evtname)) {
224 return (char**) xbt_dynar_to_array(evt);
226 // Else, I have to store it for the relevant colleague
227 xbt_dynar_t otherqueue =
228 (xbt_dynar_t) xbt_dict_get_or_null(xbt_action_queues, evtname);
229 if (otherqueue == NULL) { // Damn. Create the queue of that guy
230 otherqueue = xbt_dynar_new(sizeof(xbt_dynar_t), xbt_dynar_free_voidp);
231 xbt_dict_set(xbt_action_queues, evtname, otherqueue, NULL);
233 xbt_dynar_push(otherqueue, &evt);
236 goto todo_done; // end of file reached while searching in vain for more work
238 // Get something from my queue and return it
239 xbt_dynar_shift(myqueue, &evt);
240 return (char**) xbt_dynar_to_array(evt);
243 // I did all my actions for me in the file (either I closed the file, or a colleague did)
244 // Let's cleanup before leaving
246 if (myqueue != NULL) {
247 xbt_dynar_free(&myqueue);
248 xbt_dict_remove(xbt_action_queues, name);