Logo AND Algorithmique Numérique Distribuée

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