Logo AND Algorithmique Numérique Distribuée

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