1 /* Copyright (c) 2009, 2010. The SimGrid Team.
2 * All rights reserved. */
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. */
7 #include "simgrid_config.h" /* getline */
8 #include "msg/private.h"
10 #include "xbt/dynar.h"
12 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(msg_action,msg,"MSG actions for trace driven simulation");
14 static xbt_dict_t action_funs;
15 static xbt_dict_t action_queues;
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;
22 static xbt_dynar_t action_get_action(char *name);
24 /** \ingroup msg_actions
25 * \brief Registers a function to handle a kind of action
27 * Registers a function to handle a kind of action
28 * This table is then used by #MSG_action_trace_run
30 * The argument of the function is the line describing the action, splitted on spaces with xbt_str_split_quoted()
32 * \param name the reference name of the action.
33 * \param code the function; prototype given by the type: void...(xbt_dynar_t action)
35 void MSG_action_register(const char *action_name, msg_action_fun function)
37 xbt_dict_set(action_funs, action_name, function, NULL);
40 /** \ingroup msg_actions
41 * \brief Unregisters a function, which handled a kind of action
43 * \param name the reference name of the action.
45 void MSG_action_unregister(const char *action_name)
47 xbt_dict_remove(action_funs, action_name);
50 static int MSG_action_runner(int argc, char *argv[]) {
52 if (action_fp) { // A unique trace file
54 while ((evt = action_get_action(argv[0]))) {
55 msg_action_fun function =
56 xbt_dict_get(action_funs, xbt_dynar_get_as(evt, 1, char *));
60 } else { // Should have got my trace file in argument
62 "No simulation-wide trace file provided to MSG_action_trace_run(), "
63 "and no process-wide trace file provided in deployment file. Aborting."
68 FILE *fp = fopen(argv[1], "r");
69 xbt_assert2(fp != NULL, "Cannot open %s: %s", argv[1], strerror(errno));
72 // Read lines and execute them until I reach the end of file
73 while ((read = getline(&line, &line_len, fp)) != -1) {
74 // cleanup and split the string I just read
75 char *comment = strchr(line, '#');
78 xbt_str_trim(line, NULL);
81 evt = xbt_str_split_quoted(line);
83 char *evtname = xbt_dynar_get_as(evt, 0, char *);
84 if (!strcmp(argv[0],evtname)) {
85 msg_action_fun function =
86 xbt_dict_get(action_funs, xbt_dynar_get_as(evt, 1, char *));
89 WARN1("Ignore trace element not for me: %s",xbt_str_join(evt," "));
98 void _MSG_action_init()
100 action_funs = xbt_dict_new();
101 action_queues = xbt_dict_new();
102 MSG_function_register_default(MSG_action_runner);
105 void _MSG_action_exit() {
106 xbt_dict_free(&action_queues);
107 xbt_dict_free(&action_funs);
111 static xbt_dynar_t action_get_action(char *name) {
113 xbt_dynar_t evt=NULL;
116 xbt_dynar_t myqueue = xbt_dict_get_or_null(action_queues,name);
117 if (myqueue==NULL || xbt_dynar_length(myqueue)==0) { // nothing stored for me. Read the file further
119 if (action_fp==NULL) { // File closed now. There's nothing more to read. I'm out of here
123 // Read lines until I reach something for me (which breaks in loop body)
124 // or end of file reached
125 while ((read = getline(&action_line, &action_len, action_fp)) != -1) {
126 // cleanup and split the string I just read
127 char *comment = strchr(action_line, '#');
130 xbt_str_trim(action_line, NULL);
131 if (action_line[0] == '\0')
133 evt = xbt_str_split_quoted(action_line);
135 // if it's for me, I'm done
136 char *evtname = xbt_dynar_get_as(evt, 0, char *);
137 if (!strcmp(name,evtname)) {
140 // Else, I have to store it for the relevant colleague
141 xbt_dynar_t otherqueue = xbt_dict_get_or_null(action_queues,evtname);
142 if (otherqueue == NULL) { // Damn. Create the queue of that guy
143 otherqueue = xbt_dynar_new(sizeof(xbt_dynar_t), xbt_dynar_free_voidp);
144 xbt_dict_set(action_queues ,evtname, otherqueue, NULL);
146 xbt_dynar_push(otherqueue,&evt);
149 goto todo_done; // end of file reached while searching in vain for more work
151 // Get something from my queue and return it
152 xbt_dynar_shift(myqueue,&evt);
157 // I did all my actions for me in the file (either I closed the file, or a colleague did)
158 // Let's cleanup before leaving
160 if (myqueue != NULL) {
161 xbt_dynar_free(&myqueue);
162 xbt_dict_remove(action_queues,name);
167 /** \ingroup msg_actions
168 * \brief A trace loader
170 * If path!=NULL, load a trace file containing actions, and execute them.
171 * Else, assume that each process gets the path in its deployment file
173 MSG_error_t MSG_action_trace_run(char *path)
178 action_fp = fopen(path, "r");
179 xbt_assert2(action_fp != NULL, "Cannot open %s: %s", path, strerror(errno));
183 if (xbt_dict_size(action_queues)) {
184 WARN0("Not all actions got consumed. If the simulation ended successfully (without deadlock), you may want to add new processes to your deployment file.");
185 xbt_dict_cursor_t cursor;
189 xbt_dict_foreach(action_queues,cursor,name,todo) {
190 WARN2("Still %lu actions for %s",xbt_dynar_length(todo),name);
198 xbt_dict_free(&action_queues);
199 action_queues = xbt_dict_new();