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. */
8 #include "src/internal_config.h"
9 #include "xbt/sysdep.h"
13 #include "xbt/replay.h"
19 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(replay,xbt,"Replay trace reader");
21 typedef struct s_replay_reader {
25 char *position; /* stable storage */
28 } s_xbt_replay_reader_t;
32 xbt_dict_t xbt_action_funs = nullptr;
33 xbt_dict_t xbt_action_queues = nullptr;
35 static char *action_line = nullptr;
36 static size_t action_len = 0;
38 int is_replay_active = 0 ;
40 static char **action_get_action(char *name);
42 static char *str_tolower (const char *str)
44 char *ret = xbt_strdup (str);
45 int i, n = strlen (ret);
46 for (i = 0; i < n; i++)
47 ret[i] = tolower (str[i]);
51 int _xbt_replay_is_active(){
52 return is_replay_active;
55 xbt_replay_reader_t xbt_replay_reader_new(const char *filename)
57 xbt_replay_reader_t res = xbt_new0(s_xbt_replay_reader_t,1);
58 res->fp = fopen(filename, "r");
59 xbt_assert(res->fp != nullptr, "Cannot open %s: %s", filename, strerror(errno));
60 res->filename = xbt_strdup(filename);
64 const char **xbt_replay_reader_get(xbt_replay_reader_t reader)
68 read = xbt_getline(&reader->line, &reader->line_len, reader->fp);
69 //XBT_INFO("got from trace: %s",reader->line);
72 return nullptr; /* end of file */
73 char *comment = strchr(reader->line, '#');
74 if (comment != nullptr)
76 xbt_str_trim(reader->line, nullptr);
77 if (reader->line[0] == '\0')
78 return xbt_replay_reader_get(reader); /* Get next line */
80 d=xbt_str_split_quoted_in_place(reader->line);
81 if (xbt_dynar_is_empty(d)) {
83 return xbt_replay_reader_get(reader); /* Get next line */
85 return (const char**) xbt_dynar_to_array(d);
88 void xbt_replay_reader_free(xbt_replay_reader_t *reader)
90 free((*reader)->filename);
91 free((*reader)->position);
92 fclose((*reader)->fp);
93 free((*reader)->line);
100 * \brief Registers a function to handle a kind of action
102 * Registers a function to handle a kind of action
103 * This table is then used by \ref xbt_replay_action_runner
105 * The argument of the function is the line describing the action, splitted on spaces with xbt_str_split_quoted()
107 * \param action_name the reference name of the action.
108 * \param function prototype given by the type: void...(xbt_dynar_t action)
110 void xbt_replay_action_register(const char *action_name, action_fun function)
112 char* lowername = str_tolower (action_name);
113 xbt_dict_set(xbt_action_funs, lowername, (void*) function, nullptr);
117 /** @brief Initializes the replay mechanism, and returns true if (and only if) it was necessary
119 * It returns false if it was already done by another process.
121 int _xbt_replay_action_init()
125 is_replay_active = 1;
126 xbt_action_funs = xbt_dict_new_homogeneous(nullptr);
127 xbt_action_queues = xbt_dict_new_homogeneous(nullptr);
131 void _xbt_replay_action_exit()
133 xbt_dict_free(&xbt_action_queues);
134 xbt_dict_free(&xbt_action_funs);
136 xbt_action_queues = nullptr;
137 xbt_action_funs = nullptr;
138 action_line = nullptr;
142 * \ingroup XBT_replay
143 * \brief function used internally to actually run the replay
148 int xbt_replay_action_runner(int argc, char *argv[])
151 if (xbt_action_fp) { // A unique trace file
153 char **evt = action_get_action(argv[0]);
157 char* lowername = str_tolower (evt[1]);
158 action_fun function = (action_fun)xbt_dict_get(xbt_action_funs, lowername);
161 function((const char **)evt);
164 xbt_die("Replay error :\n %s", e.what());
166 for (i=0;evt[i]!= nullptr;i++)
170 } else { // Should have got my trace file in argument
172 xbt_assert(argc >= 2,
173 "No '%s' agent function provided, no simulation-wide trace file provided, "
174 "and no process-wide trace file provided in deployment file. Aborting.", argv[0]
176 xbt_replay_reader_t reader = xbt_replay_reader_new(argv[1]);
177 while ((evt=xbt_replay_reader_get(reader))) {
178 if (!strcmp(argv[0],evt[0])) {
179 char* lowername = str_tolower (evt[1]);
180 action_fun function = (action_fun)xbt_dict_get(xbt_action_funs, lowername);
186 xbt_die("Replay error on line %d of file %s :\n %s" , reader->linenum,reader->filename, e.what());
189 XBT_WARN("%s:%d: Ignore trace element not for me", reader->filename, reader->linenum);
193 xbt_replay_reader_free(&reader);
198 static char **action_get_action(char *name)
200 xbt_dynar_t evt = nullptr;
201 char *evtname = nullptr;
203 xbt_dynar_t myqueue = (xbt_dynar_t) xbt_dict_get_or_null(xbt_action_queues, name);
204 if (myqueue == nullptr || xbt_dynar_is_empty(myqueue)) { // nothing stored for me. Read the file further
205 if (xbt_action_fp == nullptr) { // File closed now. There's nothing more to read. I'm out of here
208 // Read lines until I reach something for me (which breaks in loop body)
209 // or end of file reached
210 while (xbt_getline(&action_line, &action_len, xbt_action_fp) != -1) {
211 // cleanup and split the string I just read
212 char *comment = strchr(action_line, '#');
213 if (comment != nullptr)
215 xbt_str_trim(action_line, nullptr);
216 if (action_line[0] == '\0')
218 /* we cannot split in place here because we parse&store several lines for
219 * the colleagues... */
220 evt = xbt_str_split_quoted(action_line);
222 // if it's for me, I'm done
223 evtname = xbt_dynar_get_as(evt, 0, char *);
224 if (!strcasecmp(name, evtname)) {
225 return (char**) xbt_dynar_to_array(evt);
227 // Else, I have to store it for the relevant colleague
228 xbt_dynar_t otherqueue =
229 (xbt_dynar_t) xbt_dict_get_or_null(xbt_action_queues, evtname);
230 if (otherqueue == nullptr) { // Damn. Create the queue of that guy
231 otherqueue = xbt_dynar_new(sizeof(xbt_dynar_t), xbt_dynar_free_voidp);
232 xbt_dict_set(xbt_action_queues, evtname, otherqueue, nullptr);
234 xbt_dynar_push(otherqueue, &evt);
237 goto todo_done; // end of file reached while searching in vain for more work
239 // Get something from my queue and return it
240 xbt_dynar_shift(myqueue, &evt);
241 return (char**) xbt_dynar_to_array(evt);
244 // I did all my actions for me in the file (either I closed the file, or a colleague did)
245 // Let's cleanup before leaving
247 if (myqueue != nullptr) {
248 xbt_dynar_free(&myqueue);
249 xbt_dict_remove(xbt_action_queues, name);