Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
2fd02d3d45ed97099bfe76387f2d4c4aaad6d6ef
[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   char* lowername = str_tolower (action_name);
113   xbt_dict_set(xbt_action_funs, lowername, (void*) function, nullptr);
114   xbt_free(lowername);
115 }
116
117 /** @brief Initializes the replay mechanism, and returns true if (and only if) it was necessary
118  *
119  * It returns false if it was already done by another process.
120  */
121 int _xbt_replay_action_init()
122 {
123   if (xbt_action_funs)
124     return 0;
125   is_replay_active = 1;
126   xbt_action_funs = xbt_dict_new_homogeneous(nullptr);
127   xbt_action_queues = xbt_dict_new_homogeneous(nullptr);
128   return 1;
129 }
130
131 void _xbt_replay_action_exit()
132 {
133   xbt_dict_free(&xbt_action_queues);
134   xbt_dict_free(&xbt_action_funs);
135   free(action_line);
136   xbt_action_queues = nullptr;
137   xbt_action_funs = nullptr;
138   action_line = nullptr;
139 }
140
141 /**
142  * \ingroup XBT_replay
143  * \brief function used internally to actually run the replay
144
145  * \param argc argc .
146  * \param argv argv
147  */
148 int xbt_replay_action_runner(int argc, char *argv[])
149 {
150   if (xbt_action_fp) {              // A unique trace file
151     while (true) {
152       char **evt = action_get_action(argv[0]);
153       if (evt == nullptr)
154         break;
155
156       char* lowername = str_tolower (evt[1]);
157       action_fun function = (action_fun)xbt_dict_get(xbt_action_funs, lowername);
158       xbt_free(lowername);
159       try {
160         function((const char **)evt);
161       }
162       catch(xbt_ex& e) {
163         xbt_die("Replay error :\n %s", e.what());
164       }
165       for (int i=0;evt[i]!= nullptr;i++)
166         free(evt[i]);
167       free(evt);
168     }
169   } else {                      // Should have got my trace file in argument
170     const char **evt;
171     xbt_assert(argc >= 2,
172                 "No '%s' agent function provided, no simulation-wide trace file provided, "
173                 "and no process-wide trace file provided in deployment file. Aborting.", argv[0]
174         );
175     xbt_replay_reader_t reader = xbt_replay_reader_new(argv[1]);
176     while ((evt=xbt_replay_reader_get(reader))) {
177       if (!strcmp(argv[0],evt[0])) {
178         char* lowername = str_tolower (evt[1]);
179         action_fun function = (action_fun)xbt_dict_get(xbt_action_funs, lowername);
180         xbt_free(lowername);
181         try {
182           function(evt);
183         } catch(xbt_ex& e) {
184           free(evt);
185           xbt_die("Replay error on line %d of file %s :\n %s" , reader->linenum,reader->filename, e.what());
186         }
187       } else {
188         XBT_WARN("%s:%d: Ignore trace element not for me", reader->filename, reader->linenum);
189       }
190       free(evt);
191     }
192     xbt_replay_reader_free(&reader);
193   }
194   return 0;
195 }
196
197 static char **action_get_action(char *name)
198 {
199   xbt_dynar_t evt = nullptr;
200   char *evtname = nullptr;
201
202   xbt_dynar_t myqueue = (xbt_dynar_t) xbt_dict_get_or_null(xbt_action_queues, name);
203   if (myqueue == nullptr || xbt_dynar_is_empty(myqueue)) {      // nothing stored for me. Read the file further
204     if (xbt_action_fp == nullptr) {    // File closed now. There's nothing more to read. I'm out of here
205       goto todo_done;
206     }
207     // Read lines until I reach something for me (which breaks in loop body)
208     // or end of file reached
209     while (xbt_getline(&action_line, &action_len, xbt_action_fp) != -1) {
210       // cleanup and split the string I just read
211       char *comment = strchr(action_line, '#');
212       if (comment != nullptr)
213         *comment = '\0';
214       xbt_str_trim(action_line, nullptr);
215       if (action_line[0] == '\0')
216         continue;
217       /* we cannot split in place here because we parse&store several lines for
218        * the colleagues... */
219       evt = xbt_str_split_quoted(action_line);
220
221       // if it's for me, I'm done
222       evtname = xbt_dynar_get_as(evt, 0, char *);
223       if (!strcasecmp(name, evtname)) {
224         return (char**) xbt_dynar_to_array(evt);
225       } else {
226         // Else, I have to store it for the relevant colleague
227         xbt_dynar_t otherqueue =
228             (xbt_dynar_t) xbt_dict_get_or_null(xbt_action_queues, evtname);
229         if (otherqueue == nullptr) {       // Damn. Create the queue of that guy
230           otherqueue = xbt_dynar_new(sizeof(xbt_dynar_t), xbt_dynar_free_voidp);
231           xbt_dict_set(xbt_action_queues, evtname, otherqueue, nullptr);
232         }
233         xbt_dynar_push(otherqueue, &evt);
234       }
235     }
236     // end of file reached while searching in vain for more work
237   } else {
238     // Get something from my queue and return it
239     xbt_dynar_shift(myqueue, &evt);
240     return (char**) xbt_dynar_to_array(evt);
241   }
242
243   // I did all my actions for me in the file (either I closed the file, or a colleague did)
244   // Let's cleanup before leaving
245 todo_done:
246   if (myqueue != nullptr) {
247     xbt_dynar_free(&myqueue);
248     xbt_dict_remove(xbt_action_queues, name);
249   }
250   return nullptr;
251 }