Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
avoid any name clashes with msg/private.h so that the java interface can do crude...
[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 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), "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   smx_process_arg_t arg = NULL;
49   void * process = NULL;
50   if(start_time>SIMIX_get_clock()) {
51     arg = xbt_new0(s_smx_process_arg_t,1);
52     arg->name = parse_argv[0];
53     arg->code = parse_code;
54     arg->data = NULL;
55     arg->hostname = 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->hostname,start_time);
62                  if (simix_global->create_process_function)
63                          surf_timer_resource->extension_public->set(start_time, (void*) simix_global->create_process_function, arg);
64                  else
65                          surf_timer_resource->extension_public->set(start_time, (void*) &SIMIX_process_create, arg);
66
67   }
68   if((start_time<0) || (start_time==SIMIX_get_clock())) {
69     DEBUG2("Starting Process %s(%s) right now", parse_argv[0], parse_host);
70
71                 if (simix_global->create_process_function)
72                         process = simix_global->create_process_function(parse_argv[0], parse_code, NULL, parse_host,    parse_argc,parse_argv);
73                 else
74                         process = SIMIX_process_create(parse_argv[0], parse_code, NULL, parse_host,     parse_argc,parse_argv, NULL);
75
76                 if(kill_time > SIMIX_get_clock()) {
77                         if (simix_global->kill_process_function)
78                                 surf_timer_resource->extension_public->set(start_time, (void*) simix_global->kill_process_function, arg);
79                         else
80                                 surf_timer_resource->extension_public->set(kill_time, (void*) &SIMIX_process_kill, (void*) process);
81                 }
82                 xbt_free(parse_host);
83   }
84 }
85
86 /** 
87  * \brief An application deployer.
88  *
89  * Creates the process described in \a file.
90  * \param file a filename of a xml description of the application. This file 
91  * follows this DTD :
92  *
93  *     \include surfxml.dtd
94  *
95  * Here is a small example of such a platform 
96  *
97  *     \include small_deployment.xml
98  *
99  */
100 void SIMIX_launch_application(const char *file) 
101 {
102   xbt_assert0(simix_global,"SIMIX_global_init has to be called before SIMIX_launch_application.");
103   STag_surfxml_process_fun = parse_process_init;
104   ETag_surfxml_argument_fun = parse_argument;
105   ETag_surfxml_process_fun = parse_process_finalize;
106   surf_parse_open(file);
107   xbt_assert1((!surf_parse()),"Parse error in %s",file);
108   surf_parse_close();
109 }
110
111 /**
112  * \brief Registers a #smx_process_code_t code in a global table.
113  *
114  * Registers a code function in a global table. 
115  * This table is then used by #SIMIX_launch_application. 
116  * \param name the reference name of the function.
117  * \param code the function
118  */
119 void SIMIX_function_register(const char *name,smx_process_code_t code)
120 {
121   xbt_assert0(simix_global,"SIMIX_global_init has to be called before SIMIX_function_register.");
122
123   xbt_dict_set(simix_global->registered_functions,name,code,NULL);
124 }
125
126 /**
127  * \brief Gets a #smx_process_t code from the global table.
128  *
129  * Gets a code function from the global table. Returns NULL if there are no function registered with the name.
130  * This table is then used by #SIMIX_launch_application. 
131  * \param name the reference name of the function.
132  * \return The #smx_process_t or NULL.
133  */
134 smx_process_code_t SIMIX_get_registered_function(const char *name)
135 {
136   smx_process_code_t code = NULL;
137
138   xbt_assert0(simix_global,"SIMIX_global_init has to be called before SIMIX_get_registered_function.");
139
140   code = xbt_dict_get_or_null(simix_global->registered_functions,name);
141
142   return code;
143 }
144