Logo AND Algorithmique Numérique Distribuée

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