Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
c5e1479c691aae2be3aeca8a04ef77df3d2e5f15
[simgrid.git] / src / surf / surfxml_parse.c
1 /* Copyright (c) 2006, 2007, 2008, 2009, 2010, 2011. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 #include "xbt/misc.h"
8 #include "xbt/log.h"
9 #include "xbt/str.h"
10 #include "xbt/dict.h"
11 #include "surf/surfxml_parse_private.h"
12 #include "surf/surf_private.h"
13
14 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_parse, surf,
15                                 "Logging specific to the SURF parsing module");
16 #undef CLEANUP
17 int ETag_surfxml_include_state(void);
18
19 #include "simgrid_dtd.c"
20
21 /*
22  * Helping functions
23  */
24 static void surf_parse_error(char *msg) {
25   xbt_die("Parse error on line %d: %s\n", surf_parse_lineno, msg);
26 }
27
28 void surf_parse_get_double(double *value, const char *string) {
29   int ret = sscanf(string, "%lg", value);
30   if (ret != 1)
31     surf_parse_error(bprintf("%s is not a double", string));
32 }
33
34 void surf_parse_get_int(int *value, const char *string) {
35   int ret = sscanf(string, "%d", value);
36   if (ret != 1)
37     surf_parse_error(bprintf("%s is not an integer", string));
38 }
39
40
41 /*
42  * All the callback lists that can be overiden anywhere.
43  * (this list should probably be reduced to the bare minimum to allow the models to work)
44  */
45
46 /* make sure these symbols are defined as strong ones in this file so that the linker can resolve them */
47 xbt_dynar_t STag_surfxml_platform_cb_list = NULL;
48 xbt_dynar_t ETag_surfxml_platform_cb_list = NULL;
49 xbt_dynar_t STag_surfxml_host_cb_list = NULL;
50 xbt_dynar_t ETag_surfxml_host_cb_list = NULL;
51 xbt_dynar_t STag_surfxml_router_cb_list = NULL;
52 xbt_dynar_t ETag_surfxml_router_cb_list = NULL;
53 xbt_dynar_t STag_surfxml_link_cb_list = NULL;
54 xbt_dynar_t ETag_surfxml_link_cb_list = NULL;
55 xbt_dynar_t STag_surfxml_route_cb_list = NULL;
56 xbt_dynar_t ETag_surfxml_route_cb_list = NULL;
57 xbt_dynar_t STag_surfxml_link_ctn_cb_list = NULL;
58 xbt_dynar_t ETag_surfxml_link_ctn_cb_list = NULL;
59 xbt_dynar_t STag_surfxml_process_cb_list = NULL;
60 xbt_dynar_t ETag_surfxml_process_cb_list = NULL;
61 xbt_dynar_t STag_surfxml_argument_cb_list = NULL;
62 xbt_dynar_t ETag_surfxml_argument_cb_list = NULL;
63 xbt_dynar_t STag_surfxml_prop_cb_list = NULL;
64 xbt_dynar_t ETag_surfxml_prop_cb_list = NULL;
65 xbt_dynar_t STag_surfxml_cluster_cb_list = NULL;
66 xbt_dynar_t ETag_surfxml_cluster_cb_list = NULL;
67 xbt_dynar_t STag_surfxml_peer_cb_list = NULL;
68 xbt_dynar_t ETag_surfxml_peer_cb_list = NULL;
69 xbt_dynar_t STag_surfxml_trace_cb_list = NULL;
70 xbt_dynar_t ETag_surfxml_trace_cb_list = NULL;
71 xbt_dynar_t STag_surfxml_trace_connect_cb_list = NULL;
72 xbt_dynar_t ETag_surfxml_trace_connect_cb_list = NULL;
73 xbt_dynar_t STag_surfxml_random_cb_list = NULL;
74 xbt_dynar_t ETag_surfxml_random_cb_list = NULL;
75 xbt_dynar_t STag_surfxml_AS_cb_list = NULL;
76 xbt_dynar_t ETag_surfxml_AS_cb_list = NULL;
77 xbt_dynar_t STag_surfxml_ASroute_cb_list = NULL;
78 xbt_dynar_t ETag_surfxml_ASroute_cb_list = NULL;
79 xbt_dynar_t STag_surfxml_bypassRoute_cb_list = NULL;
80 xbt_dynar_t ETag_surfxml_bypassRoute_cb_list = NULL;
81 xbt_dynar_t STag_surfxml_config_cb_list = NULL;
82 xbt_dynar_t ETag_surfxml_config_cb_list = NULL;
83 xbt_dynar_t STag_surfxml_include_cb_list = NULL;
84 xbt_dynar_t ETag_surfxml_include_cb_list = NULL;
85
86 /* The default current property receiver. Setup in the corresponding opening callbacks. */
87 xbt_dict_t current_property_set = NULL;
88 /* dictionary of random generator data */
89 xbt_dict_t random_data_list = NULL;
90
91 /* Call all the callbacks of a specific SAX event */
92 static XBT_INLINE void surfxml_call_cb_functions(xbt_dynar_t);
93
94 YY_BUFFER_STATE surf_input_buffer;
95 FILE *surf_file_to_parse = NULL;
96
97 static void parse_Stag_trace(void);
98 static void parse_Etag_trace(void);
99 static void parse_Stag_trace_connect(void);
100
101 static void init_randomness(void);
102 static void add_randomness(void);
103
104 /*
105  * Stuff relative to the <include> tag
106  */
107
108 static xbt_dynar_t surf_input_buffer_stack = NULL;
109 static xbt_dynar_t surf_file_to_parse_stack = NULL;
110
111 void STag_surfxml_include(void)
112 {
113   XBT_INFO("STag_surfxml_include '%s'",A_surfxml_include_file);
114   xbt_dynar_push(surf_file_to_parse_stack, &surf_file_to_parse); //save old filename
115
116   surf_file_to_parse = surf_fopen(A_surfxml_include_file, "r"); // read new filename
117   xbt_assert((surf_file_to_parse), "Unable to open \"%s\"\n",
118               A_surfxml_include_file);
119   xbt_dynar_push(surf_input_buffer_stack,&surf_input_buffer);
120   surf_input_buffer = surf_parse__create_buffer(surf_file_to_parse, YY_BUF_SIZE);
121   surf_parse_push_buffer_state(surf_input_buffer);
122   fflush(NULL);
123 }
124
125 void ETag_surfxml_include(void) {
126 /* Nothing to do when done with reading the include tag.
127  * Instead, the handling should be deferred until the EOF of current buffer -- see below */
128 }
129
130 /** @brief When reaching EOF, check whether we are in an include tag, and behave accordingly if yes
131  *
132  * This function is called automatically by sedding the parser in buildtools/Cmake/MaintainerMode.cmake
133  * Every FAIL on "Premature EOF" is preceded by a call to this function, which role is to restore the
134  * previous buffer if we reached the EOF /of an include file/. Its return code is used to avoid the
135  * error message in that case.
136  *
137  * Yeah, that's terribly hackish, but it works. A better solution should be dealed with in flexml
138  * directly: a command line flag could instruct it to do the correct thing when #include is encountered
139  * on a line.
140  */
141 int ETag_surfxml_include_state(void)
142 {
143   fflush(NULL);
144   XBT_INFO("ETag_surfxml_include_state '%s'",A_surfxml_include_file);
145   if(xbt_dynar_length(surf_input_buffer_stack)!= 0)
146           return 1;
147   fclose(surf_file_to_parse);
148   xbt_dynar_pop(surf_file_to_parse_stack, &surf_file_to_parse);
149   surf_parse_pop_buffer_state();
150   xbt_dynar_pop(surf_input_buffer_stack,surf_input_buffer);
151   return 0;
152 }
153
154
155 void surf_parse_init_callbacks(void)
156 {
157           STag_surfxml_platform_cb_list =
158               xbt_dynar_new(sizeof(void_f_void_t), NULL);
159           ETag_surfxml_platform_cb_list =
160               xbt_dynar_new(sizeof(void_f_void_t), NULL);
161           STag_surfxml_host_cb_list = xbt_dynar_new(sizeof(void_f_void_t), NULL);
162           ETag_surfxml_host_cb_list = xbt_dynar_new(sizeof(void_f_void_t), NULL);
163           STag_surfxml_router_cb_list = xbt_dynar_new(sizeof(void_f_void_t), NULL);
164           ETag_surfxml_router_cb_list = xbt_dynar_new(sizeof(void_f_void_t), NULL);
165           STag_surfxml_link_cb_list = xbt_dynar_new(sizeof(void_f_void_t), NULL);
166           ETag_surfxml_link_cb_list = xbt_dynar_new(sizeof(void_f_void_t), NULL);
167           STag_surfxml_route_cb_list = xbt_dynar_new(sizeof(void_f_void_t), NULL);
168           ETag_surfxml_route_cb_list = xbt_dynar_new(sizeof(void_f_void_t), NULL);
169           STag_surfxml_link_ctn_cb_list =
170               xbt_dynar_new(sizeof(void_f_void_t), NULL);
171           ETag_surfxml_link_ctn_cb_list =
172               xbt_dynar_new(sizeof(void_f_void_t), NULL);
173           STag_surfxml_process_cb_list =
174               xbt_dynar_new(sizeof(void_f_void_t), NULL);
175           ETag_surfxml_process_cb_list =
176               xbt_dynar_new(sizeof(void_f_void_t), NULL);
177           STag_surfxml_argument_cb_list =
178               xbt_dynar_new(sizeof(void_f_void_t), NULL);
179           ETag_surfxml_argument_cb_list =
180               xbt_dynar_new(sizeof(void_f_void_t), NULL);
181           STag_surfxml_prop_cb_list = xbt_dynar_new(sizeof(void_f_void_t), NULL);
182           ETag_surfxml_prop_cb_list = xbt_dynar_new(sizeof(void_f_void_t), NULL);
183           STag_surfxml_trace_cb_list = xbt_dynar_new(sizeof(void_f_void_t), NULL);
184           ETag_surfxml_trace_cb_list = xbt_dynar_new(sizeof(void_f_void_t), NULL);
185           STag_surfxml_trace_connect_cb_list =
186               xbt_dynar_new(sizeof(void_f_void_t), NULL);
187           ETag_surfxml_trace_connect_cb_list =
188               xbt_dynar_new(sizeof(void_f_void_t), NULL);
189           STag_surfxml_random_cb_list = xbt_dynar_new(sizeof(void_f_void_t), NULL);
190           ETag_surfxml_random_cb_list = xbt_dynar_new(sizeof(void_f_void_t), NULL);
191           STag_surfxml_AS_cb_list = xbt_dynar_new(sizeof(void_f_void_t), NULL);
192           ETag_surfxml_AS_cb_list = xbt_dynar_new(sizeof(void_f_void_t), NULL);
193           STag_surfxml_ASroute_cb_list =
194               xbt_dynar_new(sizeof(void_f_void_t), NULL);
195           ETag_surfxml_ASroute_cb_list =
196               xbt_dynar_new(sizeof(void_f_void_t), NULL);
197           STag_surfxml_bypassRoute_cb_list =
198               xbt_dynar_new(sizeof(void_f_void_t), NULL);
199           ETag_surfxml_bypassRoute_cb_list =
200               xbt_dynar_new(sizeof(void_f_void_t), NULL);
201           STag_surfxml_cluster_cb_list =
202               xbt_dynar_new(sizeof(void_f_void_t), NULL);
203           ETag_surfxml_cluster_cb_list =
204               xbt_dynar_new(sizeof(void_f_void_t), NULL);
205           STag_surfxml_peer_cb_list =
206               xbt_dynar_new(sizeof(void_f_void_t), NULL);
207           ETag_surfxml_peer_cb_list =
208               xbt_dynar_new(sizeof(void_f_void_t), NULL);
209           STag_surfxml_config_cb_list =
210                           xbt_dynar_new(sizeof(void_f_void_t), NULL);
211           ETag_surfxml_config_cb_list =
212                           xbt_dynar_new(sizeof(void_f_void_t), NULL);
213           STag_surfxml_include_cb_list =
214                           xbt_dynar_new(sizeof(void_f_void_t), NULL);
215           ETag_surfxml_include_cb_list =
216                           xbt_dynar_new(sizeof(void_f_void_t), NULL);
217 }
218
219 void surf_parse_reset_callbacks(void)
220 {
221         surf_parse_free_callbacks();
222         surf_parse_init_callbacks();
223 }
224
225 void surf_parse_free_callbacks(void)
226 {
227   xbt_dynar_free(&STag_surfxml_platform_cb_list);
228   xbt_dynar_free(&ETag_surfxml_platform_cb_list);
229   xbt_dynar_free(&STag_surfxml_host_cb_list);
230   xbt_dynar_free(&ETag_surfxml_host_cb_list);
231   xbt_dynar_free(&STag_surfxml_router_cb_list);
232   xbt_dynar_free(&ETag_surfxml_router_cb_list);
233   xbt_dynar_free(&STag_surfxml_link_cb_list);
234   xbt_dynar_free(&ETag_surfxml_link_cb_list);
235   xbt_dynar_free(&STag_surfxml_route_cb_list);
236   xbt_dynar_free(&ETag_surfxml_route_cb_list);
237   xbt_dynar_free(&STag_surfxml_link_ctn_cb_list);
238   xbt_dynar_free(&ETag_surfxml_link_ctn_cb_list);
239   xbt_dynar_free(&STag_surfxml_process_cb_list);
240   xbt_dynar_free(&ETag_surfxml_process_cb_list);
241   xbt_dynar_free(&STag_surfxml_argument_cb_list);
242   xbt_dynar_free(&ETag_surfxml_argument_cb_list);
243   xbt_dynar_free(&STag_surfxml_prop_cb_list);
244   xbt_dynar_free(&ETag_surfxml_prop_cb_list);
245   xbt_dynar_free(&STag_surfxml_trace_cb_list);
246   xbt_dynar_free(&ETag_surfxml_trace_cb_list);
247   xbt_dynar_free(&STag_surfxml_trace_connect_cb_list);
248   xbt_dynar_free(&ETag_surfxml_trace_connect_cb_list);
249   xbt_dynar_free(&STag_surfxml_random_cb_list);
250   xbt_dynar_free(&ETag_surfxml_random_cb_list);
251   xbt_dynar_free(&STag_surfxml_AS_cb_list);
252   xbt_dynar_free(&ETag_surfxml_AS_cb_list);
253   xbt_dynar_free(&STag_surfxml_ASroute_cb_list);
254   xbt_dynar_free(&ETag_surfxml_ASroute_cb_list);
255   xbt_dynar_free(&STag_surfxml_bypassRoute_cb_list);
256   xbt_dynar_free(&ETag_surfxml_bypassRoute_cb_list);
257   xbt_dynar_free(&STag_surfxml_cluster_cb_list);
258   xbt_dynar_free(&ETag_surfxml_cluster_cb_list);
259   xbt_dynar_free(&STag_surfxml_peer_cb_list);
260   xbt_dynar_free(&ETag_surfxml_peer_cb_list);
261   xbt_dynar_free(&STag_surfxml_config_cb_list);
262   xbt_dynar_free(&ETag_surfxml_config_cb_list);
263   xbt_dynar_free(&STag_surfxml_include_cb_list);
264   xbt_dynar_free(&ETag_surfxml_include_cb_list);
265 }
266
267 /* Stag and Etag parse functions */
268
269 void STag_surfxml_platform(void)
270 {
271   double version;
272   surf_parse_get_double(&version, A_surfxml_platform_version);
273
274   xbt_assert((version >= 1.0), "******* BIG FAT WARNING *********\n "
275       "You're using an ancient XML file.\n"
276       "Since SimGrid 3.1, units are Bytes, Flops, and seconds "
277       "instead of MBytes, MFlops and seconds.\n"
278
279       "Use simgrid_update_xml to update your file automatically. "
280       "This program is installed automatically with SimGrid, or "
281       "available in the tools/ directory of the source archive.\n"
282
283       "Please check also out the SURF section of the ChangeLog for "
284       "the 3.1 version for more information. \n"
285
286       "Last, do not forget to also update your values for "
287       "the calls to MSG_task_create (if any).");
288   xbt_assert((version >= 3.0), "******* BIG FAT WARNING *********\n "
289       "You're using an old XML file.\n"
290       "Use simgrid_update_xml to update your file automatically. "
291       "This program is installed automatically with SimGrid, or "
292       "available in the tools/ directory of the source archive.");
293
294   surfxml_call_cb_functions(STag_surfxml_platform_cb_list);
295
296 }
297
298 void STag_surfxml_host(void){
299 //      XBT_INFO("STag_surfxml_host [%s]",A_surfxml_host_id);
300         struct_host = xbt_new0(s_hostSG_t, 1);
301         struct_host->V_host_id = xbt_strdup(A_surfxml_host_id);
302         struct_host->V_host_power_peak = get_cpu_power(A_surfxml_host_power);
303         surf_parse_get_double(&(struct_host->V_host_power_scale), A_surfxml_host_availability);
304         surf_parse_get_int(&(struct_host->V_host_core),A_surfxml_host_core);
305         struct_host->V_host_power_trace = tmgr_trace_new(A_surfxml_host_availability_file);
306         struct_host->V_host_state_trace = tmgr_trace_new(A_surfxml_host_state_file);
307         xbt_assert((A_surfxml_host_state == A_surfxml_host_state_ON) ||
308                           (A_surfxml_host_state == A_surfxml_host_state_OFF), "Invalid state");
309         if (A_surfxml_host_state == A_surfxml_host_state_ON)
310                 struct_host->V_host_state_initial = SURF_RESOURCE_ON;
311         if (A_surfxml_host_state == A_surfxml_host_state_OFF)
312                 struct_host->V_host_state_initial = SURF_RESOURCE_OFF;
313         struct_host->V_host_coord = xbt_strdup(A_surfxml_host_coordinates);
314
315         surfxml_call_cb_functions(STag_surfxml_host_cb_list);
316 }
317 void STag_surfxml_host_cluster(void){
318         surfxml_call_cb_functions(STag_surfxml_host_cb_list);
319 }
320 void ETag_surfxml_host(void){
321         surfxml_call_cb_functions(ETag_surfxml_host_cb_list);
322         xbt_free(struct_host->V_host_id);
323         xbt_free(struct_host);
324 }
325
326 void STag_surfxml_router(void){
327         struct_router = xbt_new0(s_router_t, 1);
328         struct_router->V_router_id = xbt_strdup(A_surfxml_router_id);
329         struct_router->V_router_coord = xbt_strdup(A_surfxml_router_coordinates);
330         surfxml_call_cb_functions(STag_surfxml_router_cb_list);
331 }
332 void ETag_surfxml_router(void){
333         surfxml_call_cb_functions(ETag_surfxml_router_cb_list);
334         xbt_free(struct_router->V_router_id);
335         xbt_free(struct_router->V_router_coord);
336         xbt_free(struct_router);
337 }
338
339 void STag_surfxml_cluster(void){
340   surf_parse_models_setup(); /* ensure that the models are created after the last <config> tag. See comment in simgrid.dtd */
341
342         struct_cluster = xbt_new0(s_cluster_t, 1);
343         struct_cluster->V_cluster_id = xbt_strdup(A_surfxml_cluster_id);
344         struct_cluster->V_cluster_prefix = xbt_strdup(A_surfxml_cluster_prefix);
345         struct_cluster->V_cluster_suffix = xbt_strdup(A_surfxml_cluster_suffix);
346         struct_cluster->V_cluster_radical = xbt_strdup(A_surfxml_cluster_radical);
347         surf_parse_get_double(&struct_cluster->S_cluster_power,A_surfxml_cluster_power);
348         surf_parse_get_int(&struct_cluster->S_cluster_core,A_surfxml_cluster_core);
349         surf_parse_get_double(&struct_cluster->S_cluster_bw,A_surfxml_cluster_bw);
350         surf_parse_get_double(&struct_cluster->S_cluster_lat,A_surfxml_cluster_lat);
351         if(strcmp(A_surfxml_cluster_bb_bw,""))
352                 surf_parse_get_double(&struct_cluster->S_cluster_bb_bw,A_surfxml_cluster_bb_bw);
353         if(strcmp(A_surfxml_cluster_bb_lat,""))
354                 surf_parse_get_double(&struct_cluster->S_cluster_bb_lat,A_surfxml_cluster_bb_lat);
355         if(!strcmp(A_surfxml_cluster_router_id,""))
356                 struct_cluster->S_cluster_router_id = bprintf("%s%s_router%s",
357                                 struct_cluster->V_cluster_prefix,
358                                 struct_cluster->V_cluster_id,
359                                 struct_cluster->V_cluster_suffix);
360         else
361                 struct_cluster->S_cluster_router_id = xbt_strdup(A_surfxml_cluster_router_id);
362
363         struct_cluster->V_cluster_sharing_policy = AX_surfxml_cluster_sharing_policy;
364         struct_cluster->V_cluster_bb_sharing_policy = AX_surfxml_cluster_bb_sharing_policy;
365
366         surfxml_call_cb_functions(STag_surfxml_cluster_cb_list);
367 }
368 void ETag_surfxml_cluster(void){
369         surfxml_call_cb_functions(ETag_surfxml_cluster_cb_list);
370         xbt_free(struct_cluster->V_cluster_id);
371         xbt_free(struct_cluster->V_cluster_prefix);
372         xbt_free(struct_cluster->V_cluster_suffix);
373         xbt_free(struct_cluster->V_cluster_radical);
374         xbt_free(struct_cluster->S_cluster_router_id);
375         xbt_free(struct_cluster);
376 }
377
378 void STag_surfxml_peer(void){
379   surf_parse_models_setup(); /* ensure that the models are created after the last <config> tag. See comment in simgrid.dtd */
380
381         struct_peer = xbt_new0(s_peer_t, 1);
382         struct_peer->V_peer_id = xbt_strdup(A_surfxml_peer_id);
383         struct_peer->V_peer_power = xbt_strdup(A_surfxml_peer_power);
384         struct_peer->V_peer_bw_in = xbt_strdup(A_surfxml_peer_bw_in);
385         struct_peer->V_peer_bw_out = xbt_strdup(A_surfxml_peer_bw_out);
386         struct_peer->V_peer_lat = xbt_strdup(A_surfxml_peer_lat);
387         struct_peer->V_peer_coord = xbt_strdup(A_surfxml_peer_coordinates);
388         struct_peer->V_peer_availability_trace = xbt_strdup(A_surfxml_peer_availability_file);
389         struct_peer->V_peer_state_trace = xbt_strdup(A_surfxml_peer_state_file);
390         surfxml_call_cb_functions(STag_surfxml_peer_cb_list);
391 }
392 void ETag_surfxml_peer(void){
393         surfxml_call_cb_functions(ETag_surfxml_peer_cb_list);
394         xbt_free(struct_peer->V_peer_id);
395         xbt_free(struct_peer->V_peer_power);
396         xbt_free(struct_peer->V_peer_bw_in);
397         xbt_free(struct_peer->V_peer_bw_out);
398         xbt_free(struct_peer->V_peer_lat);
399         xbt_free(struct_peer->V_peer_coord);
400         xbt_free(struct_peer->V_peer_availability_trace);
401         xbt_free(struct_peer->V_peer_state_trace);
402         xbt_free(struct_peer);
403 }
404 void STag_surfxml_link(void){
405         struct_lnk = xbt_new0(s_link_t, 1);
406         struct_lnk->V_link_id = xbt_strdup(A_surfxml_link_id);
407         surf_parse_get_double(&(struct_lnk->V_link_bandwidth),A_surfxml_link_bandwidth);
408         struct_lnk->V_link_bandwidth_file = tmgr_trace_new(A_surfxml_link_bandwidth_file);
409         surf_parse_get_double(&(struct_lnk->V_link_latency),A_surfxml_link_latency);
410         struct_lnk->V_link_latency_file = tmgr_trace_new(A_surfxml_link_latency_file);
411         xbt_assert((A_surfxml_link_state == A_surfxml_link_state_ON) ||
412                           (A_surfxml_link_state == A_surfxml_link_state_OFF), "Invalid state");
413         if (A_surfxml_link_state == A_surfxml_link_state_ON)
414                 struct_lnk->V_link_state = SURF_RESOURCE_ON;
415         if (A_surfxml_link_state == A_surfxml_link_state_OFF)
416                 struct_lnk->V_link_state = SURF_RESOURCE_OFF;
417         struct_lnk->V_link_state_file = tmgr_trace_new(A_surfxml_link_state_file);
418         struct_lnk->V_link_sharing_policy = A_surfxml_link_sharing_policy;
419
420         if (A_surfxml_link_sharing_policy == A_surfxml_link_sharing_policy_SHARED)
421                 struct_lnk->V_policy_initial_link = SURF_LINK_SHARED;
422         else
423         {
424          if (A_surfxml_link_sharing_policy == A_surfxml_link_sharing_policy_FATPIPE)
425                  struct_lnk->V_policy_initial_link = SURF_LINK_FATPIPE;
426          else if (A_surfxml_link_sharing_policy == A_surfxml_link_sharing_policy_FULLDUPLEX)
427                  struct_lnk->V_policy_initial_link = SURF_LINK_FULLDUPLEX;
428         }
429
430         surfxml_call_cb_functions(STag_surfxml_link_cb_list);
431 }
432 void STag_surfxml_link_cluster(void){
433         surfxml_call_cb_functions(STag_surfxml_link_cb_list);
434 }
435 void ETag_surfxml_link(void){
436         surfxml_call_cb_functions(ETag_surfxml_link_cb_list);
437         xbt_free(struct_lnk->V_link_id);
438         xbt_free(struct_lnk);
439 }
440
441 void STag_surfxml_route(void){
442         surfxml_call_cb_functions(STag_surfxml_route_cb_list);
443 }
444 void STag_surfxml_link_ctn(void){
445         surfxml_call_cb_functions(STag_surfxml_link_ctn_cb_list);
446 }
447 void STag_surfxml_process(void){
448         surfxml_call_cb_functions(STag_surfxml_process_cb_list);
449 }
450 void STag_surfxml_argument(void){
451         surfxml_call_cb_functions(STag_surfxml_argument_cb_list);
452 }
453 void STag_surfxml_prop(void){
454         surfxml_call_cb_functions(STag_surfxml_prop_cb_list);
455 }
456 void STag_surfxml_trace(void){
457         surfxml_call_cb_functions(STag_surfxml_trace_cb_list);
458 }
459 void STag_surfxml_trace_connect(void){
460         surfxml_call_cb_functions(STag_surfxml_trace_connect_cb_list);
461 }
462 void STag_surfxml_AS(void){
463   surf_parse_models_setup(); /* ensure that the models are created after the last <config> tag. See comment in simgrid.dtd */
464
465         surfxml_call_cb_functions(STag_surfxml_AS_cb_list);
466 }
467 void STag_surfxml_ASroute(void){
468         surfxml_call_cb_functions(STag_surfxml_ASroute_cb_list);
469 }
470 void STag_surfxml_bypassRoute(void){
471         surfxml_call_cb_functions(STag_surfxml_bypassRoute_cb_list);
472 }
473 void STag_surfxml_config(void){
474         surfxml_call_cb_functions(STag_surfxml_config_cb_list);
475 }
476 void STag_surfxml_random(void){
477         surfxml_call_cb_functions(STag_surfxml_random_cb_list);
478 }
479
480 #define parse_method(type,name) \
481 void type##Tag_surfxml_##name(void) \
482 { surfxml_call_cb_functions(type##Tag_surfxml_##name##_cb_list); }
483 parse_method(E, platform);
484 parse_method(E, route);
485 parse_method(E, link_ctn);
486 parse_method(E, process);
487 parse_method(E, argument);
488 parse_method(E, prop);
489 parse_method(E, trace);
490 parse_method(E, trace_connect);
491 parse_method(E, random);
492 parse_method(E, AS);
493 parse_method(E, ASroute);
494 parse_method(E, bypassRoute);
495 parse_method(E, config);
496
497 /* Open and Close parse file */
498
499 void surf_parse_open(const char *file)
500 {
501   static int warned = 0;        /* warn only once */
502   if (!file) {
503     if (!warned) {
504       XBT_WARN
505           ("Bypassing the XML parser since surf_parse_open received a NULL pointer. "
506               "If it is not what you want, go fix your code.");
507       warned = 1;
508     }
509     return;
510   }
511
512   if (!surf_input_buffer_stack)
513     surf_input_buffer_stack = xbt_dynar_new(sizeof(YY_BUFFER_STATE), NULL);
514   if (!surf_file_to_parse_stack)
515     surf_file_to_parse_stack = xbt_dynar_new(sizeof(FILE *), NULL);
516
517   surf_file_to_parse = surf_fopen(file, "r");
518   xbt_assert((surf_file_to_parse), "Unable to open \"%s\"\n", file);
519   surf_input_buffer = surf_parse__create_buffer(surf_file_to_parse, YY_BUF_SIZE);
520   surf_parse__switch_to_buffer(surf_input_buffer);
521   surf_parse_lineno = 1;
522 }
523
524 void surf_parse_close(void)
525 {
526   if (surf_input_buffer_stack)
527     xbt_dynar_free(&surf_input_buffer_stack);
528   if (surf_file_to_parse_stack)
529     xbt_dynar_free(&surf_file_to_parse_stack);
530
531   if (surf_file_to_parse) {
532     surf_parse__delete_buffer(surf_input_buffer);
533     fclose(surf_file_to_parse);
534     surf_file_to_parse = NULL; //Must be reset for Bypass
535   }
536 }
537
538 /* Call the lexer to parse the currently opened file. This pointer to function enables bypassing of the parser */
539
540 static int _surf_parse(void) {
541   return surf_parse_lex();
542 }
543 int_f_void_t surf_parse = _surf_parse;
544
545
546 /* Aux parse functions */
547
548 void surfxml_add_callback(xbt_dynar_t cb_list, void_f_void_t function)
549 {
550   xbt_dynar_push(cb_list, &function);
551 }
552
553 void surfxml_del_callback(xbt_dynar_t cb_list, void_f_void_t function)
554 {
555   xbt_ex_t e;
556   unsigned int it=0;
557   void_f_void_t null_f=NULL;
558
559   TRY {
560     it = xbt_dynar_search(cb_list,&function);
561   }
562   CATCH(e) {
563     if (e.category == not_found_error) {
564       xbt_ex_free(e);
565       xbt_die("Trying to remove a callback that is not here! This should not happen");
566     }
567     RETHROW;
568   }
569
570   xbt_dynar_replace(cb_list, it,&null_f);
571 }
572
573 static XBT_INLINE void surfxml_call_cb_functions(xbt_dynar_t cb_list)
574 {
575   unsigned int iterator;
576   void_f_void_t fun;
577   xbt_dynar_foreach(cb_list, iterator, fun) {
578     if (fun) (*fun) ();
579   }
580 }
581
582
583 /* Prop tag functions */
584
585 void parse_properties(const char* prop_id, const char* prop_value)
586 {
587     char *value = NULL;
588         if (!current_property_set)
589             current_property_set = xbt_dict_new();      // Maybe, it should raise an error
590         if(!strcmp(prop_id,"coordinates")){
591                 if(!strcmp(prop_value,"yes") && !COORD_HOST_LEVEL)
592                   {
593                             XBT_INFO("Configuration change: Set '%s' to '%s'", prop_id, prop_value);
594                                 COORD_HOST_LEVEL = xbt_lib_add_level(host_lib,xbt_dynar_free_voidp);
595                                 COORD_ASR_LEVEL  = xbt_lib_add_level(as_router_lib,xbt_dynar_free_voidp);
596                   }
597                 if(strcmp(A_surfxml_prop_value,"yes"))
598                           xbt_die("Setting XML prop coordinates must be \"yes\"");
599           }
600         else{
601                   value = xbt_strdup(prop_value);
602                   xbt_dict_set(current_property_set, prop_id, value, free);
603          }
604 }
605
606 /**
607  * With XML parser
608  */
609 void parse_properties_XML(void)
610 {
611   parse_properties(A_surfxml_prop_id, A_surfxml_prop_value);
612 }
613
614
615 /* Random tag functions */
616
617 double get_cpu_power(const char *power)
618 {
619   double power_scale = 0.0;
620   const char *p, *q;
621   char *generator;
622   random_data_t random = NULL;
623   /* randomness is inserted like this: power="$rand(my_random)" */
624   if (((p = strstr(power, "$rand(")) != NULL)
625       && ((q = strstr(power, ")")) != NULL)) {
626     if (p < q) {
627       generator = xbt_malloc(q - (p + 6) + 1);
628       memcpy(generator, p + 6, q - (p + 6));
629       generator[q - (p + 6)] = '\0';
630       random = xbt_dict_get_or_null(random_data_list, generator);
631       xbt_assert(random, "Random generator %s undefined", generator);
632       power_scale = random_generate(random);
633     }
634   } else {
635     surf_parse_get_double(&power_scale, power);
636   }
637   return power_scale;
638 }
639
640 double random_min, random_max, random_mean, random_std_deviation,
641     random_generator;
642 char *random_id;
643
644 static void init_randomness(void)
645 {
646   random_id = A_surfxml_random_id;
647   surf_parse_get_double(&random_min, A_surfxml_random_min);
648   surf_parse_get_double(&random_max, A_surfxml_random_max);
649   surf_parse_get_double(&random_mean, A_surfxml_random_mean);
650   surf_parse_get_double(&random_std_deviation,
651                         A_surfxml_random_std_deviation);
652   random_generator = A_surfxml_random_generator;
653 }
654
655 static void add_randomness(void)
656 {
657   /* If needed aditional properties can be added by using the prop tag */
658   random_data_t random =
659       random_new(random_generator, 0, random_min, random_max, random_mean,
660                  random_std_deviation);
661   xbt_dict_set(random_data_list, random_id, (void *) random,
662                &xbt_free_ref);
663 }
664
665 /**
666  * create CPU resource via CPU Model
667  */
668 void* surf_host_create_resource(char *name, double power_peak,
669                                double power_scale,
670                                tmgr_trace_t power_trace, int core,
671                                e_surf_resource_state_t state_initial,
672                                tmgr_trace_t state_trace,
673                                xbt_dict_t cpu_properties)
674 {
675   return surf_cpu_model->extension.cpu.create_resource(name, power_peak,
676                                                        power_scale,
677                                                        power_trace,
678                                                        core,
679                                                        state_initial,
680                                                        state_trace,
681                                                        cpu_properties);
682 }
683
684 /**
685  * create CPU resource via worsktation_ptask_L07 model
686  */
687
688 void* surf_wsL07_host_create_resource(char *name, double power_peak,
689                                      double power_scale,
690                                      tmgr_trace_t power_trace,
691                                      e_surf_resource_state_t state_initial,
692                                      tmgr_trace_t state_trace,
693                                      xbt_dict_t cpu_properties)
694 {
695   return surf_workstation_model->extension.workstation.cpu_create_resource(name,
696                                                                     power_peak,
697                                                                     power_scale,
698                                                                     power_trace,
699                                                                     state_initial,
700                                                                     state_trace,
701                                                                     cpu_properties);
702 }
703
704 /**
705  * create link resource via network Model
706  */
707 void* surf_link_create_resource(char *name,
708                                double bw_initial,
709                                tmgr_trace_t bw_trace,
710                                double lat_initial,
711                                tmgr_trace_t lat_trace,
712                                e_surf_resource_state_t
713                                state_initial,
714                                tmgr_trace_t state_trace,
715                                e_surf_link_sharing_policy_t policy,
716                                xbt_dict_t properties)
717 {
718   return surf_network_model->extension.network.create_resource(name,
719                                                                bw_initial,
720                                                                bw_trace,
721                                                                lat_initial,
722                                                                lat_trace,
723                                                                state_initial,
724                                                                state_trace,
725                                                                policy,
726                                                                properties);
727 }
728
729 /**
730  * create link resource via workstation_ptask_L07 model
731  */
732
733 void* surf_wsL07_link_create_resource(char *name,
734                                      double bw_initial,
735                                      tmgr_trace_t bw_trace,
736                                      double lat_initial,
737                                      tmgr_trace_t lat_trace,
738                                      e_surf_resource_state_t
739                                      state_initial,
740                                      tmgr_trace_t state_trace,
741                                      e_surf_link_sharing_policy_t
742                                      policy, xbt_dict_t properties)
743 {
744   return surf_workstation_model->extension.workstation.
745       link_create_resource(name, bw_initial, bw_trace, lat_initial,
746                            lat_trace, state_initial, state_trace, policy,
747                            properties);
748 }
749
750 /**
751  *
752  *init new routing model component
753  */
754
755 void surf_AS_new(const char *AS_id, const char *AS_mode)
756 {
757   routing_AS_init(AS_id, AS_mode);
758 }
759
760 void surf_AS_finalize(const char *AS_id)
761 {
762   routing_AS_end(AS_id);
763 }
764
765 /*
766  * add host to the network element list
767  */
768 void surf_route_add_host(const char *host_id)
769 {
770   routing_add_host(host_id);
771 }
772
773 /**
774  * set route
775  */
776 void surf_routing_add_route(const char *src_id, const char *dst_id,
777                             xbt_dynar_t links_id)
778 {
779   unsigned int i;
780   const char *link_id;
781   routing_set_route(src_id, dst_id);
782   xbt_dynar_foreach(links_id, i, link_id) {
783     routing_add_link(link_id);
784   }
785
786   //store the route
787   routing_store_route();
788 }
789
790 /**
791  * Add Traces
792  */
793 void surf_add_host_traces(void)
794 {
795   return surf_cpu_model->extension.cpu.add_traces();
796 }
797
798 void surf_add_link_traces(void)
799 {
800   return surf_network_model->extension.network.add_traces();
801 }
802
803 void surf_wsL07_add_traces(void)
804 {
805   return surf_workstation_model->extension.workstation.add_traces();
806 }