Logo AND Algorithmique Numérique Distribuée

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