Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
hopefully remove leak in permanent receive mode
[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 /**
80  * \brief An application deployer.
81  *
82  * Creates the process described in \a file.
83  * \param file a filename of a xml description of the application. This file
84  * follows this DTD :
85  *
86  *     \include surfxml.dtd
87  *
88  * Here is a small example of such a platform
89  *
90  *     \include small_deployment.xml
91  *
92  */
93 void SIMIX_launch_application(const char *file)
94 {
95   xbt_ex_t e;
96
97   _XBT_GNUC_UNUSED int parse_status;
98   xbt_assert(simix_global,
99               "SIMIX_global_init has to be called before SIMIX_launch_application.");
100
101   surf_parse_reset_callbacks();
102
103   sg_platf_process_add_cb(parse_process);
104
105   surf_parse_open(file);
106   TRY {
107     parse_status = surf_parse();
108     surf_parse_close();
109     xbt_assert(!parse_status, "Parse error at %s:%d", file,surf_parse_lineno);
110   } CATCH(e) {
111     XBT_ERROR("Unrecoverable error at %s:%d. The full exception stack follows, in case it helps you to diagnose the problem.",
112         file, surf_parse_lineno);
113     RETHROW;
114   }
115 }
116
117 /**
118  * \brief Registers a #smx_process_code_t code in a global table.
119  *
120  * Registers a code function in a global table.
121  * This table is then used by #SIMIX_launch_application.
122  * \param name the reference name of the function.
123  * \param code the function
124  */
125 XBT_INLINE void SIMIX_function_register(const char *name,
126                                         xbt_main_func_t code)
127 {
128   xbt_assert(simix_global,
129               "SIMIX_global_init has to be called before SIMIX_function_register.");
130
131   xbt_dict_set(simix_global->registered_functions, name, code, NULL);
132 }
133
134 static xbt_main_func_t default_function = NULL;
135 /**
136  * \brief Registers a #smx_process_code_t code as default value.
137  *
138  * 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.
139  * \param code the function
140  */
141 void SIMIX_function_register_default(xbt_main_func_t code)
142 {
143   xbt_assert(simix_global,
144               "SIMIX_global_init has to be called before SIMIX_function_register.");
145
146   default_function = code;
147 }
148
149 /**
150  * \brief Gets a #smx_process_t code from the global table.
151  *
152  * Gets a code function from the global table. Returns NULL if there are no function registered with the name.
153  * This table is then used by #SIMIX_launch_application.
154  * \param name the reference name of the function.
155  * \return The #smx_process_t or NULL.
156  */
157 xbt_main_func_t SIMIX_get_registered_function(const char *name)
158 {
159   xbt_main_func_t res = NULL;
160   xbt_assert(simix_global,
161               "SIMIX_global_init has to be called before SIMIX_get_registered_function.");
162
163   res = (xbt_main_func_t)xbt_dict_get_or_null(simix_global->registered_functions, name);
164   return res ? res : default_function;
165 }
166
167
168 /**
169  * \brief Bypass the parser, get arguments, and set function to each process
170  */
171
172 void SIMIX_process_set_function(const char *process_host,
173                                 const char *process_function,
174                                 xbt_dynar_t arguments,
175                                 double process_start_time,
176                                 double process_kill_time)
177 {
178   s_sg_platf_process_cbarg_t process;
179   memset(&process,0,sizeof(process));
180
181   smx_host_t host = SIMIX_host_get_by_name(process_host);
182   if (!host)
183     THROWF(arg_error, 0, "Host '%s' unknown", process_host);
184   process.host = host->name;
185
186   process.argc = 1 + xbt_dynar_length(arguments);
187   process.argv = (const char**)xbt_new(char *, process.argc + 1);
188   process.argv[0] = xbt_strdup(process_function);
189   /* add arguments */
190   unsigned int i;
191   char *arg;
192   xbt_dynar_foreach(arguments, i, arg) {
193     process.argv[i + 1] = xbt_strdup(arg);
194   }
195   process.argv[process.argc] = NULL;
196
197   parse_code = SIMIX_get_registered_function(process_function);
198   xbt_assert(parse_code, "Function '%s' unknown", process_function);
199   process.function = process_function;
200
201   process.host = process_host;
202   process.kill_time = process_kill_time;
203   process.start_time = process_start_time;
204   process.on_failure = SURF_PROCESS_ON_FAILURE_DIE;
205   process.properties = NULL;
206
207   sg_platf_new_process(&process);
208 }