3 /* Copyright (c) 2007 Arnaud Legrand, Bruno Donassolo.
4 All rights reserved. */
6 /* This program is free software; you can redistribute it and/or modify it
7 * under the terms of the license (GNU LGPL) which comes with this package. */
11 #include "xbt/sysdep.h"
14 #include "surf/surfxml_parse_private.h"
16 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_deployment, simix,
17 "Logging specific to SIMIX (deployment)");
18 static int parse_argc = -1;
19 static char **parse_argv = NULL;
20 static xbt_main_func_t parse_code = NULL;
21 static char *parse_host = NULL;
22 static double start_time = 0.0;
23 static double kill_time = -1.0;
25 static void parse_process_init(void)
27 parse_host = xbt_strdup(A_surfxml_process_host);
28 xbt_assert1(SIMIX_host_get_by_name(parse_host),
29 "Host '%s' unknown", parse_host);
30 parse_code = SIMIX_get_registered_function(A_surfxml_process_function);
31 xbt_assert1(parse_code, "Function '%s' unknown",
32 A_surfxml_process_function);
36 parse_argv = xbt_realloc(parse_argv, (parse_argc) * sizeof(char *));
37 parse_argv[(parse_argc) - 1] = xbt_strdup(A_surfxml_process_function);
38 surf_parse_get_double(&start_time, A_surfxml_process_start_time);
39 surf_parse_get_double(&kill_time, A_surfxml_process_kill_time);
41 current_property_set = xbt_dict_new();
44 static void parse_argument(void)
47 parse_argv = xbt_realloc(parse_argv, (parse_argc) * sizeof(char *));
48 parse_argv[(parse_argc) - 1] = xbt_strdup(A_surfxml_argument_value);
51 static void parse_process_finalize(void)
53 smx_process_arg_t arg = NULL;
55 if (start_time > SIMIX_get_clock()) {
56 arg = xbt_new0(s_smx_process_arg_t, 1);
57 arg->name = parse_argv[0];
58 arg->code = parse_code;
60 arg->hostname = parse_host;
61 arg->argc = parse_argc;
62 arg->argv = parse_argv;
63 arg->kill_time = kill_time;
64 arg->properties = current_property_set;
66 DEBUG3("Process %s(%s) will be started at time %f", arg->name,
67 arg->hostname, start_time);
68 if (simix_global->create_process_function)
69 surf_timer_model->extension.timer.set(start_time, (void *)
70 simix_global->create_process_function,
73 surf_timer_model->extension.timer.set(start_time, (void *)
74 &SIMIX_process_create, arg);
76 } else { // start_time <= SIMIX_get_clock()
77 DEBUG2("Starting Process %s(%s) right now", parse_argv[0], parse_host);
79 if (simix_global->create_process_function)
81 (*simix_global->create_process_function) (parse_argv[0], parse_code,
83 parse_argc, parse_argv,
85 current_property_set);
87 process = SIMIX_process_create(parse_argv[0], parse_code, NULL, parse_host, parse_argc, parse_argv, /*the props */
88 current_property_set);
89 /* verify if process has been created (won't be the case if the host is currently dead, but that's fine) */
94 if (kill_time > SIMIX_get_clock()) {
95 if (simix_global->kill_process_function)
96 surf_timer_model->extension.timer.set(start_time, (void *)
97 simix_global->kill_process_function,
100 surf_timer_model->extension.timer.set(kill_time, (void *)
104 xbt_free(parse_host);
109 * \brief An application deployer.
111 * Creates the process described in \a file.
112 * \param file a filename of a xml description of the application. This file
115 * \include surfxml.dtd
117 * Here is a small example of such a platform
119 * \include small_deployment.xml
122 void SIMIX_launch_application(const char *file)
124 xbt_assert0(simix_global,
125 "SIMIX_global_init has to be called before SIMIX_launch_application.");
126 surf_parse_reset_parser();
127 surfxml_add_callback(STag_surfxml_process_cb_list, parse_process_init);
128 surfxml_add_callback(ETag_surfxml_argument_cb_list, parse_argument);
129 surfxml_add_callback(STag_surfxml_prop_cb_list, parse_properties);
130 surfxml_add_callback(ETag_surfxml_process_cb_list, parse_process_finalize);
132 surf_parse_open(file);
133 xbt_assert1((!surf_parse()), "Parse error in %s", file);
138 * \brief Registers a #smx_process_code_t code in a global table.
140 * Registers a code function in a global table.
141 * This table is then used by #SIMIX_launch_application.
142 * \param name the reference name of the function.
143 * \param code the function
145 XBT_INLINE void SIMIX_function_register(const char *name, xbt_main_func_t code)
147 xbt_assert0(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_assert0(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_assert0(simix_global,
179 "SIMIX_global_init has to be called before SIMIX_get_registered_function.");
181 xbt_main_func_t res =
182 xbt_dict_get_or_null(simix_global->registered_functions, name);
183 return res ? res : default_function;