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 void SIMIX_init_application(void)
20 surf_parse_reset_callbacks();
24 * \brief An application deployer.
26 * Creates the process described in \a file.
27 * \param file a filename of a xml description of the application. This file
30 * \include surfxml.dtd
32 * Here is a small example of such a platform
34 * \include small_deployment.xml
37 void SIMIX_launch_application(const char *file)
39 XBT_ATTRIB_UNUSED int parse_status;
40 xbt_assert(simix_global,
41 "SIMIX_global_init has to be called before SIMIX_launch_application.");
43 SIMIX_init_application();
45 surf_parse_open(file);
47 parse_status = surf_parse();
49 xbt_assert(!parse_status, "Parse error at %s:%d", file,surf_parse_lineno);
52 XBT_ERROR("Unrecoverable error at %s:%d. The full exception stack follows, in case it helps you to diagnose the problem.",
53 file, surf_parse_lineno);
59 * \brief Registers a #smx_process_code_t code in a global table.
61 * Registers a code function in a global table.
62 * This table is then used by #SIMIX_launch_application.
63 * \param name the reference name of the function.
64 * \param code the function
66 void SIMIX_function_register(const char *name,
69 xbt_assert(simix_global,
70 "SIMIX_global_init has to be called before SIMIX_function_register.");
71 xbt_dict_set(simix_global->registered_functions, name, (void*) code, NULL);
74 static xbt_main_func_t default_function = NULL;
76 * \brief Registers a #smx_process_code_t code as default value.
78 * 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.
79 * \param code the function
81 void SIMIX_function_register_default(xbt_main_func_t code)
83 xbt_assert(simix_global,
84 "SIMIX_global_init has to be called before SIMIX_function_register.");
86 default_function = code;
90 * \brief Gets a #smx_process_t code from the global table.
92 * Gets a code function from the global table. Returns NULL if there are no function registered with the name.
93 * This table is then used by #SIMIX_launch_application.
94 * \param name the reference name of the function.
95 * \return The #smx_process_t or NULL.
97 xbt_main_func_t SIMIX_get_registered_function(const char *name)
99 xbt_main_func_t res = NULL;
100 xbt_assert(simix_global,
101 "SIMIX_global_init has to be called before SIMIX_get_registered_function.");
103 res = (xbt_main_func_t)xbt_dict_get_or_null(simix_global->registered_functions, name);
104 return res ? res : default_function;
109 * \brief Bypass the parser, get arguments, and set function to each process
112 void SIMIX_process_set_function(const char *process_host,
113 const char *process_function,
114 xbt_dynar_t arguments,
115 double process_start_time,
116 double process_kill_time)
118 s_sg_platf_process_cbarg_t process = SG_PLATF_PROCESS_INITIALIZER;
120 sg_host_t host = sg_host_by_name(process_host);
122 THROWF(arg_error, 0, "Host '%s' unknown", process_host);
123 process.host = sg_host_get_name(host);
125 process.argc = 1 + xbt_dynar_length(arguments);
126 process.argv = (const char**)xbt_new(char *, process.argc + 1);
127 process.argv[0] = xbt_strdup(process_function);
131 xbt_dynar_foreach(arguments, i, arg) {
132 process.argv[i + 1] = xbt_strdup(arg);
134 process.argv[process.argc] = NULL;
136 xbt_main_func_t parse_code = SIMIX_get_registered_function(process_function);
137 xbt_assert(parse_code, "Function '%s' unknown", process_function);
138 process.function = process_function;
140 process.host = process_host;
141 process.kill_time = process_kill_time;
142 process.start_time = process_start_time;
144 sg_platf_new_process(&process);