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);
274 void ETag_surfxml_host(void)
276 surfxml_call_cb_functions(ETag_surfxml_host_cb_list);
279 void STag_surfxml_router(void)
281 surfxml_call_cb_functions(STag_surfxml_router_cb_list);
284 void ETag_surfxml_router(void)
286 surfxml_call_cb_functions(ETag_surfxml_router_cb_list);
289 void STag_surfxml_link(void)
291 surfxml_call_cb_functions(STag_surfxml_link_cb_list);
294 void ETag_surfxml_link(void)
296 surfxml_call_cb_functions(ETag_surfxml_link_cb_list);
299 void STag_surfxml_route(void)
301 surfxml_call_cb_functions(STag_surfxml_route_cb_list);
304 void ETag_surfxml_route(void)
306 surfxml_call_cb_functions(ETag_surfxml_route_cb_list);
309 void STag_surfxml_link_c_ctn(void)
311 surfxml_call_cb_functions(STag_surfxml_link_c_ctn_cb_list);
314 void ETag_surfxml_link_c_ctn(void)
316 surfxml_call_cb_functions(ETag_surfxml_link_c_ctn_cb_list);
319 void STag_surfxml_process(void)
321 surfxml_call_cb_functions(STag_surfxml_process_cb_list);
324 void ETag_surfxml_process(void)
326 surfxml_call_cb_functions(ETag_surfxml_process_cb_list);
329 void STag_surfxml_argument(void)
331 surfxml_call_cb_functions(STag_surfxml_argument_cb_list);
334 void ETag_surfxml_argument(void)
336 surfxml_call_cb_functions(ETag_surfxml_argument_cb_list);
339 void STag_surfxml_prop(void)
341 surfxml_call_cb_functions(STag_surfxml_prop_cb_list);
344 void ETag_surfxml_prop(void)
346 surfxml_call_cb_functions(ETag_surfxml_prop_cb_list);
349 void STag_surfxml_set(void)
351 surfxml_call_cb_functions(STag_surfxml_set_cb_list);
354 void ETag_surfxml_set(void)
356 surfxml_call_cb_functions(ETag_surfxml_set_cb_list);
359 void STag_surfxml_foreach(void)
361 /* Save the current buffer */
362 surfxml_bufferstack_push(0);
363 surfxml_call_cb_functions(STag_surfxml_foreach_cb_list);
366 void ETag_surfxml_foreach(void)
368 surfxml_call_cb_functions(ETag_surfxml_foreach_cb_list);
370 /* free the temporary dynar and restore original */
371 xbt_dynar_free(&STag_surfxml_host_cb_list);
372 STag_surfxml_host_cb_list = main_STag_surfxml_host_cb_list;
374 /* free the temporary dynar and restore original */
375 xbt_dynar_free(&STag_surfxml_link_cb_list);
376 STag_surfxml_link_cb_list = main_STag_surfxml_link_cb_list;
379 void STag_surfxml_route_c_multi(void)
381 surfxml_call_cb_functions(STag_surfxml_route_c_multi_cb_list);
384 void ETag_surfxml_route_c_multi(void)
386 surfxml_call_cb_functions(ETag_surfxml_route_c_multi_cb_list);
389 void STag_surfxml_cluster(void)
391 surfxml_call_cb_functions(STag_surfxml_cluster_cb_list);
394 void ETag_surfxml_cluster(void)
396 surfxml_call_cb_functions(ETag_surfxml_cluster_cb_list);
399 void STag_surfxml_trace(void)
401 surfxml_call_cb_functions(STag_surfxml_trace_cb_list);
404 void ETag_surfxml_trace(void)
406 surfxml_call_cb_functions(ETag_surfxml_trace_cb_list);
409 void STag_surfxml_trace_c_connect(void)
411 surfxml_call_cb_functions(STag_surfxml_trace_c_connect_cb_list);
414 void ETag_surfxml_trace_c_connect(void)
416 surfxml_call_cb_functions(ETag_surfxml_trace_c_connect_cb_list);
419 void STag_surfxml_random(void)
421 surfxml_call_cb_functions(STag_surfxml_random_cb_list);
424 void ETag_surfxml_random(void)
426 surfxml_call_cb_functions(ETag_surfxml_random_cb_list);
429 void surf_parse_open(const char *file)
431 static int warned = 0; /* warn only once */
435 ("Bypassing the XML parser since surf_parse_open received a NULL pointer. If it is not what you want, go fix your code.");
440 if (!surf_input_buffer_stack)
441 surf_input_buffer_stack = xbt_dynar_new(sizeof(YY_BUFFER_STATE), NULL);
442 if (!surf_file_to_parse_stack)
443 surf_file_to_parse_stack = xbt_dynar_new(sizeof(FILE *), NULL);
445 surf_file_to_parse = surf_fopen(file, "r");
446 xbt_assert1((surf_file_to_parse), "Unable to open \"%s\"\n", file);
447 surf_input_buffer = surf_parse__create_buffer(surf_file_to_parse, 10);
448 surf_parse__switch_to_buffer(surf_input_buffer);
449 surf_parse_lineno = 1;
452 void surf_parse_close(void)
454 if (surf_input_buffer_stack)
455 xbt_dynar_free(&surf_input_buffer_stack);
456 if (surf_file_to_parse_stack)
457 xbt_dynar_free(&surf_file_to_parse_stack);
459 if (surf_file_to_parse) {
460 surf_parse__delete_buffer(surf_input_buffer);
461 fclose(surf_file_to_parse);
466 static int _surf_parse(void)
468 return surf_parse_lex();
471 int_f_void_t surf_parse = _surf_parse;
473 void surf_parse_error(char *msg)
475 fprintf(stderr, "Parse error on line %d: %s\n", surf_parse_lineno, msg);
480 void surf_parse_get_double(double *value, const char *string)
484 ret = sscanf(string, "%lg", value);
486 surf_parse_error(bprintf("%s is not a double", string));
489 void surf_parse_get_int(int *value, const char *string)
493 ret = sscanf(string, "%d", value);
495 surf_parse_error(bprintf("%s is not an integer", string));
498 void parse_properties(void)
502 if (!current_property_set)
503 current_property_set = xbt_dict_new();
505 value = xbt_strdup(A_surfxml_prop_value);
506 xbt_dict_set(current_property_set, A_surfxml_prop_id, value, free);
509 void surfxml_add_callback(xbt_dynar_t cb_list, void_f_void_t function)
511 xbt_dynar_push(cb_list, &function);
514 static XBT_INLINE void surfxml_call_cb_functions(xbt_dynar_t cb_list)
516 unsigned int iterator;
518 xbt_dynar_foreach(cb_list, iterator, fun) {
523 static void parse_route_set_endpoints(void)
525 route_link_list = xbt_dynar_new(sizeof(char *), NULL);
528 static void init_data(void)
530 xbt_dict_free(&route_table);
531 xbt_dynar_free(&route_link_list);
532 route_table = xbt_dict_new();
534 if (!surfxml_bufferstack_stack)
535 surfxml_bufferstack_stack = xbt_dynar_new(sizeof(char *), NULL);
536 route_multi_table = xbt_dict_new();
537 route_multi_elements = xbt_dynar_new(sizeof(char *), NULL);
538 traces_set_list = xbt_dict_new();
539 if (set_list == NULL)
540 set_list = xbt_dict_new();
542 trace_connect_list_host_avail = xbt_dict_new();
543 trace_connect_list_power = xbt_dict_new();
544 trace_connect_list_link_avail = xbt_dict_new();
545 trace_connect_list_bandwidth = xbt_dict_new();
546 trace_connect_list_latency = xbt_dict_new();
548 random_data_list = xbt_dict_new();
549 surfxml_add_callback(STag_surfxml_prop_cb_list, &parse_properties);
550 surfxml_add_callback(ETag_surfxml_link_c_ctn_cb_list, &parse_route_elem);
551 surfxml_add_callback(STag_surfxml_route_cb_list,
552 &parse_route_set_endpoints);
553 surfxml_add_callback(STag_surfxml_set_cb_list, &parse_sets);
554 surfxml_add_callback(STag_surfxml_route_c_multi_cb_list,
555 &parse_Stag_route_multi);
556 surfxml_add_callback(ETag_surfxml_route_c_multi_cb_list,
557 &parse_Etag_route_multi);
558 surfxml_add_callback(STag_surfxml_foreach_cb_list, &parse_Stag_foreach);
559 surfxml_add_callback(STag_surfxml_trace_cb_list, &parse_Stag_trace);
560 surfxml_add_callback(ETag_surfxml_trace_cb_list, &parse_Etag_trace);
561 surfxml_add_callback(STag_surfxml_trace_c_connect_cb_list,
562 &parse_Stag_trace_c_connect);
563 surfxml_add_callback(STag_surfxml_random_cb_list, &init_randomness);
564 surfxml_add_callback(ETag_surfxml_random_cb_list, &add_randomness);
567 static void free_data(void)
570 xbt_dict_cursor_t cursor = NULL;
572 unsigned int cpt = 0;
574 xbt_dict_foreach(route_table, cursor, key, data) {
575 xbt_dynar_t links = (xbt_dynar_t) data;
577 unsigned int cpt = 0;
579 xbt_dynar_foreach(links, cpt, name) free(name);
580 xbt_dynar_free(&links);
582 xbt_dict_free(&route_table);
583 route_link_list = NULL;
585 xbt_dict_free(&route_multi_table);
587 xbt_dynar_foreach(route_multi_elements, cpt, name) free(name);
588 xbt_dynar_free(&route_multi_elements);
590 xbt_dict_foreach(set_list, cursor, key, data) {
591 xbt_dynar_t set = (xbt_dynar_t) data;
593 xbt_dynar_foreach(set, cpt, name) free(name);
594 xbt_dynar_free(&set);
596 xbt_dict_free(&set_list);
598 xbt_dynar_free(&surfxml_bufferstack_stack);
600 xbt_dict_free(&traces_set_list);
601 xbt_dict_free(&trace_connect_list_host_avail);
602 xbt_dict_free(&trace_connect_list_power);
603 xbt_dict_free(&trace_connect_list_link_avail);
604 xbt_dict_free(&trace_connect_list_bandwidth);
605 xbt_dict_free(&trace_connect_list_latency);
606 xbt_dict_free(&traces_set_list);
609 void parse_platform_file(const char *file)
611 surf_parse_open(file);
613 xbt_assert1((!(*surf_parse) ()), "Parse error in %s", file);
618 /* Functions to bypass route tag. Used by the route:multi tag */
620 static void parse_make_temporary_route(const char *src, const char *dst,
625 A_surfxml_route_action = action;
626 SURFXML_BUFFER_SET(route_src, src);
627 SURFXML_BUFFER_SET(route_dst, dst);
630 /* Functions for the sets and foreach tags */
632 static void parse_sets(void)
634 char *id, *suffix, *prefix, *radical;
636 xbt_dynar_t radical_elements;
637 xbt_dynar_t radical_ends;
638 xbt_dynar_t current_set;
639 char *value, *groups;
643 id = xbt_strdup(A_surfxml_set_id);
644 prefix = xbt_strdup(A_surfxml_set_prefix);
645 suffix = xbt_strdup(A_surfxml_set_suffix);
646 radical = xbt_strdup(A_surfxml_set_radical);
648 if (xbt_dict_get_or_null(set_list, id))
649 surf_parse_error(bprintf
650 ("Set '%s' declared several times in the platform file.",
653 current_set = xbt_dynar_new(sizeof(char *), NULL);
655 radical_elements = xbt_str_split(radical, ",");
656 xbt_dynar_foreach(radical_elements, iter, groups) {
658 radical_ends = xbt_str_split(groups, "-");
659 switch (xbt_dynar_length(radical_ends)) {
661 surf_parse_get_int(&start, xbt_dynar_get_as(radical_ends, 0, char *));
662 value = bprintf("%s%d%s", prefix, start, suffix);
663 xbt_dynar_push(current_set, &value);
668 surf_parse_get_int(&start, xbt_dynar_get_as(radical_ends, 0, char *));
669 surf_parse_get_int(&end, xbt_dynar_get_as(radical_ends, 1, char *));
672 for (i = start; i <= end; i++) {
673 value = bprintf("%s%d%s", prefix, i, suffix);
674 xbt_dynar_push(current_set, &value);
679 surf_parse_error(xbt_strdup("Malformed radical"));
682 xbt_dynar_free(&radical_ends);
685 xbt_dict_set(set_list, id, current_set, NULL);
687 xbt_dynar_free(&radical_elements);
694 static void parse_host_foreach(void){
695 xbt_dynar_t names = NULL;
696 unsigned int cpt = 0;
698 xbt_dict_cursor_t cursor = NULL;
701 const char *surfxml_host_power = A_surfxml_host_power;
702 const char *surfxml_host_availability = A_surfxml_host_availability;
703 const char *surfxml_host_availability_file = A_surfxml_host_availability_file;
704 const char *surfxml_host_state_file = A_surfxml_host_state_file;
707 xbt_dict_t cluster_host_props = current_property_set;
709 names = xbt_dict_get_or_null(set_list, foreach_set_name);
711 surf_parse_error(bprintf("Set name '%s' used in <foreach> not found.",
713 if (strcmp(A_surfxml_host_id, "$1"))
714 surf_parse_error(bprintf
715 ("The host id within <foreach> should point to the foreach set_id (use $1 instead of %s)",
719 /* foreach name in set call the main host callback */
720 xbt_dynar_foreach(names, cpt, name) {
721 int AX_ptr = 0; /* needed by the SURFXML_BUFFER_SET macro */
723 surfxml_bufferstack_push(1);
725 SURFXML_BUFFER_SET(host_id, name);
726 SURFXML_BUFFER_SET(host_power, surfxml_host_power /*hostPower */ );
727 SURFXML_BUFFER_SET(host_availability, surfxml_host_availability);
728 SURFXML_BUFFER_SET(host_availability_file, surfxml_host_availability_file);
729 SURFXML_BUFFER_SET(host_state_file, surfxml_host_state_file);
731 surfxml_call_cb_functions(main_STag_surfxml_host_cb_list);
733 xbt_dict_foreach(cluster_host_props, cursor, key, data) {
734 xbt_dict_set(current_property_set, xbt_strdup(key), xbt_strdup(data),
738 /* Call the (unmodified) callbacks of </host>, if any */
739 surfxml_call_cb_functions(ETag_surfxml_host_cb_list);
740 surfxml_bufferstack_pop(1);
743 current_property_set = xbt_dict_new();
745 surfxml_bufferstack_pop(0);
748 static void parse_link_foreach(void) {
749 const char *surfxml_link_bandwidth = A_surfxml_link_bandwidth;
750 const char *surfxml_link_bandwidth_file = A_surfxml_link_bandwidth_file;
751 const char *surfxml_link_latency = A_surfxml_link_latency;
752 const char *surfxml_link_latency_file = A_surfxml_link_latency_file;
753 const char *surfxml_link_state_file = A_surfxml_link_state_file;
755 xbt_dynar_t names = NULL;
756 unsigned int cpt = 0;
758 xbt_dict_cursor_t cursor = NULL;
761 xbt_dict_t cluster_link_props = current_property_set;
763 names = xbt_dict_get_or_null(set_list, foreach_set_name);
765 surf_parse_error(bprintf("Set name '%s' used in <foreach> not found.",
767 if (strcmp(A_surfxml_link_id, "$1"))
768 surf_parse_error(bprintf
769 ("The host id within <foreach> should point to the foreach set_id (use $1 instead of %s)",
772 /* for each name in set call the main link callback */
773 xbt_dynar_foreach(names, cpt, name) {
774 int AX_ptr = 0; /* needed by the SURFXML_BUFFER_SET */
776 surfxml_bufferstack_push(1);
778 SURFXML_BUFFER_SET(link_id, name);
779 SURFXML_BUFFER_SET(link_bandwidth, surfxml_link_bandwidth);
780 SURFXML_BUFFER_SET(link_bandwidth_file, surfxml_link_bandwidth_file);
781 SURFXML_BUFFER_SET(link_latency, surfxml_link_latency);
782 SURFXML_BUFFER_SET(link_latency_file, surfxml_link_latency_file);
783 SURFXML_BUFFER_SET(link_state_file, surfxml_link_state_file);
785 surfxml_call_cb_functions(main_STag_surfxml_link_cb_list);
787 xbt_dict_foreach(cluster_link_props, cursor, key, data) {
788 xbt_dict_set(current_property_set, xbt_strdup(key), xbt_strdup(data),
792 /* Call the (unmodified) callbacks of </link>, if any */
793 surfxml_call_cb_functions(ETag_surfxml_link_cb_list);
794 surfxml_bufferstack_pop(1);
797 current_property_set = xbt_dict_new();
799 surfxml_bufferstack_pop(0);
800 free(foreach_set_name);
801 foreach_set_name = NULL;
804 static void parse_Stag_foreach(void)
806 /* save the host & link callbacks */
807 main_STag_surfxml_host_cb_list = STag_surfxml_host_cb_list;
808 main_STag_surfxml_link_cb_list = STag_surfxml_link_cb_list;
810 /* redefine host & link callbacks to be used only by the foreach tag */
811 STag_surfxml_host_cb_list = xbt_dynar_new(sizeof(void_f_void_t), NULL);
812 STag_surfxml_link_cb_list = xbt_dynar_new(sizeof(void_f_void_t), NULL);
814 surfxml_add_callback(STag_surfxml_host_cb_list, &parse_host_foreach);
815 surfxml_add_callback(STag_surfxml_link_cb_list, &parse_link_foreach);
818 foreach_set_name = xbt_strdup(A_surfxml_foreach_set_id);
821 /* Route:multi functions */
823 static int route_multi_size = 0;
824 static char *src_name, *dst_name;
825 static int is_symmetric_route;
827 static void parse_route_elem(void)
831 val = xbt_strdup(A_surfxml_link_c_ctn_id);
833 xbt_dynar_push(route_link_list, &val);
834 //INFO2("Push %s (size now:%ld)",val,xbt_dynar_length(route_link_list));
837 static void parse_Stag_route_multi(void)
839 src_name = xbt_strdup(A_surfxml_route_c_multi_src);
840 dst_name = xbt_strdup(A_surfxml_route_c_multi_dst);
841 route_action = A_surfxml_route_c_multi_action;
842 is_symmetric_route = A_surfxml_route_c_multi_symmetric;
845 route_link_list = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
849 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.
850 The decision is based upon the value of action specified in the xml route:multi attribute action
852 void manage_route(xbt_dict_t routing_table, const char *route_name,
853 int action, int isMultiRoute)
859 /* get already existing list if it exists */
860 links = xbt_dict_get_or_null(routing_table, route_name);
861 DEBUG3("ROUTE: %s (action:%s; len:%ld)", route_name,
862 (action==A_surfxml_route_action_OVERRIDE?"override":(
863 action==A_surfxml_route_action_PREPEND?"prepend":"postpend")),
864 (links?xbt_dynar_length(links):0));
868 case A_surfxml_route_action_PREPEND: /* add existing links at the end; route_link_list + links */
869 xbt_dynar_foreach(links, cpt, value) {
870 xbt_dynar_push(route_link_list, &value);
872 xbt_dynar_free(&links);
874 case A_surfxml_route_action_POSTPEND: /* add existing links in front; links + route_link_list */
875 xbt_dynar_foreach(route_link_list, cpt, value) {
876 xbt_dynar_push(links, &value);
878 xbt_dynar_free(&route_link_list);
879 route_link_list = links;
881 case A_surfxml_route_action_OVERRIDE:
882 xbt_dynar_free(&links);
885 xbt_die(bprintf("While dealing with routes of %s, got action=%d. Please report this bug.",
890 /* this is the final route; do not add if name is a set; add only if name is in set list */
892 xbt_dict_set(routing_table, route_name, route_link_list, NULL);
896 static void parse_Etag_route_multi(void)
901 bprintf("%s#%s#%d#%d#%d", src_name, dst_name, route_action,
902 is_symmetric_route, route_multi_size);
904 xbt_dynar_push(route_multi_elements, &route_name);
907 xbt_dict_set(route_multi_table, route_name, route_link_list, NULL);
908 /* add symmetric if it is the case */
909 if (is_symmetric_route == 1) {
910 char *symmetric_name =
911 bprintf("%s#%s#%d#%d#%d", dst_name, src_name, route_action,
912 !is_symmetric_route, route_multi_size);
914 xbt_dict_set(route_multi_table, symmetric_name, route_link_list, NULL);
915 xbt_dynar_push(route_multi_elements, &symmetric_name);
916 is_symmetric_route = 0;
922 static void add_multi_links(const char *src, const char *dst,
923 xbt_dynar_t links, const char *src_name,
924 const char *dst_name)
929 surfxml_bufferstack_push(1);
931 parse_make_temporary_route(src_name, dst_name, route_action);
932 surfxml_call_cb_functions(STag_surfxml_route_cb_list);
933 DEBUG2("\tADDING ROUTE: %s -> %s", src_name, dst_name);
934 /* Build link list */
935 xbt_dynar_foreach(links, cpt, value) {
936 if (strcmp(value, src) == 0)
937 val = xbt_strdup(src_name);
938 else if (strcmp(value, dst) == 0)
939 val = xbt_strdup(dst_name);
940 else if (strcmp(value, "$dst") == 0)
941 val = xbt_strdup(dst_name);
942 else if (strcmp(value, "$src") == 0)
943 val = xbt_strdup(src_name);
945 val = xbt_strdup(value);
946 DEBUG1("\t\tELEMENT: %s", val);
947 xbt_dynar_push(route_link_list, &val);
949 surfxml_call_cb_functions(ETag_surfxml_route_cb_list);
950 surfxml_bufferstack_pop(1);
953 static void convert_route_multi_to_routes(void)
955 xbt_dict_cursor_t cursor_w;
957 unsigned int cpt, cpt2, cursor;
958 char *src_host_name, *dst_host_name, *key, *src, *dst, *val, *key_w,
960 const char *sep = "#";
961 xbt_dict_t set = NULL;
962 xbt_dynar_t src_names = NULL, dst_names = NULL, links;
964 if (!route_multi_elements)
968 set = surf_model_resource_set(surf_cpu_model);
969 if (surf_workstation_model != NULL &&
970 surf_model_resource_set(surf_workstation_model) != NULL &&
971 xbt_dict_length(surf_model_resource_set(surf_workstation_model)) > 0)
972 set = surf_model_resource_set(surf_workstation_model);
975 surfxml_bufferstack_push(0);
976 /* Get all routes in the exact order they were entered in the platform file */
977 xbt_dynar_foreach(route_multi_elements, cursor, key) {
978 /* Get links for the route */
979 links = (xbt_dynar_t) xbt_dict_get_or_null(route_multi_table, key);
980 keys = xbt_str_split_str(key, sep);
982 src = xbt_dynar_get_as(keys, 0, char *);
983 dst = xbt_dynar_get_as(keys, 1, char *);
984 route_action = atoi(xbt_dynar_get_as(keys, 2, char *));
985 symmetric = atoi(xbt_dynar_get_as(keys, 3, char *));
987 /* Create the dynar of src and dst hosts for the new routes */
988 /* NOTE: src and dst can be either set names or simple host names */
989 src_names = (xbt_dynar_t) xbt_dict_get_or_null(set_list, src);
990 dst_names = (xbt_dynar_t) xbt_dict_get_or_null(set_list, dst);
991 /* Add to dynar even if they are simple names */
992 if (src_names == NULL) {
993 src_names = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
994 val = xbt_strdup(src);
995 xbt_dynar_push(src_names, &val);
996 if (strcmp(val, "$*") != 0 && NULL == xbt_dict_get_or_null(set, val))
997 THROW3(unknown_error, 0,
998 "(In route:multi (%s -> %s) source %s does not exist (not a set or a host)",
1001 if (dst_names == NULL) {
1002 dst_names = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
1003 val = xbt_strdup(dst);
1004 if (strcmp(val, "$*") != 0 && NULL == xbt_dict_get_or_null(set, val))
1005 THROW3(unknown_error, 0,
1006 "(In route:multi (%s -> %s) destination %s does not exist (not a set or a host)",
1008 xbt_dynar_push(dst_names, &val);
1011 /* Build the routes */
1012 DEBUG2("ADDING MULTI ROUTE: %s -> %s", xbt_dynar_get_as(keys, 0, char *),
1013 xbt_dynar_get_as(keys, 1, char *));
1014 xbt_dynar_foreach(src_names, cpt, src_host_name) {
1015 xbt_dynar_foreach(dst_names, cpt2, dst_host_name) {
1016 /* If dst is $* then set this route to have its dst point to all hosts */
1017 if (strcmp(src_host_name, "$*") != 0
1018 && strcmp(dst_host_name, "$*") == 0) {
1019 xbt_dict_foreach(set, cursor_w, key_w, data_w) {
1020 //int n = xbt_dynar_member(src_names, (char*)key_w);
1021 add_multi_links(src, dst, links, src_host_name, key_w);
1024 /* If src is $* then set this route to have its dst point to all hosts */
1025 if (strcmp(src_host_name, "$*") == 0
1026 && strcmp(dst_host_name, "$*") != 0) {
1027 xbt_dict_foreach(set, cursor_w, key_w, data_w) {
1028 // if (!symmetric || (symmetric && !contains(dst_names, key_w)))
1029 add_multi_links(src, dst, links, key_w, dst_host_name);
1032 /* if none of them are equal to $* */
1033 if (strcmp(src_host_name, "$*") != 0
1034 && strcmp(dst_host_name, "$*") != 0) {
1035 add_multi_links(src, dst, links, src_host_name, dst_host_name);
1039 xbt_dynar_free(&keys);
1041 surfxml_bufferstack_pop(0);
1045 /* Trace management functions */
1047 static double trace_periodicity = -1.0;
1048 static char *trace_file = NULL;
1049 static char *trace_id;
1051 static void parse_Stag_trace(void)
1053 trace_id = strdup(A_surfxml_trace_id);
1054 trace_file = strdup(A_surfxml_trace_file);
1055 surf_parse_get_double(&trace_periodicity, A_surfxml_trace_periodicity);
1058 static void parse_Etag_trace(void)
1061 if (!trace_file || strcmp(trace_file, "") != 0) {
1062 trace = tmgr_trace_new(trace_file);
1064 if (strcmp(surfxml_pcdata, "") == 0)
1068 tmgr_trace_new_from_string(trace_id, surfxml_pcdata,
1071 xbt_dict_set(traces_set_list, trace_id, (void *) trace, NULL);
1074 static void parse_Stag_trace_c_connect(void)
1076 xbt_assert2(xbt_dict_get_or_null
1077 (traces_set_list, A_surfxml_trace_c_connect_trace),
1078 "Cannot connect trace %s to %s: trace unknown",
1079 A_surfxml_trace_c_connect_trace,
1080 A_surfxml_trace_c_connect_element);
1082 switch (A_surfxml_trace_c_connect_kind) {
1083 case A_surfxml_trace_c_connect_kind_HOST_AVAIL:
1084 xbt_dict_set(trace_connect_list_host_avail,
1085 A_surfxml_trace_c_connect_trace,
1086 xbt_strdup(A_surfxml_trace_c_connect_element), free);
1088 case A_surfxml_trace_c_connect_kind_POWER:
1089 xbt_dict_set(trace_connect_list_power, A_surfxml_trace_c_connect_trace,
1090 xbt_strdup(A_surfxml_trace_c_connect_element), free);
1092 case A_surfxml_trace_c_connect_kind_LINK_AVAIL:
1093 xbt_dict_set(trace_connect_list_link_avail,
1094 A_surfxml_trace_c_connect_trace,
1095 xbt_strdup(A_surfxml_trace_c_connect_element), free);
1097 case A_surfxml_trace_c_connect_kind_BANDWIDTH:
1098 xbt_dict_set(trace_connect_list_bandwidth,
1099 A_surfxml_trace_c_connect_trace,
1100 xbt_strdup(A_surfxml_trace_c_connect_element), free);
1102 case A_surfxml_trace_c_connect_kind_LATENCY:
1103 xbt_dict_set(trace_connect_list_latency, A_surfxml_trace_c_connect_trace,
1104 xbt_strdup(A_surfxml_trace_c_connect_element), free);
1107 xbt_die(bprintf("Cannot connect trace %s to %s: kind of trace unknown",
1108 A_surfxml_trace_c_connect_trace,
1109 A_surfxml_trace_c_connect_element));
1113 /* Random tag functions */
1115 double get_cpu_power(const char *power)
1117 double power_scale = 0.0;
1120 random_data_t random = NULL;
1121 /* randomness is inserted like this: power="$rand(my_random)" */
1122 if (((p = strstr(power, "$rand(")) != NULL)
1123 && ((q = strstr(power, ")")) != NULL)) {
1125 generator = xbt_malloc(q - (p + 6) + 1);
1126 memcpy(generator, p + 6, q - (p + 6));
1127 generator[q - (p + 6)] = '\0';
1128 xbt_assert1((random =
1129 xbt_dict_get_or_null(random_data_list, generator)),
1130 "Random generator %s undefined", generator);
1131 power_scale = random_generate(random);
1134 surf_parse_get_double(&power_scale, power);
1139 double random_min, random_max, random_mean, random_std_deviation,
1143 static void init_randomness(void)
1145 random_id = A_surfxml_random_id;
1146 surf_parse_get_double(&random_min, A_surfxml_random_min);
1147 surf_parse_get_double(&random_max, A_surfxml_random_max);
1148 surf_parse_get_double(&random_mean, A_surfxml_random_mean);
1149 surf_parse_get_double(&random_std_deviation,
1150 A_surfxml_random_std_deviation);
1151 random_generator = A_surfxml_random_generator;
1154 static void add_randomness(void)
1156 /* If needed aditional properties can be added by using the prop tag */
1157 random_data_t random =
1158 random_new(random_generator, 0, random_min, random_max, random_mean,
1159 random_std_deviation);
1160 xbt_dict_set(random_data_list, random_id, (void *) random, NULL);