Logo AND Algorithmique Numérique Distribuée

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