Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
smpi_replay: allow to register extra events before things start (+plug a memleak)
[simgrid.git] / src / xbt / xbt_replay.cpp
1 /* Copyright (c) 2010, 2012-2015. The SimGrid Team.
2  * All rights reserved.                                                     */
3
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
7 #include <xbt/ex.hpp>
8 #include "src/internal_config.h"
9 #include "xbt/sysdep.h"
10 #include "xbt/log.h"
11 #include "xbt/str.h"
12 #include "xbt/file.h"
13 #include "xbt/replay.h"
14
15 #include <errno.h>
16 #include <ctype.h>
17 #include <wchar.h>
18
19 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(replay,xbt,"Replay trace reader");
20
21 typedef struct s_replay_reader {
22   FILE *fp;
23   char *line;
24   size_t line_len;
25   char *position; /* stable storage */
26   char *filename; 
27   int linenum;
28 } s_xbt_replay_reader_t;
29
30 FILE *xbt_action_fp;
31
32 xbt_dict_t xbt_action_funs = nullptr;
33 xbt_dict_t xbt_action_queues = nullptr;
34
35 static char *action_line = nullptr;
36 static size_t action_len = 0;
37
38 int is_replay_active = 0 ;
39
40 static char **action_get_action(char *name);
41
42 static char *str_tolower (const char *str)
43 {
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]);
48   return ret;
49 }
50
51 int _xbt_replay_is_active(){
52   return is_replay_active;
53 }
54
55 xbt_replay_reader_t xbt_replay_reader_new(const char *filename)
56 {
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);
61   return res;
62 }
63
64 const char **xbt_replay_reader_get(xbt_replay_reader_t reader)
65 {
66   ssize_t read;
67   xbt_dynar_t d;
68   read = xbt_getline(&reader->line, &reader->line_len, reader->fp);
69   //XBT_INFO("got from trace: %s",reader->line);
70   reader->linenum++;
71   if (read==-1)
72     return nullptr; /* end of file */
73   char *comment = strchr(reader->line, '#');
74   if (comment != nullptr)
75     *comment = '\0';
76   xbt_str_trim(reader->line, nullptr);
77   if (reader->line[0] == '\0')
78     return xbt_replay_reader_get(reader); /* Get next line */
79
80   d=xbt_str_split_quoted_in_place(reader->line);
81   if (xbt_dynar_is_empty(d)) {
82     xbt_dynar_free(&d);
83     return xbt_replay_reader_get(reader); /* Get next line */
84   }
85   return (const char**) xbt_dynar_to_array(d);
86 }
87
88 void xbt_replay_reader_free(xbt_replay_reader_t *reader)
89 {
90   free((*reader)->filename);
91   free((*reader)->position);
92   fclose((*reader)->fp);
93   free((*reader)->line);
94   free(*reader);
95   *reader=nullptr;
96 }
97
98 /**
99  * \ingroup XBT_replay
100  * \brief Registers a function to handle a kind of action
101  *
102  * Registers a function to handle a kind of action
103  * This table is then used by \ref xbt_replay_action_runner
104  *
105  * The argument of the function is the line describing the action, splitted on spaces with xbt_str_split_quoted()
106  *
107  * \param action_name the reference name of the action.
108  * \param function prototype given by the type: void...(xbt_dynar_t action)
109  */
110 void xbt_replay_action_register(const char *action_name, action_fun function)
111 {
112   if (xbt_action_funs == nullptr) // If the user registers a function before the start
113     _xbt_replay_action_init();
114
115   char* lowername = str_tolower (action_name);
116   xbt_dict_set(xbt_action_funs, lowername, (void*) function, nullptr);
117   xbt_free(lowername);
118 }
119
120 /** @brief Initializes the replay mechanism, and returns true if (and only if) it was necessary
121  *
122  * It returns false if it was already done by another process.
123  */
124 int _xbt_replay_action_init()
125 {
126   if (xbt_action_funs)
127     return 0;
128   is_replay_active = 1;
129   xbt_action_funs = xbt_dict_new_homogeneous(nullptr);
130   xbt_action_queues = xbt_dict_new_homogeneous(nullptr);
131   return 1;
132 }
133
134 void _xbt_replay_action_exit()
135 {
136   xbt_dict_free(&xbt_action_queues);
137   xbt_dict_free(&xbt_action_funs);
138   free(action_line);
139   xbt_action_queues = nullptr;
140   xbt_action_funs = nullptr;
141   action_line = nullptr;
142 }
143
144 /**
145  * \ingroup XBT_replay
146  * \brief function used internally to actually run the replay
147
148  * \param argc argc .
149  * \param argv argv
150  */
151 int xbt_replay_action_runner(int argc, char *argv[])
152 {
153   if (xbt_action_fp) {              // A unique trace file
154     while (true) {
155       char **evt = action_get_action(argv[0]);
156       if (evt == nullptr)
157         break;
158
159       char* lowername = str_tolower (evt[1]);
160       action_fun function = (action_fun)xbt_dict_get(xbt_action_funs, lowername);
161       xbt_free(lowername);
162       try {
163         function((const char **)evt);
164       }
165       catch(xbt_ex& e) {
166         xbt_die("Replay error :\n %s", e.what());
167       }
168       for (int i=0;evt[i]!= nullptr;i++)
169         free(evt[i]);
170       free(evt);
171     }
172   } else {                      // Should have got my trace file in argument
173     const char **evt;
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]
177         );
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);
183         xbt_free(lowername);
184         try {
185           function(evt);
186         } catch(xbt_ex& e) {
187           free(evt);
188           xbt_die("Replay error on line %d of file %s :\n %s" , reader->linenum,reader->filename, e.what());
189         }
190       } else {
191         XBT_WARN("%s:%d: Ignore trace element not for me", reader->filename, reader->linenum);
192       }
193       free(evt);
194     }
195     xbt_replay_reader_free(&reader);
196   }
197   return 0;
198 }
199
200 static char **action_get_action(char *name)
201 {
202   xbt_dynar_t evt = nullptr;
203   char *evtname = nullptr;
204
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
208       goto todo_done;
209     }
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)
216         *comment = '\0';
217       xbt_str_trim(action_line, nullptr);
218       if (action_line[0] == '\0')
219         continue;
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);
223
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);
228       } else {
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);
235         }
236         xbt_dynar_push(otherqueue, &evt);
237       }
238     }
239     // end of file reached while searching in vain for more work
240   } else {
241     // Get something from my queue and return it
242     xbt_dynar_shift(myqueue, &evt);
243     return (char**) xbt_dynar_to_array(evt);
244   }
245
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
248 todo_done:
249   if (myqueue != nullptr) {
250     xbt_dynar_free(&myqueue);
251     xbt_dict_remove(xbt_action_queues, name);
252   }
253   return nullptr;
254 }