Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Reverting Mt's modifications and making use of the brand new SURF timer.
[simgrid.git] / src / msg / deployment.c
1 /*      $Id$     */
2
3 /* Copyright (c) 2002,2003,2004 Arnaud Legrand. All rights reserved.        */
4
5 /* This program is free software; you can redistribute it and/or modify it
6  * under the terms of the license (GNU LGPL) which comes with this package. */
7
8 #include"private.h"
9 #include"xbt/sysdep.h"
10 #include "xbt/error.h"
11 #include "surf/surf_parse.h"
12 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(deployment, msg,
13                                 "Logging specific to MSG (environment)");
14
15 static int parse_argc = -1 ;
16 static char **parse_argv = NULL;
17 static m_process_code_t parse_code = NULL;
18 static m_host_t 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 = MSG_get_host_by_name(A_process_host);
25   xbt_assert1(parse_host, "Unknown host %s",A_process_host);
26   parse_code = MSG_get_registered_function(A_process_function);
27   xbt_assert1(parse_code, "Unknown function %s",A_process_function);
28   parse_argc = 0 ;
29   parse_argv = NULL;
30   parse_argc++;
31   parse_argv = xbt_realloc(parse_argv, (parse_argc) * sizeof(char *));
32   parse_argv[(parse_argc) - 1] = xbt_strdup(A_process_function);
33   surf_parse_get_double(&start_time,A_process_start_time);
34   surf_parse_get_double(&kill_time,A_process_kill_time);
35 }
36
37 static void parse_argument(void)
38 {
39   parse_argc++;
40   parse_argv = xbt_realloc(parse_argv, (parse_argc) * sizeof(char *));
41   parse_argv[(parse_argc) - 1] = xbt_strdup(A_argument_value);
42 }
43
44 static void parse_process_finalize(void)
45 {
46   process_arg_t arg = NULL;
47   m_process_t process = NULL;
48   if(start_time>MSG_get_clock()) {
49     arg = xbt_new0(s_process_arg_t,1);
50     arg->name = parse_argv[0];
51     arg->code = parse_code;
52     arg->data = NULL;
53     arg->host = parse_host;
54     arg->argc = parse_argc;
55     arg->argv = parse_argv;
56     arg-> kill_time = kill_time;
57
58     surf_timer_resource->extension_public->set(start_time, (void*) &MSG_process_create_with_arguments,
59                                                arg);
60   }
61   if(start_time==MSG_get_clock()) {
62     process = MSG_process_create_with_arguments(parse_argv[0], parse_code, 
63                                                 NULL, parse_host,
64                                                 parse_argc,parse_argv);
65     if(kill_time > MSG_getClock()) {
66       surf_timer_resource->extension_public->set(kill_time, 
67                                                  (void*) &MSG_process_kill,
68                                                  (void*) process);
69     }
70   }
71 }
72
73 /** \ingroup msg_easier_life
74  * \brief An application deployer.
75  *
76  * Creates the process described in \a file.
77  * \param file a filename of a xml description of the application. This file 
78  * follows this DTD :
79  *
80  *     \include surfxml.dtd
81  *
82  * Here is a small example of such a platform 
83  *
84  *     \include small_deployment.xml
85  *
86  * Have a look in the directory examples/msg/ to have a bigger example.
87  */
88 void MSG_launch_application(const char *file) 
89 {
90   xbt_assert0(msg_global,"MSG_global_init_args has to be called before MSG_launch_application.");
91   STag_process_fun = parse_process_init;
92   ETag_argument_fun = parse_argument;
93   ETag_process_fun = parse_process_finalize;
94   surf_parse_open(file);
95   xbt_assert1((!surf_parse()),"Parse error in %s",file);
96   surf_parse_close();
97 }
98
99 /** \ingroup msg_easier_life
100  * \brief Registers a #m_process_code_t code in a global table.
101  *
102  * Registers a code function in a global table. 
103  * This table is then used by #MSG_launch_application. 
104  * \param name the reference name of the function.
105  * \param code the function
106  */
107 void MSG_function_register(const char *name,m_process_code_t code)
108 {
109   xbt_assert0(msg_global,"MSG_global_init_args has to be called before MSG_function_register.");
110
111   xbt_dict_set(msg_global->registered_functions,name,code,NULL);
112 }
113
114 /** \ingroup msg_easier_life
115  * \brief Registers a #m_process_t code in a global table.
116  *
117  * Registers a code function in a global table. 
118  * This table is then used by #MSG_launch_application. 
119  * \param name the reference name of the function.
120  */
121 m_process_code_t MSG_get_registered_function(const char *name)
122 {
123   m_process_code_t code = NULL;
124
125   xbt_assert0(msg_global,"MSG_global_init_args has to be called before MSG_get_registered_function.");
126  
127   xbt_dict_get(msg_global->registered_functions,name,(void **) &code);
128
129   return code;
130 }
131
132 /** \ingroup msg_easier_life
133  * \brief Get the arguments of the current process.
134  * \deprecated{Not useful since #m_process_code_t is int (*)(int argc, char *argv[])}
135  *
136  * This functions returns the values set for the current process 
137  * using #MSG_set_arguments or #MSG_launch_application.
138  * \param argc the number of arguments
139  * \param argv the arguments table
140  */
141 MSG_error_t MSG_get_arguments(int *argc, char ***argv)
142 {
143   m_process_t process = MSG_process_self();
144   simdata_process_t simdata = NULL;
145
146   xbt_assert0((argc) && (argv), "Invalid parameters");
147   simdata = process->simdata;
148   *argc = simdata->argc;
149   *argv = simdata->argv;
150
151   return MSG_OK;
152 }
153
154 /** \ingroup msg_easier_life
155  * \brief Set the arguments of a process.
156  *
157  * This functions sets the argument number and the arguments table for a
158  * proces.
159  * \param process is the process you want to modify
160  * \param argc the number of arguments
161  * \param argv the arguments table
162  */
163 MSG_error_t MSG_set_arguments(m_process_t process,int argc, char *argv[])
164 {
165   simdata_process_t simdata = NULL;
166
167   xbt_assert0(0,"Deprecated ! Do not use anymore. "
168               "Use MSG_process_create_with_arguments instead.\n");
169
170   return MSG_OK;
171 }
172