Logo AND Algorithmique Numérique Distribuée

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