Logo AND Algorithmique Numérique Distribuée

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