Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
348a8577d355f1e29e57b91c75e13bcf59e08f45
[simgrid.git] / src / surf / surfxml_parse.c
1 /*      $Id$     */
2
3 /* Copyright (c) 2004 Arnaud Legrand. All rights reserved.                  */
4
5 /* This program is free software; you can redistribute it and/or modify it
6  * under the terms of the license (GNU LGPL) which comes with this package. */
7
8 #include "xbt/misc.h"
9 #include "xbt/log.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
17 #undef CLEANUP
18 #include "surfxml.c"
19
20 /* Initialize the parsing globals */
21 int route_action = 0;
22 xbt_dict_t traces_set_list = NULL;
23 xbt_dynar_t traces_connect_list = NULL;
24
25 /* This buffer is used to store the original buffer before substituing it by out own buffer. Usefull for the foreach tag */
26 char* old_buff;
27 /* Stores the set name reffered to by the foreach tag */
28 static const char* foreach_set_name;
29 static xbt_dynar_t main_STag_surfxml_host_cb_list = NULL;
30 static xbt_dynar_t main_ETag_surfxml_host_cb_list = NULL;
31 static xbt_dynar_t main_STag_surfxml_link_cb_list = NULL;
32 static xbt_dynar_t main_ETag_surfxml_link_cb_list = NULL;
33
34 /* make sure these symbols are defined as strong ones in this file so that the linked can resolve them */
35 xbt_dynar_t STag_surfxml_platform_cb_list = NULL;
36 xbt_dynar_t ETag_surfxml_platform_cb_list = NULL;
37 xbt_dynar_t STag_surfxml_host_cb_list = NULL;
38 xbt_dynar_t ETag_surfxml_host_cb_list = NULL;
39 xbt_dynar_t STag_surfxml_router_cb_list = NULL;
40 xbt_dynar_t ETag_surfxml_router_cb_list = NULL;
41 xbt_dynar_t STag_surfxml_link_cb_list = NULL;
42 xbt_dynar_t ETag_surfxml_link_cb_list = NULL;
43 xbt_dynar_t STag_surfxml_route_cb_list = NULL;
44 xbt_dynar_t ETag_surfxml_route_cb_list = NULL;
45 xbt_dynar_t STag_surfxml_link_c_ctn_cb_list = NULL;
46 xbt_dynar_t ETag_surfxml_link_c_ctn_cb_list = NULL;
47 xbt_dynar_t STag_surfxml_process_cb_list = NULL;
48 xbt_dynar_t ETag_surfxml_process_cb_list = NULL;
49 xbt_dynar_t STag_surfxml_argument_cb_list = NULL;
50 xbt_dynar_t ETag_surfxml_argument_cb_list = NULL;
51 xbt_dynar_t STag_surfxml_prop_cb_list = NULL;
52 xbt_dynar_t ETag_surfxml_prop_cb_list = NULL;
53 xbt_dynar_t STag_surfxml_set_cb_list = NULL;
54 xbt_dynar_t ETag_surfxml_set_cb_list = NULL;
55 xbt_dynar_t STag_surfxml_foreach_cb_list = NULL;
56 xbt_dynar_t ETag_surfxml_foreach_cb_list = NULL;
57 xbt_dynar_t STag_surfxml_route_c_multi_cb_list = NULL;
58 xbt_dynar_t ETag_surfxml_route_c_multi_cb_list = NULL;
59 xbt_dynar_t STag_surfxml_cluster_cb_list = NULL;
60 xbt_dynar_t ETag_surfxml_cluster_cb_list = NULL;
61 xbt_dynar_t STag_surfxml_trace_cb_list = NULL;
62 xbt_dynar_t ETag_surfxml_trace_cb_list = NULL;
63 xbt_dynar_t STag_surfxml_trace_c_connect_cb_list = NULL;
64 xbt_dynar_t ETag_surfxml_trace_c_connect_cb_list = NULL;
65 xbt_dynar_t STag_surfxml_random_cb_list = NULL;
66 xbt_dynar_t ETag_surfxml_random_cb_list = NULL;
67
68 /* Stores the sets defined in the XML */
69 xbt_dict_t set_list = NULL;
70
71 xbt_dict_t current_property_set = NULL;
72
73 /* For the route:multi tag */
74 xbt_dict_t route_table = NULL;
75 xbt_dict_t route_multi_table = NULL;
76 xbt_dynar_t route_multi_elements = NULL;
77 xbt_dynar_t route_link_list = NULL;
78 xbt_dynar_t links = NULL;
79 xbt_dynar_t keys = NULL;
80
81 xbt_dict_t random_data_list = NULL;
82
83 static xbt_dynar_t surf_input_buffer_stack = NULL;
84 static xbt_dynar_t surf_file_to_parse_stack = NULL;
85
86 static XBT_INLINE void surfxml_call_cb_functions(xbt_dynar_t);
87
88 YY_BUFFER_STATE surf_input_buffer;
89 FILE *surf_file_to_parse = NULL;
90
91 static void convert_route_multi_to_routes(void);
92
93 void surf_parse_free_callbacks(void)
94 {
95   xbt_dynar_free(&STag_surfxml_platform_cb_list);
96   xbt_dynar_free(&ETag_surfxml_platform_cb_list);
97   xbt_dynar_free(&STag_surfxml_host_cb_list);
98   xbt_dynar_free(&ETag_surfxml_host_cb_list);
99   xbt_dynar_free(&STag_surfxml_router_cb_list);
100   xbt_dynar_free(&ETag_surfxml_router_cb_list);
101   xbt_dynar_free(&STag_surfxml_link_cb_list);
102   xbt_dynar_free(&ETag_surfxml_link_cb_list);
103   xbt_dynar_free(&STag_surfxml_route_cb_list);
104   xbt_dynar_free(&ETag_surfxml_route_cb_list);
105   xbt_dynar_free(&STag_surfxml_link_c_ctn_cb_list);
106   xbt_dynar_free(&ETag_surfxml_link_c_ctn_cb_list);
107   xbt_dynar_free(&STag_surfxml_process_cb_list);
108   xbt_dynar_free(&ETag_surfxml_process_cb_list);
109   xbt_dynar_free(&STag_surfxml_argument_cb_list);
110   xbt_dynar_free(&ETag_surfxml_argument_cb_list);
111   xbt_dynar_free(&STag_surfxml_prop_cb_list);
112   xbt_dynar_free(&ETag_surfxml_prop_cb_list);
113   xbt_dynar_free(&STag_surfxml_set_cb_list);
114   xbt_dynar_free(&ETag_surfxml_set_cb_list);
115   xbt_dynar_free(&STag_surfxml_foreach_cb_list);
116   xbt_dynar_free(&ETag_surfxml_foreach_cb_list);
117   xbt_dynar_free(&STag_surfxml_route_c_multi_cb_list);
118   xbt_dynar_free(&ETag_surfxml_route_c_multi_cb_list);
119   xbt_dynar_free(&STag_surfxml_cluster_cb_list);
120   xbt_dynar_free(&ETag_surfxml_cluster_cb_list);
121   xbt_dynar_free(&STag_surfxml_trace_cb_list);
122   xbt_dynar_free(&ETag_surfxml_trace_cb_list);
123   xbt_dynar_free(&STag_surfxml_trace_c_connect_cb_list);
124   xbt_dynar_free(&ETag_surfxml_trace_c_connect_cb_list);
125   xbt_dynar_free(&STag_surfxml_random_cb_list);
126   xbt_dynar_free(&ETag_surfxml_random_cb_list);
127 }
128
129 void surf_parse_reset_parser(void)
130 {
131   surf_parse_free_callbacks();
132   STag_surfxml_platform_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
133   ETag_surfxml_platform_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
134   STag_surfxml_host_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
135   ETag_surfxml_host_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
136   STag_surfxml_router_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
137   ETag_surfxml_router_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
138   STag_surfxml_link_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
139   ETag_surfxml_link_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
140   STag_surfxml_route_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
141   ETag_surfxml_route_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
142   STag_surfxml_link_c_ctn_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
143   ETag_surfxml_link_c_ctn_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
144   STag_surfxml_process_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
145   ETag_surfxml_process_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
146   STag_surfxml_argument_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
147   ETag_surfxml_argument_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
148   STag_surfxml_prop_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
149   ETag_surfxml_prop_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
150   STag_surfxml_set_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
151   ETag_surfxml_set_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
152   STag_surfxml_foreach_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
153   ETag_surfxml_foreach_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
154   STag_surfxml_route_c_multi_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
155   ETag_surfxml_route_c_multi_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
156   STag_surfxml_cluster_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
157   ETag_surfxml_cluster_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
158   STag_surfxml_trace_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
159   ETag_surfxml_trace_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
160   STag_surfxml_trace_c_connect_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
161   ETag_surfxml_trace_c_connect_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
162   STag_surfxml_random_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
163   ETag_surfxml_random_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
164 }
165
166 void STag_surfxml_include(void)
167 {
168   xbt_dynar_push(surf_input_buffer_stack, &surf_input_buffer);
169   xbt_dynar_push(surf_file_to_parse_stack, &surf_file_to_parse);
170
171   surf_file_to_parse = surf_fopen(A_surfxml_include_file, "r");
172   xbt_assert1((surf_file_to_parse), "Unable to open \"%s\"\n",
173               A_surfxml_include_file);
174   surf_input_buffer = surf_parse__create_buffer(surf_file_to_parse, 10);
175   surf_parse__switch_to_buffer(surf_input_buffer);
176   printf("STAG\n");
177   fflush(NULL);
178 }
179
180 void ETag_surfxml_include(void)
181 {
182   printf("ETAG\n");
183   fflush(NULL);
184   surf_parse__delete_buffer(surf_input_buffer);
185   fclose(surf_file_to_parse);
186   xbt_dynar_pop(surf_file_to_parse_stack, &surf_file_to_parse);
187   xbt_dynar_pop(surf_input_buffer_stack, &surf_input_buffer);
188 }
189
190 void STag_surfxml_platform(void)
191 {
192   double version = 0.0;
193
194   sscanf(A_surfxml_platform_version, "%lg", &version);
195
196   xbt_assert0((version >= 1.0), "******* BIG FAT WARNING *********\n "
197               "You're using an ancient XML file. "
198               "Since SimGrid 3.1, units are Bytes, Flops, and seconds "
199               "instead of MBytes, MFlops and seconds. "
200               "A script (surfxml_update.pl) to help you convert your old "
201               "platform files "
202               "is available in the contrib/platform_generation directory "
203               "of the simgrid repository. Please check also out the "
204               "SURF section of the ChangeLog for the 3.1 version. "
205               "Last, do not forget to also update your values for "
206               "the calls to MSG_task_create (if any).");
207   xbt_assert0((version >= 2.0), "******* BIG FAT WARNING *********\n "
208               "You're using an old XML file. "
209               "A script (surfxml_update.pl) to help you convert your old "
210               "platform files "
211               "is available in the contrib/platform_generation directory "
212               "of the simgrid repository.");
213
214   if (set_list == NULL) set_list = xbt_dict_new(); 
215
216   surfxml_call_cb_functions(STag_surfxml_platform_cb_list);
217
218 }
219
220 void ETag_surfxml_platform(void)
221 {
222   convert_route_multi_to_routes();
223
224   surfxml_call_cb_functions(ETag_surfxml_platform_cb_list);
225
226   xbt_dict_free(&random_data_list);
227   xbt_dict_free(&set_list);
228
229 }
230
231 void STag_surfxml_host(void)
232 {
233   surfxml_call_cb_functions(STag_surfxml_host_cb_list);
234 }
235
236 void ETag_surfxml_host(void)
237 {
238   surfxml_call_cb_functions(ETag_surfxml_host_cb_list);
239 }
240
241 void STag_surfxml_router(void)
242 {
243   surfxml_call_cb_functions(STag_surfxml_router_cb_list);
244 }
245
246 void ETag_surfxml_router(void)
247 {
248   surfxml_call_cb_functions(ETag_surfxml_router_cb_list);
249 }
250
251 void STag_surfxml_link(void)
252 {
253   surfxml_call_cb_functions(STag_surfxml_link_cb_list);
254 }
255
256 void ETag_surfxml_link(void)
257 {
258   surfxml_call_cb_functions(ETag_surfxml_link_cb_list);
259 }
260
261 void STag_surfxml_route(void)
262 {
263   surfxml_call_cb_functions(STag_surfxml_route_cb_list);
264 }
265
266 void ETag_surfxml_route(void)
267 {
268   surfxml_call_cb_functions(ETag_surfxml_route_cb_list);
269 }
270
271 void STag_surfxml_link_c_ctn(void)
272 {
273   surfxml_call_cb_functions(STag_surfxml_link_c_ctn_cb_list);
274 }
275
276 void ETag_surfxml_link_c_ctn(void)
277 {
278   surfxml_call_cb_functions(ETag_surfxml_link_c_ctn_cb_list);
279 }
280
281 void STag_surfxml_process(void)
282 {
283   surfxml_call_cb_functions(STag_surfxml_process_cb_list);
284 }
285
286 void ETag_surfxml_process(void)
287 {
288   surfxml_call_cb_functions(ETag_surfxml_process_cb_list);
289 }
290
291 void STag_surfxml_argument(void)
292 {
293   surfxml_call_cb_functions(STag_surfxml_argument_cb_list);
294 }
295
296 void ETag_surfxml_argument(void)
297 {
298   surfxml_call_cb_functions(ETag_surfxml_argument_cb_list);
299 }
300
301 void STag_surfxml_prop(void)
302 {
303   surfxml_call_cb_functions(STag_surfxml_prop_cb_list);
304 }
305 void ETag_surfxml_prop(void)
306 {
307   surfxml_call_cb_functions(ETag_surfxml_prop_cb_list);
308 }
309
310 void STag_surfxml_set(void)
311 {
312   surfxml_call_cb_functions(STag_surfxml_set_cb_list);
313 }
314
315 void ETag_surfxml_set(void)
316 {
317   surfxml_call_cb_functions(ETag_surfxml_set_cb_list);
318 }
319
320 void STag_surfxml_foreach(void)
321 {
322   /* Save the current buffer */
323   old_buff = surfxml_bufferstack;
324   surfxml_call_cb_functions(STag_surfxml_foreach_cb_list);
325 }
326
327 void ETag_surfxml_foreach(void)
328
329   surfxml_call_cb_functions(ETag_surfxml_foreach_cb_list);
330
331   /* free the temporary dynar and restore original */
332   xbt_dynar_free(&STag_surfxml_host_cb_list);
333   xbt_dynar_free(&ETag_surfxml_host_cb_list);
334
335   STag_surfxml_host_cb_list = main_STag_surfxml_host_cb_list;
336   ETag_surfxml_host_cb_list = main_ETag_surfxml_host_cb_list;
337
338   /* free the temporary dynar and restore original */
339   xbt_dynar_free(&STag_surfxml_link_cb_list);
340   xbt_dynar_free(&ETag_surfxml_link_cb_list);
341
342   STag_surfxml_link_cb_list = main_STag_surfxml_link_cb_list;
343   ETag_surfxml_link_cb_list = main_ETag_surfxml_link_cb_list;
344
345 }
346
347 void STag_surfxml_route_c_multi(void)
348 {
349   surfxml_call_cb_functions(STag_surfxml_route_c_multi_cb_list);
350 }
351
352 void ETag_surfxml_route_c_multi(void)
353 {
354   surfxml_call_cb_functions(ETag_surfxml_route_c_multi_cb_list);
355 }
356
357 void STag_surfxml_cluster(void)
358 {
359   surfxml_call_cb_functions(STag_surfxml_cluster_cb_list);
360 }
361
362 void ETag_surfxml_cluster(void)
363 {
364   surfxml_call_cb_functions(ETag_surfxml_cluster_cb_list);
365 }
366
367 void STag_surfxml_trace(void)
368 {
369   surfxml_call_cb_functions(STag_surfxml_trace_cb_list);
370 }
371
372 void ETag_surfxml_trace(void)
373 {
374   surfxml_call_cb_functions(ETag_surfxml_trace_cb_list);
375 }
376
377 void STag_surfxml_trace_c_connect(void)
378 {
379   surfxml_call_cb_functions(STag_surfxml_trace_c_connect_cb_list);
380 }
381
382 void ETag_surfxml_trace_c_connect(void)
383 {
384   surfxml_call_cb_functions(ETag_surfxml_trace_c_connect_cb_list);
385 }
386
387 void STag_surfxml_random(void)
388 {
389   surfxml_call_cb_functions(STag_surfxml_random_cb_list);
390 }
391
392 void ETag_surfxml_random(void)
393 {
394   surfxml_call_cb_functions(ETag_surfxml_random_cb_list);
395 }
396
397 void surf_parse_open(const char *file)
398 {
399   static int warned = 0;        /* warn only once */
400   if (!file) {
401     if (!warned) {
402       WARN0
403           ("Bypassing the XML parser since surf_parse_open received a NULL pointer. If it is not what you want, go fix your code.");
404       warned = 1;
405     }
406     return;
407   }
408   if (!surf_input_buffer_stack)
409     surf_input_buffer_stack = xbt_dynar_new(sizeof(YY_BUFFER_STATE), NULL);
410   if (!surf_file_to_parse_stack)
411     surf_file_to_parse_stack = xbt_dynar_new(sizeof(FILE *), NULL);
412
413   surf_file_to_parse = surf_fopen(file, "r");
414   xbt_assert1((surf_file_to_parse), "Unable to open \"%s\"\n", file);
415   surf_input_buffer = surf_parse__create_buffer(surf_file_to_parse, 10);
416   surf_parse__switch_to_buffer(surf_input_buffer);
417   surf_parse_lineno = 1;
418 }
419
420 void surf_parse_close(void)
421 {
422   if (surf_input_buffer_stack)
423     xbt_dynar_free(&surf_input_buffer_stack);
424   if (surf_file_to_parse_stack)
425     xbt_dynar_free(&surf_file_to_parse_stack);
426
427   if (surf_file_to_parse) {
428     surf_parse__delete_buffer(surf_input_buffer);
429     fclose(surf_file_to_parse);
430   }
431 }
432
433
434 static int _surf_parse(void)
435 {
436   return surf_parse_lex();
437 }
438
439 int_f_void_t surf_parse = _surf_parse;
440
441 void surf_parse_get_double(double *value, const char *string)
442 {
443   int ret = 0;
444
445   ret = sscanf(string, "%lg", value);
446   xbt_assert2((ret == 1), "Parse error line %d : %s not a number",
447               surf_parse_lineno, string);
448 }
449
450 void surf_parse_get_int(int *value, const char *string)
451 {
452   int ret = 0;
453
454   ret = sscanf(string, "%d", value);
455   xbt_assert2((ret == 1), "Parse error line %d : %s not a number",
456               surf_parse_lineno, string);
457 }
458
459 void surf_parse_get_trace(tmgr_trace_t * trace, const char *string)
460 {
461   if ((!string) || (strcmp(string, "") == 0))
462     *trace = NULL;
463   else
464     *trace = tmgr_trace_new(string);
465 }
466
467 void parse_properties(void)
468 {
469   char *value = NULL;
470
471   if(!current_property_set) current_property_set = xbt_dict_new();
472
473    value = xbt_strdup(A_surfxml_prop_value);  
474    xbt_dict_set(current_property_set, A_surfxml_prop_id, value, free);
475
476 }
477
478 void free_string(void *d)
479 {
480   free(*(void**)d);
481 }
482
483 void surfxml_add_callback(xbt_dynar_t cb_list, void_f_void_t function)
484 {
485   xbt_dynar_push(cb_list, &function);
486 }
487
488 static XBT_INLINE void surfxml_call_cb_functions(xbt_dynar_t cb_list)
489 {
490   unsigned int iterator;
491   void_f_void_t fun;
492   xbt_dynar_foreach(cb_list, iterator, fun){
493        DEBUG2("call %p %p",fun,*fun);
494        (*fun)();
495     }
496 }
497
498 void init_data(void)
499 {
500   xbt_dict_free(&route_table);
501   xbt_dynar_free(&route_link_list);
502   route_table = xbt_dict_new();
503
504   route_multi_table = xbt_dict_new();
505   route_multi_elements = xbt_dynar_new(sizeof(char*), NULL);
506   traces_set_list = xbt_dict_new();
507   traces_connect_list = xbt_dynar_new(sizeof(char*), NULL);
508   random_data_list = xbt_dict_new();
509
510 }
511
512 void parse_platform_file(const char* file)
513 {
514   surf_parse_open(file);
515   xbt_assert1((!(*surf_parse)()), "Parse error in %s", file);
516   surf_parse_close();
517 }
518
519 /* Functions to bypass route, host and link tags. Used by the foreach and route:multi tags */
520
521 static void parse_make_temporary_route(const char *src, const char *dst, int action)
522 {
523   int AX_ptr = 0;
524   surfxml_bufferstack = xbt_new0(char, 2048);
525   
526   A_surfxml_route_action = action;
527   SURFXML_BUFFER_SET(route_src,                     src);
528   SURFXML_BUFFER_SET(route_dst,                     dst);
529   SURFXML_BUFFER_SET(route_impact_on_src,                 "0.0");
530   SURFXML_BUFFER_SET(route_impact_on_dst,                 "0.0");
531   SURFXML_BUFFER_SET(route_impact_on_src_with_other_recv, "0.0");
532   SURFXML_BUFFER_SET(route_impact_on_dst_with_other_send, "0.0");
533 }
534
535 static void parse_change_cpu_data(const char* hostName, const char* surfxml_host_power, const char* surfxml_host_availability,
536                                         const char* surfxml_host_availability_file, const char* surfxml_host_state_file,
537                                         const char* surfxml_host_interference_send, const char* surfxml_host_interference_recv,
538                                         const char* surfxml_host_interference_send_recv, const char* surfxml_host_max_outgoing_rate)
539 {
540   int AX_ptr = 0;
541   surfxml_bufferstack = xbt_new0(char, 2048);
542  
543   SURFXML_BUFFER_SET(host_id,                     hostName);
544   SURFXML_BUFFER_SET(host_power,                  surfxml_host_power /*hostPower*/);
545   SURFXML_BUFFER_SET(host_availability,           surfxml_host_availability);
546   SURFXML_BUFFER_SET(host_availability_file,      surfxml_host_availability_file);
547   SURFXML_BUFFER_SET(host_state_file,             surfxml_host_state_file);
548   SURFXML_BUFFER_SET(host_interference_send,      surfxml_host_interference_send);
549   SURFXML_BUFFER_SET(host_interference_recv,      surfxml_host_interference_recv);
550   SURFXML_BUFFER_SET(host_interference_send_recv, surfxml_host_interference_send_recv);
551   SURFXML_BUFFER_SET(host_max_outgoing_rate,      surfxml_host_max_outgoing_rate);
552 }
553
554 static void parse_change_link_data(const char* linkName, const char* surfxml_link_bandwidth, const char* surfxml_link_bandwidth_file,
555                                         const char* surfxml_link_latency, const char* surfxml_link_latency_file, const char* surfxml_link_state_file)
556 {
557   int AX_ptr = 0;
558   surfxml_bufferstack = xbt_new0(char, 2048);
559  
560   SURFXML_BUFFER_SET(link_id,                linkName);
561   SURFXML_BUFFER_SET(link_bandwidth,         surfxml_link_bandwidth);
562   SURFXML_BUFFER_SET(link_bandwidth_file,    surfxml_link_bandwidth_file);
563   SURFXML_BUFFER_SET(link_latency,           surfxml_link_latency);
564   SURFXML_BUFFER_SET(link_latency_file,      surfxml_link_latency_file);
565   SURFXML_BUFFER_SET(link_state_file,        surfxml_link_state_file);
566 }
567
568 /**
569 * \brief Restores the original surfxml buffer
570 */
571 static void parse_restore_original_buffer(void)
572 {
573   free(surfxml_bufferstack);
574   surfxml_bufferstack = old_buff;
575 }
576
577 /* Functions for the sets and foreach tags */
578
579 void parse_sets(void)
580 {
581   char *id, *suffix, *prefix, *radical;
582   int start, end;
583   xbt_dynar_t radical_ends;
584   xbt_dynar_t current_set;
585   char *value;
586   int i;
587
588   id = xbt_strdup(A_surfxml_set_id);
589   prefix = xbt_strdup(A_surfxml_set_prefix);
590   suffix = xbt_strdup(A_surfxml_set_suffix);
591   radical = xbt_strdup(A_surfxml_set_radical);
592   
593   xbt_assert1(!xbt_dict_get_or_null(set_list, id),
594               "Set '%s' declared several times in the platform file.",id);  
595   radical_ends = xbt_str_split(radical, "-");
596   xbt_assert1((xbt_dynar_length(radical_ends)==2), "Radical must be in the form lvalue-rvalue! Provided value: %s", radical);
597
598   surf_parse_get_int(&start, xbt_dynar_get_as(radical_ends, 0, char*));
599   surf_parse_get_int(&end, xbt_dynar_get_as(radical_ends, 1, char*));
600
601   current_set = xbt_dynar_new(sizeof(char*), NULL);
602
603   
604   for (i=start; i<end; i++) {
605      value = bprintf("%s%d%s", prefix, i, suffix);
606      xbt_dynar_push(current_set, &value);
607   } 
608   
609   xbt_dict_set(set_list, id, current_set, NULL);
610 }
611
612 static void parse_host_foreach(void)
613 {
614   xbt_dynar_t names = NULL;
615   unsigned int cpt = 0;
616   char *name;
617   const char* surfxml_host_power;
618   const char* surfxml_host_availability;
619   const char* surfxml_host_availability_file;
620   const char* surfxml_host_state_file;
621   const char* surfxml_host_interference_send;
622   const char* surfxml_host_interference_recv;
623   const char *surfxml_host_interference_send_recv;
624   const char* surfxml_host_max_outgoing_rate;
625   
626   xbt_assert1((names = xbt_dict_get_or_null(set_list, foreach_set_name)),
627               "Set name '%s' reffered by foreach tag not found.", foreach_set_name);  
628
629   xbt_assert1((strcmp(A_surfxml_host_id, "$1") == 0), "The id of the host within the foreach should point to the foreach set_id (use $1). Your value: %s", A_surfxml_host_id);
630
631   surfxml_host_power = A_surfxml_host_power;
632   surfxml_host_availability = A_surfxml_host_availability;
633   surfxml_host_availability_file = A_surfxml_host_availability_file;
634   surfxml_host_state_file = A_surfxml_host_state_file;
635   surfxml_host_interference_send = A_surfxml_host_interference_send;
636   surfxml_host_interference_recv = A_surfxml_host_interference_recv;
637   surfxml_host_interference_send_recv = A_surfxml_host_interference_send_recv;
638   surfxml_host_max_outgoing_rate = A_surfxml_host_max_outgoing_rate;
639
640   /* foreach name in set call the main host callback */
641   xbt_dynar_foreach (names, cpt, name) {
642     parse_change_cpu_data(name, surfxml_host_power, surfxml_host_availability,
643                                         surfxml_host_availability_file, surfxml_host_state_file,
644                                         surfxml_host_interference_send, surfxml_host_interference_recv,
645                                         surfxml_host_interference_send_recv, surfxml_host_max_outgoing_rate);
646     surfxml_call_cb_functions(main_STag_surfxml_host_cb_list);
647     surfxml_call_cb_functions(main_ETag_surfxml_host_cb_list);
648     free(surfxml_bufferstack);
649   }
650
651   surfxml_bufferstack = old_buff;
652   
653 }
654
655 static void parse_link_foreach(void)
656 {
657   xbt_dynar_t names = NULL;
658   unsigned int cpt = 0;
659   char *name;
660   const char* surfxml_link_bandwidth;
661   const char* surfxml_link_bandwidth_file;
662   const char* surfxml_link_latency;
663   const char* surfxml_link_latency_file;
664   const char* surfxml_link_state_file;
665
666   xbt_assert1((names = xbt_dict_get_or_null(set_list, foreach_set_name)),
667               "Set name '%s' reffered by foreach tag not found.", foreach_set_name); 
668
669   xbt_assert1((strcmp(A_surfxml_link_id, "$1") == 0), "The id of the link within the foreach should point to the foreach set_id (use $1). Your value: %s", A_surfxml_link_id);
670
671   surfxml_link_bandwidth = A_surfxml_link_bandwidth;
672   surfxml_link_bandwidth_file = A_surfxml_link_bandwidth_file;
673   surfxml_link_latency = A_surfxml_link_latency;
674   surfxml_link_latency_file = A_surfxml_link_latency_file;
675   surfxml_link_state_file = A_surfxml_link_state_file;
676   /* for each name in set call the main link callback */
677   xbt_dynar_foreach (names, cpt, name) {
678     parse_change_link_data(name, surfxml_link_bandwidth, surfxml_link_bandwidth_file,
679                                         surfxml_link_latency, surfxml_link_latency_file, surfxml_link_state_file);
680     surfxml_call_cb_functions(main_STag_surfxml_link_cb_list);
681     surfxml_call_cb_functions(main_ETag_surfxml_link_cb_list);
682    free(surfxml_bufferstack);
683
684   }
685
686   surfxml_bufferstack = old_buff;
687 }
688
689 void parse_foreach(void)
690 {
691   /* save the host & link callbacks */
692   main_STag_surfxml_host_cb_list = STag_surfxml_host_cb_list;
693   main_ETag_surfxml_host_cb_list = ETag_surfxml_host_cb_list;
694   main_STag_surfxml_link_cb_list = STag_surfxml_link_cb_list;
695   main_ETag_surfxml_link_cb_list = ETag_surfxml_link_cb_list;
696
697   /* define host & link callbacks to be used only by the foreach tag */
698   STag_surfxml_host_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
699   ETag_surfxml_host_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
700   STag_surfxml_link_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
701   ETag_surfxml_link_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
702
703   surfxml_add_callback(STag_surfxml_host_cb_list, &parse_host_foreach);
704   surfxml_add_callback(STag_surfxml_link_cb_list, &parse_link_foreach);
705
706   /* get set name */
707   foreach_set_name = xbt_strdup(A_surfxml_foreach_set_id); 
708 }
709
710 /* Route:multi functions */
711
712 static int route_multi_size=0;
713 static char* src_name, *dst_name;
714 static int is_symmetric_route;
715
716 void parse_route_elem(void)
717 {
718   char *val;
719
720   val = xbt_strdup(A_surfxml_link_c_ctn_id);
721   
722   xbt_dynar_push(route_link_list, &val);
723 }
724
725 void parse_route_multi_set_endpoints(void)
726 {
727   src_name = xbt_strdup(A_surfxml_route_c_multi_src); 
728   dst_name = xbt_strdup(A_surfxml_route_c_multi_dst); 
729   route_action = A_surfxml_route_c_multi_action;
730   is_symmetric_route = A_surfxml_route_c_multi_symmetric;
731   route_multi_size++;
732
733   route_link_list = xbt_dynar_new(sizeof(char *), &free_string);
734 }
735
736 static int contains(xbt_dynar_t list, const char* value)
737 {
738  unsigned int cpt;
739  char * val;
740  xbt_dynar_foreach(list, cpt, val) {
741    if (strcmp(val, value) == 0)
742       return 1;
743   }
744   return 0;
745 }
746
747 /* 
748    This function is used to append or override the contents of an alread existing route in the case a new one with its name is found.
749    The decision is based upon the value of action specified in the xml route:multi attribute action
750  */
751 void manage_route(xbt_dict_t routing_table, const char *route_name, int action, int isMultiRoute)
752 {
753   unsigned int cpt;
754   xbt_dynar_t links;
755   char *value;
756
757   /* get already existing list if it exists */
758   links = xbt_dict_get_or_null(routing_table, route_name);
759   DEBUG1("ROUTE: %s", route_name);
760   if (links != NULL) {
761      switch (action) {
762         case A_surfxml_route_c_multi_action_PREPEND: /* add existing links at the end; route_link_list + links */
763                                     xbt_dynar_foreach(links, cpt, value) {      
764                                        xbt_dynar_push(route_link_list,&value);
765                                     }
766                                     break;
767         case A_surfxml_route_c_multi_action_POSTPEND: /* add existing links in front; links + route_link_list */ 
768                                     xbt_dynar_foreach(route_link_list, cpt, value) {
769                                        xbt_dynar_push(links,&value);
770                                     }
771                                     route_link_list = links;
772                                     break;
773         case A_surfxml_route_c_multi_action_OVERRIDE:
774                                     break;
775         default:break;
776      }
777   }
778   /* this is the final route; do not add if name is a set; add only if name is in set list */
779   if (!isMultiRoute){    
780     xbt_dict_set(routing_table, route_name, route_link_list, NULL);
781   }
782 }
783
784 void parse_route_multi_set_route(void)
785 {
786   char* route_name;
787
788   route_name = bprintf("%s#%s#%d#%d#%d", src_name, dst_name, route_action, is_symmetric_route, route_multi_size);
789
790   xbt_dynar_push(route_multi_elements, &route_name);
791
792   /* Add route */
793   xbt_dict_set(route_multi_table, route_name, route_link_list, NULL);
794   /* add symmetric if it is the case */ 
795   if (is_symmetric_route == 1) {
796     char * symmetric_name = bprintf("%s#%s#%d#%d#%d", dst_name, src_name, route_action, !is_symmetric_route, route_multi_size);
797   
798     xbt_dict_set(route_multi_table, symmetric_name, route_link_list, NULL);
799     xbt_dynar_push(route_multi_elements, &symmetric_name);
800     is_symmetric_route = 0;
801   }
802   free(src_name);
803   free(dst_name);
804 }
805
806 static void add_multi_links(const char* src, const char* dst, xbt_dynar_t links, const char* src_name, const char* dst_name)
807 {
808   unsigned int cpt;
809   char* value, *val;
810    parse_make_temporary_route(src_name, dst_name, route_action);
811    surfxml_call_cb_functions(STag_surfxml_route_cb_list);
812    DEBUG2("\tADDING ROUTE: %s -> %s", src_name, dst_name);
813    /* Build link list */ 
814    xbt_dynar_foreach(links, cpt, value) {
815      if (strcmp(value, src) == 0)
816        val =  xbt_strdup(src_name);
817      else if (strcmp(value, dst) == 0)
818        val = xbt_strdup(dst_name);
819      else if (strcmp(value, "$dst") == 0)
820        val = xbt_strdup(dst_name);
821      else if (strcmp(value, "$src") == 0)
822        val = xbt_strdup(src_name);
823      else
824        val = xbt_strdup(value);
825      DEBUG1("\t\tELEMENT: %s", val);
826      xbt_dynar_push(route_link_list, &val);
827    }    
828    surfxml_call_cb_functions(ETag_surfxml_route_cb_list);
829    free(surfxml_bufferstack);
830 }
831
832 static void convert_route_multi_to_routes(void)
833 {
834   xbt_dict_cursor_t cursor_w;
835   int symmetric;
836   unsigned int cpt, cpt2, cursor;
837   char *src_host_name, *dst_host_name, *key, *src, *dst, *val, *key_w, *data_w; 
838   const char* sep="#";
839   xbt_dynar_t src_names = NULL, dst_names = NULL, links;
840
841   if (!route_multi_elements) return;
842
843   old_buff = surfxml_bufferstack;
844   /* Get all routes in the exact order they were entered in the platform file */
845   xbt_dynar_foreach(route_multi_elements, cursor, key) {
846      /* Get links for the route */     
847      links = (xbt_dynar_t)xbt_dict_get_or_null(route_multi_table, key);
848      keys = xbt_str_split_str(key, sep);
849      /* Get route ends */
850      src = xbt_dynar_get_as(keys, 0, char*);
851      dst = xbt_dynar_get_as(keys, 1, char*);
852      route_action = atoi(xbt_dynar_get_as(keys, 2, char*));
853      symmetric = atoi(xbt_dynar_get_as(keys, 3, char*));
854
855     /* Create the dynar of src and dst hosts for the new routes */ 
856     /* NOTE: src and dst can be either set names or simple host names */
857     src_names = (xbt_dynar_t)xbt_dict_get_or_null(set_list, src);
858     dst_names = (xbt_dynar_t)xbt_dict_get_or_null(set_list, dst);
859     /* Add to dynar even if they are simple names */
860     if (src_names == NULL) {
861        src_names = xbt_dynar_new(sizeof(char *), &free_string);
862        val = xbt_strdup(src);
863        xbt_dynar_push(src_names, &val);
864     }
865     if (dst_names == NULL) {
866        dst_names = xbt_dynar_new(sizeof(char *), &free_string);
867        val = xbt_strdup(dst);
868        xbt_dynar_push(dst_names, &val);
869     }
870     /* Build the routes */
871     DEBUG2("ADDING MULTI ROUTE: %s -> %s", xbt_dynar_get_as(keys, 0, char*), xbt_dynar_get_as(keys, 1, char*));
872     xbt_dynar_foreach(src_names, cpt, src_host_name) {
873       xbt_dynar_foreach(dst_names, cpt2, dst_host_name) {
874         /* If dst is $* then set this route to have its dst point to all hosts */
875         if (strcmp(src_host_name,"$*") != 0 && strcmp(dst_host_name,"$*") == 0){
876                   xbt_dict_foreach(workstation_set, cursor_w, key_w, data_w) {
877                           //int n = xbt_dynar_member(src_names, (char*)key_w);
878                             add_multi_links(src, dst, links, src_host_name, key_w);               
879                   }
880         }
881         /* If src is $* then set this route to have its dst point to all hosts */
882         if (strcmp(src_host_name,"$*") == 0 && strcmp(dst_host_name,"$*") != 0){
883                   xbt_dict_foreach(workstation_set, cursor_w, key_w, data_w) {
884                      // if (!symmetric || (symmetric && !contains(dst_names, key_w)))
885                         add_multi_links(src, dst, links, key_w, dst_host_name);               
886                 }
887         }
888         /* if none of them are equal to $* */
889         if (strcmp(src_host_name,"$*") != 0 && strcmp(dst_host_name,"$*") != 0) {
890                         add_multi_links(src, dst, links, src_host_name, dst_host_name);               
891         }     
892       }
893     }
894   }  
895   surfxml_bufferstack = old_buff;
896   xbt_dict_free(&route_multi_table);
897   xbt_dynar_free(&route_multi_elements);
898 }
899
900 /* Cluster tag functions */
901
902 void parse_cluster(void)
903 {  
904    static int AX_ptr = 0;
905    static int surfxml_bufferstack_size = 2048;
906  
907    char* cluster_id = A_surfxml_cluster_id;
908    char* cluster_prefix = A_surfxml_cluster_prefix;
909    char* cluster_suffix = A_surfxml_cluster_suffix;
910    char* cluster_radical = A_surfxml_cluster_radical;
911    char* cluster_power = A_surfxml_cluster_power;
912    char* cluster_bw = A_surfxml_cluster_bw;
913    char* cluster_lat = A_surfxml_cluster_lat;
914    char* cluster_bb_bw = A_surfxml_cluster_bb_bw;
915    char* cluster_bb_lat = A_surfxml_cluster_bb_lat;
916  
917    char* saved_buff = surfxml_bufferstack;
918
919    char * backbone_name;
920
921    surfxml_bufferstack = xbt_new0(char, surfxml_bufferstack_size);
922
923    /* Make set */
924    SURFXML_BUFFER_SET(set_id, cluster_id);
925    SURFXML_BUFFER_SET(set_prefix, cluster_prefix);
926    SURFXML_BUFFER_SET(set_suffix, cluster_suffix);
927    SURFXML_BUFFER_SET(set_radical, cluster_radical);
928   
929    SURFXML_START_TAG(set);
930    SURFXML_END_TAG(set);
931
932    /* Make foreach */
933    SURFXML_BUFFER_SET(foreach_set_id, cluster_id);
934
935    SURFXML_START_TAG(foreach);
936
937      /* Make host for the foreach */
938      parse_change_cpu_data("$1", cluster_power, "1.0", "", "", "1.0", "1.0", "1.0","-1.0");
939      A_surfxml_host_state = A_surfxml_host_state_ON;
940
941      SURFXML_START_TAG(host);
942      SURFXML_END_TAG(host);
943
944      /* Make link for the foreach */
945      parse_change_link_data("$1", cluster_bw, "", cluster_lat, "", "");
946      A_surfxml_link_state = A_surfxml_link_state_ON;
947      A_surfxml_link_sharing_policy = A_surfxml_link_sharing_policy_SHARED;
948  
949      SURFXML_START_TAG(link);
950      SURFXML_END_TAG(link);
951
952    SURFXML_END_TAG(foreach);
953
954    /* Make backbone link */
955    backbone_name = bprintf("%s_bb", cluster_id);
956    parse_change_link_data(backbone_name, cluster_bb_bw, "", cluster_bb_lat, "", "");
957    A_surfxml_link_state = A_surfxml_link_state_ON;
958    A_surfxml_link_sharing_policy = A_surfxml_link_sharing_policy_FATPIPE;
959  
960    SURFXML_START_TAG(link);
961    SURFXML_END_TAG(link);
962
963    /* Make route multi with the outside world, i.e. cluster->$* */
964    SURFXML_BUFFER_SET(route_c_multi_src, cluster_id);
965    SURFXML_BUFFER_SET(route_c_multi_dst, "$*");
966    A_surfxml_route_c_multi_symmetric = A_surfxml_route_c_multi_symmetric_NO;
967    A_surfxml_route_c_multi_action = A_surfxml_route_c_multi_action_OVERRIDE;
968    
969    SURFXML_START_TAG(route_c_multi);
970
971      SURFXML_BUFFER_SET(link_c_ctn_id, "$src");
972     
973      SURFXML_START_TAG(link_c_ctn);
974      SURFXML_END_TAG(link_c_ctn);
975
976    SURFXML_END_TAG(route_c_multi);
977
978    /* Make route multi between cluster hosts, i.e. cluster->cluster */
979    SURFXML_BUFFER_SET(route_c_multi_src, cluster_id);
980    SURFXML_BUFFER_SET(route_c_multi_dst, cluster_id);
981    A_surfxml_route_c_multi_action = A_surfxml_route_c_multi_action_POSTPEND;
982    A_surfxml_route_c_multi_symmetric = A_surfxml_route_c_multi_symmetric_NO;
983    
984    SURFXML_START_TAG(route_c_multi);
985     
986      SURFXML_BUFFER_SET(link_c_ctn_id, backbone_name);
987     
988      SURFXML_START_TAG(link_c_ctn);
989      SURFXML_END_TAG(link_c_ctn);
990     
991    SURFXML_END_TAG(route_c_multi);
992
993
994    /* Restore buff */
995    free(surfxml_bufferstack);
996    surfxml_bufferstack = saved_buff;
997 }
998
999 /* Trace management functions */
1000
1001 static double trace_periodicity = -1.0;
1002 static char* trace_file = NULL;
1003 static char* trace_id;
1004
1005 void parse_trace_init(void)
1006 {
1007    trace_id = strdup(A_surfxml_trace_id);
1008    trace_file = strdup(A_surfxml_trace_file);
1009    surf_parse_get_double(&trace_periodicity, A_surfxml_trace_periodicity);
1010 }
1011
1012 void parse_trace_finalize(void)
1013 {
1014   tmgr_trace_t trace;
1015   if (!trace_file || strcmp(trace_file,"") != 0) {
1016     surf_parse_get_trace(&trace, trace_file);
1017   }
1018   else {
1019     if (strcmp(surfxml_pcdata, "") == 0) trace = NULL;
1020     else
1021       trace = tmgr_trace_new_from_string(trace_id, surfxml_pcdata, trace_periodicity);  
1022   }
1023   xbt_dict_set(traces_set_list, trace_id, (void *)trace, NULL);
1024 }
1025
1026 void parse_trace_c_connect(void)
1027 {
1028         char* trace_connect;
1029    xbt_assert1(xbt_dict_get_or_null(traces_set_list, A_surfxml_trace_c_connect_trace_id),
1030               "Trace %s undefined", A_surfxml_trace_c_connect_trace_id);
1031    trace_connect = bprintf("%s#%d#%d#%s", A_surfxml_trace_c_connect_trace_id, A_surfxml_trace_c_connect_element, 
1032                                    A_surfxml_trace_c_connect_kind, A_surfxml_trace_c_connect_connector_id);
1033    xbt_dynar_push(traces_connect_list, &trace_connect);
1034 }
1035
1036 /* Random tag functions */
1037
1038 double get_cpu_power(const char *power)
1039
1040   double power_scale = 0.0;
1041   const char *p, *q;
1042   char *generator;
1043   random_data_t random = NULL; 
1044   /* randomness is inserted like this: power="$rand(my_random)" */
1045   if (((p = strstr(power, "$rand(")) != NULL) && ((q = strstr(power, ")")) != NULL)) {
1046      if (p < q) {
1047        generator = xbt_malloc(q - (p + 6) + 1);
1048        memcpy(generator, p + 6, q - (p + 6)); 
1049        generator[q - (p + 6)] = '\0';
1050        xbt_assert1((random = xbt_dict_get_or_null(random_data_list, generator)),
1051               "Random generator %s undefined", generator);
1052        power_scale = random_generate(random);
1053      }
1054   }
1055   else {
1056     surf_parse_get_double(&power_scale, power);
1057   }
1058   return power_scale;
1059 }
1060
1061 int random_min, random_max, random_mean, random_std_deviation, random_generator;
1062 char *random_id;
1063
1064 void init_randomness(void)
1065 {
1066   random_id = A_surfxml_random_id;
1067   surf_parse_get_int(&random_min, A_surfxml_random_min);
1068   surf_parse_get_int(&random_max, A_surfxml_random_max);
1069   surf_parse_get_int(&random_mean, A_surfxml_random_mean);
1070   surf_parse_get_int(&random_std_deviation, A_surfxml_random_std_deviation);
1071   random_generator = A_surfxml_random_generator;
1072 }
1073
1074 void add_randomness(void)
1075 {
1076    /* If needed aditional properties can be added by using the prop tag */
1077    random_data_t random = random_new(random_generator, random_min, random_max, random_mean, random_std_deviation);
1078    xbt_dict_set(random_data_list, random_id, (void *)random, NULL);
1079 }