1 /* Copyright (c) 2007, 2009-2014. 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)");
16 static xbt_main_func_t parse_code = NULL;
17 static double start_time = 0.0;
18 static double kill_time = -1.0;
20 static int auto_restart = 0;
22 extern int surf_parse_lineno;
24 static void parse_process(sg_platf_process_cbarg_t process)
26 smx_host_t host = SIMIX_host_get_by_name(process->host);
28 THROWF(arg_error, 0, "Host '%s' unknown", process->host);
29 parse_code = SIMIX_get_registered_function(process->function);
30 xbt_assert(parse_code, "Function '%s' unknown", process->function);
32 start_time = process->start_time;
33 kill_time = process->kill_time;
34 auto_restart = process->on_failure == SURF_PROCESS_ON_FAILURE_DIE ? 0 : 1;
36 smx_process_arg_t arg = NULL;
37 smx_process_t process_created = NULL;
39 arg = xbt_new0(s_smx_process_arg_t, 1);
40 arg->code = parse_code;
42 arg->hostname = sg_host_name(host);
43 arg->argc = process->argc;
44 arg->argv = xbt_new(char *,process->argc);
46 for (i=0; i<process->argc; i++)
47 arg->argv[i] = xbt_strdup(process->argv[i]);
48 arg->name = xbt_strdup(arg->argv[0]);
49 arg->kill_time = kill_time;
50 arg->properties = current_property_set;
51 if (!SIMIX_host_priv(host)->boot_processes) {
52 SIMIX_host_priv(host)->boot_processes = xbt_dynar_new(sizeof(smx_process_arg_t), _SIMIX_host_free_process_arg);
54 xbt_dynar_push_as(SIMIX_host_priv(host)->boot_processes,smx_process_arg_t,arg);
56 if (start_time > SIMIX_get_clock()) {
57 arg = xbt_new0(s_smx_process_arg_t, 1);
58 arg->name = (char*)(process->argv)[0];
59 arg->code = parse_code;
61 arg->hostname = sg_host_name(host);
62 arg->argc = process->argc;
63 arg->argv = (char**)(process->argv);
64 arg->kill_time = kill_time;
65 arg->properties = current_property_set;
67 XBT_DEBUG("Process %s(%s) will be started at time %f", arg->name,
68 arg->hostname, start_time);
69 SIMIX_timer_set(start_time, &SIMIX_process_create_from_wrapper, arg);
70 } else { // start_time <= SIMIX_get_clock()
71 XBT_DEBUG("Starting Process %s(%s) right now", process->argv[0], sg_host_name(host));
73 if (simix_global->create_process_function)
74 simix_global->create_process_function(&process_created,
75 (char*)(process->argv)[0],
81 (char**)(process->argv),
85 simcall_process_create(&process_created, (char*)(process->argv)[0], parse_code, NULL, sg_host_name(host), kill_time, process->argc,
86 (char**)process->argv, current_property_set,auto_restart);
88 /* verify if process has been created (won't be the case if the host is currently dead, but that's fine) */
89 if (!process_created) {
93 current_property_set = NULL;
96 void SIMIX_init_application(void){
97 surf_parse_reset_callbacks();
98 sg_platf_process_add_cb(parse_process);
102 * \brief An application deployer.
104 * Creates the process described in \a file.
105 * \param file a filename of a xml description of the application. This file
108 * \include surfxml.dtd
110 * Here is a small example of such a platform
112 * \include small_deployment.xml
115 void SIMIX_launch_application(const char *file)
117 _XBT_GNUC_UNUSED int parse_status;
118 xbt_assert(simix_global,
119 "SIMIX_global_init has to be called before SIMIX_launch_application.");
121 SIMIX_init_application();
123 surf_parse_open(file);
125 parse_status = surf_parse();
127 xbt_assert(!parse_status, "Parse error at %s:%d", file,surf_parse_lineno);
130 XBT_ERROR("Unrecoverable error at %s:%d. The full exception stack follows, in case it helps you to diagnose the problem.",
131 file, surf_parse_lineno);
137 * \brief Registers a #smx_process_code_t code in a global table.
139 * Registers a code function in a global table.
140 * This table is then used by #SIMIX_launch_application.
141 * \param name the reference name of the function.
142 * \param code the function
144 XBT_INLINE void SIMIX_function_register(const char *name,
145 xbt_main_func_t code)
147 xbt_assert(simix_global,
148 "SIMIX_global_init has to be called before SIMIX_function_register.");
150 xbt_dict_set(simix_global->registered_functions, name, code, NULL);
153 static xbt_main_func_t default_function = NULL;
155 * \brief Registers a #smx_process_code_t code as default value.
157 * 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.
158 * \param code the function
160 void SIMIX_function_register_default(xbt_main_func_t code)
162 xbt_assert(simix_global,
163 "SIMIX_global_init has to be called before SIMIX_function_register.");
165 default_function = code;
169 * \brief Gets a #smx_process_t code from the global table.
171 * Gets a code function from the global table. Returns NULL if there are no function registered with the name.
172 * This table is then used by #SIMIX_launch_application.
173 * \param name the reference name of the function.
174 * \return The #smx_process_t or NULL.
176 xbt_main_func_t SIMIX_get_registered_function(const char *name)
178 xbt_main_func_t res = NULL;
179 xbt_assert(simix_global,
180 "SIMIX_global_init has to be called before SIMIX_get_registered_function.");
182 res = (xbt_main_func_t)xbt_dict_get_or_null(simix_global->registered_functions, name);
183 return res ? res : default_function;
188 * \brief Bypass the parser, get arguments, and set function to each process
191 void SIMIX_process_set_function(const char *process_host,
192 const char *process_function,
193 xbt_dynar_t arguments,
194 double process_start_time,
195 double process_kill_time)
197 s_sg_platf_process_cbarg_t process = SG_PLATF_PROCESS_INITIALIZER;
199 smx_host_t host = SIMIX_host_get_by_name(process_host);
201 THROWF(arg_error, 0, "Host '%s' unknown", process_host);
202 process.host = sg_host_name(host);
204 process.argc = 1 + xbt_dynar_length(arguments);
205 process.argv = (const char**)xbt_new(char *, process.argc + 1);
206 process.argv[0] = xbt_strdup(process_function);
210 xbt_dynar_foreach(arguments, i, arg) {
211 process.argv[i + 1] = xbt_strdup(arg);
213 process.argv[process.argc] = NULL;
215 parse_code = SIMIX_get_registered_function(process_function);
216 xbt_assert(parse_code, "Function '%s' unknown", process_function);
217 process.function = process_function;
219 process.host = process_host;
220 process.kill_time = process_kill_time;
221 process.start_time = process_start_time;
223 sg_platf_new_process(&process);