Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[simix] Make some static variables local
[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 static void parse_process(sg_platf_process_cbarg_t process)
19 {
20   sg_host_t host = sg_host_by_name(process->host);
21   if (!host)
22     THROWF(arg_error, 0, "Host '%s' unknown", process->host);
23   xbt_main_func_t parse_code = SIMIX_get_registered_function(process->function);
24   xbt_assert(parse_code, "Function '%s' unknown", process->function);
25
26   double start_time = process->start_time;
27   double kill_time  = process->kill_time;
28   int auto_restart = process->on_failure == SURF_PROCESS_ON_FAILURE_DIE ? 0 : 1;
29
30   smx_process_arg_t arg = NULL;
31   smx_process_t process_created = NULL;
32
33   arg = xbt_new0(s_smx_process_arg_t, 1);
34   arg->code = parse_code;
35   arg->data = NULL;
36   arg->hostname = sg_host_get_name(host);
37   arg->argc = process->argc;
38   arg->argv = xbt_new(char *,process->argc);
39   int i;
40   for (i=0; i<process->argc; i++)
41     arg->argv[i] = xbt_strdup(process->argv[i]);
42   arg->name = xbt_strdup(arg->argv[0]);
43   arg->kill_time = kill_time;
44   arg->properties = current_property_set;
45   if (!sg_host_simix(host)->boot_processes) {
46     sg_host_simix(host)->boot_processes = xbt_dynar_new(sizeof(smx_process_arg_t), _SIMIX_host_free_process_arg);
47   }
48   xbt_dynar_push_as(sg_host_simix(host)->boot_processes,smx_process_arg_t,arg);
49
50   if (start_time > SIMIX_get_clock()) {
51     arg = xbt_new0(s_smx_process_arg_t, 1);
52     arg->name = (char*)(process->argv)[0];
53     arg->code = parse_code;
54     arg->data = NULL;
55     arg->hostname = sg_host_get_name(host);
56     arg->argc = process->argc;
57     arg->argv = (char**)(process->argv);
58     arg->kill_time = kill_time;
59     arg->properties = current_property_set;
60
61     XBT_DEBUG("Process %s(%s) will be started at time %f", arg->name,
62            arg->hostname, start_time);
63     SIMIX_timer_set(start_time, &SIMIX_process_create_from_wrapper, arg);
64   } else {                      // start_time <= SIMIX_get_clock()
65     XBT_DEBUG("Starting Process %s(%s) right now", process->argv[0], sg_host_get_name(host));
66
67     if (simix_global->create_process_function)
68       process_created = simix_global->create_process_function(
69                                             (char*)(process->argv)[0],
70                                             parse_code,
71                                             NULL,
72                                             sg_host_get_name(host),
73                                             kill_time,
74                                             process->argc,
75                                             (char**)(process->argv),
76                                             current_property_set,
77                                             auto_restart, NULL);
78     else
79       process_created = simcall_process_create((char*)(process->argv)[0], parse_code, NULL, sg_host_get_name(host), kill_time, process->argc,
80           (char**)process->argv, current_property_set,auto_restart);
81     
82     /* verify if process has been created (won't be the case if the host is currently dead, but that's fine) */
83     if (!process_created) {
84       return;
85     }
86   }
87   current_property_set = NULL;
88 }
89
90 void SIMIX_init_application(void){
91   surf_parse_reset_callbacks();
92   sg_platf_process_add_cb(parse_process);
93 }
94
95 /**
96  * \brief An application deployer.
97  *
98  * Creates the process described in \a file.
99  * \param file a filename of a xml description of the application. This file
100  * follows this DTD :
101  *
102  *     \include surfxml.dtd
103  *
104  * Here is a small example of such a platform
105  *
106  *     \include small_deployment.xml
107  *
108  */
109 void SIMIX_launch_application(const char *file)
110 {
111   XBT_ATTRIB_UNUSED int parse_status;
112   xbt_assert(simix_global,
113               "SIMIX_global_init has to be called before SIMIX_launch_application.");
114
115   SIMIX_init_application();
116
117   surf_parse_open(file);
118   TRY {
119     parse_status = surf_parse();
120     surf_parse_close();
121     xbt_assert(!parse_status, "Parse error at %s:%d", file,surf_parse_lineno);
122   }
123   CATCH_ANONYMOUS {
124     XBT_ERROR("Unrecoverable error at %s:%d. The full exception stack follows, in case it helps you to diagnose the problem.",
125         file, surf_parse_lineno);
126     RETHROW;
127   }
128 }
129
130 /**
131  * \brief Registers a #smx_process_code_t code in a global table.
132  *
133  * Registers a code function in a global table.
134  * This table is then used by #SIMIX_launch_application.
135  * \param name the reference name of the function.
136  * \param code the function
137  */
138 XBT_INLINE void SIMIX_function_register(const char *name,
139                                         xbt_main_func_t code)
140 {
141   xbt_assert(simix_global,
142               "SIMIX_global_init has to be called before SIMIX_function_register.");
143
144   xbt_dict_set(simix_global->registered_functions, name, code, NULL);
145 }
146
147 static xbt_main_func_t default_function = NULL;
148 /**
149  * \brief Registers a #smx_process_code_t code as default value.
150  *
151  * 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.
152  * \param code the function
153  */
154 void SIMIX_function_register_default(xbt_main_func_t code)
155 {
156   xbt_assert(simix_global,
157               "SIMIX_global_init has to be called before SIMIX_function_register.");
158
159   default_function = code;
160 }
161
162 /**
163  * \brief Gets a #smx_process_t code from the global table.
164  *
165  * Gets a code function from the global table. Returns NULL if there are no function registered with the name.
166  * This table is then used by #SIMIX_launch_application.
167  * \param name the reference name of the function.
168  * \return The #smx_process_t or NULL.
169  */
170 xbt_main_func_t SIMIX_get_registered_function(const char *name)
171 {
172   xbt_main_func_t res = NULL;
173   xbt_assert(simix_global,
174               "SIMIX_global_init has to be called before SIMIX_get_registered_function.");
175
176   res = (xbt_main_func_t)xbt_dict_get_or_null(simix_global->registered_functions, name);
177   return res ? res : default_function;
178 }
179
180
181 /**
182  * \brief Bypass the parser, get arguments, and set function to each process
183  */
184
185 void SIMIX_process_set_function(const char *process_host,
186                                 const char *process_function,
187                                 xbt_dynar_t arguments,
188                                 double process_start_time,
189                                 double process_kill_time)
190 {
191   s_sg_platf_process_cbarg_t process = SG_PLATF_PROCESS_INITIALIZER;
192
193   sg_host_t host = sg_host_by_name(process_host);
194   if (!host)
195     THROWF(arg_error, 0, "Host '%s' unknown", process_host);
196   process.host = sg_host_get_name(host);
197
198   process.argc = 1 + xbt_dynar_length(arguments);
199   process.argv = (const char**)xbt_new(char *, process.argc + 1);
200   process.argv[0] = xbt_strdup(process_function);
201   /* add arguments */
202   unsigned int i;
203   char *arg;
204   xbt_dynar_foreach(arguments, i, arg) {
205     process.argv[i + 1] = xbt_strdup(arg);
206   }
207   process.argv[process.argc] = NULL;
208
209   xbt_main_func_t parse_code = SIMIX_get_registered_function(process_function);
210   xbt_assert(parse_code, "Function '%s' unknown", process_function);
211   process.function = process_function;
212
213   process.host = process_host;
214   process.kill_time = process_kill_time;
215   process.start_time = process_start_time;
216
217   sg_platf_new_process(&process);
218 }