Logo AND Algorithmique Numérique Distribuée

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