Logo AND Algorithmique Numérique Distribuée

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