1 /* Copyright (c) 2010. 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. */
6 #include "simgrid_config.h" //For getline, keep that include first
8 #include "internal_config.h"
10 #include "xbt/sysdep.h"
13 #include "xbt/replay.h"
15 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(replay,xbt,"Replay trace reader");
17 typedef struct s_replay_reader {
21 char *position; /* stable storage */
22 char *filename; int linenum;
23 } s_xbt_replay_reader_t;
27 xbt_dict_t action_funs;
28 xbt_dict_t action_queues;
30 static char *action_line = NULL;
31 static size_t action_len = 0;
33 static const char **action_get_action(char *name);
35 xbt_replay_reader_t xbt_replay_reader_new(const char *filename)
37 xbt_replay_reader_t res = xbt_new0(s_xbt_replay_reader_t,1);
38 res->fp = fopen(filename, "r");
39 xbt_assert(res->fp != NULL, "Cannot open %s: %s", filename,
41 res->filename = xbt_strdup(filename);
45 const char *xbt_replay_reader_position(xbt_replay_reader_t reader)
47 free(reader->position);
48 reader->position = bprintf("%s:%d",reader->filename,reader->linenum);
49 return reader->position;
52 const char **xbt_replay_reader_get(xbt_replay_reader_t reader)
56 read = getline(&reader->line, &reader->line_len, reader->fp);
57 //XBT_INFO("got from trace: %s",reader->line);
60 return NULL; /* end of file */
61 char *comment = strchr(reader->line, '#');
64 xbt_str_trim(reader->line, NULL);
65 if (reader->line[0] == '\0')
66 return xbt_replay_reader_get(reader); /* Get next line */
68 d=xbt_str_split_quoted_in_place(reader->line);
69 if (xbt_dynar_is_empty(d)) {
71 return xbt_replay_reader_get(reader); /* Get next line */
73 return xbt_dynar_to_array(d);
76 void xbt_replay_reader_free(xbt_replay_reader_t *reader)
78 free((*reader)->filename);
79 free((*reader)->position);
80 fclose((*reader)->fp);
81 free((*reader)->line);
88 * \brief Registers a function to handle a kind of action
90 * Registers a function to handle a kind of action
91 * This table is then used by \ref xbt_replay_action_runner
93 * The argument of the function is the line describing the action, splitted on spaces with xbt_str_split_quoted()
95 * \param action_name the reference name of the action.
96 * \param function prototype given by the type: void...(xbt_dynar_t action)
98 void xbt_replay_action_register(const char *action_name, action_fun function)
100 xbt_dict_set(action_funs, action_name, function, NULL);
103 /** \ingroup XBT_replay
104 * \brief Unregisters a function, which handled a kind of action
106 * \param action_name the reference name of the action.
108 void xbt_replay_action_unregister(const char *action_name)
110 xbt_dict_remove(action_funs, action_name);
113 void _xbt_replay_action_init(void)
115 action_funs = xbt_dict_new_homogeneous(NULL);
116 action_queues = xbt_dict_new_homogeneous(NULL);
119 void _xbt_replay_action_exit(void)
121 xbt_dict_free(&action_queues);
122 xbt_dict_free(&action_funs);
127 * \ingroup XBT_replay
133 int xbt_replay_action_runner(int argc, char *argv[])
137 if (action_fp) { // A unique trace file
139 while ((evt = action_get_action(argv[0]))) {
140 action_fun function =
141 (action_fun)xbt_dict_get(action_funs, evt[1]);
143 for (i=0;evt[i]!= NULL;i++)
147 } else { // Should have got my trace file in argument
148 xbt_assert(argc >= 2,
149 "No '%s' agent function provided, no simulation-wide trace file provided, "
150 "and no process-wide trace file provided in deployment file. Aborting.",
153 xbt_replay_reader_t reader = xbt_replay_reader_new(argv[1]);
154 while ((evt=xbt_replay_reader_get(reader))) {
155 if (!strcmp(argv[0],evt[0])) {
156 action_fun function =
157 (action_fun)xbt_dict_get(action_funs, evt[1]);
161 XBT_WARN("%s: Ignore trace element not for me",
162 xbt_replay_reader_position(reader));
165 xbt_replay_reader_free(&reader);
171 static const char **action_get_action(char *name)
173 xbt_dynar_t evt = NULL;
174 char *evtname = NULL;
176 xbt_dynar_t myqueue = xbt_dict_get_or_null(action_queues, name);
177 if (myqueue == NULL || xbt_dynar_is_empty(myqueue)) { // nothing stored for me. Read the file further
179 if (action_fp == NULL) { // File closed now. There's nothing more to read. I'm out of here
182 // Read lines until I reach something for me (which breaks in loop body)
183 // or end of file reached
184 while (getline(&action_line, &action_len, action_fp) != -1) {
185 // cleanup and split the string I just read
186 char *comment = strchr(action_line, '#');
189 xbt_str_trim(action_line, NULL);
190 if (action_line[0] == '\0')
192 /* we cannot split in place here because we parse&store several lines for
193 * the colleagues... */
194 evt = xbt_str_split_quoted(action_line);
196 // if it's for me, I'm done
197 evtname = xbt_dynar_get_as(evt, 0, char *);
198 if (!strcmp(name, evtname)) {
199 return xbt_dynar_to_array(evt);
201 // Else, I have to store it for the relevant colleague
202 xbt_dynar_t otherqueue =
203 xbt_dict_get_or_null(action_queues, evtname);
204 if (otherqueue == NULL) { // Damn. Create the queue of that guy
206 xbt_dynar_new(sizeof(xbt_dynar_t), xbt_dynar_free_voidp);
207 xbt_dict_set(action_queues, evtname, otherqueue, NULL);
209 xbt_dynar_push(otherqueue, &evt);
212 goto todo_done; // end of file reached while searching in vain for more work
214 // Get something from my queue and return it
215 xbt_dynar_shift(myqueue, &evt);
216 return xbt_dynar_to_array(evt);
220 // I did all my actions for me in the file (either I closed the file, or a colleague did)
221 // Let's cleanup before leaving
223 if (myqueue != NULL) {
224 xbt_dynar_free(&myqueue);
225 xbt_dict_remove(action_queues, name);