Logo AND Algorithmique Numérique Distribuée

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