1 /* Copyright (c) 2007-2018. The SimGrid Team. All rights reserved. */
3 /* This program is free software; you can redistribute it and/or modify it
4 * under the terms of the license (GNU LGPL) which comes with this package. */
9 #include "simgrid/s4u/Host.hpp"
10 #include "smx_private.hpp"
11 #include "src/surf/xml/platf_private.hpp" // FIXME: KILLME. There must be a better way than mimicking XML here
14 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_deployment, simix, "Logging specific to SIMIX (deployment)");
16 extern int surf_parse_lineno;
18 void SIMIX_init_application()
25 * \brief An application deployer.
27 * Creates the process described in \a file.
28 * \param file a filename of a xml description of the application. This file
31 * \include surfxml.dtd
33 * Here is a small example of such a platform
35 * \include small_deployment.xml
38 void SIMIX_launch_application(const char *file)
40 XBT_ATTRIB_UNUSED int parse_status;
41 xbt_assert(simix_global, "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(not 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);
58 // Wrap a main() function into a ActorCodeFactory:
59 static simgrid::simix::ActorCodeFactory toActorCodeFactory(xbt_main_func_t code)
61 return [code](std::vector<std::string> args) { return simgrid::xbt::wrap_main(code, std::move(args)); };
65 * \brief Registers a #xbt_main_func_t code in a global table.
67 * Registers a code function in a global table.
68 * This table is then used by #SIMIX_launch_application.
69 * \param name the reference name of the function.
70 * \param code the function
72 void SIMIX_function_register(const char *name, xbt_main_func_t code)
74 xbt_assert(simix_global,
75 "SIMIX_global_init has to be called before SIMIX_function_register.");
76 simix_global->registered_functions[name] = toActorCodeFactory(code);
80 * \brief Registers a #xbt_main_func_t code as default value.
82 * 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.
83 * \param code the function
85 void SIMIX_function_register_default(xbt_main_func_t code)
87 xbt_assert(simix_global, "SIMIX_global_init has to be called before SIMIX_function_register.");
88 simix_global->default_function = toActorCodeFactory(code);
92 * \brief Gets a #smx_actor_t code from the global table.
94 * Gets a code function from the global table. Returns nullptr if there are no function registered with the name.
95 * This table is then used by #SIMIX_launch_application.
96 * \param name the reference name of the function.
97 * \return The #smx_actor_t or nullptr.
99 simgrid::simix::ActorCodeFactory& SIMIX_get_actor_code_factory(const char *name)
101 xbt_assert(simix_global,
102 "SIMIX_global_init has to be called before SIMIX_get_actor_code_factory.");
104 auto i = simix_global->registered_functions.find(name);
105 if (i == simix_global->registered_functions.end())
106 return simix_global->default_function;
112 * \brief Bypass the parser, get arguments, and set function to each process
115 void SIMIX_process_set_function(const char* process_host, const char* process_function, xbt_dynar_t arguments,
116 double process_start_time, double process_kill_time)
118 simgrid::kernel::routing::ActorCreationArgs actor;
120 sg_host_t host = sg_host_by_name(process_host);
122 THROWF(arg_error, 0, "Host '%s' unknown", process_host);
123 actor.host = process_host;
124 actor.args.push_back(process_function);
128 xbt_dynar_foreach(arguments, i, arg) {
129 actor.args.push_back(arg);
132 // Check we know how to handle this function name:
133 simgrid::simix::ActorCodeFactory& parse_code = SIMIX_get_actor_code_factory(process_function);
134 xbt_assert(parse_code, "Function '%s' unknown", process_function);
136 actor.function = process_function;
137 actor.host = process_host;
138 actor.kill_time = process_kill_time;
139 actor.start_time = process_start_time;
140 actor.on_failure = simgrid::kernel::routing::ActorOnFailure::DIE;
141 sg_platf_new_actor(&actor);
147 void register_function(const char* name, ActorCodeFactory factory)
149 simix_global->registered_functions[name] = std::move(factory);