Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
fixed bug in route:multi generation; appeared after the KCC.. model was eliminated
[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 void free_string(void *d)
478 {
479   free(*(void**)d);
480 }
481
482 void surfxml_add_callback(xbt_dynar_t cb_list, void_f_void_t function)
483 {
484   xbt_dynar_push(cb_list, &function);
485 }
486
487 static XBT_INLINE void surfxml_call_cb_functions(xbt_dynar_t cb_list)
488 {
489   unsigned int iterator;
490   void_f_void_t fun;
491   xbt_dynar_foreach(cb_list, iterator, fun){
492        DEBUG2("call %p %p",fun,*fun);
493        (*fun)();
494     }
495 }
496
497 void init_data(void)
498 {
499   xbt_dict_free(&route_table);
500   xbt_dynar_free(&route_link_list);
501   route_table = xbt_dict_new();
502
503   route_multi_table = xbt_dict_new();
504   route_multi_elements = xbt_dynar_new(sizeof(char*), NULL);
505   traces_set_list = xbt_dict_new();
506   traces_connect_list = xbt_dynar_new(sizeof(char*), NULL);
507   random_data_list = xbt_dict_new();
508
509 }
510
511 void parse_platform_file(const char* file)
512 {
513   surf_parse_open(file);
514   xbt_assert1((!(*surf_parse)()), "Parse error in %s", file);
515   surf_parse_close();
516 }
517
518 /* Functions to bypass route, host and link tags. Used by the foreach and route:multi tags */
519
520 static void parse_make_temporary_route(const char *src, const char *dst, int action)
521 {
522   int AX_ptr = 0;
523   surfxml_bufferstack = xbt_new0(char, 2048);
524   
525   A_surfxml_route_action = action;
526   SURFXML_BUFFER_SET(route_src,                     src);
527   SURFXML_BUFFER_SET(route_dst,                     dst);
528 }
529
530 static void parse_change_cpu_data(const char* hostName, const char* surfxml_host_power, const char* surfxml_host_availability,
531                                         const char* surfxml_host_availability_file, const char* surfxml_host_state_file)
532 {
533   int AX_ptr = 0;
534   surfxml_bufferstack = xbt_new0(char, 2048);
535  
536   SURFXML_BUFFER_SET(host_id,                     hostName);
537   SURFXML_BUFFER_SET(host_power,                  surfxml_host_power /*hostPower*/);
538   SURFXML_BUFFER_SET(host_availability,           surfxml_host_availability);
539   SURFXML_BUFFER_SET(host_availability_file,      surfxml_host_availability_file);
540   SURFXML_BUFFER_SET(host_state_file,             surfxml_host_state_file);
541 }
542
543 static void parse_change_link_data(const char* linkName, const char* surfxml_link_bandwidth, const char* surfxml_link_bandwidth_file,
544                                         const char* surfxml_link_latency, const char* surfxml_link_latency_file, const char* surfxml_link_state_file)
545 {
546   int AX_ptr = 0;
547   surfxml_bufferstack = xbt_new0(char, 2048);
548  
549   SURFXML_BUFFER_SET(link_id,                linkName);
550   SURFXML_BUFFER_SET(link_bandwidth,         surfxml_link_bandwidth);
551   SURFXML_BUFFER_SET(link_bandwidth_file,    surfxml_link_bandwidth_file);
552   SURFXML_BUFFER_SET(link_latency,           surfxml_link_latency);
553   SURFXML_BUFFER_SET(link_latency_file,      surfxml_link_latency_file);
554   SURFXML_BUFFER_SET(link_state_file,        surfxml_link_state_file);
555 }
556
557 /**
558 * \brief Restores the original surfxml buffer
559 */
560 static void parse_restore_original_buffer(void)
561 {
562   free(surfxml_bufferstack);
563   surfxml_bufferstack = old_buff;
564 }
565
566 /* Functions for the sets and foreach tags */
567
568 void parse_sets(void)
569 {
570   char *id, *suffix, *prefix, *radical;
571   int start, end;
572   xbt_dynar_t radical_ends;
573   xbt_dynar_t current_set;
574   char *value;
575   int i;
576
577   id = xbt_strdup(A_surfxml_set_id);
578   prefix = xbt_strdup(A_surfxml_set_prefix);
579   suffix = xbt_strdup(A_surfxml_set_suffix);
580   radical = xbt_strdup(A_surfxml_set_radical);
581   
582   xbt_assert1(!xbt_dict_get_or_null(set_list, id),
583               "Set '%s' declared several times in the platform file.",id);  
584   radical_ends = xbt_str_split(radical, "-");
585   xbt_assert1((xbt_dynar_length(radical_ends)==2), "Radical must be in the form lvalue-rvalue! Provided value: %s", radical);
586
587   surf_parse_get_int(&start, xbt_dynar_get_as(radical_ends, 0, char*));
588   surf_parse_get_int(&end, xbt_dynar_get_as(radical_ends, 1, char*));
589
590   current_set = xbt_dynar_new(sizeof(char*), NULL);
591
592   
593   for (i=start; i<end; i++) {
594      value = bprintf("%s%d%s", prefix, i, suffix);
595      xbt_dynar_push(current_set, &value);
596   } 
597   
598   xbt_dict_set(set_list, id, current_set, NULL);
599 }
600
601 static const char* surfxml_host_power;
602 static const char* surfxml_host_availability;
603 static const char* surfxml_host_availability_file;
604 static const char* surfxml_host_state_file;
605
606 static void parse_host_foreach(void)
607 {
608   surfxml_host_power = A_surfxml_host_power;
609   surfxml_host_availability = A_surfxml_host_availability;
610   surfxml_host_availability_file = A_surfxml_host_availability_file;
611   surfxml_host_state_file = A_surfxml_host_state_file;
612 }
613
614 static void finalize_host_foreach(void)
615 {
616   xbt_dynar_t names = NULL;
617   unsigned int cpt = 0;
618   char *name;
619   xbt_dict_cursor_t cursor = NULL;
620   char *key,*data; 
621
622   xbt_dict_t cluster_host_props = current_property_set;
623   
624   xbt_assert1((names = xbt_dict_get_or_null(set_list, foreach_set_name)),
625               "Set name '%s' reffered by foreach tag not found.", foreach_set_name);  
626
627   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);
628
629         
630   /* foreach name in set call the main host callback */
631   xbt_dynar_foreach (names, cpt, name) {
632     parse_change_cpu_data(name, surfxml_host_power, surfxml_host_availability,
633                                         surfxml_host_availability_file, surfxml_host_state_file);
634     surfxml_call_cb_functions(main_STag_surfxml_host_cb_list);
635
636     xbt_dict_foreach(cluster_host_props,cursor,key,data) {
637            xbt_dict_set(current_property_set, xbt_strdup(key), xbt_strdup(data), free);
638     }
639
640     surfxml_call_cb_functions(main_ETag_surfxml_host_cb_list);
641     free(surfxml_bufferstack);
642   }
643
644   current_property_set = xbt_dict_new();
645
646   surfxml_bufferstack = old_buff;
647   
648 }
649
650 static const char* surfxml_link_bandwidth;
651 static const char* surfxml_link_bandwidth_file;
652 static const char* surfxml_link_latency;
653 static const char* surfxml_link_latency_file;
654 static const char* surfxml_link_state_file;
655
656 static void parse_link_foreach(void)
657 {
658   surfxml_link_bandwidth = A_surfxml_link_bandwidth;
659   surfxml_link_bandwidth_file = A_surfxml_link_bandwidth_file;
660   surfxml_link_latency = A_surfxml_link_latency;
661   surfxml_link_latency_file = A_surfxml_link_latency_file;
662   surfxml_link_state_file = A_surfxml_link_state_file;
663 }
664
665 static void finalize_link_foreach(void)
666 {
667   xbt_dynar_t names = NULL;
668   unsigned int cpt = 0;
669   char *name;
670   xbt_dict_cursor_t cursor = NULL;
671   char *key,*data; 
672
673   xbt_dict_t cluster_link_props = current_property_set;
674
675   xbt_assert1((names = xbt_dict_get_or_null(set_list, foreach_set_name)),
676               "Set name '%s' reffered by foreach tag not found.", foreach_set_name); 
677
678   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);
679
680
681   /* for each name in set call the main link callback */
682   xbt_dynar_foreach (names, cpt, name) {
683     parse_change_link_data(name, surfxml_link_bandwidth, surfxml_link_bandwidth_file,
684                                         surfxml_link_latency, surfxml_link_latency_file, surfxml_link_state_file);
685     surfxml_call_cb_functions(main_STag_surfxml_link_cb_list);
686
687     xbt_dict_foreach(cluster_link_props,cursor,key,data) {
688            xbt_dict_set(current_property_set, xbt_strdup(key), xbt_strdup(data), free);
689     }
690
691     surfxml_call_cb_functions(main_ETag_surfxml_link_cb_list);
692    free(surfxml_bufferstack);
693
694   }
695
696   current_property_set = xbt_dict_new();
697
698   surfxml_bufferstack = old_buff;
699 }
700
701 void parse_foreach(void)
702 {
703   /* save the host & link callbacks */
704   main_STag_surfxml_host_cb_list = STag_surfxml_host_cb_list;
705   main_ETag_surfxml_host_cb_list = ETag_surfxml_host_cb_list;
706   main_STag_surfxml_link_cb_list = STag_surfxml_link_cb_list;
707   main_ETag_surfxml_link_cb_list = ETag_surfxml_link_cb_list;
708
709   /* define host & link callbacks to be used only by the foreach tag */
710   STag_surfxml_host_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
711   ETag_surfxml_host_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
712   STag_surfxml_link_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
713   ETag_surfxml_link_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
714
715   surfxml_add_callback(STag_surfxml_host_cb_list, &parse_host_foreach);
716   surfxml_add_callback(ETag_surfxml_host_cb_list, &finalize_host_foreach);
717   surfxml_add_callback(STag_surfxml_link_cb_list, &parse_link_foreach);
718   surfxml_add_callback(ETag_surfxml_link_cb_list, &finalize_link_foreach);
719
720   /* get set name */
721   foreach_set_name = xbt_strdup(A_surfxml_foreach_set_id); 
722 }
723
724 /* Route:multi functions */
725
726 static int route_multi_size=0;
727 static char* src_name, *dst_name;
728 static int is_symmetric_route;
729
730 void parse_route_elem(void)
731 {
732   char *val;
733
734   val = xbt_strdup(A_surfxml_link_c_ctn_id);
735   
736   xbt_dynar_push(route_link_list, &val);
737 }
738
739 void parse_route_multi_set_endpoints(void)
740 {
741   src_name = xbt_strdup(A_surfxml_route_c_multi_src); 
742   dst_name = xbt_strdup(A_surfxml_route_c_multi_dst); 
743   route_action = A_surfxml_route_c_multi_action;
744   is_symmetric_route = A_surfxml_route_c_multi_symmetric;
745   route_multi_size++;
746
747   route_link_list = xbt_dynar_new(sizeof(char *), &free_string);
748 }
749
750 static int contains(xbt_dynar_t list, const char* value)
751 {
752  unsigned int cpt;
753  char * val;
754  xbt_dynar_foreach(list, cpt, val) {
755    if (strcmp(val, value) == 0)
756       return 1;
757   }
758   return 0;
759 }
760
761 /* 
762    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.
763    The decision is based upon the value of action specified in the xml route:multi attribute action
764  */
765 void manage_route(xbt_dict_t routing_table, const char *route_name, int action, int isMultiRoute)
766 {
767   unsigned int cpt;
768   xbt_dynar_t links;
769   char *value;
770
771   /* get already existing list if it exists */
772   links = xbt_dict_get_or_null(routing_table, route_name);
773   DEBUG1("ROUTE: %s", route_name);
774   if (links != NULL) {
775      switch (action) {
776         case A_surfxml_route_action_PREPEND: /* add existing links at the end; route_link_list + links */
777                                     xbt_dynar_foreach(links, cpt, value) {      
778                                        xbt_dynar_push(route_link_list,&value);
779                                     }
780                                     break;
781         case A_surfxml_route_action_POSTPEND: /* add existing links in front; links + route_link_list */ 
782                                     xbt_dynar_foreach(route_link_list, cpt, value) {
783                                        xbt_dynar_push(links,&value);
784                                     }
785                                     route_link_list = links;
786                                     break;
787         case A_surfxml_route_action_OVERRIDE:
788                                     break;
789         default:break;
790      }
791   }
792   /* this is the final route; do not add if name is a set; add only if name is in set list */
793   if (!isMultiRoute){    
794     xbt_dict_set(routing_table, route_name, route_link_list, NULL);
795   }
796 }
797
798 void parse_route_multi_set_route(void)
799 {
800   char* route_name;
801
802   route_name = bprintf("%s#%s#%d#%d#%d", src_name, dst_name, route_action, is_symmetric_route, route_multi_size);
803
804   xbt_dynar_push(route_multi_elements, &route_name);
805
806   /* Add route */
807   xbt_dict_set(route_multi_table, route_name, route_link_list, NULL);
808   /* add symmetric if it is the case */ 
809   if (is_symmetric_route == 1) {
810     char * symmetric_name = bprintf("%s#%s#%d#%d#%d", dst_name, src_name, route_action, !is_symmetric_route, route_multi_size);
811   
812     xbt_dict_set(route_multi_table, symmetric_name, route_link_list, NULL);
813     xbt_dynar_push(route_multi_elements, &symmetric_name);
814     is_symmetric_route = 0;
815   }
816   free(src_name);
817   free(dst_name);
818 }
819
820 static void add_multi_links(const char* src, const char* dst, xbt_dynar_t links, const char* src_name, const char* dst_name)
821 {
822   unsigned int cpt;
823   char* value, *val;
824    parse_make_temporary_route(src_name, dst_name, route_action);
825    surfxml_call_cb_functions(STag_surfxml_route_cb_list);
826    DEBUG2("\tADDING ROUTE: %s -> %s", src_name, dst_name);
827    /* Build link list */ 
828    xbt_dynar_foreach(links, cpt, value) {
829      if (strcmp(value, src) == 0)
830        val =  xbt_strdup(src_name);
831      else if (strcmp(value, dst) == 0)
832        val = xbt_strdup(dst_name);
833      else if (strcmp(value, "$dst") == 0)
834        val = xbt_strdup(dst_name);
835      else if (strcmp(value, "$src") == 0)
836        val = xbt_strdup(src_name);
837      else
838        val = xbt_strdup(value);
839      DEBUG1("\t\tELEMENT: %s", val);
840      xbt_dynar_push(route_link_list, &val);
841    }    
842    surfxml_call_cb_functions(ETag_surfxml_route_cb_list);
843    free(surfxml_bufferstack);
844 }
845
846 static void convert_route_multi_to_routes(void)
847 {
848   xbt_dict_cursor_t cursor_w;
849   int symmetric;
850   unsigned int cpt, cpt2, cursor;
851   char *src_host_name, *dst_host_name, *key, *src, *dst, *val, *key_w, *data_w; 
852   const char* sep="#";
853   xbt_dynar_t src_names = NULL, dst_names = NULL, links;
854
855   if (!route_multi_elements) return;
856
857   xbt_dict_t set = cpu_set;
858   if (workstation_set != NULL)
859      set = workstation_set;
860   
861
862   old_buff = surfxml_bufferstack;
863   /* Get all routes in the exact order they were entered in the platform file */
864   xbt_dynar_foreach(route_multi_elements, cursor, key) {
865      /* Get links for the route */     
866      links = (xbt_dynar_t)xbt_dict_get_or_null(route_multi_table, key);
867      keys = xbt_str_split_str(key, sep);
868      /* Get route ends */
869      src = xbt_dynar_get_as(keys, 0, char*);
870      dst = xbt_dynar_get_as(keys, 1, char*);
871      route_action = atoi(xbt_dynar_get_as(keys, 2, char*));
872      symmetric = atoi(xbt_dynar_get_as(keys, 3, char*));
873
874     /* Create the dynar of src and dst hosts for the new routes */ 
875     /* NOTE: src and dst can be either set names or simple host names */
876     src_names = (xbt_dynar_t)xbt_dict_get_or_null(set_list, src);
877     dst_names = (xbt_dynar_t)xbt_dict_get_or_null(set_list, dst);
878     /* Add to dynar even if they are simple names */
879     if (src_names == NULL) {
880        src_names = xbt_dynar_new(sizeof(char *), &free_string);
881        val = xbt_strdup(src);
882        xbt_dynar_push(src_names, &val);
883     }
884     if (dst_names == NULL) {
885        dst_names = xbt_dynar_new(sizeof(char *), &free_string);
886        val = xbt_strdup(dst);
887        xbt_dynar_push(dst_names, &val);
888     }
889     /* Build the routes */
890     DEBUG2("ADDING MULTI ROUTE: %s -> %s", xbt_dynar_get_as(keys, 0, char*), xbt_dynar_get_as(keys, 1, char*));
891     xbt_dynar_foreach(src_names, cpt, src_host_name) {
892       xbt_dynar_foreach(dst_names, cpt2, dst_host_name) {
893         /* If dst is $* then set this route to have its dst point to all hosts */
894         if (strcmp(src_host_name,"$*") != 0 && strcmp(dst_host_name,"$*") == 0){                        
895                   xbt_dict_foreach(set, cursor_w, key_w, data_w) {
896                           //int n = xbt_dynar_member(src_names, (char*)key_w);
897                             add_multi_links(src, dst, links, src_host_name, key_w);               
898                   }
899         }
900         /* If src is $* then set this route to have its dst point to all hosts */
901         if (strcmp(src_host_name,"$*") == 0 && strcmp(dst_host_name,"$*") != 0){
902                   xbt_dict_foreach(set, cursor_w, key_w, data_w) {
903                      // if (!symmetric || (symmetric && !contains(dst_names, key_w)))
904                         add_multi_links(src, dst, links, key_w, dst_host_name);               
905                 }
906         }
907         /* if none of them are equal to $* */
908         if (strcmp(src_host_name,"$*") != 0 && strcmp(dst_host_name,"$*") != 0) {
909                         add_multi_links(src, dst, links, src_host_name, dst_host_name);               
910         }     
911       }
912     }
913   }  
914   surfxml_bufferstack = old_buff;
915   xbt_dict_free(&route_multi_table);
916   xbt_dynar_free(&route_multi_elements);
917 }
918
919 /* Cluster tag functions */
920
921 void parse_cluster(void)
922 {  
923    static int AX_ptr = 0;
924    static int surfxml_bufferstack_size = 2048;
925  
926    char* cluster_id = A_surfxml_cluster_id;
927    char* cluster_prefix = A_surfxml_cluster_prefix;
928    char* cluster_suffix = A_surfxml_cluster_suffix;
929    char* cluster_radical = A_surfxml_cluster_radical;
930    char* cluster_power = A_surfxml_cluster_power;
931    char* cluster_bw = A_surfxml_cluster_bw;
932    char* cluster_lat = A_surfxml_cluster_lat;
933    char* cluster_bb_bw = A_surfxml_cluster_bb_bw;
934    char* cluster_bb_lat = A_surfxml_cluster_bb_lat;
935  
936    char* saved_buff = surfxml_bufferstack;
937
938    char * backbone_name;
939
940    surfxml_bufferstack = xbt_new0(char, surfxml_bufferstack_size);
941
942    /* Make set */
943    SURFXML_BUFFER_SET(set_id, cluster_id);
944    SURFXML_BUFFER_SET(set_prefix, cluster_prefix);
945    SURFXML_BUFFER_SET(set_suffix, cluster_suffix);
946    SURFXML_BUFFER_SET(set_radical, cluster_radical);
947   
948    SURFXML_START_TAG(set);
949    SURFXML_END_TAG(set);
950
951    /* Make foreach */
952    SURFXML_BUFFER_SET(foreach_set_id, cluster_id);
953
954    SURFXML_START_TAG(foreach);
955
956      /* Make host for the foreach */
957      parse_change_cpu_data("$1", cluster_power, "1.0", "", "");
958      A_surfxml_host_state = A_surfxml_host_state_ON;
959
960      SURFXML_START_TAG(host);
961      SURFXML_END_TAG(host);
962
963      /* Make link for the foreach */
964      parse_change_link_data("$1", cluster_bw, "", cluster_lat, "", "");
965      A_surfxml_link_state = A_surfxml_link_state_ON;
966      A_surfxml_link_sharing_policy = A_surfxml_link_sharing_policy_SHARED;
967  
968      SURFXML_START_TAG(link);
969      SURFXML_END_TAG(link);
970
971    SURFXML_END_TAG(foreach);
972
973    /* Make backbone link */
974    backbone_name = bprintf("%s_bb", cluster_id);
975    parse_change_link_data(backbone_name, cluster_bb_bw, "", cluster_bb_lat, "", "");
976    A_surfxml_link_state = A_surfxml_link_state_ON;
977    A_surfxml_link_sharing_policy = A_surfxml_link_sharing_policy_FATPIPE;
978  
979    SURFXML_START_TAG(link);
980    SURFXML_END_TAG(link);
981
982    /* Make route multi with the outside world, i.e. cluster->$* */
983    SURFXML_BUFFER_SET(route_c_multi_src, cluster_id);
984    SURFXML_BUFFER_SET(route_c_multi_dst, "$*");
985    A_surfxml_route_c_multi_symmetric = A_surfxml_route_c_multi_symmetric_NO;
986    A_surfxml_route_c_multi_action = A_surfxml_route_c_multi_action_OVERRIDE;
987    
988    SURFXML_START_TAG(route_c_multi);
989
990      SURFXML_BUFFER_SET(link_c_ctn_id, "$src");
991     
992      SURFXML_START_TAG(link_c_ctn);
993      SURFXML_END_TAG(link_c_ctn);
994
995    SURFXML_END_TAG(route_c_multi);
996
997    /* Make route multi between cluster hosts, i.e. cluster->cluster */
998    SURFXML_BUFFER_SET(route_c_multi_src, cluster_id);
999    SURFXML_BUFFER_SET(route_c_multi_dst, cluster_id);
1000    A_surfxml_route_c_multi_action = A_surfxml_route_c_multi_action_POSTPEND;
1001    A_surfxml_route_c_multi_symmetric = A_surfxml_route_c_multi_symmetric_NO;
1002    
1003    SURFXML_START_TAG(route_c_multi);
1004     
1005      SURFXML_BUFFER_SET(link_c_ctn_id, backbone_name);
1006     
1007      SURFXML_START_TAG(link_c_ctn);
1008      SURFXML_END_TAG(link_c_ctn);
1009     
1010    SURFXML_END_TAG(route_c_multi);
1011
1012
1013    /* Restore buff */
1014    free(surfxml_bufferstack);
1015    surfxml_bufferstack = saved_buff;
1016 }
1017
1018 /* Trace management functions */
1019
1020 static double trace_periodicity = -1.0;
1021 static char* trace_file = NULL;
1022 static char* trace_id;
1023
1024 void parse_trace_init(void)
1025 {
1026    trace_id = strdup(A_surfxml_trace_id);
1027    trace_file = strdup(A_surfxml_trace_file);
1028    surf_parse_get_double(&trace_periodicity, A_surfxml_trace_periodicity);
1029 }
1030
1031 void parse_trace_finalize(void)
1032 {
1033   tmgr_trace_t trace;
1034   if (!trace_file || strcmp(trace_file,"") != 0) {
1035     surf_parse_get_trace(&trace, trace_file);
1036   }
1037   else {
1038     if (strcmp(surfxml_pcdata, "") == 0) trace = NULL;
1039     else
1040       trace = tmgr_trace_new_from_string(trace_id, surfxml_pcdata, trace_periodicity);  
1041   }
1042   xbt_dict_set(traces_set_list, trace_id, (void *)trace, NULL);
1043 }
1044
1045 void parse_trace_c_connect(void)
1046 {
1047         char* trace_connect;
1048    xbt_assert1(xbt_dict_get_or_null(traces_set_list, A_surfxml_trace_c_connect_trace_id),
1049               "Trace %s undefined", A_surfxml_trace_c_connect_trace_id);
1050    trace_connect = bprintf("%s#%d#%d#%s", A_surfxml_trace_c_connect_trace_id, A_surfxml_trace_c_connect_element, 
1051                                    A_surfxml_trace_c_connect_kind, A_surfxml_trace_c_connect_connector_id);
1052    xbt_dynar_push(traces_connect_list, &trace_connect);
1053 }
1054
1055 /* Random tag functions */
1056
1057 double get_cpu_power(const char *power)
1058
1059   double power_scale = 0.0;
1060   const char *p, *q;
1061   char *generator;
1062   random_data_t random = NULL; 
1063   /* randomness is inserted like this: power="$rand(my_random)" */
1064   if (((p = strstr(power, "$rand(")) != NULL) && ((q = strstr(power, ")")) != NULL)) {
1065      if (p < q) {
1066        generator = xbt_malloc(q - (p + 6) + 1);
1067        memcpy(generator, p + 6, q - (p + 6)); 
1068        generator[q - (p + 6)] = '\0';
1069        xbt_assert1((random = xbt_dict_get_or_null(random_data_list, generator)),
1070               "Random generator %s undefined", generator);
1071        power_scale = random_generate(random);
1072      }
1073   }
1074   else {
1075     surf_parse_get_double(&power_scale, power);
1076   }
1077   return power_scale;
1078 }
1079
1080 int random_min, random_max, random_mean, random_std_deviation, random_generator;
1081 char *random_id;
1082
1083 void init_randomness(void)
1084 {
1085   random_id = A_surfxml_random_id;
1086   surf_parse_get_int(&random_min, A_surfxml_random_min);
1087   surf_parse_get_int(&random_max, A_surfxml_random_max);
1088   surf_parse_get_int(&random_mean, A_surfxml_random_mean);
1089   surf_parse_get_int(&random_std_deviation, A_surfxml_random_std_deviation);
1090   random_generator = A_surfxml_random_generator;
1091 }
1092
1093 void add_randomness(void)
1094 {
1095    /* If needed aditional properties can be added by using the prop tag */
1096    random_data_t random = random_new(random_generator, random_min, random_max, random_mean, random_std_deviation);
1097    xbt_dict_set(random_data_list, random_id, (void *)random, NULL);
1098 }
1099