Logo AND Algorithmique Numérique Distribuée

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