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)");
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 if (start_time > SIMIX_get_clock()) {
40 arg = xbt_new0(s_smx_process_arg_t, 1);
41 arg->name = (char*)(process->argv)[0];
42 arg->code = parse_code;
44 arg->hostname = sg_host_name(host);
45 arg->argc = process->argc;
46 arg->argv = (char**)(process->argv);
47 arg->kill_time = kill_time;
48 arg->properties = current_property_set;
50 XBT_DEBUG("Process %s(%s) will be started at time %f", arg->name,
51 arg->hostname, start_time);
52 SIMIX_timer_set(start_time, &SIMIX_process_create_from_wrapper, arg);
53 } else { // start_time <= SIMIX_get_clock()
54 XBT_DEBUG("Starting Process %s(%s) right now", process->argv[0], sg_host_name(host));
56 if (simix_global->create_process_function)
57 simix_global->create_process_function(&process_created,
58 (char*)(process->argv)[0],
64 (char**)(process->argv),
68 simcall_process_create(&process_created, (char*)(process->argv)[0], parse_code, NULL, sg_host_name(host), kill_time, process->argc,
69 (char**)process->argv, current_property_set,auto_restart);
71 /* verify if process has been created (won't be the case if the host is currently dead, but that's fine) */
72 if (!process_created) {
76 current_property_set = NULL;
79 void SIMIX_init_application(void){
80 surf_parse_reset_callbacks();
81 sg_platf_process_add_cb(parse_process);
85 * \brief An application deployer.
87 * Creates the process described in \a file.
88 * \param file a filename of a xml description of the application. This file
91 * \include surfxml.dtd
93 * Here is a small example of such a platform
95 * \include small_deployment.xml
98 void SIMIX_launch_application(const char *file)
100 _XBT_GNUC_UNUSED int parse_status;
101 xbt_assert(simix_global,
102 "SIMIX_global_init has to be called before SIMIX_launch_application.");
104 SIMIX_init_application();
106 surf_parse_open(file);
108 parse_status = surf_parse();
110 xbt_assert(!parse_status, "Parse error at %s:%d", file,surf_parse_lineno);
113 XBT_ERROR("Unrecoverable error at %s:%d. The full exception stack follows, in case it helps you to diagnose the problem.",
114 file, surf_parse_lineno);
120 * \brief Registers a #smx_process_code_t code in a global table.
122 * Registers a code function in a global table.
123 * This table is then used by #SIMIX_launch_application.
124 * \param name the reference name of the function.
125 * \param code the function
127 XBT_INLINE void SIMIX_function_register(const char *name,
128 xbt_main_func_t code)
130 xbt_assert(simix_global,
131 "SIMIX_global_init has to be called before SIMIX_function_register.");
133 xbt_dict_set(simix_global->registered_functions, name, code, NULL);
136 static xbt_main_func_t default_function = NULL;
138 * \brief Registers a #smx_process_code_t code as default value.
140 * 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.
141 * \param code the function
143 void SIMIX_function_register_default(xbt_main_func_t code)
145 xbt_assert(simix_global,
146 "SIMIX_global_init has to be called before SIMIX_function_register.");
148 default_function = code;
152 * \brief Gets a #smx_process_t code from the global table.
154 * Gets a code function from the global table. Returns NULL if there are no function registered with the name.
155 * This table is then used by #SIMIX_launch_application.
156 * \param name the reference name of the function.
157 * \return The #smx_process_t or NULL.
159 xbt_main_func_t SIMIX_get_registered_function(const char *name)
161 xbt_main_func_t res = NULL;
162 xbt_assert(simix_global,
163 "SIMIX_global_init has to be called before SIMIX_get_registered_function.");
165 res = (xbt_main_func_t)xbt_dict_get_or_null(simix_global->registered_functions, name);
166 return res ? res : default_function;
171 * \brief Bypass the parser, get arguments, and set function to each process
174 void SIMIX_process_set_function(const char *process_host,
175 const char *process_function,
176 xbt_dynar_t arguments,
177 double process_start_time,
178 double process_kill_time)
180 s_sg_platf_process_cbarg_t process;
181 memset(&process,0,sizeof(process));
183 smx_host_t host = SIMIX_host_get_by_name(process_host);
185 THROWF(arg_error, 0, "Host '%s' unknown", process_host);
186 process.host = sg_host_name(host);
188 process.argc = 1 + xbt_dynar_length(arguments);
189 process.argv = (const char**)xbt_new(char *, process.argc + 1);
190 process.argv[0] = xbt_strdup(process_function);
194 xbt_dynar_foreach(arguments, i, arg) {
195 process.argv[i + 1] = xbt_strdup(arg);
197 process.argv[process.argc] = NULL;
199 parse_code = SIMIX_get_registered_function(process_function);
200 xbt_assert(parse_code, "Function '%s' unknown", process_function);
201 process.function = process_function;
203 process.host = process_host;
204 process.kill_time = process_kill_time;
205 process.start_time = process_start_time;
206 process.on_failure = SURF_PROCESS_ON_FAILURE_DIE;
207 process.properties = NULL;
209 sg_platf_new_process(&process);