1 /* Copyright (c) 2007, 2009-2015. 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 extern int surf_parse_lineno;
18 static void parse_process(sg_platf_process_cbarg_t process)
20 sg_host_t host = sg_host_by_name(process->host);
22 THROWF(arg_error, 0, "Host '%s' unknown", process->host);
23 xbt_main_func_t parse_code = SIMIX_get_registered_function(process->function);
24 xbt_assert(parse_code, "Function '%s' unknown", process->function);
26 double start_time = process->start_time;
27 double kill_time = process->kill_time;
28 int auto_restart = process->on_failure == SURF_PROCESS_ON_FAILURE_DIE ? 0 : 1;
30 smx_process_arg_t arg = NULL;
31 smx_process_t process_created = NULL;
33 arg = xbt_new0(s_smx_process_arg_t, 1);
34 arg->code = parse_code;
36 arg->hostname = sg_host_get_name(host);
37 arg->argc = process->argc;
38 arg->argv = xbt_new(char *,process->argc);
40 for (i=0; i<process->argc; i++)
41 arg->argv[i] = xbt_strdup(process->argv[i]);
42 arg->name = xbt_strdup(arg->argv[0]);
43 arg->kill_time = kill_time;
44 arg->properties = current_property_set;
45 if (!sg_host_simix(host)->boot_processes) {
46 sg_host_simix(host)->boot_processes = xbt_dynar_new(sizeof(smx_process_arg_t), _SIMIX_host_free_process_arg);
48 xbt_dynar_push_as(sg_host_simix(host)->boot_processes,smx_process_arg_t,arg);
50 if (start_time > SIMIX_get_clock()) {
51 arg = xbt_new0(s_smx_process_arg_t, 1);
52 arg->name = (char*)(process->argv)[0];
53 arg->code = parse_code;
55 arg->hostname = sg_host_get_name(host);
56 arg->argc = process->argc;
57 arg->argv = (char**)(process->argv);
58 arg->kill_time = kill_time;
59 arg->properties = current_property_set;
61 XBT_DEBUG("Process %s(%s) will be started at time %f", arg->name,
62 arg->hostname, start_time);
63 SIMIX_timer_set(start_time, &SIMIX_process_create_from_wrapper, arg);
64 } else { // start_time <= SIMIX_get_clock()
65 XBT_DEBUG("Starting Process %s(%s) right now", process->argv[0], sg_host_get_name(host));
67 if (simix_global->create_process_function)
68 process_created = simix_global->create_process_function(
69 (char*)(process->argv)[0],
72 sg_host_get_name(host),
75 (char**)(process->argv),
79 process_created = simcall_process_create((char*)(process->argv)[0], parse_code, NULL, sg_host_get_name(host), kill_time, process->argc,
80 (char**)process->argv, current_property_set,auto_restart);
82 /* verify if process has been created (won't be the case if the host is currently dead, but that's fine) */
83 if (!process_created) {
87 current_property_set = NULL;
90 void SIMIX_init_application(void){
91 surf_parse_reset_callbacks();
92 sg_platf_process_add_cb(parse_process);
96 * \brief An application deployer.
98 * Creates the process described in \a file.
99 * \param file a filename of a xml description of the application. This file
102 * \include surfxml.dtd
104 * Here is a small example of such a platform
106 * \include small_deployment.xml
109 void SIMIX_launch_application(const char *file)
111 XBT_ATTRIB_UNUSED int parse_status;
112 xbt_assert(simix_global,
113 "SIMIX_global_init has to be called before SIMIX_launch_application.");
115 SIMIX_init_application();
117 surf_parse_open(file);
119 parse_status = surf_parse();
121 xbt_assert(!parse_status, "Parse error at %s:%d", file,surf_parse_lineno);
124 XBT_ERROR("Unrecoverable error at %s:%d. The full exception stack follows, in case it helps you to diagnose the problem.",
125 file, surf_parse_lineno);
131 * \brief Registers a #smx_process_code_t code in a global table.
133 * Registers a code function in a global table.
134 * This table is then used by #SIMIX_launch_application.
135 * \param name the reference name of the function.
136 * \param code the function
138 XBT_INLINE void SIMIX_function_register(const char *name,
139 xbt_main_func_t code)
141 xbt_assert(simix_global,
142 "SIMIX_global_init has to be called before SIMIX_function_register.");
144 xbt_dict_set(simix_global->registered_functions, name, code, NULL);
147 static xbt_main_func_t default_function = NULL;
149 * \brief Registers a #smx_process_code_t code as default value.
151 * 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.
152 * \param code the function
154 void SIMIX_function_register_default(xbt_main_func_t code)
156 xbt_assert(simix_global,
157 "SIMIX_global_init has to be called before SIMIX_function_register.");
159 default_function = code;
163 * \brief Gets a #smx_process_t code from the global table.
165 * Gets a code function from the global table. Returns NULL if there are no function registered with the name.
166 * This table is then used by #SIMIX_launch_application.
167 * \param name the reference name of the function.
168 * \return The #smx_process_t or NULL.
170 xbt_main_func_t SIMIX_get_registered_function(const char *name)
172 xbt_main_func_t res = NULL;
173 xbt_assert(simix_global,
174 "SIMIX_global_init has to be called before SIMIX_get_registered_function.");
176 res = (xbt_main_func_t)xbt_dict_get_or_null(simix_global->registered_functions, name);
177 return res ? res : default_function;
182 * \brief Bypass the parser, get arguments, and set function to each process
185 void SIMIX_process_set_function(const char *process_host,
186 const char *process_function,
187 xbt_dynar_t arguments,
188 double process_start_time,
189 double process_kill_time)
191 s_sg_platf_process_cbarg_t process = SG_PLATF_PROCESS_INITIALIZER;
193 sg_host_t host = sg_host_by_name(process_host);
195 THROWF(arg_error, 0, "Host '%s' unknown", process_host);
196 process.host = sg_host_get_name(host);
198 process.argc = 1 + xbt_dynar_length(arguments);
199 process.argv = (const char**)xbt_new(char *, process.argc + 1);
200 process.argv[0] = xbt_strdup(process_function);
204 xbt_dynar_foreach(arguments, i, arg) {
205 process.argv[i + 1] = xbt_strdup(arg);
207 process.argv[process.argc] = NULL;
209 xbt_main_func_t parse_code = SIMIX_get_registered_function(process_function);
210 xbt_assert(parse_code, "Function '%s' unknown", process_function);
211 process.function = process_function;
213 process.host = process_host;
214 process.kill_time = process_kill_time;
215 process.start_time = process_start_time;
217 sg_platf_new_process(&process);