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 if (xbt_action_funs == nullptr) // If the user registers a function before the start
113 _xbt_replay_action_init();
115 char* lowername = str_tolower (action_name);
116 xbt_dict_set(xbt_action_funs, lowername, (void*) function, nullptr);
120 /** @brief Initializes the replay mechanism, and returns true if (and only if) it was necessary
122 * It returns false if it was already done by another process.
124 int _xbt_replay_action_init()
128 is_replay_active = 1;
129 xbt_action_funs = xbt_dict_new_homogeneous(nullptr);
130 xbt_action_queues = xbt_dict_new_homogeneous(nullptr);
134 void _xbt_replay_action_exit()
136 xbt_dict_free(&xbt_action_queues);
137 xbt_dict_free(&xbt_action_funs);
139 xbt_action_queues = nullptr;
140 xbt_action_funs = nullptr;
141 action_line = nullptr;
145 * \ingroup XBT_replay
146 * \brief function used internally to actually run the replay
151 int xbt_replay_action_runner(int argc, char *argv[])
153 if (xbt_action_fp) { // A unique trace file
155 char **evt = action_get_action(argv[0]);
159 char* lowername = str_tolower (evt[1]);
160 action_fun function = (action_fun)xbt_dict_get(xbt_action_funs, lowername);
163 function((const char **)evt);
166 xbt_die("Replay error :\n %s", e.what());
168 for (int i=0;evt[i]!= nullptr;i++)
172 } else { // Should have got my trace file in argument
174 xbt_assert(argc >= 2,
175 "No '%s' agent function provided, no simulation-wide trace file provided, "
176 "and no process-wide trace file provided in deployment file. Aborting.", argv[0]
178 xbt_replay_reader_t reader = xbt_replay_reader_new(argv[1]);
179 while ((evt=xbt_replay_reader_get(reader))) {
180 if (!strcmp(argv[0],evt[0])) {
181 char* lowername = str_tolower (evt[1]);
182 action_fun function = (action_fun)xbt_dict_get(xbt_action_funs, lowername);
188 xbt_die("Replay error on line %d of file %s :\n %s" , reader->linenum,reader->filename, e.what());
191 XBT_WARN("%s:%d: Ignore trace element not for me", reader->filename, reader->linenum);
195 xbt_replay_reader_free(&reader);
200 static char **action_get_action(char *name)
202 xbt_dynar_t evt = nullptr;
203 char *evtname = nullptr;
205 xbt_dynar_t myqueue = (xbt_dynar_t) xbt_dict_get_or_null(xbt_action_queues, name);
206 if (myqueue == nullptr || xbt_dynar_is_empty(myqueue)) { // nothing stored for me. Read the file further
207 if (xbt_action_fp == nullptr) { // File closed now. There's nothing more to read. I'm out of here
210 // Read lines until I reach something for me (which breaks in loop body)
211 // or end of file reached
212 while (xbt_getline(&action_line, &action_len, xbt_action_fp) != -1) {
213 // cleanup and split the string I just read
214 char *comment = strchr(action_line, '#');
215 if (comment != nullptr)
217 xbt_str_trim(action_line, nullptr);
218 if (action_line[0] == '\0')
220 /* we cannot split in place here because we parse&store several lines for
221 * the colleagues... */
222 evt = xbt_str_split_quoted(action_line);
224 // if it's for me, I'm done
225 evtname = xbt_dynar_get_as(evt, 0, char *);
226 if (!strcasecmp(name, evtname)) {
227 return (char**) xbt_dynar_to_array(evt);
229 // Else, I have to store it for the relevant colleague
230 xbt_dynar_t otherqueue =
231 (xbt_dynar_t) xbt_dict_get_or_null(xbt_action_queues, evtname);
232 if (otherqueue == nullptr) { // Damn. Create the queue of that guy
233 otherqueue = xbt_dynar_new(sizeof(xbt_dynar_t), xbt_dynar_free_voidp);
234 xbt_dict_set(xbt_action_queues, evtname, otherqueue, nullptr);
236 xbt_dynar_push(otherqueue, &evt);
239 // end of file reached while searching in vain for more work
241 // Get something from my queue and return it
242 xbt_dynar_shift(myqueue, &evt);
243 return (char**) xbt_dynar_to_array(evt);
246 // I did all my actions for me in the file (either I closed the file, or a colleague did)
247 // Let's cleanup before leaving
249 if (myqueue != nullptr) {
250 xbt_dynar_free(&myqueue);
251 xbt_dict_remove(xbt_action_queues, name);