Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
dd0680734b72f0ee284363733df6c92aa2695379
[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 smx_process_code_t parse_code = NULL;
20 static smx_host_t 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 = SIMIX_get_host_by_name(A_surfxml_process_host);
27   xbt_assert1(parse_host, "Unknown host %s",A_surfxml_process_host);
28   parse_code = SIMIX_get_registered_function(A_surfxml_process_function);
29   xbt_assert1(parse_code, "Unknown function %s",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
39 static void parse_argument(void)
40 {
41   parse_argc++;
42   parse_argv = xbt_realloc(parse_argv, (parse_argc) * sizeof(char *));
43   parse_argv[(parse_argc) - 1] = xbt_strdup(A_surfxml_argument_value);
44 }
45
46 static void parse_process_finalize(void)
47 {
48   process_arg_t arg = NULL;
49   smx_process_t process = NULL;
50   if(start_time>SIMIX_get_clock()) {
51     arg = xbt_new0(s_process_arg_t,1);
52     arg->name = parse_argv[0];
53     arg->code = parse_code;
54     arg->data = NULL;
55     arg->host = parse_host;
56     arg->argc = parse_argc;
57     arg->argv = parse_argv;
58     arg->kill_time = kill_time;
59
60     DEBUG3("Process %s(%s) will be started at time %f", arg->name, 
61            arg->host->name,start_time);
62     surf_timer_resource->extension_public->set(start_time, (void*) &SIMIX_process_create_with_arguments,
63                                                arg);
64   }
65   if((start_time<0) || (start_time==SIMIX_get_clock())) {
66     DEBUG2("Starting Process %s(%s) right now", parse_argv[0],
67            parse_host->name);
68     process = SIMIX_process_create_with_arguments(parse_argv[0], parse_code, 
69                                                 NULL, parse_host,
70                                                 parse_argc,parse_argv);
71     if(kill_time > SIMIX_get_clock()) {
72       surf_timer_resource->extension_public->set(kill_time, 
73                                                  (void*) &SIMIX_process_kill,
74                                                  (void*) process);
75     }
76   }
77 }
78
79 /** \ingroup msg_easier_life
80  * \brief An application deployer.
81  *
82  * Creates the process described in \a file.
83  * \param file a filename of a xml description of the application. This file 
84  * follows this DTD :
85  *
86  *     \include surfxml.dtd
87  *
88  * Here is a small example of such a platform 
89  *
90  *     \include small_deployment.xml
91  *
92  * Have a look in the directory examples/msg/ to have a bigger example.
93  */
94 void SIMIX_launch_application(const char *file) 
95 {
96   xbt_assert0(simix_global,"SIMIX_global_init_args has to be called before SIMIX_launch_application.");
97   STag_surfxml_process_fun = parse_process_init;
98   ETag_surfxml_argument_fun = parse_argument;
99   ETag_surfxml_process_fun = parse_process_finalize;
100   surf_parse_open(file);
101   xbt_assert1((!surf_parse()),"Parse error in %s",file);
102   surf_parse_close();
103 }
104
105 /** \ingroup msg_easier_life
106  * \brief Registers a #m_process_code_t code in a global table.
107  *
108  * Registers a code function in a global table. 
109  * This table is then used by #MSG_launch_application. 
110  * \param name the reference name of the function.
111  * \param code the function
112  */
113 void SIMIX_function_register(const char *name,smx_process_code_t code)
114 {
115   xbt_assert0(simix_global,"SIMIX_global_init has to be called before SIMIX_function_register.");
116
117   xbt_dict_set(simix_global->registered_functions,name,code,NULL);
118 }
119
120 /** \ingroup msg_easier_life
121  * \brief Registers a #m_process_t code in a global table.
122  *
123  * Registers a code function in a global table. 
124  * This table is then used by #MSG_launch_application. 
125  * \param name the reference name of the function.
126  */
127 smx_process_code_t SIMIX_get_registered_function(const char *name)
128 {
129   smx_process_code_t code = NULL;
130
131   xbt_assert0(simix_global,"SIMIX_global_init has to be called before SIMIX_get_registered_function.");
132
133   code = xbt_dict_get_or_null(simix_global->registered_functions,name);
134
135   return code;
136 }
137