Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
9ebd945cb2b2cd8059a5a2f2c2fd76a626508b7c
[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 "private.h"
8 #include "xbt/sysdep.h"
9 #include "xbt/log.h"
10 #include "xbt/dict.h"
11 #include "surf/surfxml_parse_private.h"
12
13 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_deployment, simix,
14                                 "Logging specific to SIMIX (deployment)");
15 static int parse_argc = -1;
16 static char **parse_argv = NULL;
17 static xbt_main_func_t parse_code = NULL;
18 static char *parse_host = NULL;
19 static double start_time = 0.0;
20 static double kill_time = -1.0;
21
22 static void parse_process_init(void)
23 {
24   smx_host_t host = SIMIX_host_get_by_name(A_surfxml_process_host);
25   if (!host)
26     THROWF(arg_error, 0, "Host '%s' unknown", A_surfxml_process_host);
27   parse_host = host->name;
28   parse_code = SIMIX_get_registered_function(A_surfxml_process_function);
29   xbt_assert(parse_code, "Function '%s' unknown",
30               A_surfxml_process_function);
31   parse_argc = 0;
32   parse_argv = NULL;
33   parse_argc++;
34   parse_argv = xbt_realloc(parse_argv, (parse_argc) * sizeof(char *));
35   parse_argv[(parse_argc) - 1] = xbt_strdup(A_surfxml_process_function);
36   start_time= surf_parse_get_double(A_surfxml_process_start_time);
37   kill_time = surf_parse_get_double(A_surfxml_process_kill_time);
38 }
39
40 static void parse_argument(void)
41 {
42   parse_argc++;
43   parse_argv = xbt_realloc(parse_argv, (parse_argc) * sizeof(char *));
44   parse_argv[(parse_argc) - 1] = xbt_strdup(A_surfxml_argument_value);
45 }
46
47 static void parse_process_finalize(void)
48 {
49   smx_process_arg_t arg = NULL;
50   smx_process_t process = NULL;
51   if (start_time > SIMIX_get_clock()) {
52     arg = xbt_new0(s_smx_process_arg_t, 1);
53     arg->name = parse_argv[0];
54     arg->code = parse_code;
55     arg->data = NULL;
56     arg->hostname = parse_host;
57     arg->argc = parse_argc;
58     arg->argv = parse_argv;
59     arg->kill_time = kill_time;
60     arg->properties = current_property_set;
61
62     XBT_DEBUG("Process %s(%s) will be started at time %f", arg->name,
63            arg->hostname, start_time);
64     SIMIX_timer_set(start_time, &SIMIX_process_create_from_wrapper, arg);
65   } else {                      // start_time <= SIMIX_get_clock()
66     XBT_DEBUG("Starting Process %s(%s) right now", parse_argv[0], parse_host);
67
68     if (simix_global->create_process_function)
69       (*simix_global->create_process_function) (&process,
70                                                 parse_argv[0],
71                                                 parse_code, NULL,
72                                                 parse_host, parse_argc,
73                                                 parse_argv,
74                                                 current_property_set);
75     else
76       SIMIX_req_process_create(&process, parse_argv[0], parse_code, NULL, parse_host, parse_argc, parse_argv,
77                                current_property_set);
78     /* verify if process has been created (won't be the case if the host is currently dead, but that's fine) */
79     if (!process) {
80       return;
81     }
82     if (kill_time > SIMIX_get_clock()) {
83       if (simix_global->kill_process_function) {
84         SIMIX_timer_set(start_time, simix_global->kill_process_function, process);
85       }
86     }
87   }
88   current_property_set = NULL;
89 }
90
91 /**
92  * \brief An application deployer.
93  *
94  * Creates the process described in \a file.
95  * \param file a filename of a xml description of the application. This file
96  * follows this DTD :
97  *
98  *     \include surfxml.dtd
99  *
100  * Here is a small example of such a platform
101  *
102  *     \include small_deployment.xml
103  *
104  */
105 void SIMIX_launch_application(const char *file)
106 {
107   _XBT_GNUC_UNUSED int parse_status;
108   xbt_assert(simix_global,
109               "SIMIX_global_init has to be called before SIMIX_launch_application.");
110
111   surf_parse_reset_callbacks();
112
113   surfxml_add_callback(STag_surfxml_process_cb_list, parse_process_init);
114   surfxml_add_callback(ETag_surfxml_argument_cb_list, parse_argument);
115   surfxml_add_callback(STag_surfxml_prop_cb_list, parse_properties_XML);
116   surfxml_add_callback(ETag_surfxml_process_cb_list,
117                        parse_process_finalize);
118
119   surf_parse_open(file);
120   parse_status = surf_parse();
121   surf_parse_close();
122   xbt_assert(!parse_status, "Parse error in %s", file);
123 }
124
125 /**
126  * \brief Registers a #smx_process_code_t code in a global table.
127  *
128  * Registers a code function in a global table.
129  * This table is then used by #SIMIX_launch_application.
130  * \param name the reference name of the function.
131  * \param code the function
132  */
133 XBT_INLINE void SIMIX_function_register(const char *name,
134                                         xbt_main_func_t code)
135 {
136   xbt_assert(simix_global,
137               "SIMIX_global_init has to be called before SIMIX_function_register.");
138
139   xbt_dict_set(simix_global->registered_functions, name, code, NULL);
140 }
141
142 static xbt_main_func_t default_function = NULL;
143 /**
144  * \brief Registers a #smx_process_code_t code as default value.
145  *
146  * 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.
147  * \param code the function
148  */
149 void SIMIX_function_register_default(xbt_main_func_t code)
150 {
151   xbt_assert(simix_global,
152               "SIMIX_global_init has to be called before SIMIX_function_register.");
153
154   default_function = code;
155 }
156
157 /**
158  * \brief Gets a #smx_process_t code from the global table.
159  *
160  * Gets a code function from the global table. Returns NULL if there are no function registered with the name.
161  * This table is then used by #SIMIX_launch_application.
162  * \param name the reference name of the function.
163  * \return The #smx_process_t or NULL.
164  */
165 xbt_main_func_t SIMIX_get_registered_function(const char *name)
166 {
167   xbt_main_func_t res = NULL;
168   xbt_assert(simix_global,
169               "SIMIX_global_init has to be called before SIMIX_get_registered_function.");
170
171   res = xbt_dict_get_or_null(simix_global->registered_functions, name);
172   return res ? res : default_function;
173 }
174
175
176 /**
177  * \brief Bypass the parser, get arguments, and set function to each process
178  */
179
180 void SIMIX_process_set_function(const char *process_host,
181                                 const char *process_function,
182                                 xbt_dynar_t arguments,
183                                 double process_start_time,
184                                 double process_kill_time)
185 {
186   unsigned int i;
187   char *arg;
188
189   /* init process */
190   smx_host_t host = SIMIX_host_get_by_name(process_host);
191   if (!host)
192     THROWF(arg_error, 0, "Host '%s' unknown", process_host);
193   parse_host = host->name;
194   parse_code = SIMIX_get_registered_function(process_function);
195   xbt_assert(parse_code, "Function '%s' unknown", process_function);
196
197   parse_argc = 0;
198   parse_argv = NULL;
199   parse_argc++;
200   parse_argv = xbt_realloc(parse_argv, (parse_argc) * sizeof(char *));
201   parse_argv[(parse_argc) - 1] = xbt_strdup(process_function);
202   start_time = process_start_time;
203   kill_time = process_kill_time;
204
205   /* add arguments */
206   xbt_dynar_foreach(arguments, i, arg) {
207     parse_argc++;
208     parse_argv = xbt_realloc(parse_argv, (parse_argc) * sizeof(char *));
209     parse_argv[(parse_argc) - 1] = xbt_strdup(arg);
210   }
211
212   /* finalize */
213   parse_process_finalize();
214 }