Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
a740981cf3c91ab269bbad7d343f6bdd2bc1dd3f
[simgrid.git] / src / simix / smx_deployment.cpp
1 /* Copyright (c) 2007, 2009-2015. The SimGrid Team.
2  * All rights reserved.                                                     */
3
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. */
6
7 #include "smx_private.h"
8 #include "xbt/sysdep.h"
9 #include "xbt/log.h"
10 #include "xbt/dict.h"
11 #include "src/surf/xml/platf_private.hpp" // FIXME: KILLME. There must be a better way than mimicking XML here
12
13 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_deployment, simix,
14                                 "Logging specific to SIMIX (deployment)");
15
16 extern int surf_parse_lineno;
17
18 void SIMIX_init_application(void)
19 {
20   sg_platf_exit();
21   sg_platf_init();
22 }
23
24 /**
25  * \brief An application deployer.
26  *
27  * Creates the process described in \a file.
28  * \param file a filename of a xml description of the application. This file
29  * follows this DTD :
30  *
31  *     \include surfxml.dtd
32  *
33  * Here is a small example of such a platform
34  *
35  *     \include small_deployment.xml
36  *
37  */
38 void SIMIX_launch_application(const char *file)
39 {
40   XBT_ATTRIB_UNUSED int parse_status;
41   xbt_assert(simix_global,
42               "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(!parse_status, "Parse error at %s:%d", file,surf_parse_lineno);
51   }
52   catch (xbt_ex& e) {
53     XBT_ERROR("Unrecoverable error at %s:%d. The full exception stack follows, in case it helps you to diagnose the problem.",
54         file, surf_parse_lineno);
55     throw;
56   }
57 }
58
59 // Wrap a main() function into a ActorCodeFactory:
60 static simgrid::simix::ActorCodeFactory toActorCodeFactory(xbt_main_func_t code)
61 {
62   return [code](simgrid::xbt::args args) {
63     return simgrid::xbt::wrapMain(code, std::move(args));
64   };
65 }
66
67 /**
68  * \brief Registers a #xbt_main_func_t code in a global table.
69  *
70  * Registers a code function in a global table.
71  * This table is then used by #SIMIX_launch_application.
72  * \param name the reference name of the function.
73  * \param code the function
74  */
75 void SIMIX_function_register(const char *name, xbt_main_func_t code)
76 {
77   xbt_assert(simix_global,
78     "SIMIX_global_init has to be called before SIMIX_function_register.");
79   simix_global->registered_functions[name] = toActorCodeFactory(code);
80 }
81
82 /**
83  * \brief Registers a #xbt_main_func_t code as default value.
84  *
85  * 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.
86  * \param code the function
87  */
88 void SIMIX_function_register_default(xbt_main_func_t code)
89 {
90   xbt_assert(simix_global, "SIMIX_global_init has to be called before SIMIX_function_register.");
91   simix_global->default_function = toActorCodeFactory(code);
92 }
93
94 /**
95  * \brief Gets a #smx_process_t code from the global table.
96  *
97  * Gets a code function from the global table. Returns nullptr if there are no function registered with the name.
98  * This table is then used by #SIMIX_launch_application.
99  * \param name the reference name of the function.
100  * \return The #smx_process_t or nullptr.
101  */
102 simgrid::simix::ActorCodeFactory& SIMIX_get_actor_code_factory(const char *name)
103 {
104   xbt_assert(simix_global,
105               "SIMIX_global_init has to be called before SIMIX_get_actor_code_factory.");
106
107   auto i = simix_global->registered_functions.find(name);
108   if (i == simix_global->registered_functions.end())
109     return simix_global->default_function;
110   else
111     return i->second;
112 }
113
114 /**
115  * \brief Bypass the parser, get arguments, and set function to each process
116  */
117
118 void SIMIX_process_set_function(const char *process_host,
119                                 const char *process_function,
120                                 xbt_dynar_t arguments,
121                                 double process_start_time,
122                                 double process_kill_time)
123 {
124   s_sg_platf_process_cbarg_t process;
125   memset(&process,0,sizeof(process));
126
127   sg_host_t host = sg_host_by_name(process_host);
128   if (!host)
129     THROWF(arg_error, 0, "Host '%s' unknown", process_host);
130   process.host = sg_host_get_name(host);
131
132   process.argc = 1 + xbt_dynar_length(arguments);
133   process.argv = (const char**)xbt_new(char *, process.argc + 1);
134   process.argv[0] = xbt_strdup(process_function);
135   /* add arguments */
136   unsigned int i;
137   char *arg;
138   xbt_dynar_foreach(arguments, i, arg) {
139     process.argv[i + 1] = xbt_strdup(arg);
140   }
141   process.argv[process.argc] = nullptr;
142
143   // Check we know how to handle this function name:
144   simgrid::simix::ActorCodeFactory& parse_code = SIMIX_get_actor_code_factory(process_function);
145   xbt_assert(parse_code, "Function '%s' unknown", process_function);
146
147   process.function = process_function;
148   process.host = process_host;
149   process.kill_time = process_kill_time;
150   process.start_time = process_start_time;
151   process.on_failure = SURF_PROCESS_ON_FAILURE_DIE;
152   sg_platf_new_process(&process);
153 }
154
155 namespace simgrid {
156 namespace simix {
157
158 void registerFunction(const char* name, ActorCodeFactory factory)
159 {
160   simix_global->registered_functions[name] = std::move(factory);
161 }
162
163 }
164 }