Logo AND Algorithmique Numérique Distribuée

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