Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Take into account that some process may actually not be launched because the host...
[simgrid.git] / src / simix / smx_deployment.c
1 /*      $Id$     */
2
3 /* Copyright (c) 2007 Arnaud Legrand, Bruno Donassolo.
4    All rights reserved.                                          */
5
6 /* This program is free software; you can redistribute it and/or modify it
7  * under the terms of the license (GNU LGPL) which comes with this package. */
8
9
10 #include "private.h"
11 #include "xbt/sysdep.h"
12 #include "xbt/log.h"
13 #include "xbt/dict.h"
14 #include "surf/surfxml_parse_private.h"
15
16 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_deployment, simix,
17                                 "Logging specific to SIMIX (deployment)");
18 static int parse_argc = -1;
19 static char **parse_argv = NULL;
20 static xbt_main_func_t parse_code = NULL;
21 static char *parse_host = NULL;
22 static double start_time = 0.0;
23 static double kill_time = -1.0;
24
25 static void parse_process_init(void)
26 {
27   parse_host = xbt_strdup(A_surfxml_process_host);
28   xbt_assert1(SIMIX_host_get_by_name(parse_host),
29               "Host '%s' unknown", parse_host);
30   parse_code = SIMIX_get_registered_function(A_surfxml_process_function);
31   xbt_assert1(parse_code, "Function '%s' unknown",
32               A_surfxml_process_function);
33   parse_argc = 0;
34   parse_argv = NULL;
35   parse_argc++;
36   parse_argv = xbt_realloc(parse_argv, (parse_argc) * sizeof(char *));
37   parse_argv[(parse_argc) - 1] = xbt_strdup(A_surfxml_process_function);
38   surf_parse_get_double(&start_time, A_surfxml_process_start_time);
39   surf_parse_get_double(&kill_time, A_surfxml_process_kill_time);
40
41   current_property_set = xbt_dict_new();
42 }
43
44 static void parse_argument(void)
45 {
46   parse_argc++;
47   parse_argv = xbt_realloc(parse_argv, (parse_argc) * sizeof(char *));
48   parse_argv[(parse_argc) - 1] = xbt_strdup(A_surfxml_argument_value);
49 }
50
51 static void parse_process_finalize(void)
52 {
53   smx_process_arg_t arg = NULL;
54   void *process = NULL;
55   if (start_time > SIMIX_get_clock()) {
56     arg = xbt_new0(s_smx_process_arg_t, 1);
57     arg->name = parse_argv[0];
58     arg->code = parse_code;
59     arg->data = NULL;
60     arg->hostname = parse_host;
61     arg->argc = parse_argc;
62     arg->argv = parse_argv;
63     arg->kill_time = kill_time;
64     arg->properties = current_property_set;
65
66     DEBUG3("Process %s(%s) will be started at time %f", arg->name,
67            arg->hostname, start_time);
68     if (simix_global->create_process_function)
69       surf_timer_model->extension_public->set(start_time, (void *)
70                                               simix_global->create_process_function,
71                                               arg);
72     else
73       surf_timer_model->extension_public->set(start_time, (void *)
74                                               &SIMIX_process_create, arg);
75
76   }
77   if ((start_time < 0) || (start_time == SIMIX_get_clock())) {
78     DEBUG2("Starting Process %s(%s) right now", parse_argv[0], parse_host);
79
80     if (simix_global->create_process_function)
81       process =
82         (*simix_global->create_process_function) (parse_argv[0], parse_code,
83                                                   NULL, parse_host,
84                                                   parse_argc, parse_argv,
85                                                   /*the props */
86                                                   current_property_set);
87     else
88       process = SIMIX_process_create(parse_argv[0], parse_code, NULL, parse_host, parse_argc, parse_argv,       /*the props */
89                                      current_property_set);
90
91     if (process && kill_time > SIMIX_get_clock()) {
92       if (simix_global->kill_process_function)
93         surf_timer_model->extension_public->set(start_time, (void *) 
94                                                 simix_global->kill_process_function,
95                                                 process);
96       else
97         surf_timer_model->extension_public->set(kill_time, (void *)
98                                                 &SIMIX_process_kill,
99                                                 (void *) process);
100     }
101     xbt_free(parse_host);
102   }
103 }
104
105 /** 
106  * \brief An application deployer.
107  *
108  * Creates the process described in \a file.
109  * \param file a filename of a xml description of the application. This file 
110  * follows this DTD :
111  *
112  *     \include surfxml.dtd
113  *
114  * Here is a small example of such a platform 
115  *
116  *     \include small_deployment.xml
117  *
118  */
119 void SIMIX_launch_application(const char *file)
120 {
121   xbt_assert0(simix_global,
122               "SIMIX_global_init has to be called before SIMIX_launch_application.");
123   surf_parse_reset_parser();
124   surfxml_add_callback(STag_surfxml_process_cb_list, parse_process_init);
125   surfxml_add_callback(ETag_surfxml_argument_cb_list, parse_argument);
126   surfxml_add_callback(STag_surfxml_prop_cb_list, parse_properties);
127   surfxml_add_callback(ETag_surfxml_process_cb_list, parse_process_finalize);
128
129   surf_parse_open(file);
130   xbt_assert1((!surf_parse()), "Parse error in %s", file);
131   surf_parse_close();
132 }
133
134 /**
135  * \brief Registers a #smx_process_code_t code in a global table.
136  *
137  * Registers a code function in a global table. 
138  * This table is then used by #SIMIX_launch_application. 
139  * \param name the reference name of the function.
140  * \param code the function
141  */
142 void SIMIX_function_register(const char *name, xbt_main_func_t code)
143 {
144   xbt_assert0(simix_global,
145               "SIMIX_global_init has to be called before SIMIX_function_register.");
146
147   xbt_dict_set(simix_global->registered_functions, name, code, NULL);
148 }
149
150 static xbt_main_func_t default_function = NULL;
151 /**
152  * \brief Registers a #smx_process_code_t code as default value.
153  *
154  * 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.
155  * \param code the function
156  */
157 void SIMIX_function_register_default(xbt_main_func_t code)
158 {
159   xbt_assert0(simix_global,
160               "SIMIX_global_init has to be called before SIMIX_function_register.");
161
162   default_function = code;
163 }
164
165 /**
166  * \brief Gets a #smx_process_t code from the global table.
167  *
168  * Gets a code function from the global table. Returns NULL if there are no function registered with the name.
169  * This table is then used by #SIMIX_launch_application. 
170  * \param name the reference name of the function.
171  * \return The #smx_process_t or NULL.
172  */
173 xbt_main_func_t SIMIX_get_registered_function(const char *name)
174 {
175   xbt_assert0(simix_global,
176               "SIMIX_global_init has to be called before SIMIX_get_registered_function.");
177
178   xbt_main_func_t res =
179     xbt_dict_get_or_null(simix_global->registered_functions, name);
180   return res ? res : default_function;
181 }