Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
cosmetics
[simgrid.git] / src / simix / smx_deployment.c
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 "surf/surfxml_parse.h"
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   surf_parse_reset_callbacks();
21 }
22
23 /**
24  * \brief An application deployer.
25  *
26  * Creates the process described in \a file.
27  * \param file a filename of a xml description of the application. This file
28  * follows this DTD :
29  *
30  *     \include surfxml.dtd
31  *
32  * Here is a small example of such a platform
33  *
34  *     \include small_deployment.xml
35  *
36  */
37 void SIMIX_launch_application(const char *file)
38 {
39   XBT_ATTRIB_UNUSED int parse_status;
40   xbt_assert(simix_global,
41               "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(!parse_status, "Parse error at %s:%d", file,surf_parse_lineno);
50   }
51   CATCH_ANONYMOUS {
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     RETHROW;
55   }
56 }
57
58 /**
59  * \brief Registers a #smx_process_code_t code in a global table.
60  *
61  * Registers a code function in a global table.
62  * This table is then used by #SIMIX_launch_application.
63  * \param name the reference name of the function.
64  * \param code the function
65  */
66 XBT_INLINE void SIMIX_function_register(const char *name,
67                                         xbt_main_func_t code)
68 {
69   xbt_assert(simix_global,
70               "SIMIX_global_init has to be called before SIMIX_function_register.");
71
72   xbt_dict_set(simix_global->registered_functions, name, code, NULL);
73 }
74
75 static xbt_main_func_t default_function = NULL;
76 /**
77  * \brief Registers a #smx_process_code_t code as default value.
78  *
79  * 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.
80  * \param code the function
81  */
82 void SIMIX_function_register_default(xbt_main_func_t code)
83 {
84   xbt_assert(simix_global,
85               "SIMIX_global_init has to be called before SIMIX_function_register.");
86
87   default_function = code;
88 }
89
90 /**
91  * \brief Gets a #smx_process_t code from the global table.
92  *
93  * Gets a code function from the global table. Returns NULL if there are no function registered with the name.
94  * This table is then used by #SIMIX_launch_application.
95  * \param name the reference name of the function.
96  * \return The #smx_process_t or NULL.
97  */
98 xbt_main_func_t SIMIX_get_registered_function(const char *name)
99 {
100   xbt_main_func_t res = NULL;
101   xbt_assert(simix_global,
102               "SIMIX_global_init has to be called before SIMIX_get_registered_function.");
103
104   res = (xbt_main_func_t)xbt_dict_get_or_null(simix_global->registered_functions, name);
105   return res ? res : default_function;
106 }
107
108
109 /**
110  * \brief Bypass the parser, get arguments, and set function to each process
111  */
112
113 void SIMIX_process_set_function(const char *process_host,
114                                 const char *process_function,
115                                 xbt_dynar_t arguments,
116                                 double process_start_time,
117                                 double process_kill_time)
118 {
119   s_sg_platf_process_cbarg_t process = SG_PLATF_PROCESS_INITIALIZER;
120
121   sg_host_t host = sg_host_by_name(process_host);
122   if (!host)
123     THROWF(arg_error, 0, "Host '%s' unknown", process_host);
124   process.host = sg_host_get_name(host);
125
126   process.argc = 1 + xbt_dynar_length(arguments);
127   process.argv = (const char**)xbt_new(char *, process.argc + 1);
128   process.argv[0] = xbt_strdup(process_function);
129   /* add arguments */
130   unsigned int i;
131   char *arg;
132   xbt_dynar_foreach(arguments, i, arg) {
133     process.argv[i + 1] = xbt_strdup(arg);
134   }
135   process.argv[process.argc] = NULL;
136
137   xbt_main_func_t parse_code = SIMIX_get_registered_function(process_function);
138   xbt_assert(parse_code, "Function '%s' unknown", process_function);
139   process.function = process_function;
140
141   process.host = process_host;
142   process.kill_time = process_kill_time;
143   process.start_time = process_start_time;
144
145   sg_platf_new_process(&process);
146 }