Logo AND Algorithmique Numérique Distribuée

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