Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Reindent!
[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 "surf/surfxml_parse_private.h"
14
15 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_deployment, simix,
16                                 "Logging specific to SIMIX (deployment)");
17 static int parse_argc = -1;
18 static char **parse_argv = NULL;
19 static xbt_main_func_t parse_code = NULL;
20 static char *parse_host = NULL;
21 static double start_time = 0.0;
22 static double kill_time = -1.0;
23
24 static void parse_process_init(void)
25 {
26   parse_host = xbt_strdup(A_surfxml_process_host);
27   xbt_assert1(SIMIX_host_get_by_name(A_surfxml_process_host),
28               "Unknown host %s", A_surfxml_process_host);
29   parse_code = SIMIX_get_registered_function(A_surfxml_process_function);
30   xbt_assert1(parse_code, "Unknown function %s",
31               A_surfxml_process_function);
32   parse_argc = 0;
33   parse_argv = NULL;
34   parse_argc++;
35   parse_argv = xbt_realloc(parse_argv, (parse_argc) * sizeof(char *));
36   parse_argv[(parse_argc) - 1] = xbt_strdup(A_surfxml_process_function);
37   surf_parse_get_double(&start_time, A_surfxml_process_start_time);
38   surf_parse_get_double(&kill_time, A_surfxml_process_kill_time);
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
62     DEBUG3("Process %s(%s) will be started at time %f", arg->name,
63            arg->hostname, start_time);
64     if (simix_global->create_process_function)
65       surf_timer_resource->extension_public->set(start_time,
66                                                  (void *) simix_global->
67                                                  create_process_function,
68                                                  arg);
69     else
70       surf_timer_resource->extension_public->set(start_time,
71                                                  (void *)
72                                                  &SIMIX_process_create,
73                                                  arg);
74
75   }
76   if ((start_time < 0) || (start_time == SIMIX_get_clock())) {
77     DEBUG2("Starting Process %s(%s) right now", parse_argv[0], parse_host);
78
79     if (simix_global->create_process_function)
80       process =
81           simix_global->create_process_function(parse_argv[0], parse_code,
82                                                 NULL, parse_host,
83                                                 parse_argc, parse_argv);
84     else
85       process =
86           SIMIX_process_create(parse_argv[0], parse_code, NULL, parse_host,
87                                parse_argc, parse_argv);
88
89     if (kill_time > SIMIX_get_clock()) {
90       if (simix_global->kill_process_function)
91         surf_timer_resource->extension_public->set(start_time,
92                                                    (void *) simix_global->
93                                                    kill_process_function,
94                                                    arg);
95       else
96         surf_timer_resource->extension_public->set(kill_time,
97                                                    (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   STag_surfxml_process_fun = parse_process_init;
124   ETag_surfxml_argument_fun = parse_argument;
125   ETag_surfxml_process_fun = parse_process_finalize;
126   surf_parse_open(file);
127   xbt_assert1((!surf_parse()), "Parse error in %s", file);
128   surf_parse_close();
129 }
130
131 /**
132  * \brief Registers a #smx_process_code_t code in a global table.
133  *
134  * Registers a code function in a global table. 
135  * This table is then used by #SIMIX_launch_application. 
136  * \param name the reference name of the function.
137  * \param code the function
138  */
139 void SIMIX_function_register(const char *name, 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 /**
148  * \brief Gets a #smx_process_t code from the global table.
149  *
150  * Gets a code function from the global table. Returns NULL if there are no function registered with the name.
151  * This table is then used by #SIMIX_launch_application. 
152  * \param name the reference name of the function.
153  * \return The #smx_process_t or NULL.
154  */
155 xbt_main_func_t SIMIX_get_registered_function(const char *name)
156 {
157   xbt_assert0(simix_global,
158               "SIMIX_global_init has to be called before SIMIX_get_registered_function.");
159
160   return xbt_dict_get_or_null(simix_global->registered_functions, name);
161 }