Logo AND Algorithmique Numérique Distribuée

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