Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
AFAICS, there is no problem with e.msg.
[simgrid.git] / src / simix / smx_deployment.c
1 /* Copyright (c) 2007, 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 "private.h"
8 #include "xbt/sysdep.h"
9 #include "xbt/log.h"
10 #include "xbt/dict.h"
11 #include "surf/surfxml_parse.h"
12
13 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_deployment, simix,
14                                 "Logging specific to SIMIX (deployment)");
15 static int parse_argc = -1;
16 static char **parse_argv = NULL;
17 static xbt_main_func_t parse_code = NULL;
18 static char *parse_host = NULL;
19 static double start_time = 0.0;
20 static double kill_time = -1.0;
21
22 extern int surf_parse_lineno;
23
24 static void parse_process_init(void)
25 {
26   smx_host_t host = SIMIX_host_get_by_name(A_surfxml_process_host);
27   if (!host)
28     THROWF(arg_error, 0, "Host '%s' unknown", A_surfxml_process_host);
29   parse_host = host->name;
30   parse_code = SIMIX_get_registered_function(A_surfxml_process_function);
31   xbt_assert(parse_code, "Function '%s' unknown",
32               A_surfxml_process_function);
33   parse_argc = 0;
34   parse_argv = NULL;
35   parse_argc++;
36   parse_argv = xbt_realloc(parse_argv, (parse_argc) * sizeof(char *));
37   parse_argv[(parse_argc) - 1] = xbt_strdup(A_surfxml_process_function);
38   start_time= surf_parse_get_double(A_surfxml_process_start_time);
39   kill_time = surf_parse_get_double(A_surfxml_process_kill_time);
40 }
41
42 static void parse_argument(void)
43 {
44   parse_argc++;
45   parse_argv = xbt_realloc(parse_argv, (parse_argc) * sizeof(char *));
46   parse_argv[(parse_argc) - 1] = xbt_strdup(A_surfxml_argument_value);
47 }
48
49 static void parse_process_finalize(void)
50 {
51   smx_process_arg_t arg = NULL;
52   smx_process_t process = NULL;
53   if (start_time > SIMIX_get_clock()) {
54     arg = xbt_new0(s_smx_process_arg_t, 1);
55     arg->name = parse_argv[0];
56     arg->code = parse_code;
57     arg->data = NULL;
58     arg->hostname = parse_host;
59     arg->argc = parse_argc;
60     arg->argv = parse_argv;
61     arg->kill_time = kill_time;
62     arg->properties = current_property_set;
63
64     XBT_DEBUG("Process %s(%s) will be started at time %f", arg->name,
65            arg->hostname, start_time);
66     SIMIX_timer_set(start_time, &SIMIX_process_create_from_wrapper, arg);
67   } else {                      // start_time <= SIMIX_get_clock()
68     XBT_DEBUG("Starting Process %s(%s) right now", parse_argv[0], parse_host);
69
70     if (simix_global->create_process_function)
71       simix_global->create_process_function(&process,
72                                             parse_argv[0],
73                                             parse_code, NULL,
74                                             parse_host, parse_argc,
75                                             parse_argv,
76                                             current_property_set);
77     else
78       SIMIX_req_process_create(&process, parse_argv[0], parse_code, NULL, parse_host, parse_argc, parse_argv,
79                                current_property_set);
80     /* verify if process has been created (won't be the case if the host is currently dead, but that's fine) */
81     if (!process) {
82       return;
83     }
84     if (kill_time > SIMIX_get_clock()) {
85       if (simix_global->kill_process_function) {
86         SIMIX_timer_set(start_time, simix_global->kill_process_function, process);
87       }
88     }
89   }
90   current_property_set = NULL;
91 }
92
93 /**
94  * \brief An application deployer.
95  *
96  * Creates the process described in \a file.
97  * \param file a filename of a xml description of the application. This file
98  * follows this DTD :
99  *
100  *     \include surfxml.dtd
101  *
102  * Here is a small example of such a platform
103  *
104  *     \include small_deployment.xml
105  *
106  */
107 void SIMIX_launch_application(const char *file)
108 {
109   xbt_ex_t e;
110
111   _XBT_GNUC_UNUSED int parse_status;
112   xbt_assert(simix_global,
113               "SIMIX_global_init has to be called before SIMIX_launch_application.");
114
115   surf_parse_reset_callbacks();
116
117   surfxml_add_callback(STag_surfxml_process_cb_list, parse_process_init);
118   surfxml_add_callback(ETag_surfxml_argument_cb_list, parse_argument);
119   surfxml_add_callback(STag_surfxml_prop_cb_list, parse_properties);
120   surfxml_add_callback(ETag_surfxml_process_cb_list,
121                        parse_process_finalize);
122
123   surf_parse_open(file);
124   TRY {
125           parse_status = surf_parse();
126           surf_parse_close();
127           xbt_assert(!parse_status, "Parse error at %s:%d", file,surf_parse_lineno);
128   } CATCH(e) {
129           xbt_die("Unrecoverable error at %s:%d: %s",
130                   file, surf_parse_lineno, e.msg);
131   }
132 }
133
134 /**
135  * \brief Registers a #smx_process_code_t code in a global table.
136  *
137  * Registers a code function in a global table.
138  * This table is then used by #SIMIX_launch_application.
139  * \param name the reference name of the function.
140  * \param code the function
141  */
142 XBT_INLINE void SIMIX_function_register(const char *name,
143                                         xbt_main_func_t code)
144 {
145   xbt_assert(simix_global,
146               "SIMIX_global_init has to be called before SIMIX_function_register.");
147
148   xbt_dict_set(simix_global->registered_functions, name, code, NULL);
149 }
150
151 static xbt_main_func_t default_function = NULL;
152 /**
153  * \brief Registers a #smx_process_code_t code as default value.
154  *
155  * Registers a code function as being the default value. This function will get used by SIMIX_launch_application() when there is no registered function of the requested name in.
156  * \param code the function
157  */
158 void SIMIX_function_register_default(xbt_main_func_t code)
159 {
160   xbt_assert(simix_global,
161               "SIMIX_global_init has to be called before SIMIX_function_register.");
162
163   default_function = code;
164 }
165
166 /**
167  * \brief Gets a #smx_process_t code from the global table.
168  *
169  * Gets a code function from the global table. Returns NULL if there are no function registered with the name.
170  * This table is then used by #SIMIX_launch_application.
171  * \param name the reference name of the function.
172  * \return The #smx_process_t or NULL.
173  */
174 xbt_main_func_t SIMIX_get_registered_function(const char *name)
175 {
176   xbt_main_func_t res = NULL;
177   xbt_assert(simix_global,
178               "SIMIX_global_init has to be called before SIMIX_get_registered_function.");
179
180   res = xbt_dict_get_or_null(simix_global->registered_functions, name);
181   return res ? res : default_function;
182 }
183
184
185 /**
186  * \brief Bypass the parser, get arguments, and set function to each process
187  */
188
189 void SIMIX_process_set_function(const char *process_host,
190                                 const char *process_function,
191                                 xbt_dynar_t arguments,
192                                 double process_start_time,
193                                 double process_kill_time)
194 {
195   unsigned int i;
196   char *arg;
197
198   /* init process */
199   smx_host_t host = SIMIX_host_get_by_name(process_host);
200   if (!host)
201     THROWF(arg_error, 0, "Host '%s' unknown", process_host);
202   parse_host = host->name;
203   parse_code = SIMIX_get_registered_function(process_function);
204   xbt_assert(parse_code, "Function '%s' unknown", process_function);
205
206   parse_argc = 0;
207   parse_argv = NULL;
208   parse_argc++;
209   parse_argv = xbt_realloc(parse_argv, (parse_argc) * sizeof(char *));
210   parse_argv[(parse_argc) - 1] = xbt_strdup(process_function);
211   start_time = process_start_time;
212   kill_time = process_kill_time;
213
214   /* add arguments */
215   xbt_dynar_foreach(arguments, i, arg) {
216     parse_argc++;
217     parse_argv = xbt_realloc(parse_argv, (parse_argc) * sizeof(char *));
218     parse_argv[(parse_argc) - 1] = xbt_strdup(arg);
219   }
220
221   /* finalize */
222   parse_process_finalize();
223 }