Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Enable clobbered variable warnings again and fix the last one (for me)
[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   surf_parse_models_setup(); /* ensure that the models are created after the last <config> tag. See comment in simgrid.dtd */
479
480         surfxml_call_cb_functions(STag_surfxml_AS_cb_list);
481 }
482 void STag_surfxml_ASroute(void){
483         surfxml_call_cb_functions(STag_surfxml_ASroute_cb_list);
484 }
485 void STag_surfxml_bypassRoute(void){
486         surfxml_call_cb_functions(STag_surfxml_bypassRoute_cb_list);
487 }
488 void STag_surfxml_config(void){
489         surfxml_call_cb_functions(STag_surfxml_config_cb_list);
490 }
491 void STag_surfxml_random(void){
492         surfxml_call_cb_functions(STag_surfxml_random_cb_list);
493 }
494
495 #define parse_method(type,name) \
496 void type##Tag_surfxml_##name(void) \
497 { surfxml_call_cb_functions(type##Tag_surfxml_##name##_cb_list); }
498 parse_method(E, platform);
499 parse_method(E, route);
500 parse_method(E, link_ctn);
501 parse_method(E, process);
502 parse_method(E, argument);
503 parse_method(E, prop);
504 parse_method(E, trace);
505 parse_method(E, trace_connect);
506 parse_method(E, random);
507 parse_method(E, AS);
508 parse_method(E, ASroute);
509 parse_method(E, bypassRoute);
510 parse_method(E, config);
511
512 /* Open and Close parse file */
513
514 void surf_parse_open(const char *file)
515 {
516   static int warned = 0;        /* warn only once */
517   if (!file) {
518     if (!warned) {
519       XBT_WARN
520           ("Bypassing the XML parser since surf_parse_open received a NULL pointer. "
521               "If it is not what you want, go fix your code.");
522       warned = 1;
523     }
524     return;
525   }
526
527   if (!surf_input_buffer_stack)
528     surf_input_buffer_stack = xbt_dynar_new(sizeof(YY_BUFFER_STATE), NULL);
529   if (!surf_file_to_parse_stack)
530     surf_file_to_parse_stack = xbt_dynar_new(sizeof(FILE *), NULL);
531
532   surf_file_to_parse = surf_fopen(file, "r");
533   xbt_assert((surf_file_to_parse), "Unable to open \"%s\"\n", file);
534   surf_input_buffer = surf_parse__create_buffer(surf_file_to_parse, YY_BUF_SIZE);
535   surf_parse__switch_to_buffer(surf_input_buffer);
536   surf_parse_lineno = 1;
537 }
538
539 void surf_parse_close(void)
540 {
541   if (surf_input_buffer_stack)
542     xbt_dynar_free(&surf_input_buffer_stack);
543   if (surf_file_to_parse_stack)
544     xbt_dynar_free(&surf_file_to_parse_stack);
545
546   if (surf_file_to_parse) {
547     surf_parse__delete_buffer(surf_input_buffer);
548     fclose(surf_file_to_parse);
549     surf_file_to_parse = NULL; //Must be reset for Bypass
550   }
551 }
552
553 /* Call the lexer to parse the currently opened file. This pointer to function enables bypassing of the parser */
554
555 static int _surf_parse(void) {
556   return surf_parse_lex();
557 }
558 int_f_void_t surf_parse = _surf_parse;
559
560
561 /* Aux parse functions */
562
563 void surfxml_add_callback(xbt_dynar_t cb_list, void_f_void_t function)
564 {
565   xbt_dynar_push(cb_list, &function);
566 }
567
568 void surfxml_del_callback(xbt_dynar_t cb_list, void_f_void_t function)
569 {
570   xbt_ex_t e;
571   unsigned int it=0;
572   void_f_void_t null_f=NULL;
573
574   TRY {
575     it = xbt_dynar_search(cb_list,&function);
576   }
577   CATCH(e) {
578     if (e.category == not_found_error) {
579       xbt_ex_free(e);
580       xbt_die("Trying to remove a callback that is not here! This should not happen");
581     }
582     RETHROW;
583   }
584
585   xbt_dynar_replace(cb_list, it,&null_f);
586 }
587
588 static XBT_INLINE void surfxml_call_cb_functions(xbt_dynar_t cb_list)
589 {
590   unsigned int iterator;
591   void_f_void_t fun;
592   xbt_dynar_foreach(cb_list, iterator, fun) {
593     if (fun) (*fun) ();
594   }
595 }
596
597
598 /* Prop tag functions */
599
600 void parse_properties(const char* prop_id, const char* prop_value)
601 {
602     char *value = NULL;
603         if (!current_property_set)
604             current_property_set = xbt_dict_new();      // Maybe, it should raise an error
605         if(!strcmp(prop_id,"coordinates")){
606                 if(!strcmp(prop_value,"yes") && !COORD_HOST_LEVEL)
607                   {
608                             XBT_INFO("Configuration change: Set '%s' to '%s'", prop_id, prop_value);
609                                 COORD_HOST_LEVEL = xbt_lib_add_level(host_lib,xbt_dynar_free_voidp);
610                                 COORD_ASR_LEVEL  = xbt_lib_add_level(as_router_lib,xbt_dynar_free_voidp);
611                   }
612                 if(strcmp(A_surfxml_prop_value,"yes"))
613                           xbt_die("Setting XML prop coordinates must be \"yes\"");
614           }
615         else{
616                   value = xbt_strdup(prop_value);
617                   xbt_dict_set(current_property_set, prop_id, value, free);
618          }
619 }
620
621 /**
622  * With XML parser
623  */
624 void parse_properties_XML(void)
625 {
626   parse_properties(A_surfxml_prop_id, A_surfxml_prop_value);
627 }
628
629
630 /* Random tag functions */
631
632 double get_cpu_power(const char *power)
633 {
634   double power_scale = 0.0;
635   const char *p, *q;
636   char *generator;
637   random_data_t random = NULL;
638   /* randomness is inserted like this: power="$rand(my_random)" */
639   if (((p = strstr(power, "$rand(")) != NULL)
640       && ((q = strstr(power, ")")) != NULL)) {
641     if (p < q) {
642       generator = xbt_malloc(q - (p + 6) + 1);
643       memcpy(generator, p + 6, q - (p + 6));
644       generator[q - (p + 6)] = '\0';
645       random = xbt_dict_get_or_null(random_data_list, generator);
646       xbt_assert(random, "Random generator %s undefined", generator);
647       power_scale = random_generate(random);
648     }
649   } else {
650     surf_parse_get_double(&power_scale, power);
651   }
652   return power_scale;
653 }
654
655 double random_min, random_max, random_mean, random_std_deviation,
656     random_generator;
657 char *random_id;
658
659 static void init_randomness(void)
660 {
661   random_id = A_surfxml_random_id;
662   surf_parse_get_double(&random_min, A_surfxml_random_min);
663   surf_parse_get_double(&random_max, A_surfxml_random_max);
664   surf_parse_get_double(&random_mean, A_surfxml_random_mean);
665   surf_parse_get_double(&random_std_deviation,
666                         A_surfxml_random_std_deviation);
667   random_generator = A_surfxml_random_generator;
668 }
669
670 static void add_randomness(void)
671 {
672   /* If needed aditional properties can be added by using the prop tag */
673   random_data_t random =
674       random_new(random_generator, 0, random_min, random_max, random_mean,
675                  random_std_deviation);
676   xbt_dict_set(random_data_list, random_id, (void *) random,
677                &xbt_free_ref);
678 }
679
680 /**
681  * create CPU resource via CPU Model
682  */
683 void* surf_host_create_resource(char *name, double power_peak,
684                                double power_scale,
685                                tmgr_trace_t power_trace, int core,
686                                e_surf_resource_state_t state_initial,
687                                tmgr_trace_t state_trace,
688                                xbt_dict_t cpu_properties)
689 {
690   return surf_cpu_model->extension.cpu.create_resource(name, power_peak,
691                                                        power_scale,
692                                                        power_trace,
693                                                        core,
694                                                        state_initial,
695                                                        state_trace,
696                                                        cpu_properties);
697 }
698
699 /**
700  * create CPU resource via worsktation_ptask_L07 model
701  */
702
703 void* surf_wsL07_host_create_resource(char *name, double power_peak,
704                                      double power_scale,
705                                      tmgr_trace_t power_trace,
706                                      e_surf_resource_state_t state_initial,
707                                      tmgr_trace_t state_trace,
708                                      xbt_dict_t cpu_properties)
709 {
710   return surf_workstation_model->extension.workstation.cpu_create_resource(name,
711                                                                     power_peak,
712                                                                     power_scale,
713                                                                     power_trace,
714                                                                     state_initial,
715                                                                     state_trace,
716                                                                     cpu_properties);
717 }
718
719 /**
720  * create link resource via network Model
721  */
722 void* surf_link_create_resource(char *name,
723                                double bw_initial,
724                                tmgr_trace_t bw_trace,
725                                double lat_initial,
726                                tmgr_trace_t lat_trace,
727                                e_surf_resource_state_t
728                                state_initial,
729                                tmgr_trace_t state_trace,
730                                e_surf_link_sharing_policy_t policy,
731                                xbt_dict_t properties)
732 {
733   return surf_network_model->extension.network.create_resource(name,
734                                                                bw_initial,
735                                                                bw_trace,
736                                                                lat_initial,
737                                                                lat_trace,
738                                                                state_initial,
739                                                                state_trace,
740                                                                policy,
741                                                                properties);
742 }
743
744 /**
745  * create link resource via workstation_ptask_L07 model
746  */
747
748 void* surf_wsL07_link_create_resource(char *name,
749                                      double bw_initial,
750                                      tmgr_trace_t bw_trace,
751                                      double lat_initial,
752                                      tmgr_trace_t lat_trace,
753                                      e_surf_resource_state_t
754                                      state_initial,
755                                      tmgr_trace_t state_trace,
756                                      e_surf_link_sharing_policy_t
757                                      policy, xbt_dict_t properties)
758 {
759   return surf_workstation_model->extension.workstation.
760       link_create_resource(name, bw_initial, bw_trace, lat_initial,
761                            lat_trace, state_initial, state_trace, policy,
762                            properties);
763 }
764
765 /**
766  *
767  *init new routing model component
768  */
769
770 void surf_AS_new(const char *AS_id, const char *AS_mode)
771 {
772   routing_AS_init(AS_id, AS_mode);
773 }
774
775 void surf_AS_finalize(const char *AS_id)
776 {
777   routing_AS_end(AS_id);
778 }
779
780 /*
781  * add host to the network element list
782  */
783 void surf_route_add_host(const char *host_id)
784 {
785   routing_add_host(host_id);
786 }
787
788 /**
789  * set route
790  */
791 void surf_routing_add_route(const char *src_id, const char *dst_id,
792                             xbt_dynar_t links_id)
793 {
794   unsigned int i;
795   const char *link_id;
796   routing_set_route(src_id, dst_id);
797   xbt_dynar_foreach(links_id, i, link_id) {
798     routing_add_link(link_id);
799   }
800
801   //store the route
802   routing_store_route();
803 }
804
805 /**
806  * Add Traces
807  */
808 void surf_add_host_traces(void)
809 {
810   return surf_cpu_model->extension.cpu.add_traces();
811 }
812
813 void surf_add_link_traces(void)
814 {
815   return surf_network_model->extension.network.add_traces();
816 }
817
818 void surf_wsL07_add_traces(void)
819 {
820   return surf_workstation_model->extension.workstation.add_traces();
821 }