Logo AND Algorithmique Numérique Distribuée

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