Logo AND Algorithmique Numérique Distribuée

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