1 /* Copyright (c) 2007-2019. 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. */
6 #include "simgrid/Exception.hpp"
7 #include "simgrid/s4u/Host.hpp"
8 #include "smx_private.hpp"
9 #include "src/surf/xml/platf_private.hpp" // FIXME: KILLME. There must be a better way than mimicking XML here
10 #include <simgrid/engine.h>
15 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_deployment, simix, "Logging specific to SIMIX (deployment)");
17 extern int surf_parse_lineno;
19 void SIMIX_init_application()
26 * @brief An application deployer.
28 * Creates the process described in @a file.
29 * @param file a filename of a xml description of the application. This file
32 * @include surfxml.dtd
34 * Here is a small example of such a platform
36 * @include small_deployment.xml
39 void SIMIX_launch_application(const std::string& file)
41 XBT_ATTRIB_UNUSED int parse_status;
42 xbt_assert(simix_global, "SIMIX_global_init has to be called before SIMIX_launch_application.");
44 SIMIX_init_application();
46 surf_parse_open(file);
48 parse_status = surf_parse();
50 xbt_assert(not parse_status, "Parse error at %s:%d", file.c_str(), surf_parse_lineno);
51 } catch (const xbt_ex&) {
53 "Unrecoverable error at %s:%d. The full exception stack follows, in case it helps you to diagnose the problem.",
54 file.c_str(), surf_parse_lineno);
59 // Wrap a main() function into a ActorCodeFactory:
60 static simgrid::simix::ActorCodeFactory toActorCodeFactory(xbt_main_func_t code)
62 return [code](std::vector<std::string> args) { return simgrid::xbt::wrap_main(code, std::move(args)); };
64 static simgrid::simix::ActorCodeFactory toActorCodeFactory(void (*code)(std::vector<std::string>))
66 return [code](std::vector<std::string> args) { return std::bind(std::move(code), std::move(args)); };
70 * @brief Registers a #xbt_main_func_t code in a global table.
72 * Registers a code function in a global table.
73 * This table is then used by #SIMIX_launch_application.
74 * @param name the reference name of the function.
75 * @param code the function
77 void SIMIX_function_register(const std::string& name, xbt_main_func_t code)
79 simix_global->registered_functions[name] = toActorCodeFactory(code);
81 void SIMIX_function_register(const std::string& name, void (*code)(std::vector<std::string>))
83 simix_global->registered_functions[name] = toActorCodeFactory(code);
87 * @brief Registers a #xbt_main_func_t code as default value.
89 * Registers a code function as being the default value. This function will get used by SIMIX_launch_application() when
90 * there is no registered function of the requested name in.
91 * @param code the function
93 void SIMIX_function_register_default(xbt_main_func_t code)
95 xbt_assert(simix_global, "SIMIX_global_init has to be called before SIMIX_function_register.");
96 simix_global->default_function = toActorCodeFactory(code);
100 * @brief Gets a #smx_actor_t code from the global table.
102 * Gets a code function from the global table. Returns nullptr if there are no function registered with the name.
103 * This table is then used by #SIMIX_launch_application.
104 * @param name the reference name of the function.
105 * @return The #smx_actor_t or nullptr.
107 simgrid::simix::ActorCodeFactory& SIMIX_get_actor_code_factory(const std::string& name)
109 xbt_assert(simix_global,
110 "SIMIX_global_init has to be called before SIMIX_get_actor_code_factory.");
112 auto i = simix_global->registered_functions.find(name);
113 if (i == simix_global->registered_functions.end())
114 return simix_global->default_function;
119 /** @brief Bypass the parser, get arguments, and set function to each process */
121 void SIMIX_process_set_function(const char* process_host, const char* process_function, xbt_dynar_t arguments,
122 double process_start_time, double process_kill_time)
124 simgrid::kernel::routing::ActorCreationArgs actor;
126 sg_host_t host = sg_host_by_name(process_host);
128 THROWF(arg_error, 0, "Host '%s' unknown", process_host);
129 actor.host = process_host;
130 actor.args.push_back(process_function);
134 xbt_dynar_foreach(arguments, i, arg) {
135 actor.args.push_back(arg);
138 // Check we know how to handle this function name:
139 simgrid::simix::ActorCodeFactory& parse_code = SIMIX_get_actor_code_factory(process_function);
140 xbt_assert(parse_code, "Function '%s' unknown", process_function);
142 actor.function = process_function;
143 actor.host = process_host;
144 actor.kill_time = process_kill_time;
145 actor.start_time = process_start_time;
146 actor.on_failure = simgrid::kernel::routing::ActorOnFailure::DIE;
147 sg_platf_new_actor(&actor);
153 void register_function(const std::string& name, const ActorCodeFactory& factory)
155 simix_global->registered_functions[name] = factory;