Logo AND Algorithmique Numérique Distribuée

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