1 /* Copyright (c) 2010, 2012-2013. 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 */
23 char *filename; int linenum;
24 } s_xbt_replay_reader_t;
28 xbt_dict_t action_funs;
29 xbt_dict_t action_queues;
31 static char *action_line = NULL;
32 static size_t action_len = 0;
34 static char **action_get_action(char *name);
36 static char *str_tolower (const char *str)
38 char *ret = xbt_strdup (str);
39 int i, n = strlen (ret);
40 for (i = 0; i < n; i++)
41 ret[i] = tolower (str[i]);
46 xbt_replay_reader_t xbt_replay_reader_new(const char *filename)
48 xbt_replay_reader_t res = xbt_new0(s_xbt_replay_reader_t,1);
49 res->fp = fopen(filename, "r");
50 xbt_assert(res->fp != NULL, "Cannot open %s: %s", filename,
52 res->filename = xbt_strdup(filename);
56 const char *xbt_replay_reader_position(xbt_replay_reader_t reader)
58 free(reader->position);
59 reader->position = bprintf("%s:%d",reader->filename,reader->linenum);
60 return reader->position;
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 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 xbt_dict_set(action_funs, str_tolower (action_name), function, NULL);
114 /** \ingroup XBT_replay
115 * \brief Unregisters a function, which handled a kind of action
117 * \param action_name the reference name of the action.
119 void xbt_replay_action_unregister(const char *action_name)
121 xbt_dict_remove(action_funs, str_tolower (action_name));
124 void _xbt_replay_action_init(void)
126 action_funs = xbt_dict_new_homogeneous(NULL);
127 action_queues = xbt_dict_new_homogeneous(NULL);
130 void _xbt_replay_action_exit(void)
132 xbt_dict_free(&action_queues);
133 xbt_dict_free(&action_funs);
138 * \ingroup XBT_replay
144 int xbt_replay_action_runner(int argc, char *argv[])
147 if (action_fp) { // A unique trace file
149 while ((evt = action_get_action(argv[0]))) {
150 action_fun function =
151 (action_fun)xbt_dict_get(action_funs, str_tolower (evt[1]));
152 function((const char **)evt);
153 for (i=0;evt[i]!= NULL;i++)
157 } else { // Should have got my trace file in argument
159 xbt_assert(argc >= 2,
160 "No '%s' agent function provided, no simulation-wide trace file provided, "
161 "and no process-wide trace file provided in deployment file. Aborting.",
164 xbt_replay_reader_t reader = xbt_replay_reader_new(argv[1]);
165 while ((evt=xbt_replay_reader_get(reader))) {
166 if (!strcmp(argv[0],evt[0])) {
167 action_fun function = (action_fun)xbt_dict_get(action_funs, str_tolower (evt[1]));
170 XBT_WARN("%s: Ignore trace element not for me",
171 xbt_replay_reader_position(reader));
175 xbt_replay_reader_free(&reader);
181 static char **action_get_action(char *name)
183 xbt_dynar_t evt = NULL;
184 char *evtname = NULL;
186 xbt_dynar_t myqueue = xbt_dict_get_or_null(action_queues, name);
187 if (myqueue == NULL || xbt_dynar_is_empty(myqueue)) { // nothing stored for me. Read the file further
189 if (action_fp == NULL) { // File closed now. There's nothing more to read. I'm out of here
192 // Read lines until I reach something for me (which breaks in loop body)
193 // or end of file reached
194 while (xbt_getline(&action_line, &action_len, action_fp) != -1) {
195 // cleanup and split the string I just read
196 char *comment = strchr(action_line, '#');
199 xbt_str_trim(action_line, NULL);
200 if (action_line[0] == '\0')
202 /* we cannot split in place here because we parse&store several lines for
203 * the colleagues... */
204 evt = xbt_str_split_quoted(action_line);
206 // if it's for me, I'm done
207 evtname = xbt_dynar_get_as(evt, 0, char *);
208 if (!strcasecmp(name, evtname)) {
209 return xbt_dynar_to_array(evt);
211 // Else, I have to store it for the relevant colleague
212 xbt_dynar_t otherqueue =
213 xbt_dict_get_or_null(action_queues, evtname);
214 if (otherqueue == NULL) { // Damn. Create the queue of that guy
216 xbt_dynar_new(sizeof(xbt_dynar_t), xbt_dynar_free_voidp);
217 xbt_dict_set(action_queues, evtname, otherqueue, NULL);
219 xbt_dynar_push(otherqueue, &evt);
222 goto todo_done; // end of file reached while searching in vain for more work
224 // Get something from my queue and return it
225 xbt_dynar_shift(myqueue, &evt);
226 return xbt_dynar_to_array(evt);
230 // I did all my actions for me in the file (either I closed the file, or a colleague did)
231 // Let's cleanup before leaving
233 if (myqueue != NULL) {
234 xbt_dynar_free(&myqueue);
235 xbt_dict_remove(action_queues, name);