Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
4a21d8c67a12b634194fa3b7238bce8e36cb8417
[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   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
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   int parse_status;
108   xbt_assert(simix_global,
109               "SIMIX_global_init has to be called before SIMIX_launch_application.");
110
111   // Reset callbacks
112   surf_parse_reset_callbacks();
113
114   surfxml_add_callback(STag_surfxml_process_cb_list, parse_process_init);
115   surfxml_add_callback(ETag_surfxml_argument_cb_list, parse_argument);
116   surfxml_add_callback(STag_surfxml_prop_cb_list, parse_properties_XML);
117   surfxml_add_callback(ETag_surfxml_process_cb_list,
118                        parse_process_finalize);
119
120   surf_parse_open(file);
121   parse_status = surf_parse();
122   surf_parse_close();
123   xbt_assert(!parse_status, "Parse error in %s", file);
124 }
125
126 /**
127  * \brief Registers a #smx_process_code_t code in a global table.
128  *
129  * Registers a code function in a global table.
130  * This table is then used by #SIMIX_launch_application.
131  * \param name the reference name of the function.
132  * \param code the function
133  */
134 XBT_INLINE void SIMIX_function_register(const char *name,
135                                         xbt_main_func_t code)
136 {
137   xbt_assert(simix_global,
138               "SIMIX_global_init has to be called before SIMIX_function_register.");
139
140   xbt_dict_set(simix_global->registered_functions, name, code, NULL);
141 }
142
143 static xbt_main_func_t default_function = NULL;
144 /**
145  * \brief Registers a #smx_process_code_t code as default value.
146  *
147  * 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.
148  * \param code the function
149  */
150 void SIMIX_function_register_default(xbt_main_func_t code)
151 {
152   xbt_assert(simix_global,
153               "SIMIX_global_init has to be called before SIMIX_function_register.");
154
155   default_function = code;
156 }
157
158 /**
159  * \brief Gets a #smx_process_t code from the global table.
160  *
161  * Gets a code function from the global table. Returns NULL if there are no function registered with the name.
162  * This table is then used by #SIMIX_launch_application.
163  * \param name the reference name of the function.
164  * \return The #smx_process_t or NULL.
165  */
166 xbt_main_func_t SIMIX_get_registered_function(const char *name)
167 {
168   xbt_main_func_t res = NULL;
169   xbt_assert(simix_global,
170               "SIMIX_global_init has to be called before SIMIX_get_registered_function.");
171
172   res = xbt_dict_get_or_null(simix_global->registered_functions, name);
173   return res ? res : default_function;
174 }
175
176
177 /**
178  * \brief Bypass the parser, get arguments, and set function to each process
179  */
180
181 void SIMIX_process_set_function(const char *process_host,
182                                 const char *process_function,
183                                 xbt_dynar_t arguments,
184                                 double process_start_time,
185                                 double process_kill_time)
186 {
187   unsigned int i;
188   char *arg;
189
190   /* init process */
191   smx_host_t host = SIMIX_host_get_by_name(process_host);
192   if (!host)
193     THROWF(arg_error, 0, "Host '%s' unknown", process_host);
194   parse_host = host->name;
195   parse_code = SIMIX_get_registered_function(process_function);
196   xbt_assert(parse_code, "Function '%s' unknown", process_function);
197
198   parse_argc = 0;
199   parse_argv = NULL;
200   parse_argc++;
201   parse_argv = xbt_realloc(parse_argv, (parse_argc) * sizeof(char *));
202   parse_argv[(parse_argc) - 1] = xbt_strdup(process_function);
203   start_time = process_start_time;
204   kill_time = process_kill_time;
205
206   /* add arguments */
207   xbt_dynar_foreach(arguments, i, arg) {
208     parse_argc++;
209     parse_argv = xbt_realloc(parse_argv, (parse_argc) * sizeof(char *));
210     parse_argv[(parse_argc) - 1] = xbt_strdup(arg);
211   }
212
213   /* finalize */
214   parse_process_finalize();
215 }