Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
split raw parsing functions from platform parsing, which is somehow similar to the...
[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 ETag_surfxml_host(void){
318         surfxml_call_cb_functions(ETag_surfxml_host_cb_list);
319         //xbt_free(struct_host->V_host_id);
320         struct_host->V_host_power_peak = 0.0;
321         struct_host->V_host_core = 0;
322         struct_host->V_host_power_scale = 0.0;
323         struct_host->V_host_state_initial = SURF_RESOURCE_ON;
324         struct_host->V_host_power_trace = NULL;
325         struct_host->V_host_state_trace = NULL;
326         xbt_free(struct_host->V_host_coord);
327         //xbt_free(host);
328 }
329
330 void STag_surfxml_router(void){
331         struct_router = xbt_new0(s_router_t, 1);
332         struct_router->V_router_id = xbt_strdup(A_surfxml_router_id);
333         struct_router->V_router_coord = xbt_strdup(A_surfxml_router_coordinates);
334         surfxml_call_cb_functions(STag_surfxml_router_cb_list);
335 }
336 void ETag_surfxml_router(void){
337         surfxml_call_cb_functions(ETag_surfxml_router_cb_list);
338         xbt_free(struct_router->V_router_id);
339         xbt_free(struct_router->V_router_coord);
340         xbt_free(struct_router);
341 }
342
343 void STag_surfxml_cluster(void){
344   surf_parse_models_setup(); /* ensure that the models are created after the last <config> tag. See comment in simgrid.dtd */
345
346         struct_cluster = xbt_new0(s_cluster_t, 1);
347         struct_cluster->V_cluster_id = xbt_strdup(A_surfxml_cluster_id);
348         struct_cluster->V_cluster_prefix = xbt_strdup(A_surfxml_cluster_prefix);
349         struct_cluster->V_cluster_suffix = xbt_strdup(A_surfxml_cluster_suffix);
350         struct_cluster->V_cluster_radical = xbt_strdup(A_surfxml_cluster_radical);
351         struct_cluster->S_cluster_power = xbt_strdup(A_surfxml_cluster_power);
352         struct_cluster->S_cluster_core = xbt_strdup(A_surfxml_cluster_core);
353         struct_cluster->S_cluster_bw = xbt_strdup(A_surfxml_cluster_bw);
354         struct_cluster->S_cluster_lat = xbt_strdup(A_surfxml_cluster_lat);
355         struct_cluster->S_cluster_bb_bw = xbt_strdup(A_surfxml_cluster_bb_bw);
356         struct_cluster->S_cluster_bb_lat = xbt_strdup(A_surfxml_cluster_bb_lat);
357         if(!strcmp(A_surfxml_cluster_router_id,""))
358                 struct_cluster->S_cluster_router_id = bprintf("%s%s_router%s",
359                                 struct_cluster->V_cluster_prefix,
360                                 struct_cluster->V_cluster_id,
361                                 struct_cluster->V_cluster_suffix);
362         else
363                 struct_cluster->S_cluster_router_id = xbt_strdup(A_surfxml_cluster_router_id);
364
365         struct_cluster->V_cluster_sharing_policy = AX_surfxml_cluster_sharing_policy;
366         struct_cluster->V_cluster_bb_sharing_policy = AX_surfxml_cluster_bb_sharing_policy;
367
368         surfxml_call_cb_functions(STag_surfxml_cluster_cb_list);
369 }
370 void ETag_surfxml_cluster(void){
371         surfxml_call_cb_functions(ETag_surfxml_cluster_cb_list);
372         xbt_free(struct_cluster->V_cluster_id);
373         xbt_free(struct_cluster->V_cluster_prefix);
374         xbt_free(struct_cluster->V_cluster_suffix);
375         xbt_free(struct_cluster->V_cluster_radical);
376         xbt_free(struct_cluster->S_cluster_power);
377         xbt_free(struct_cluster->S_cluster_core);
378         xbt_free(struct_cluster->S_cluster_bw);
379         xbt_free(struct_cluster->S_cluster_lat);
380         xbt_free(struct_cluster->S_cluster_bb_bw);
381         xbt_free(struct_cluster->S_cluster_bb_lat);
382         xbt_free(struct_cluster->S_cluster_router_id);
383         struct_cluster->V_cluster_sharing_policy = 0;
384         struct_cluster->V_cluster_bb_sharing_policy = 0;
385         xbt_free(struct_cluster);
386 }
387
388 void STag_surfxml_peer(void){
389   surf_parse_models_setup(); /* ensure that the models are created after the last <config> tag. See comment in simgrid.dtd */
390
391         struct_peer = xbt_new0(s_peer_t, 1);
392         struct_peer->V_peer_id = xbt_strdup(A_surfxml_peer_id);
393         struct_peer->V_peer_power = xbt_strdup(A_surfxml_peer_power);
394         struct_peer->V_peer_bw_in = xbt_strdup(A_surfxml_peer_bw_in);
395         struct_peer->V_peer_bw_out = xbt_strdup(A_surfxml_peer_bw_out);
396         struct_peer->V_peer_lat = xbt_strdup(A_surfxml_peer_lat);
397         struct_peer->V_peer_coord = xbt_strdup(A_surfxml_peer_coordinates);
398         struct_peer->V_peer_availability_trace = xbt_strdup(A_surfxml_peer_availability_file);
399         struct_peer->V_peer_state_trace = xbt_strdup(A_surfxml_peer_state_file);
400         surfxml_call_cb_functions(STag_surfxml_peer_cb_list);
401 }
402 void ETag_surfxml_peer(void){
403         surfxml_call_cb_functions(ETag_surfxml_peer_cb_list);
404         xbt_free(struct_peer->V_peer_id);
405         xbt_free(struct_peer->V_peer_power);
406         xbt_free(struct_peer->V_peer_bw_in);
407         xbt_free(struct_peer->V_peer_bw_out);
408         xbt_free(struct_peer->V_peer_lat);
409         xbt_free(struct_peer->V_peer_coord);
410         xbt_free(struct_peer->V_peer_availability_trace);
411         xbt_free(struct_peer->V_peer_state_trace);
412         xbt_free(struct_peer);
413 }
414 void STag_surfxml_link(void){
415         struct_lnk = xbt_new0(s_link_t, 1);
416         struct_lnk->V_link_id = xbt_strdup(A_surfxml_link_id);
417         surf_parse_get_double(&(struct_lnk->V_link_bandwidth),A_surfxml_link_bandwidth);
418         struct_lnk->V_link_bandwidth_file = tmgr_trace_new(A_surfxml_link_bandwidth_file);
419         surf_parse_get_double(&(struct_lnk->V_link_latency),A_surfxml_link_latency);
420         struct_lnk->V_link_latency_file = tmgr_trace_new(A_surfxml_link_latency_file);
421         xbt_assert((A_surfxml_link_state == A_surfxml_link_state_ON) ||
422                           (A_surfxml_link_state == A_surfxml_link_state_OFF), "Invalid state");
423         if (A_surfxml_link_state == A_surfxml_link_state_ON)
424                 struct_lnk->V_link_state = SURF_RESOURCE_ON;
425         if (A_surfxml_link_state == A_surfxml_link_state_OFF)
426                 struct_lnk->V_link_state = SURF_RESOURCE_OFF;
427         struct_lnk->V_link_state_file = tmgr_trace_new(A_surfxml_link_state_file);
428         struct_lnk->V_link_sharing_policy = A_surfxml_link_sharing_policy;
429
430         if (A_surfxml_link_sharing_policy == A_surfxml_link_sharing_policy_SHARED)
431                 struct_lnk->V_policy_initial_link = SURF_LINK_SHARED;
432         else
433         {
434          if (A_surfxml_link_sharing_policy == A_surfxml_link_sharing_policy_FATPIPE)
435                  struct_lnk->V_policy_initial_link = SURF_LINK_FATPIPE;
436          else if (A_surfxml_link_sharing_policy == A_surfxml_link_sharing_policy_FULLDUPLEX)
437                  struct_lnk->V_policy_initial_link = SURF_LINK_FULLDUPLEX;
438         }
439
440
441         surfxml_call_cb_functions(STag_surfxml_link_cb_list);
442 }
443 void ETag_surfxml_link(void){
444         surfxml_call_cb_functions(ETag_surfxml_link_cb_list);
445         xbt_free(struct_lnk->V_link_id);
446         struct_lnk->V_link_bandwidth = 0;
447         struct_lnk->V_link_bandwidth_file = NULL;
448         struct_lnk->V_link_latency = 0;
449         struct_lnk->V_link_latency_file = NULL;
450         struct_lnk->V_link_state = SURF_RESOURCE_ON;
451         struct_lnk->V_link_state_file = NULL;
452         struct_lnk->V_link_sharing_policy = 0;
453         xbt_free(struct_lnk);
454 }
455
456 void STag_surfxml_route(void){
457         surfxml_call_cb_functions(STag_surfxml_route_cb_list);
458 }
459 void STag_surfxml_link_ctn(void){
460         surfxml_call_cb_functions(STag_surfxml_link_ctn_cb_list);
461 }
462 void STag_surfxml_process(void){
463         surfxml_call_cb_functions(STag_surfxml_process_cb_list);
464 }
465 void STag_surfxml_argument(void){
466         surfxml_call_cb_functions(STag_surfxml_argument_cb_list);
467 }
468 void STag_surfxml_prop(void){
469         surfxml_call_cb_functions(STag_surfxml_prop_cb_list);
470 }
471 void STag_surfxml_trace(void){
472         surfxml_call_cb_functions(STag_surfxml_trace_cb_list);
473 }
474 void STag_surfxml_trace_connect(void){
475         surfxml_call_cb_functions(STag_surfxml_trace_connect_cb_list);
476 }
477 void STag_surfxml_AS(void){
478         surfxml_call_cb_functions(STag_surfxml_AS_cb_list);
479 }
480 void STag_surfxml_ASroute(void){
481         surfxml_call_cb_functions(STag_surfxml_ASroute_cb_list);
482 }
483 void STag_surfxml_bypassRoute(void){
484         surfxml_call_cb_functions(STag_surfxml_bypassRoute_cb_list);
485 }
486 void STag_surfxml_config(void){
487         surfxml_call_cb_functions(STag_surfxml_config_cb_list);
488 }
489 void STag_surfxml_random(void){
490         surfxml_call_cb_functions(STag_surfxml_random_cb_list);
491 }
492
493 #define parse_method(type,name) \
494 void type##Tag_surfxml_##name(void) \
495 { surfxml_call_cb_functions(type##Tag_surfxml_##name##_cb_list); }
496 parse_method(E, platform);
497 parse_method(E, route);
498 parse_method(E, link_ctn);
499 parse_method(E, process);
500 parse_method(E, argument);
501 parse_method(E, prop);
502 parse_method(E, trace);
503 parse_method(E, trace_connect);
504 parse_method(E, random);
505 parse_method(E, AS);
506 parse_method(E, ASroute);
507 parse_method(E, bypassRoute);
508 parse_method(E, config);
509
510 /* Open and Close parse file */
511
512 void surf_parse_open(const char *file)
513 {
514   static int warned = 0;        /* warn only once */
515   if (!file) {
516     if (!warned) {
517       XBT_WARN
518           ("Bypassing the XML parser since surf_parse_open received a NULL pointer. "
519               "If it is not what you want, go fix your code.");
520       warned = 1;
521     }
522     return;
523   }
524
525   if (!surf_input_buffer_stack)
526     surf_input_buffer_stack = xbt_dynar_new(sizeof(YY_BUFFER_STATE), NULL);
527   if (!surf_file_to_parse_stack)
528     surf_file_to_parse_stack = xbt_dynar_new(sizeof(FILE *), NULL);
529
530   surf_file_to_parse = surf_fopen(file, "r");
531   xbt_assert((surf_file_to_parse), "Unable to open \"%s\"\n", file);
532   surf_input_buffer = surf_parse__create_buffer(surf_file_to_parse, YY_BUF_SIZE);
533   surf_parse__switch_to_buffer(surf_input_buffer);
534   surf_parse_lineno = 1;
535 }
536
537 void surf_parse_close(void)
538 {
539   if (surf_input_buffer_stack)
540     xbt_dynar_free(&surf_input_buffer_stack);
541   if (surf_file_to_parse_stack)
542     xbt_dynar_free(&surf_file_to_parse_stack);
543
544   if (surf_file_to_parse) {
545     surf_parse__delete_buffer(surf_input_buffer);
546     fclose(surf_file_to_parse);
547     surf_file_to_parse = NULL; //Must be reset for Bypass
548   }
549 }
550
551 /* Call the lexer to parse the currently opened file. This pointer to function enables bypassing of the parser */
552
553 static int _surf_parse(void) {
554   return surf_parse_lex();
555 }
556 int_f_void_t surf_parse = _surf_parse;
557
558
559 /* Aux parse functions */
560
561 void surfxml_add_callback(xbt_dynar_t cb_list, void_f_void_t function)
562 {
563   xbt_dynar_push(cb_list, &function);
564 }
565
566 void surfxml_del_callback(xbt_dynar_t cb_list, void_f_void_t function)
567 {
568   xbt_ex_t e;
569   unsigned int it=0;
570   void_f_void_t null_f=NULL;
571
572   TRY {
573     it = xbt_dynar_search(cb_list,&function);
574   }
575   CATCH(e) {
576     if (e.category == not_found_error) {
577       xbt_ex_free(e);
578       xbt_die("Trying to remove a callback that is not here! This should not happen");
579     }
580     RETHROW;
581   }
582
583   xbt_dynar_replace(cb_list, it,&null_f);
584 }
585
586 static XBT_INLINE void surfxml_call_cb_functions(xbt_dynar_t cb_list)
587 {
588   unsigned int iterator;
589   void_f_void_t fun;
590   xbt_dynar_foreach(cb_list, iterator, fun) {
591     if (fun) (*fun) ();
592   }
593 }
594
595
596 /* Prop tag functions */
597
598 void parse_properties(const char* prop_id, const char* prop_value)
599 {
600     char *value = NULL;
601         if (!current_property_set)
602             current_property_set = xbt_dict_new();      // Maybe, it should raise an error
603         if(!strcmp(prop_id,"coordinates")){
604                 if(!strcmp(prop_value,"yes") && !COORD_HOST_LEVEL)
605                   {
606                             XBT_INFO("Configuration change: Set '%s' to '%s'", prop_id, prop_value);
607                                 COORD_HOST_LEVEL = xbt_lib_add_level(host_lib,xbt_dynar_free_voidp);
608                                 COORD_ASR_LEVEL  = xbt_lib_add_level(as_router_lib,xbt_dynar_free_voidp);
609                   }
610                 if(strcmp(A_surfxml_prop_value,"yes"))
611                           xbt_die("Setting XML prop coordinates must be \"yes\"");
612           }
613         else{
614                   value = xbt_strdup(prop_value);
615                   xbt_dict_set(current_property_set, prop_id, value, free);
616          }
617 }
618
619 /**
620  * With XML parser
621  */
622 void parse_properties_XML(void)
623 {
624   parse_properties(A_surfxml_prop_id, A_surfxml_prop_value);
625 }
626
627
628 /* Random tag functions */
629
630 double get_cpu_power(const char *power)
631 {
632   double power_scale = 0.0;
633   const char *p, *q;
634   char *generator;
635   random_data_t random = NULL;
636   /* randomness is inserted like this: power="$rand(my_random)" */
637   if (((p = strstr(power, "$rand(")) != NULL)
638       && ((q = strstr(power, ")")) != NULL)) {
639     if (p < q) {
640       generator = xbt_malloc(q - (p + 6) + 1);
641       memcpy(generator, p + 6, q - (p + 6));
642       generator[q - (p + 6)] = '\0';
643       random = xbt_dict_get_or_null(random_data_list, generator);
644       xbt_assert(random, "Random generator %s undefined", generator);
645       power_scale = random_generate(random);
646     }
647   } else {
648     surf_parse_get_double(&power_scale, power);
649   }
650   return power_scale;
651 }
652
653 double random_min, random_max, random_mean, random_std_deviation,
654     random_generator;
655 char *random_id;
656
657 static void init_randomness(void)
658 {
659   random_id = A_surfxml_random_id;
660   surf_parse_get_double(&random_min, A_surfxml_random_min);
661   surf_parse_get_double(&random_max, A_surfxml_random_max);
662   surf_parse_get_double(&random_mean, A_surfxml_random_mean);
663   surf_parse_get_double(&random_std_deviation,
664                         A_surfxml_random_std_deviation);
665   random_generator = A_surfxml_random_generator;
666 }
667
668 static void add_randomness(void)
669 {
670   /* If needed aditional properties can be added by using the prop tag */
671   random_data_t random =
672       random_new(random_generator, 0, random_min, random_max, random_mean,
673                  random_std_deviation);
674   xbt_dict_set(random_data_list, random_id, (void *) random,
675                &xbt_free_ref);
676 }
677
678 /**
679  * create CPU resource via CPU Model
680  */
681 void* surf_host_create_resource(char *name, double power_peak,
682                                double power_scale,
683                                tmgr_trace_t power_trace, int core,
684                                e_surf_resource_state_t state_initial,
685                                tmgr_trace_t state_trace,
686                                xbt_dict_t cpu_properties)
687 {
688   return surf_cpu_model->extension.cpu.create_resource(name, power_peak,
689                                                        power_scale,
690                                                        power_trace,
691                                                        core,
692                                                        state_initial,
693                                                        state_trace,
694                                                        cpu_properties);
695 }
696
697 /**
698  * create CPU resource via worsktation_ptask_L07 model
699  */
700
701 void* surf_wsL07_host_create_resource(char *name, double power_peak,
702                                      double power_scale,
703                                      tmgr_trace_t power_trace,
704                                      e_surf_resource_state_t state_initial,
705                                      tmgr_trace_t state_trace,
706                                      xbt_dict_t cpu_properties)
707 {
708   return surf_workstation_model->extension.workstation.cpu_create_resource(name,
709                                                                     power_peak,
710                                                                     power_scale,
711                                                                     power_trace,
712                                                                     state_initial,
713                                                                     state_trace,
714                                                                     cpu_properties);
715 }
716
717 /**
718  * create link resource via network Model
719  */
720 void* surf_link_create_resource(char *name,
721                                double bw_initial,
722                                tmgr_trace_t bw_trace,
723                                double lat_initial,
724                                tmgr_trace_t lat_trace,
725                                e_surf_resource_state_t
726                                state_initial,
727                                tmgr_trace_t state_trace,
728                                e_surf_link_sharing_policy_t policy,
729                                xbt_dict_t properties)
730 {
731   return surf_network_model->extension.network.create_resource(name,
732                                                                bw_initial,
733                                                                bw_trace,
734                                                                lat_initial,
735                                                                lat_trace,
736                                                                state_initial,
737                                                                state_trace,
738                                                                policy,
739                                                                properties);
740 }
741
742 /**
743  * create link resource via workstation_ptask_L07 model
744  */
745
746 void* surf_wsL07_link_create_resource(char *name,
747                                      double bw_initial,
748                                      tmgr_trace_t bw_trace,
749                                      double lat_initial,
750                                      tmgr_trace_t lat_trace,
751                                      e_surf_resource_state_t
752                                      state_initial,
753                                      tmgr_trace_t state_trace,
754                                      e_surf_link_sharing_policy_t
755                                      policy, xbt_dict_t properties)
756 {
757   return surf_workstation_model->extension.workstation.
758       link_create_resource(name, bw_initial, bw_trace, lat_initial,
759                            lat_trace, state_initial, state_trace, policy,
760                            properties);
761 }
762
763 /**
764  *
765  *init new routing model component
766  */
767
768 void surf_AS_new(const char *AS_id, const char *AS_mode)
769 {
770   routing_AS_init(AS_id, AS_mode);
771 }
772
773 void surf_AS_finalize(const char *AS_id)
774 {
775   routing_AS_end(AS_id);
776 }
777
778 /*
779  * add host to the network element list
780  */
781 void surf_route_add_host(const char *host_id)
782 {
783   routing_add_host(host_id);
784 }
785
786 /**
787  * set route
788  */
789 void surf_routing_add_route(const char *src_id, const char *dst_id,
790                             xbt_dynar_t links_id)
791 {
792   unsigned int i;
793   const char *link_id;
794   routing_set_route(src_id, dst_id);
795   xbt_dynar_foreach(links_id, i, link_id) {
796     routing_add_link(link_id);
797   }
798
799   //store the route
800   routing_store_route();
801 }
802
803 /**
804  * Add Traces
805  */
806 void surf_add_host_traces(void)
807 {
808   return surf_cpu_model->extension.cpu.add_traces();
809 }
810
811 void surf_add_link_traces(void)
812 {
813   return surf_network_model->extension.network.add_traces();
814 }
815
816 void surf_wsL07_add_traces(void)
817 {
818   return surf_workstation_model->extension.workstation.add_traces();
819 }