Logo AND Algorithmique Numérique Distribuée

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