Logo AND Algorithmique Numérique Distribuée

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