1 /* Copyright (c) 2007, 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 "smx_private.h"
8 #include "xbt/sysdep.h"
11 #include "surf/surfxml_parse.h"
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;
22 static int auto_restart = 0;
24 extern int surf_parse_lineno;
26 static void parse_process_init(void)
28 smx_host_t host = SIMIX_host_get_by_name(A_surfxml_process_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);
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;
42 static void parse_argument(void)
44 parse_argv = xbt_realloc(parse_argv, (parse_argc + 2) * sizeof(char *));
45 parse_argv[parse_argc++] = xbt_strdup(A_surfxml_argument_value);
48 static void parse_process_finalize(void)
50 smx_process_arg_t arg = NULL;
51 smx_process_t process = NULL;
52 parse_argv[parse_argc] = NULL;
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;
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;
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);
71 if (simix_global->create_process_function)
72 simix_global->create_process_function(&process,
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);
86 /* verify if process has been created (won't be the case if the host is currently dead, but that's fine) */
91 current_property_set = NULL;
95 * \brief An application deployer.
97 * Creates the process described in \a file.
98 * \param file a filename of a xml description of the application. This file
101 * \include surfxml.dtd
103 * Here is a small example of such a platform
105 * \include small_deployment.xml
108 void SIMIX_launch_application(const char *file)
112 _XBT_GNUC_UNUSED int parse_status;
113 xbt_assert(simix_global,
114 "SIMIX_global_init has to be called before SIMIX_launch_application.");
116 surf_parse_reset_callbacks();
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);
124 surf_parse_open(file);
126 parse_status = surf_parse();
128 xbt_assert(!parse_status, "Parse error at %s:%d", file,surf_parse_lineno);
130 xbt_die("Unrecoverable error at %s:%d: %s",
131 file, surf_parse_lineno, e.msg);
136 * \brief Registers a #smx_process_code_t code in a global table.
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
143 XBT_INLINE void SIMIX_function_register(const char *name,
144 xbt_main_func_t code)
146 xbt_assert(simix_global,
147 "SIMIX_global_init has to be called before SIMIX_function_register.");
149 xbt_dict_set(simix_global->registered_functions, name, code, NULL);
152 static xbt_main_func_t default_function = NULL;
154 * \brief Registers a #smx_process_code_t code as default value.
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
159 void SIMIX_function_register_default(xbt_main_func_t code)
161 xbt_assert(simix_global,
162 "SIMIX_global_init has to be called before SIMIX_function_register.");
164 default_function = code;
168 * \brief Gets a #smx_process_t code from the global table.
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.
175 xbt_main_func_t SIMIX_get_registered_function(const char *name)
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.");
181 res = (xbt_main_func_t)xbt_dict_get_or_null(simix_global->registered_functions, name);
182 return res ? res : default_function;
187 * \brief Bypass the parser, get arguments, and set function to each process
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)
200 smx_host_t host = SIMIX_host_get_by_name(process_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);
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);
212 xbt_dynar_foreach(arguments, i, arg) {
213 parse_argv[i + 1] = xbt_strdup(arg);
215 parse_argv[parse_argc] = NULL;
217 start_time = process_start_time;
218 kill_time = process_kill_time;
221 parse_process_finalize();