Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
docker: fix the build of images for simgrid stable
[simgrid.git] / src / simix / smx_deployment.cpp
1 /* Copyright (c) 2007-2019. The SimGrid Team. All rights reserved.          */
2
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. */
5
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>
11
12 #include <string>
13 #include <vector>
14
15 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_deployment, simix, "Logging specific to SIMIX (deployment)");
16
17 extern int surf_parse_lineno;
18
19 void SIMIX_init_application()
20 {
21   sg_platf_exit();
22   sg_platf_init();
23 }
24
25 /**
26  * @brief An application deployer.
27  *
28  * Creates the process described in @a file.
29  * @param file a filename of a xml description of the application. This file
30  * follows this DTD :
31  *
32  *     @include surfxml.dtd
33  *
34  * Here is a small example of such a platform
35  *
36  *     @include small_deployment.xml
37  *
38  */
39 void SIMIX_launch_application(const std::string& file)
40 {
41   XBT_ATTRIB_UNUSED int parse_status;
42   xbt_assert(simix_global, "SIMIX_global_init has to be called before SIMIX_launch_application.");
43
44   SIMIX_init_application();
45
46   surf_parse_open(file);
47   try {
48     parse_status = surf_parse();
49     surf_parse_close();
50     xbt_assert(not parse_status, "Parse error at %s:%d", file.c_str(), surf_parse_lineno);
51   } catch (const xbt_ex&) {
52     XBT_ERROR(
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);
55     throw;
56   }
57 }
58
59 void SIMIX_launch_application(const char* file) // deprecated
60 {
61   simgrid_load_deployment(file);
62 }
63
64 // Wrap a main() function into a ActorCodeFactory:
65 static simgrid::simix::ActorCodeFactory toActorCodeFactory(xbt_main_func_t code)
66 {
67   return [code](std::vector<std::string> args) { return simgrid::xbt::wrap_main(code, std::move(args)); };
68 }
69 static simgrid::simix::ActorCodeFactory toActorCodeFactory(void (*code)(std::vector<std::string>))
70 {
71   return [code](std::vector<std::string> args) { return std::bind(std::move(code), std::move(args)); };
72 }
73
74 /**
75  * @brief Registers a #xbt_main_func_t code in a global table.
76  *
77  * Registers a code function in a global table.
78  * This table is then used by #SIMIX_launch_application.
79  * @param name the reference name of the function.
80  * @param code the function
81  */
82 void SIMIX_function_register(const std::string& name, xbt_main_func_t code)
83 {
84   simix_global->registered_functions[name] = toActorCodeFactory(code);
85 }
86 void SIMIX_function_register(const std::string& name, void (*code)(std::vector<std::string>))
87 {
88   simix_global->registered_functions[name] = toActorCodeFactory(code);
89 }
90
91 void SIMIX_function_register(const char* name, xbt_main_func_t code) // deprecated
92 {
93   simgrid_register_function(name, code);
94 }
95
96 /**
97  * @brief Registers a #xbt_main_func_t code as default value.
98  *
99  * Registers a code function as being the default value. This function will get used by SIMIX_launch_application() when
100  * there is no registered function of the requested name in.
101  * @param code the function
102  */
103 void SIMIX_function_register_default(xbt_main_func_t code)
104 {
105   xbt_assert(simix_global, "SIMIX_global_init has to be called before SIMIX_function_register.");
106   simix_global->default_function = toActorCodeFactory(code);
107 }
108
109 /**
110  * @brief Gets a #smx_actor_t code from the global table.
111  *
112  * Gets a code function from the global table. Returns nullptr if there are no function registered with the name.
113  * This table is then used by #SIMIX_launch_application.
114  * @param name the reference name of the function.
115  * @return The #smx_actor_t or nullptr.
116  */
117 simgrid::simix::ActorCodeFactory& SIMIX_get_actor_code_factory(const std::string& name)
118 {
119   xbt_assert(simix_global,
120               "SIMIX_global_init has to be called before SIMIX_get_actor_code_factory.");
121
122   auto i = simix_global->registered_functions.find(name);
123   if (i == simix_global->registered_functions.end())
124     return simix_global->default_function;
125   else
126     return i->second;
127 }
128
129 /** @brief Bypass the parser, get arguments, and set function to each process */
130
131 void SIMIX_process_set_function(const char* process_host, const char* process_function, xbt_dynar_t arguments,
132                                 double process_start_time, double process_kill_time)
133 {
134   simgrid::kernel::routing::ActorCreationArgs actor;
135
136   sg_host_t host = sg_host_by_name(process_host);
137   if (not host)
138     THROWF(arg_error, 0, "Host '%s' unknown", process_host);
139   actor.host = process_host;
140   actor.args.push_back(process_function);
141   /* add arguments */
142   unsigned int i;
143   char *arg;
144   xbt_dynar_foreach(arguments, i, arg) {
145     actor.args.push_back(arg);
146   }
147
148   // Check we know how to handle this function name:
149   simgrid::simix::ActorCodeFactory& parse_code = SIMIX_get_actor_code_factory(process_function);
150   xbt_assert(parse_code, "Function '%s' unknown", process_function);
151
152   actor.function   = process_function;
153   actor.host       = process_host;
154   actor.kill_time  = process_kill_time;
155   actor.start_time = process_start_time;
156   actor.on_failure = simgrid::kernel::routing::ActorOnFailure::DIE;
157   sg_platf_new_actor(&actor);
158 }
159
160 namespace simgrid {
161 namespace simix {
162
163 void register_function(const std::string& name, const ActorCodeFactory& factory)
164 {
165   simix_global->registered_functions[name] = factory;
166 }
167
168 }
169 }