Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
aab6aaac451fe0a6bd50992c33d39eb672a4c764
[simgrid.git] / src / msg / msg_actions.c
1 /* Copyright (c) 2009, 2010. 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 "simgrid_config.h"     /* getline */
8 #include "msg/private.h"
9 #include "xbt/str.h"
10 #include "xbt/dynar.h"
11
12 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(msg_action, msg,
13                                 "MSG actions for trace driven simulation");
14
15 static int paranoid_action_replayer=1;
16 /** \ingroup msg_actions
17  *  \brief set the paranoid mode: true if we must check our input, false if it's well formated
18  */
19 void MSG_action_paranoid_mode_set(int mode) {
20   paranoid_action_replayer = mode;
21 }
22
23 static xbt_dict_t action_funs;
24 static xbt_dict_t action_queues;
25
26 /* To split the file if a unique one is given (specific variable for the other case live in runner()) */
27 static FILE *action_fp = NULL;
28 static char *action_line = NULL;
29 static size_t action_len = 0;
30
31 static xbt_dynar_t action_get_action(char *name);
32
33 /** \ingroup msg_actions
34  * \brief Registers a function to handle a kind of action
35  *
36  * Registers a function to handle a kind of action
37  * This table is then used by #MSG_action_trace_run
38  *
39  * The argument of the function is the line describing the action, splitted on spaces with xbt_str_split_quoted()
40  *
41  * \param name the reference name of the action.
42  * \param code the function; prototype given by the type: void...(xbt_dynar_t action)
43  */
44 void MSG_action_register(const char *action_name, msg_action_fun function)
45 {
46   xbt_dict_set(action_funs, action_name, function, NULL);
47 }
48
49 /** \ingroup msg_actions
50  * \brief Unregisters a function, which handled a kind of action
51  *
52  * \param name the reference name of the action.
53  */
54 void MSG_action_unregister(const char *action_name)
55 {
56   xbt_dict_remove(action_funs, action_name);
57 }
58
59 static int MSG_action_runner(int argc, char *argv[])
60 {
61   xbt_dynar_t evt = NULL;
62   char *line = NULL;
63   size_t line_len = 0;
64   FILE *fp = NULL;
65   char *comment = NULL;
66   char *evtname = NULL;
67   ssize_t read;
68   if (action_fp) {              // A unique trace file
69
70     while ((evt = action_get_action(argv[0]))) {
71       msg_action_fun function =
72           xbt_dict_get(action_funs, xbt_dynar_get_as(evt, 1, char *));
73       (*function) (evt);
74       xbt_dynar_free(&evt);
75     }
76   } else {                      // Should have got my trace file in argument
77     xbt_assert1(argc >= 2,
78                 "No '%s' agent function provided, no simulation-wide trace file provided to MSG_action_trace_run(), "
79                 "and no process-wide trace file provided in deployment file. Aborting.",
80                 argv[0]
81         );
82
83     fp = fopen(argv[1], "r");
84     xbt_assert2(fp != NULL, "Cannot open %s: %s", argv[1],
85                 strerror(errno));
86
87     // Read lines and execute them until I reach the end of file
88     while ((read = getline(&line, &line_len, fp)) != -1) {
89       // cleanup and split the string I just read
90       if (paranoid_action_replayer) {
91         comment = strchr(line, '#');
92         if (comment != NULL)
93           *comment = '\0';
94         xbt_str_trim(line, NULL);
95       }
96       evt = xbt_str_split_quoted_in_place(line);
97       if (xbt_dynar_length(evt)==0) {
98         xbt_dynar_free(&evt);
99         continue;
100       }
101
102       evtname = xbt_dynar_get_as(evt, 0, char *);
103       if (!strcmp(argv[0], evtname)) {
104         msg_action_fun function =
105             xbt_dict_get(action_funs, xbt_dynar_get_as(evt, 1, char *));
106         (*function) (evt);
107       } else {
108         WARN1("Ignore trace element not for me: %s",
109               xbt_str_join(evt, " "));
110       }
111       xbt_dynar_free(&evt);
112     }
113     fclose(fp);
114
115   }
116   return 0;
117 }
118
119 void _MSG_action_init()
120 {
121   action_funs = xbt_dict_new();
122   action_queues = xbt_dict_new();
123   MSG_function_register_default(MSG_action_runner);
124 }
125
126 void _MSG_action_exit()
127 {
128   xbt_dict_free(&action_queues);
129   xbt_dict_free(&action_funs);
130 }
131
132
133 static xbt_dynar_t action_get_action(char *name)
134 {
135   ssize_t read;
136   xbt_dynar_t evt = NULL;
137   char *evtname = NULL;
138
139   xbt_dynar_t myqueue = xbt_dict_get_or_null(action_queues, name);
140   if (myqueue == NULL || xbt_dynar_length(myqueue) == 0) {      // nothing stored for me. Read the file further
141
142     if (action_fp == NULL) {    // File closed now. There's nothing more to read. I'm out of here
143       goto todo_done;
144     }
145     // Read lines until I reach something for me (which breaks in loop body)
146     // or end of file reached
147     while ((read = getline(&action_line, &action_len, action_fp)) != -1) {
148       // cleanup and split the string I just read
149       char *comment = strchr(action_line, '#');
150       if (comment != NULL)
151         *comment = '\0';
152       xbt_str_trim(action_line, NULL);
153       if (action_line[0] == '\0')
154         continue;
155       /* we cannot split in place here because we parse&store several lines for
156        * the colleagues... */
157       evt = xbt_str_split_quoted(action_line);
158
159       // if it's for me, I'm done
160       evtname = xbt_dynar_get_as(evt, 0, char *);
161       if (!strcmp(name, evtname)) {
162         return evt;
163       } else {
164         // Else, I have to store it for the relevant colleague
165         xbt_dynar_t otherqueue =
166             xbt_dict_get_or_null(action_queues, evtname);
167         if (otherqueue == NULL) {       // Damn. Create the queue of that guy
168           otherqueue =
169               xbt_dynar_new(sizeof(xbt_dynar_t), xbt_dynar_free_voidp);
170           xbt_dict_set(action_queues, evtname, otherqueue, NULL);
171         }
172         xbt_dynar_push(otherqueue, &evt);
173       }
174     }
175     goto todo_done;             // end of file reached while searching in vain for more work
176   } else {
177     // Get something from my queue and return it
178     xbt_dynar_shift(myqueue, &evt);
179     return evt;
180   }
181
182
183   // I did all my actions for me in the file (either I closed the file, or a colleague did)
184   // Let's cleanup before leaving
185 todo_done:
186   if (myqueue != NULL) {
187     xbt_dynar_free(&myqueue);
188     xbt_dict_remove(action_queues, name);
189   }
190   return NULL;
191 }
192
193 /** \ingroup msg_actions
194  * \brief A trace loader
195  *
196  *  If path!=NULL, load a trace file containing actions, and execute them.
197  *  Else, assume that each process gets the path in its deployment file
198  */
199 MSG_error_t MSG_action_trace_run(char *path)
200 {
201   MSG_error_t res;
202   char *name;
203   xbt_dynar_t todo;
204   xbt_dict_cursor_t cursor;
205
206   if (path) {
207     action_fp = fopen(path, "r");
208     xbt_assert2(action_fp != NULL, "Cannot open %s: %s", path,
209                 strerror(errno));
210   }
211   res = MSG_main();
212
213   if (xbt_dict_size(action_queues)) {
214     WARN0
215         ("Not all actions got consumed. If the simulation ended successfully (without deadlock), you may want to add new processes to your deployment file.");
216
217
218     xbt_dict_foreach(action_queues, cursor, name, todo) {
219       WARN2("Still %lu actions for %s", xbt_dynar_length(todo), name);
220     }
221   }
222
223   if (action_line)
224     free(action_line);
225   if (path)
226     fclose(action_fp);
227   xbt_dict_free(&action_queues);
228   action_queues = xbt_dict_new();
229
230   return res;
231 }