Logo AND Algorithmique Numérique Distribuée

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