1 /* Copyright (c) 2006, 2007, 2008, 2009, 2010. The SimGrid Team.
2 * All rights reserved. */
4 /* This program is free software; you can redistribute it and/or modify it
5 * under the terms of the license (GNU LGPL) which comes with this package. */
11 #include "surf/surfxml_parse_private.h"
12 #include "surf/surf_private.h"
14 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_parse, surf,
15 "Logging specific to the SURF parsing module");
17 #include "simgrid_dtd.c"
19 /* Initialize the parsing globals */
21 xbt_dict_t traces_set_list = NULL;
22 //xbt_dynar_t traces_connect_list = NULL;
23 xbt_dict_t trace_connect_list_host_avail = NULL;
24 xbt_dict_t trace_connect_list_power = NULL;
25 xbt_dict_t trace_connect_list_link_avail = NULL;
26 xbt_dict_t trace_connect_list_bandwidth = NULL;
27 xbt_dict_t trace_connect_list_latency = NULL;
29 /* This buffer is used to store the original buffer before substituing it by out own buffer. Usefull for the foreach tag */
30 static xbt_dynar_t surfxml_bufferstack_stack = NULL;
31 int surfxml_bufferstack_size = 2048;
32 static char *old_buff = NULL;
33 static void surf_parse_error(char *msg);
35 void surfxml_bufferstack_push(int new)
38 old_buff = surfxml_bufferstack;
40 xbt_dynar_push(surfxml_bufferstack_stack, &surfxml_bufferstack);
41 surfxml_bufferstack = xbt_new0(char, surfxml_bufferstack_size);
45 void surfxml_bufferstack_pop(int new)
48 surfxml_bufferstack = old_buff;
50 free(surfxml_bufferstack);
51 xbt_dynar_pop(surfxml_bufferstack_stack, &surfxml_bufferstack);
55 /* Stores the set name reffered to by the foreach tag */
56 static char *foreach_set_name;
57 static xbt_dynar_t main_STag_surfxml_host_cb_list = NULL;
58 static xbt_dynar_t main_STag_surfxml_link_cb_list = NULL;
60 /* make sure these symbols are defined as strong ones in this file so that the linked can resolve them */
61 xbt_dynar_t STag_surfxml_platform_cb_list = NULL;
62 xbt_dynar_t ETag_surfxml_platform_cb_list = NULL;
63 xbt_dynar_t STag_surfxml_host_cb_list = NULL;
64 xbt_dynar_t ETag_surfxml_host_cb_list = NULL;
65 xbt_dynar_t STag_surfxml_router_cb_list = NULL;
66 xbt_dynar_t ETag_surfxml_router_cb_list = NULL;
67 xbt_dynar_t STag_surfxml_link_cb_list = NULL;
68 xbt_dynar_t ETag_surfxml_link_cb_list = NULL;
69 xbt_dynar_t STag_surfxml_route_cb_list = NULL;
70 xbt_dynar_t ETag_surfxml_route_cb_list = NULL;
71 xbt_dynar_t STag_surfxml_link_c_ctn_cb_list = NULL;
72 xbt_dynar_t ETag_surfxml_link_c_ctn_cb_list = NULL;
73 xbt_dynar_t STag_surfxml_process_cb_list = NULL;
74 xbt_dynar_t ETag_surfxml_process_cb_list = NULL;
75 xbt_dynar_t STag_surfxml_argument_cb_list = NULL;
76 xbt_dynar_t ETag_surfxml_argument_cb_list = NULL;
77 xbt_dynar_t STag_surfxml_prop_cb_list = NULL;
78 xbt_dynar_t ETag_surfxml_prop_cb_list = NULL;
79 xbt_dynar_t STag_surfxml_set_cb_list = NULL;
80 xbt_dynar_t ETag_surfxml_set_cb_list = NULL;
81 xbt_dynar_t STag_surfxml_foreach_cb_list = NULL;
82 xbt_dynar_t ETag_surfxml_foreach_cb_list = NULL;
83 xbt_dynar_t STag_surfxml_route_c_multi_cb_list = NULL;
84 xbt_dynar_t ETag_surfxml_route_c_multi_cb_list = NULL;
85 xbt_dynar_t STag_surfxml_cluster_cb_list = NULL;
86 xbt_dynar_t ETag_surfxml_cluster_cb_list = NULL;
87 xbt_dynar_t STag_surfxml_trace_cb_list = NULL;
88 xbt_dynar_t ETag_surfxml_trace_cb_list = NULL;
89 xbt_dynar_t STag_surfxml_trace_c_connect_cb_list = NULL;
90 xbt_dynar_t ETag_surfxml_trace_c_connect_cb_list = NULL;
91 xbt_dynar_t STag_surfxml_random_cb_list = NULL;
92 xbt_dynar_t ETag_surfxml_random_cb_list = NULL;
94 /* Stores the sets defined in the XML */
95 xbt_dict_t set_list = NULL;
97 xbt_dict_t current_property_set = NULL;
99 /* For the route:multi tag */
100 xbt_dict_t route_table = NULL;
101 xbt_dict_t route_multi_table = NULL;
102 xbt_dynar_t route_multi_elements = NULL;
103 static xbt_dynar_t route_link_list = NULL;
104 xbt_dynar_t links = NULL;
105 xbt_dynar_t keys = NULL;
107 xbt_dict_t random_data_list = NULL;
109 static xbt_dynar_t surf_input_buffer_stack = NULL;
110 static xbt_dynar_t surf_file_to_parse_stack = NULL;
112 static XBT_INLINE void surfxml_call_cb_functions(xbt_dynar_t);
114 YY_BUFFER_STATE surf_input_buffer;
115 FILE *surf_file_to_parse = NULL;
117 static void convert_route_multi_to_routes(void);
118 static void parse_route_elem(void);
119 static void parse_Stag_foreach(void);
120 static void parse_sets(void);
121 static void parse_Stag_route_multi(void);
122 static void parse_Etag_route_multi(void);
123 static void parse_Stag_trace(void);
124 static void parse_Etag_trace(void);
125 static void parse_Stag_trace_c_connect(void);
126 static void init_randomness(void);
127 static void add_randomness(void);
129 void surf_parse_free_callbacks(void)
131 xbt_dynar_free(&STag_surfxml_platform_cb_list);
132 xbt_dynar_free(&ETag_surfxml_platform_cb_list);
133 xbt_dynar_free(&STag_surfxml_host_cb_list);
134 xbt_dynar_free(&ETag_surfxml_host_cb_list);
135 xbt_dynar_free(&STag_surfxml_router_cb_list);
136 xbt_dynar_free(&ETag_surfxml_router_cb_list);
137 xbt_dynar_free(&STag_surfxml_link_cb_list);
138 xbt_dynar_free(&ETag_surfxml_link_cb_list);
139 xbt_dynar_free(&STag_surfxml_route_cb_list);
140 xbt_dynar_free(&ETag_surfxml_route_cb_list);
141 xbt_dynar_free(&STag_surfxml_link_c_ctn_cb_list);
142 xbt_dynar_free(&ETag_surfxml_link_c_ctn_cb_list);
143 xbt_dynar_free(&STag_surfxml_process_cb_list);
144 xbt_dynar_free(&ETag_surfxml_process_cb_list);
145 xbt_dynar_free(&STag_surfxml_argument_cb_list);
146 xbt_dynar_free(&ETag_surfxml_argument_cb_list);
147 xbt_dynar_free(&STag_surfxml_prop_cb_list);
148 xbt_dynar_free(&ETag_surfxml_prop_cb_list);
149 xbt_dynar_free(&STag_surfxml_set_cb_list);
150 xbt_dynar_free(&ETag_surfxml_set_cb_list);
151 xbt_dynar_free(&STag_surfxml_foreach_cb_list);
152 xbt_dynar_free(&ETag_surfxml_foreach_cb_list);
153 xbt_dynar_free(&STag_surfxml_route_c_multi_cb_list);
154 xbt_dynar_free(&ETag_surfxml_route_c_multi_cb_list);
155 xbt_dynar_free(&STag_surfxml_cluster_cb_list);
156 xbt_dynar_free(&ETag_surfxml_cluster_cb_list);
157 xbt_dynar_free(&STag_surfxml_trace_cb_list);
158 xbt_dynar_free(&ETag_surfxml_trace_cb_list);
159 xbt_dynar_free(&STag_surfxml_trace_c_connect_cb_list);
160 xbt_dynar_free(&ETag_surfxml_trace_c_connect_cb_list);
161 xbt_dynar_free(&STag_surfxml_random_cb_list);
162 xbt_dynar_free(&ETag_surfxml_random_cb_list);
165 void surf_parse_reset_parser(void)
167 surf_parse_free_callbacks();
168 STag_surfxml_platform_cb_list = xbt_dynar_new(sizeof(void_f_void_t), NULL);
169 ETag_surfxml_platform_cb_list = xbt_dynar_new(sizeof(void_f_void_t), NULL);
170 STag_surfxml_host_cb_list = xbt_dynar_new(sizeof(void_f_void_t), NULL);
171 ETag_surfxml_host_cb_list = xbt_dynar_new(sizeof(void_f_void_t), NULL);
172 STag_surfxml_router_cb_list = xbt_dynar_new(sizeof(void_f_void_t), NULL);
173 ETag_surfxml_router_cb_list = xbt_dynar_new(sizeof(void_f_void_t), NULL);
174 STag_surfxml_link_cb_list = xbt_dynar_new(sizeof(void_f_void_t), NULL);
175 ETag_surfxml_link_cb_list = xbt_dynar_new(sizeof(void_f_void_t), NULL);
176 STag_surfxml_route_cb_list = xbt_dynar_new(sizeof(void_f_void_t), NULL);
177 ETag_surfxml_route_cb_list = xbt_dynar_new(sizeof(void_f_void_t), NULL);
178 STag_surfxml_link_c_ctn_cb_list =
179 xbt_dynar_new(sizeof(void_f_void_t), NULL);
180 ETag_surfxml_link_c_ctn_cb_list =
181 xbt_dynar_new(sizeof(void_f_void_t), NULL);
182 STag_surfxml_process_cb_list = xbt_dynar_new(sizeof(void_f_void_t), NULL);
183 ETag_surfxml_process_cb_list = xbt_dynar_new(sizeof(void_f_void_t), NULL);
184 STag_surfxml_argument_cb_list = xbt_dynar_new(sizeof(void_f_void_t), NULL);
185 ETag_surfxml_argument_cb_list = xbt_dynar_new(sizeof(void_f_void_t), NULL);
186 STag_surfxml_prop_cb_list = xbt_dynar_new(sizeof(void_f_void_t), NULL);
187 ETag_surfxml_prop_cb_list = xbt_dynar_new(sizeof(void_f_void_t), NULL);
188 STag_surfxml_set_cb_list = xbt_dynar_new(sizeof(void_f_void_t), NULL);
189 ETag_surfxml_set_cb_list = xbt_dynar_new(sizeof(void_f_void_t), NULL);
190 STag_surfxml_foreach_cb_list = xbt_dynar_new(sizeof(void_f_void_t), NULL);
191 ETag_surfxml_foreach_cb_list = xbt_dynar_new(sizeof(void_f_void_t), NULL);
192 STag_surfxml_route_c_multi_cb_list =
193 xbt_dynar_new(sizeof(void_f_void_t), NULL);
194 ETag_surfxml_route_c_multi_cb_list =
195 xbt_dynar_new(sizeof(void_f_void_t), NULL);
196 STag_surfxml_cluster_cb_list = xbt_dynar_new(sizeof(void_f_void_t), NULL);
197 ETag_surfxml_cluster_cb_list = xbt_dynar_new(sizeof(void_f_void_t), NULL);
198 STag_surfxml_trace_cb_list = xbt_dynar_new(sizeof(void_f_void_t), NULL);
199 ETag_surfxml_trace_cb_list = xbt_dynar_new(sizeof(void_f_void_t), NULL);
200 STag_surfxml_trace_c_connect_cb_list =
201 xbt_dynar_new(sizeof(void_f_void_t), NULL);
202 ETag_surfxml_trace_c_connect_cb_list =
203 xbt_dynar_new(sizeof(void_f_void_t), NULL);
204 STag_surfxml_random_cb_list = xbt_dynar_new(sizeof(void_f_void_t), NULL);
205 ETag_surfxml_random_cb_list = xbt_dynar_new(sizeof(void_f_void_t), NULL);
208 void STag_surfxml_include(void)
210 xbt_dynar_push(surf_input_buffer_stack, &surf_input_buffer);
211 xbt_dynar_push(surf_file_to_parse_stack, &surf_file_to_parse);
213 surf_file_to_parse = surf_fopen(A_surfxml_include_file, "r");
214 xbt_assert1((surf_file_to_parse), "Unable to open \"%s\"\n",
215 A_surfxml_include_file);
216 surf_input_buffer = surf_parse__create_buffer(surf_file_to_parse, 10);
217 surf_parse__switch_to_buffer(surf_input_buffer);
222 void ETag_surfxml_include(void)
226 surf_parse__delete_buffer(surf_input_buffer);
227 fclose(surf_file_to_parse);
228 xbt_dynar_pop(surf_file_to_parse_stack, &surf_file_to_parse);
229 xbt_dynar_pop(surf_input_buffer_stack, &surf_input_buffer);
232 void STag_surfxml_platform(void)
234 double version = 0.0;
236 sscanf(A_surfxml_platform_version, "%lg", &version);
238 xbt_assert0((version >= 1.0), "******* BIG FAT WARNING *********\n "
239 "You're using an ancient XML file. "
240 "Since SimGrid 3.1, units are Bytes, Flops, and seconds "
241 "instead of MBytes, MFlops and seconds. "
242 "A script (surfxml_update.pl) to help you convert your old "
244 "is available in the contrib/platform_generation directory "
245 "of the simgrid repository. Please check also out the "
246 "SURF section of the ChangeLog for the 3.1 version. "
247 "Last, do not forget to also update your values for "
248 "the calls to MSG_task_create (if any).");
249 xbt_assert0((version >= 2.0), "******* BIG FAT WARNING *********\n "
250 "You're using an old XML file. "
251 "A script (surfxml_update.pl) to help you convert your old "
253 "is available in the contrib/platform_generation directory "
254 "of the simgrid repository.");
256 surfxml_call_cb_functions(STag_surfxml_platform_cb_list);
260 void ETag_surfxml_platform(void)
262 convert_route_multi_to_routes();
264 surfxml_call_cb_functions(ETag_surfxml_platform_cb_list);
266 xbt_dict_free(&random_data_list);
269 void STag_surfxml_host(void)
271 surfxml_call_cb_functions(STag_surfxml_host_cb_list);
275 void ETag_surfxml_host(void)
277 surfxml_call_cb_functions(ETag_surfxml_host_cb_list);
280 void STag_surfxml_router(void)
282 surfxml_call_cb_functions(STag_surfxml_router_cb_list);
285 void ETag_surfxml_router(void)
287 surfxml_call_cb_functions(ETag_surfxml_router_cb_list);
290 void STag_surfxml_link(void)
292 surfxml_call_cb_functions(STag_surfxml_link_cb_list);
295 void ETag_surfxml_link(void)
297 surfxml_call_cb_functions(ETag_surfxml_link_cb_list);
300 void STag_surfxml_route(void)
302 surfxml_call_cb_functions(STag_surfxml_route_cb_list);
305 void ETag_surfxml_route(void)
307 surfxml_call_cb_functions(ETag_surfxml_route_cb_list);
310 void STag_surfxml_link_c_ctn(void)
312 surfxml_call_cb_functions(STag_surfxml_link_c_ctn_cb_list);
315 void ETag_surfxml_link_c_ctn(void)
317 surfxml_call_cb_functions(ETag_surfxml_link_c_ctn_cb_list);
320 void STag_surfxml_process(void)
322 surfxml_call_cb_functions(STag_surfxml_process_cb_list);
325 void ETag_surfxml_process(void)
327 surfxml_call_cb_functions(ETag_surfxml_process_cb_list);
330 void STag_surfxml_argument(void)
332 surfxml_call_cb_functions(STag_surfxml_argument_cb_list);
335 void ETag_surfxml_argument(void)
337 surfxml_call_cb_functions(ETag_surfxml_argument_cb_list);
340 void STag_surfxml_prop(void)
342 surfxml_call_cb_functions(STag_surfxml_prop_cb_list);
345 void ETag_surfxml_prop(void)
347 surfxml_call_cb_functions(ETag_surfxml_prop_cb_list);
350 void STag_surfxml_set(void)
352 surfxml_call_cb_functions(STag_surfxml_set_cb_list);
355 void ETag_surfxml_set(void)
357 surfxml_call_cb_functions(ETag_surfxml_set_cb_list);
360 void STag_surfxml_foreach(void)
362 /* Save the current buffer */
363 surfxml_bufferstack_push(0);
364 surfxml_call_cb_functions(STag_surfxml_foreach_cb_list);
367 void ETag_surfxml_foreach(void)
369 surfxml_call_cb_functions(ETag_surfxml_foreach_cb_list);
371 /* free the temporary dynar and restore original */
372 xbt_dynar_free(&STag_surfxml_host_cb_list);
373 STag_surfxml_host_cb_list = main_STag_surfxml_host_cb_list;
375 /* free the temporary dynar and restore original */
376 xbt_dynar_free(&STag_surfxml_link_cb_list);
377 STag_surfxml_link_cb_list = main_STag_surfxml_link_cb_list;
380 void STag_surfxml_route_c_multi(void)
382 surfxml_call_cb_functions(STag_surfxml_route_c_multi_cb_list);
385 void ETag_surfxml_route_c_multi(void)
387 surfxml_call_cb_functions(ETag_surfxml_route_c_multi_cb_list);
390 void STag_surfxml_cluster(void)
392 surfxml_call_cb_functions(STag_surfxml_cluster_cb_list);
395 void ETag_surfxml_cluster(void)
397 surfxml_call_cb_functions(ETag_surfxml_cluster_cb_list);
400 void STag_surfxml_trace(void)
402 surfxml_call_cb_functions(STag_surfxml_trace_cb_list);
405 void ETag_surfxml_trace(void)
407 surfxml_call_cb_functions(ETag_surfxml_trace_cb_list);
410 void STag_surfxml_trace_c_connect(void)
412 surfxml_call_cb_functions(STag_surfxml_trace_c_connect_cb_list);
415 void ETag_surfxml_trace_c_connect(void)
417 surfxml_call_cb_functions(ETag_surfxml_trace_c_connect_cb_list);
420 void STag_surfxml_random(void)
422 surfxml_call_cb_functions(STag_surfxml_random_cb_list);
425 void ETag_surfxml_random(void)
427 surfxml_call_cb_functions(ETag_surfxml_random_cb_list);
430 void surf_parse_open(const char *file)
432 static int warned = 0; /* warn only once */
436 ("Bypassing the XML parser since surf_parse_open received a NULL pointer. If it is not what you want, go fix your code.");
441 surf_file_to_parse = surf_fopen(file, "r");
442 xbt_assert1((surf_file_to_parse), "Unable to open \"%s\"\n", file);
444 if (!surf_input_buffer_stack)
445 surf_input_buffer_stack = xbt_dynar_new(sizeof(YY_BUFFER_STATE), NULL);
446 if (!surf_file_to_parse_stack)
447 surf_file_to_parse_stack = xbt_dynar_new(sizeof(FILE *), NULL);
449 surf_input_buffer = surf_parse__create_buffer(surf_file_to_parse, 10);
450 surf_parse__switch_to_buffer(surf_input_buffer);
451 surf_parse_lineno = 1;
454 void surf_parse_close(void)
456 if (surf_input_buffer_stack)
457 xbt_dynar_free(&surf_input_buffer_stack);
458 if (surf_file_to_parse_stack)
459 xbt_dynar_free(&surf_file_to_parse_stack);
461 if (surf_file_to_parse) {
462 surf_parse__delete_buffer(surf_input_buffer);
463 fclose(surf_file_to_parse);
468 static int _surf_parse(void)
470 return surf_parse_lex();
473 int_f_void_t surf_parse = _surf_parse;
475 void surf_parse_error(char *msg)
477 fprintf(stderr, "Parse error on line %d: %s\n", surf_parse_lineno, msg);
482 void surf_parse_get_double(double *value, const char *string)
486 ret = sscanf(string, "%lg", value);
488 surf_parse_error(bprintf("%s is not a double", string));
491 void surf_parse_get_int(int *value, const char *string)
495 ret = sscanf(string, "%d", value);
497 surf_parse_error(bprintf("%s is not an integer", string));
500 void parse_properties(void)
504 if (!current_property_set)
505 current_property_set = xbt_dict_new();
507 value = xbt_strdup(A_surfxml_prop_value);
508 xbt_dict_set(current_property_set, A_surfxml_prop_id, value, free);
511 void surfxml_add_callback(xbt_dynar_t cb_list, void_f_void_t function)
513 xbt_dynar_push(cb_list, &function);
516 static XBT_INLINE void surfxml_call_cb_functions(xbt_dynar_t cb_list)
518 unsigned int iterator;
520 xbt_dynar_foreach(cb_list, iterator, fun) {
525 static void parse_route_set_endpoints(void)
527 route_link_list = xbt_dynar_new(sizeof(char *), NULL);
530 static void init_data(void)
532 xbt_dict_free(&route_table);
533 xbt_dynar_free(&route_link_list);
534 route_table = xbt_dict_new();
536 if (!surfxml_bufferstack_stack)
537 surfxml_bufferstack_stack = xbt_dynar_new(sizeof(char *), NULL);
538 route_multi_table = xbt_dict_new();
539 route_multi_elements = xbt_dynar_new(sizeof(char *), NULL);
540 traces_set_list = xbt_dict_new();
541 if (set_list == NULL)
542 set_list = xbt_dict_new();
544 trace_connect_list_host_avail = xbt_dict_new();
545 trace_connect_list_power = xbt_dict_new();
546 trace_connect_list_link_avail = xbt_dict_new();
547 trace_connect_list_bandwidth = xbt_dict_new();
548 trace_connect_list_latency = xbt_dict_new();
550 random_data_list = xbt_dict_new();
551 surfxml_add_callback(STag_surfxml_prop_cb_list, &parse_properties);
552 surfxml_add_callback(ETag_surfxml_link_c_ctn_cb_list, &parse_route_elem);
553 surfxml_add_callback(STag_surfxml_route_cb_list,
554 &parse_route_set_endpoints);
555 surfxml_add_callback(STag_surfxml_set_cb_list, &parse_sets);
556 surfxml_add_callback(STag_surfxml_route_c_multi_cb_list,
557 &parse_Stag_route_multi);
558 surfxml_add_callback(ETag_surfxml_route_c_multi_cb_list,
559 &parse_Etag_route_multi);
560 surfxml_add_callback(STag_surfxml_foreach_cb_list, &parse_Stag_foreach);
561 surfxml_add_callback(STag_surfxml_trace_cb_list, &parse_Stag_trace);
562 surfxml_add_callback(ETag_surfxml_trace_cb_list, &parse_Etag_trace);
563 surfxml_add_callback(STag_surfxml_trace_c_connect_cb_list,
564 &parse_Stag_trace_c_connect);
565 surfxml_add_callback(STag_surfxml_random_cb_list, &init_randomness);
566 surfxml_add_callback(ETag_surfxml_random_cb_list, &add_randomness);
569 static void free_data(void)
572 xbt_dict_cursor_t cursor = NULL;
574 unsigned int cpt = 0;
576 xbt_dict_foreach(route_table, cursor, key, data) {
577 xbt_dynar_t links = (xbt_dynar_t) data;
579 unsigned int cpt = 0;
581 xbt_dynar_foreach(links, cpt, name) free(name);
582 xbt_dynar_free(&links);
584 xbt_dict_free(&route_table);
585 route_link_list = NULL;
587 xbt_dict_free(&route_multi_table);
589 xbt_dynar_foreach(route_multi_elements, cpt, name) free(name);
590 xbt_dynar_free(&route_multi_elements);
592 xbt_dict_foreach(set_list, cursor, key, data) {
593 xbt_dynar_t set = (xbt_dynar_t) data;
595 xbt_dynar_foreach(set, cpt, name) free(name);
596 xbt_dynar_free(&set);
598 xbt_dict_free(&set_list);
600 xbt_dynar_free(&surfxml_bufferstack_stack);
602 xbt_dict_free(&traces_set_list);
603 xbt_dict_free(&trace_connect_list_host_avail);
604 xbt_dict_free(&trace_connect_list_power);
605 xbt_dict_free(&trace_connect_list_link_avail);
606 xbt_dict_free(&trace_connect_list_bandwidth);
607 xbt_dict_free(&trace_connect_list_latency);
608 xbt_dict_free(&traces_set_list);
611 void parse_platform_file(const char *file)
614 surf_parse_open(file);
616 parse_status = surf_parse();
620 xbt_dict_free(&random_data_list);
621 xbt_assert1(!parse_status, "Parse error in %s", file);
624 /* Functions to bypass route tag. Used by the route:multi tag */
626 static void parse_make_temporary_route(const char *src, const char *dst,
631 A_surfxml_route_action = action;
632 SURFXML_BUFFER_SET(route_src, src);
633 SURFXML_BUFFER_SET(route_dst, dst);
636 /* Functions for the sets and foreach tags */
638 static void parse_sets(void)
640 char *id, *suffix, *prefix, *radical;
642 xbt_dynar_t radical_elements;
643 xbt_dynar_t radical_ends;
644 xbt_dynar_t current_set;
645 char *value, *groups;
649 id = xbt_strdup(A_surfxml_set_id);
650 prefix = xbt_strdup(A_surfxml_set_prefix);
651 suffix = xbt_strdup(A_surfxml_set_suffix);
652 radical = xbt_strdup(A_surfxml_set_radical);
654 if (xbt_dict_get_or_null(set_list, id))
655 surf_parse_error(bprintf
656 ("Set '%s' declared several times in the platform file.",
659 current_set = xbt_dynar_new(sizeof(char *), NULL);
661 radical_elements = xbt_str_split(radical, ",");
662 xbt_dynar_foreach(radical_elements, iter, groups) {
664 radical_ends = xbt_str_split(groups, "-");
665 switch (xbt_dynar_length(radical_ends)) {
667 surf_parse_get_int(&start, xbt_dynar_get_as(radical_ends, 0, char *));
668 value = bprintf("%s%d%s", prefix, start, suffix);
669 xbt_dynar_push(current_set, &value);
674 surf_parse_get_int(&start, xbt_dynar_get_as(radical_ends, 0, char *));
675 surf_parse_get_int(&end, xbt_dynar_get_as(radical_ends, 1, char *));
678 for (i = start; i <= end; i++) {
679 value = bprintf("%s%d%s", prefix, i, suffix);
680 xbt_dynar_push(current_set, &value);
685 surf_parse_error(xbt_strdup("Malformed radical"));
688 xbt_dynar_free(&radical_ends);
691 xbt_dict_set(set_list, id, current_set, NULL);
693 xbt_dynar_free(&radical_elements);
700 static void parse_host_foreach(void){
702 xbt_dynar_t names = NULL;
703 unsigned int cpt = 0;
705 xbt_dict_cursor_t cursor = NULL;
708 const char *surfxml_host_power = A_surfxml_host_power;
709 const char *surfxml_host_availability = A_surfxml_host_availability;
710 const char *surfxml_host_availability_file = A_surfxml_host_availability_file;
711 const char *surfxml_host_state_file = A_surfxml_host_state_file;
714 xbt_dict_t cluster_host_props = current_property_set;
716 names = xbt_dict_get_or_null(set_list, foreach_set_name);
718 surf_parse_error(bprintf("Set name '%s' used in <foreach> not found.",
720 if (strcmp(A_surfxml_host_id, "$1"))
721 surf_parse_error(bprintf
722 ("The host id within <foreach> should point to the foreach set_id (use $1 instead of %s)",
726 /* foreach name in set call the main host callback */
727 xbt_dynar_foreach(names, cpt, name) {
728 int AX_ptr = 0; /* needed by the SURFXML_BUFFER_SET macro */
730 surfxml_bufferstack_push(1);
732 SURFXML_BUFFER_SET(host_id, name);
733 SURFXML_BUFFER_SET(host_power, surfxml_host_power /*hostPower */ );
734 SURFXML_BUFFER_SET(host_availability, surfxml_host_availability);
735 SURFXML_BUFFER_SET(host_availability_file, surfxml_host_availability_file);
736 SURFXML_BUFFER_SET(host_state_file, surfxml_host_state_file);
738 surfxml_call_cb_functions(main_STag_surfxml_host_cb_list);
740 xbt_dict_foreach(cluster_host_props, cursor, key, data) {
741 xbt_dict_set(current_property_set, xbt_strdup(key), xbt_strdup(data),
745 /* Call the (unmodified) callbacks of </host>, if any */
746 surfxml_call_cb_functions(ETag_surfxml_host_cb_list);
747 surfxml_bufferstack_pop(1);
750 current_property_set = xbt_dict_new();
752 surfxml_bufferstack_pop(0);
755 static void parse_link_foreach(void) {
756 const char *surfxml_link_bandwidth = A_surfxml_link_bandwidth;
757 const char *surfxml_link_bandwidth_file = A_surfxml_link_bandwidth_file;
758 const char *surfxml_link_latency = A_surfxml_link_latency;
759 const char *surfxml_link_latency_file = A_surfxml_link_latency_file;
760 const char *surfxml_link_state_file = A_surfxml_link_state_file;
762 xbt_dynar_t names = NULL;
763 unsigned int cpt = 0;
765 xbt_dict_cursor_t cursor = NULL;
768 xbt_dict_t cluster_link_props = current_property_set;
770 names = xbt_dict_get_or_null(set_list, foreach_set_name);
772 surf_parse_error(bprintf("Set name '%s' used in <foreach> not found.",
774 if (strcmp(A_surfxml_link_id, "$1"))
775 surf_parse_error(bprintf
776 ("The host id within <foreach> should point to the foreach set_id (use $1 instead of %s)",
779 /* for each name in set call the main link callback */
780 xbt_dynar_foreach(names, cpt, name) {
781 int AX_ptr = 0; /* needed by the SURFXML_BUFFER_SET */
783 surfxml_bufferstack_push(1);
785 SURFXML_BUFFER_SET(link_id, name);
786 SURFXML_BUFFER_SET(link_bandwidth, surfxml_link_bandwidth);
787 SURFXML_BUFFER_SET(link_bandwidth_file, surfxml_link_bandwidth_file);
788 SURFXML_BUFFER_SET(link_latency, surfxml_link_latency);
789 SURFXML_BUFFER_SET(link_latency_file, surfxml_link_latency_file);
790 SURFXML_BUFFER_SET(link_state_file, surfxml_link_state_file);
792 surfxml_call_cb_functions(main_STag_surfxml_link_cb_list);
794 xbt_dict_foreach(cluster_link_props, cursor, key, data) {
795 xbt_dict_set(current_property_set, xbt_strdup(key), xbt_strdup(data),
799 /* Call the (unmodified) callbacks of </link>, if any */
800 surfxml_call_cb_functions(ETag_surfxml_link_cb_list);
801 surfxml_bufferstack_pop(1);
804 current_property_set = xbt_dict_new();
806 surfxml_bufferstack_pop(0);
807 free(foreach_set_name);
808 foreach_set_name = NULL;
811 static void parse_Stag_foreach(void)
813 /* save the host & link callbacks */
814 main_STag_surfxml_host_cb_list = STag_surfxml_host_cb_list;
815 main_STag_surfxml_link_cb_list = STag_surfxml_link_cb_list;
817 /* redefine host & link callbacks to be used only by the foreach tag */
818 STag_surfxml_host_cb_list = xbt_dynar_new(sizeof(void_f_void_t), NULL);
819 STag_surfxml_link_cb_list = xbt_dynar_new(sizeof(void_f_void_t), NULL);
821 surfxml_add_callback(STag_surfxml_host_cb_list, &parse_host_foreach);
822 surfxml_add_callback(STag_surfxml_link_cb_list, &parse_link_foreach);
825 foreach_set_name = xbt_strdup(A_surfxml_foreach_set_id);
828 /* Route:multi functions */
830 static int route_multi_size = 0;
831 static char *src_name, *dst_name;
832 static int is_symmetric_route;
834 static void parse_route_elem(void)
838 val = xbt_strdup(A_surfxml_link_c_ctn_id);
840 xbt_dynar_push(route_link_list, &val);
841 //INFO2("Push %s (size now:%ld)",val,xbt_dynar_length(route_link_list));
844 static void parse_Stag_route_multi(void)
846 src_name = xbt_strdup(A_surfxml_route_c_multi_src);
847 dst_name = xbt_strdup(A_surfxml_route_c_multi_dst);
848 route_action = A_surfxml_route_c_multi_action;
849 is_symmetric_route = A_surfxml_route_c_multi_symmetric;
852 route_link_list = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
856 This function is used to append or override the contents of an already existing route in the case a new one with its name is found.
857 The decision is based upon the value of action specified in the xml route:multi attribute action
859 void manage_route(xbt_dict_t routing_table, const char *route_name,
860 int action, int isMultiRoute)
866 /* get already existing list if it exists */
867 links = xbt_dict_get_or_null(routing_table, route_name);
868 DEBUG3("ROUTE: %s (action:%s; len:%ld)", route_name,
869 (action==A_surfxml_route_action_OVERRIDE?"override":(
870 action==A_surfxml_route_action_PREPEND?"prepend":"postpend")),
871 (links?xbt_dynar_length(links):0));
875 case A_surfxml_route_action_PREPEND: /* add existing links at the end; route_link_list + links */
876 xbt_dynar_foreach(links, cpt, value) {
877 xbt_dynar_push(route_link_list, &value);
879 xbt_dynar_free(&links);
881 case A_surfxml_route_action_POSTPEND: /* add existing links in front; links + route_link_list */
882 xbt_dynar_foreach(route_link_list, cpt, value) {
883 xbt_dynar_push(links, &value);
885 xbt_dynar_free(&route_link_list);
886 route_link_list = links;
888 case A_surfxml_route_action_OVERRIDE:
889 xbt_dynar_free(&links);
892 xbt_die(bprintf("While dealing with routes of %s, got action=%d. Please report this bug.",
897 /* this is the final route; do not add if name is a set; add only if name is in set list */
899 xbt_dict_set(routing_table, route_name, route_link_list, NULL);
903 static void parse_Etag_route_multi(void)
908 bprintf("%s#%s#%d#%d#%d", src_name, dst_name, route_action,
909 is_symmetric_route, route_multi_size);
911 xbt_dynar_push(route_multi_elements, &route_name);
914 xbt_dict_set(route_multi_table, route_name, route_link_list, NULL);
915 /* add symmetric if it is the case */
916 if (is_symmetric_route == 1) {
917 char *symmetric_name =
918 bprintf("%s#%s#%d#%d#%d", dst_name, src_name, route_action,
919 !is_symmetric_route, route_multi_size);
921 xbt_dict_set(route_multi_table, symmetric_name, route_link_list, NULL);
922 xbt_dynar_push(route_multi_elements, &symmetric_name);
923 is_symmetric_route = 0;
929 static void add_multi_links(const char *src, const char *dst,
930 xbt_dynar_t links, const char *src_name,
931 const char *dst_name)
936 surfxml_bufferstack_push(1);
938 parse_make_temporary_route(src_name, dst_name, route_action);
939 surfxml_call_cb_functions(STag_surfxml_route_cb_list);
940 DEBUG2("\tADDING ROUTE: %s -> %s", src_name, dst_name);
941 /* Build link list */
942 xbt_dynar_foreach(links, cpt, value) {
943 if (strcmp(value, src) == 0)
944 val = xbt_strdup(src_name);
945 else if (strcmp(value, dst) == 0)
946 val = xbt_strdup(dst_name);
947 else if (strcmp(value, "$dst") == 0)
948 val = xbt_strdup(dst_name);
949 else if (strcmp(value, "$src") == 0)
950 val = xbt_strdup(src_name);
952 val = xbt_strdup(value);
953 DEBUG1("\t\tELEMENT: %s", val);
954 xbt_dynar_push(route_link_list, &val);
956 surfxml_call_cb_functions(ETag_surfxml_route_cb_list);
957 surfxml_bufferstack_pop(1);
960 static void convert_route_multi_to_routes(void)
962 xbt_dict_cursor_t cursor_w;
964 unsigned int cpt, cpt2, cursor;
965 char *src_host_name, *dst_host_name, *key, *src, *dst, *val, *key_w,
967 const char *sep = "#";
968 xbt_dict_t set = NULL;
969 xbt_dynar_t src_names = NULL, dst_names = NULL, links;
971 if (!route_multi_elements)
975 set = surf_model_resource_set(surf_cpu_model);
976 if (surf_workstation_model != NULL &&
977 surf_model_resource_set(surf_workstation_model) != NULL &&
978 xbt_dict_length(surf_model_resource_set(surf_workstation_model)) > 0)
979 set = surf_model_resource_set(surf_workstation_model);
982 surfxml_bufferstack_push(0);
983 /* Get all routes in the exact order they were entered in the platform file */
984 xbt_dynar_foreach(route_multi_elements, cursor, key) {
985 /* Get links for the route */
986 links = (xbt_dynar_t) xbt_dict_get_or_null(route_multi_table, key);
987 keys = xbt_str_split_str(key, sep);
989 src = xbt_dynar_get_as(keys, 0, char *);
990 dst = xbt_dynar_get_as(keys, 1, char *);
991 route_action = atoi(xbt_dynar_get_as(keys, 2, char *));
992 symmetric = atoi(xbt_dynar_get_as(keys, 3, char *));
994 /* Create the dynar of src and dst hosts for the new routes */
995 /* NOTE: src and dst can be either set names or simple host names */
996 src_names = (xbt_dynar_t) xbt_dict_get_or_null(set_list, src);
997 dst_names = (xbt_dynar_t) xbt_dict_get_or_null(set_list, dst);
998 /* Add to dynar even if they are simple names */
999 if (src_names == NULL) {
1000 src_names = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
1001 val = xbt_strdup(src);
1002 xbt_dynar_push(src_names, &val);
1003 if (strcmp(val, "$*") != 0 && NULL == xbt_dict_get_or_null(set, val))
1004 THROW3(unknown_error, 0,
1005 "(In route:multi (%s -> %s) source %s does not exist (not a set or a host)",
1008 if (dst_names == NULL) {
1009 dst_names = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
1010 val = xbt_strdup(dst);
1011 if (strcmp(val, "$*") != 0 && NULL == xbt_dict_get_or_null(set, val))
1012 THROW3(unknown_error, 0,
1013 "(In route:multi (%s -> %s) destination %s does not exist (not a set or a host)",
1015 xbt_dynar_push(dst_names, &val);
1018 /* Build the routes */
1019 DEBUG2("ADDING MULTI ROUTE: %s -> %s", xbt_dynar_get_as(keys, 0, char *),
1020 xbt_dynar_get_as(keys, 1, char *));
1021 xbt_dynar_foreach(src_names, cpt, src_host_name) {
1022 xbt_dynar_foreach(dst_names, cpt2, dst_host_name) {
1023 /* If dst is $* then set this route to have its dst point to all hosts */
1024 if (strcmp(src_host_name, "$*") != 0
1025 && strcmp(dst_host_name, "$*") == 0) {
1026 xbt_dict_foreach(set, cursor_w, key_w, data_w) {
1027 //int n = xbt_dynar_member(src_names, (char*)key_w);
1028 add_multi_links(src, dst, links, src_host_name, key_w);
1031 /* If src is $* then set this route to have its dst point to all hosts */
1032 if (strcmp(src_host_name, "$*") == 0
1033 && strcmp(dst_host_name, "$*") != 0) {
1034 xbt_dict_foreach(set, cursor_w, key_w, data_w) {
1035 // if (!symmetric || (symmetric && !contains(dst_names, key_w)))
1036 add_multi_links(src, dst, links, key_w, dst_host_name);
1039 /* if none of them are equal to $* */
1040 if (strcmp(src_host_name, "$*") != 0
1041 && strcmp(dst_host_name, "$*") != 0) {
1042 add_multi_links(src, dst, links, src_host_name, dst_host_name);
1046 xbt_dynar_free(&keys);
1048 surfxml_bufferstack_pop(0);
1052 /* Trace management functions */
1054 static double trace_periodicity = -1.0;
1055 static char *trace_file = NULL;
1056 static char *trace_id;
1058 static void parse_Stag_trace(void)
1060 trace_id = strdup(A_surfxml_trace_id);
1061 trace_file = strdup(A_surfxml_trace_file);
1062 surf_parse_get_double(&trace_periodicity, A_surfxml_trace_periodicity);
1065 static void parse_Etag_trace(void)
1068 if (!trace_file || strcmp(trace_file, "") != 0) {
1069 trace = tmgr_trace_new(trace_file);
1071 if (strcmp(surfxml_pcdata, "") == 0)
1075 tmgr_trace_new_from_string(trace_id, surfxml_pcdata,
1078 xbt_dict_set(traces_set_list, trace_id, (void *) trace, NULL);
1081 static void parse_Stag_trace_c_connect(void)
1083 xbt_assert2(xbt_dict_get_or_null
1084 (traces_set_list, A_surfxml_trace_c_connect_trace),
1085 "Cannot connect trace %s to %s: trace unknown",
1086 A_surfxml_trace_c_connect_trace,
1087 A_surfxml_trace_c_connect_element);
1089 switch (A_surfxml_trace_c_connect_kind) {
1090 case A_surfxml_trace_c_connect_kind_HOST_AVAIL:
1091 xbt_dict_set(trace_connect_list_host_avail,
1092 A_surfxml_trace_c_connect_trace,
1093 xbt_strdup(A_surfxml_trace_c_connect_element), free);
1095 case A_surfxml_trace_c_connect_kind_POWER:
1096 xbt_dict_set(trace_connect_list_power, A_surfxml_trace_c_connect_trace,
1097 xbt_strdup(A_surfxml_trace_c_connect_element), free);
1099 case A_surfxml_trace_c_connect_kind_LINK_AVAIL:
1100 xbt_dict_set(trace_connect_list_link_avail,
1101 A_surfxml_trace_c_connect_trace,
1102 xbt_strdup(A_surfxml_trace_c_connect_element), free);
1104 case A_surfxml_trace_c_connect_kind_BANDWIDTH:
1105 xbt_dict_set(trace_connect_list_bandwidth,
1106 A_surfxml_trace_c_connect_trace,
1107 xbt_strdup(A_surfxml_trace_c_connect_element), free);
1109 case A_surfxml_trace_c_connect_kind_LATENCY:
1110 xbt_dict_set(trace_connect_list_latency, A_surfxml_trace_c_connect_trace,
1111 xbt_strdup(A_surfxml_trace_c_connect_element), free);
1114 xbt_die(bprintf("Cannot connect trace %s to %s: kind of trace unknown",
1115 A_surfxml_trace_c_connect_trace,
1116 A_surfxml_trace_c_connect_element));
1120 /* Random tag functions */
1122 double get_cpu_power(const char *power)
1124 double power_scale = 0.0;
1127 random_data_t random = NULL;
1128 /* randomness is inserted like this: power="$rand(my_random)" */
1129 if (((p = strstr(power, "$rand(")) != NULL)
1130 && ((q = strstr(power, ")")) != NULL)) {
1132 generator = xbt_malloc(q - (p + 6) + 1);
1133 memcpy(generator, p + 6, q - (p + 6));
1134 generator[q - (p + 6)] = '\0';
1135 xbt_assert1((random =
1136 xbt_dict_get_or_null(random_data_list, generator)),
1137 "Random generator %s undefined", generator);
1138 power_scale = random_generate(random);
1141 surf_parse_get_double(&power_scale, power);
1146 double random_min, random_max, random_mean, random_std_deviation,
1150 static void init_randomness(void)
1152 random_id = A_surfxml_random_id;
1153 surf_parse_get_double(&random_min, A_surfxml_random_min);
1154 surf_parse_get_double(&random_max, A_surfxml_random_max);
1155 surf_parse_get_double(&random_mean, A_surfxml_random_mean);
1156 surf_parse_get_double(&random_std_deviation,
1157 A_surfxml_random_std_deviation);
1158 random_generator = A_surfxml_random_generator;
1161 static void add_randomness(void)
1163 /* If needed aditional properties can be added by using the prop tag */
1164 random_data_t random =
1165 random_new(random_generator, 0, random_min, random_max, random_mean,
1166 random_std_deviation);
1167 xbt_dict_set(random_data_list, random_id, (void *) random, NULL);
1171 /* Init Host bypassing XML*/
1172 void init_host_bypass(char* name,double power)
1174 surf_cpu_im_init_bypass(name,power);