3 /* Copyright (c) 2004 Arnaud Legrand. All rights reserved. */
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. */
12 #include "surf/surfxml_parse_private.h"
13 #include "surf/surf_private.h"
15 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_parse, surf,
16 "Logging specific to the SURF parsing module");
19 #include "simgrid_dtd.c"
21 /* Initialize the parsing globals */
23 xbt_dict_t traces_set_list = NULL;
24 //xbt_dynar_t traces_connect_list = NULL;
25 xbt_dict_t trace_connect_list_host_avail = NULL;
26 xbt_dict_t trace_connect_list_power = NULL;
27 xbt_dict_t trace_connect_list_link_avail = NULL;
28 xbt_dict_t trace_connect_list_bandwidth = NULL;
29 xbt_dict_t trace_connect_list_latency = NULL;
31 /* This buffer is used to store the original buffer before substituing it by out own buffer. Usefull for the foreach tag */
33 /* Stores the set name reffered to by the foreach tag */
34 static const char* foreach_set_name;
35 static xbt_dynar_t main_STag_surfxml_host_cb_list = NULL;
36 static xbt_dynar_t main_ETag_surfxml_host_cb_list = NULL;
37 static xbt_dynar_t main_STag_surfxml_link_cb_list = NULL;
38 static xbt_dynar_t main_ETag_surfxml_link_cb_list = NULL;
40 /* make sure these symbols are defined as strong ones in this file so that the linked can resolve them */
41 xbt_dynar_t STag_surfxml_platform_cb_list = NULL;
42 xbt_dynar_t ETag_surfxml_platform_cb_list = NULL;
43 xbt_dynar_t STag_surfxml_host_cb_list = NULL;
44 xbt_dynar_t ETag_surfxml_host_cb_list = NULL;
45 xbt_dynar_t STag_surfxml_router_cb_list = NULL;
46 xbt_dynar_t ETag_surfxml_router_cb_list = NULL;
47 xbt_dynar_t STag_surfxml_link_cb_list = NULL;
48 xbt_dynar_t ETag_surfxml_link_cb_list = NULL;
49 xbt_dynar_t STag_surfxml_route_cb_list = NULL;
50 xbt_dynar_t ETag_surfxml_route_cb_list = NULL;
51 xbt_dynar_t STag_surfxml_link_c_ctn_cb_list = NULL;
52 xbt_dynar_t ETag_surfxml_link_c_ctn_cb_list = NULL;
53 xbt_dynar_t STag_surfxml_process_cb_list = NULL;
54 xbt_dynar_t ETag_surfxml_process_cb_list = NULL;
55 xbt_dynar_t STag_surfxml_argument_cb_list = NULL;
56 xbt_dynar_t ETag_surfxml_argument_cb_list = NULL;
57 xbt_dynar_t STag_surfxml_prop_cb_list = NULL;
58 xbt_dynar_t ETag_surfxml_prop_cb_list = NULL;
59 xbt_dynar_t STag_surfxml_set_cb_list = NULL;
60 xbt_dynar_t ETag_surfxml_set_cb_list = NULL;
61 xbt_dynar_t STag_surfxml_foreach_cb_list = NULL;
62 xbt_dynar_t ETag_surfxml_foreach_cb_list = NULL;
63 xbt_dynar_t STag_surfxml_route_c_multi_cb_list = NULL;
64 xbt_dynar_t ETag_surfxml_route_c_multi_cb_list = NULL;
65 xbt_dynar_t STag_surfxml_cluster_cb_list = NULL;
66 xbt_dynar_t ETag_surfxml_cluster_cb_list = NULL;
67 xbt_dynar_t STag_surfxml_trace_cb_list = NULL;
68 xbt_dynar_t ETag_surfxml_trace_cb_list = NULL;
69 xbt_dynar_t STag_surfxml_trace_c_connect_cb_list = NULL;
70 xbt_dynar_t ETag_surfxml_trace_c_connect_cb_list = NULL;
71 xbt_dynar_t STag_surfxml_random_cb_list = NULL;
72 xbt_dynar_t ETag_surfxml_random_cb_list = NULL;
74 /* Stores the sets defined in the XML */
75 xbt_dict_t set_list = NULL;
77 xbt_dict_t current_property_set = NULL;
79 /* For the route:multi tag */
80 xbt_dict_t route_table = NULL;
81 xbt_dict_t route_multi_table = NULL;
82 xbt_dynar_t route_multi_elements = NULL;
83 static xbt_dynar_t route_link_list = NULL;
84 xbt_dynar_t links = NULL;
85 xbt_dynar_t keys = NULL;
87 xbt_dict_t random_data_list = NULL;
89 static xbt_dynar_t surf_input_buffer_stack = NULL;
90 static xbt_dynar_t surf_file_to_parse_stack = NULL;
92 static XBT_INLINE void surfxml_call_cb_functions(xbt_dynar_t);
94 YY_BUFFER_STATE surf_input_buffer;
95 FILE *surf_file_to_parse = NULL;
97 static void convert_route_multi_to_routes(void);
98 static void parse_route_elem(void);
99 static void parse_foreach(void);
100 static void parse_sets(void);
101 static void parse_route_multi_set_endpoints(void);
102 static void parse_route_multi_set_route(void);
103 static void parse_cluster(void);
104 static void parse_trace_init(void);
105 static void parse_trace_finalize(void);
106 static void parse_trace_c_connect(void);
107 static void init_randomness(void);
108 static void add_randomness(void);
110 void surf_parse_free_callbacks(void)
112 xbt_dynar_free(&STag_surfxml_platform_cb_list);
113 xbt_dynar_free(&ETag_surfxml_platform_cb_list);
114 xbt_dynar_free(&STag_surfxml_host_cb_list);
115 xbt_dynar_free(&ETag_surfxml_host_cb_list);
116 xbt_dynar_free(&STag_surfxml_router_cb_list);
117 xbt_dynar_free(&ETag_surfxml_router_cb_list);
118 xbt_dynar_free(&STag_surfxml_link_cb_list);
119 xbt_dynar_free(&ETag_surfxml_link_cb_list);
120 xbt_dynar_free(&STag_surfxml_route_cb_list);
121 xbt_dynar_free(&ETag_surfxml_route_cb_list);
122 xbt_dynar_free(&STag_surfxml_link_c_ctn_cb_list);
123 xbt_dynar_free(&ETag_surfxml_link_c_ctn_cb_list);
124 xbt_dynar_free(&STag_surfxml_process_cb_list);
125 xbt_dynar_free(&ETag_surfxml_process_cb_list);
126 xbt_dynar_free(&STag_surfxml_argument_cb_list);
127 xbt_dynar_free(&ETag_surfxml_argument_cb_list);
128 xbt_dynar_free(&STag_surfxml_prop_cb_list);
129 xbt_dynar_free(&ETag_surfxml_prop_cb_list);
130 xbt_dynar_free(&STag_surfxml_set_cb_list);
131 xbt_dynar_free(&ETag_surfxml_set_cb_list);
132 xbt_dynar_free(&STag_surfxml_foreach_cb_list);
133 xbt_dynar_free(&ETag_surfxml_foreach_cb_list);
134 xbt_dynar_free(&STag_surfxml_route_c_multi_cb_list);
135 xbt_dynar_free(&ETag_surfxml_route_c_multi_cb_list);
136 xbt_dynar_free(&STag_surfxml_cluster_cb_list);
137 xbt_dynar_free(&ETag_surfxml_cluster_cb_list);
138 xbt_dynar_free(&STag_surfxml_trace_cb_list);
139 xbt_dynar_free(&ETag_surfxml_trace_cb_list);
140 xbt_dynar_free(&STag_surfxml_trace_c_connect_cb_list);
141 xbt_dynar_free(&ETag_surfxml_trace_c_connect_cb_list);
142 xbt_dynar_free(&STag_surfxml_random_cb_list);
143 xbt_dynar_free(&ETag_surfxml_random_cb_list);
146 void surf_parse_reset_parser(void)
148 surf_parse_free_callbacks();
149 STag_surfxml_platform_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
150 ETag_surfxml_platform_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
151 STag_surfxml_host_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
152 ETag_surfxml_host_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
153 STag_surfxml_router_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
154 ETag_surfxml_router_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
155 STag_surfxml_link_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
156 ETag_surfxml_link_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
157 STag_surfxml_route_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
158 ETag_surfxml_route_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
159 STag_surfxml_link_c_ctn_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
160 ETag_surfxml_link_c_ctn_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
161 STag_surfxml_process_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
162 ETag_surfxml_process_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
163 STag_surfxml_argument_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
164 ETag_surfxml_argument_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
165 STag_surfxml_prop_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
166 ETag_surfxml_prop_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
167 STag_surfxml_set_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
168 ETag_surfxml_set_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
169 STag_surfxml_foreach_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
170 ETag_surfxml_foreach_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
171 STag_surfxml_route_c_multi_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
172 ETag_surfxml_route_c_multi_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
173 STag_surfxml_cluster_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
174 ETag_surfxml_cluster_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
175 STag_surfxml_trace_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
176 ETag_surfxml_trace_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
177 STag_surfxml_trace_c_connect_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
178 ETag_surfxml_trace_c_connect_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
179 STag_surfxml_random_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
180 ETag_surfxml_random_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
183 void STag_surfxml_include(void)
185 xbt_dynar_push(surf_input_buffer_stack, &surf_input_buffer);
186 xbt_dynar_push(surf_file_to_parse_stack, &surf_file_to_parse);
188 surf_file_to_parse = surf_fopen(A_surfxml_include_file, "r");
189 xbt_assert1((surf_file_to_parse), "Unable to open \"%s\"\n",
190 A_surfxml_include_file);
191 surf_input_buffer = surf_parse__create_buffer(surf_file_to_parse, 10);
192 surf_parse__switch_to_buffer(surf_input_buffer);
197 void ETag_surfxml_include(void)
201 surf_parse__delete_buffer(surf_input_buffer);
202 fclose(surf_file_to_parse);
203 xbt_dynar_pop(surf_file_to_parse_stack, &surf_file_to_parse);
204 xbt_dynar_pop(surf_input_buffer_stack, &surf_input_buffer);
207 void STag_surfxml_platform(void)
209 double version = 0.0;
211 sscanf(A_surfxml_platform_version, "%lg", &version);
213 xbt_assert0((version >= 1.0), "******* BIG FAT WARNING *********\n "
214 "You're using an ancient XML file. "
215 "Since SimGrid 3.1, units are Bytes, Flops, and seconds "
216 "instead of MBytes, MFlops and seconds. "
217 "A script (surfxml_update.pl) to help you convert your old "
219 "is available in the contrib/platform_generation directory "
220 "of the simgrid repository. Please check also out the "
221 "SURF section of the ChangeLog for the 3.1 version. "
222 "Last, do not forget to also update your values for "
223 "the calls to MSG_task_create (if any).");
224 xbt_assert0((version >= 2.0), "******* BIG FAT WARNING *********\n "
225 "You're using an old XML file. "
226 "A script (surfxml_update.pl) to help you convert your old "
228 "is available in the contrib/platform_generation directory "
229 "of the simgrid repository.");
231 surfxml_call_cb_functions(STag_surfxml_platform_cb_list);
235 void ETag_surfxml_platform(void)
237 convert_route_multi_to_routes();
239 surfxml_call_cb_functions(ETag_surfxml_platform_cb_list);
241 xbt_dict_free(&random_data_list);
242 xbt_dict_free(&set_list);
245 void STag_surfxml_host(void)
247 surfxml_call_cb_functions(STag_surfxml_host_cb_list);
250 void ETag_surfxml_host(void)
252 surfxml_call_cb_functions(ETag_surfxml_host_cb_list);
255 void STag_surfxml_router(void)
257 surfxml_call_cb_functions(STag_surfxml_router_cb_list);
260 void ETag_surfxml_router(void)
262 surfxml_call_cb_functions(ETag_surfxml_router_cb_list);
265 void STag_surfxml_link(void)
267 surfxml_call_cb_functions(STag_surfxml_link_cb_list);
270 void ETag_surfxml_link(void)
272 surfxml_call_cb_functions(ETag_surfxml_link_cb_list);
275 void STag_surfxml_route(void)
277 surfxml_call_cb_functions(STag_surfxml_route_cb_list);
280 void ETag_surfxml_route(void)
282 surfxml_call_cb_functions(ETag_surfxml_route_cb_list);
285 void STag_surfxml_link_c_ctn(void)
287 surfxml_call_cb_functions(STag_surfxml_link_c_ctn_cb_list);
290 void ETag_surfxml_link_c_ctn(void)
292 surfxml_call_cb_functions(ETag_surfxml_link_c_ctn_cb_list);
295 void STag_surfxml_process(void)
297 surfxml_call_cb_functions(STag_surfxml_process_cb_list);
300 void ETag_surfxml_process(void)
302 surfxml_call_cb_functions(ETag_surfxml_process_cb_list);
305 void STag_surfxml_argument(void)
307 surfxml_call_cb_functions(STag_surfxml_argument_cb_list);
310 void ETag_surfxml_argument(void)
312 surfxml_call_cb_functions(ETag_surfxml_argument_cb_list);
315 void STag_surfxml_prop(void)
317 surfxml_call_cb_functions(STag_surfxml_prop_cb_list);
319 void ETag_surfxml_prop(void)
321 surfxml_call_cb_functions(ETag_surfxml_prop_cb_list);
324 void STag_surfxml_set(void)
326 surfxml_call_cb_functions(STag_surfxml_set_cb_list);
329 void ETag_surfxml_set(void)
331 surfxml_call_cb_functions(ETag_surfxml_set_cb_list);
334 void STag_surfxml_foreach(void)
336 /* Save the current buffer */
337 old_buff = surfxml_bufferstack;
338 surfxml_call_cb_functions(STag_surfxml_foreach_cb_list);
341 void ETag_surfxml_foreach(void)
343 surfxml_call_cb_functions(ETag_surfxml_foreach_cb_list);
345 /* free the temporary dynar and restore original */
346 xbt_dynar_free(&STag_surfxml_host_cb_list);
347 xbt_dynar_free(&ETag_surfxml_host_cb_list);
349 STag_surfxml_host_cb_list = main_STag_surfxml_host_cb_list;
350 ETag_surfxml_host_cb_list = main_ETag_surfxml_host_cb_list;
352 /* free the temporary dynar and restore original */
353 xbt_dynar_free(&STag_surfxml_link_cb_list);
354 xbt_dynar_free(&ETag_surfxml_link_cb_list);
356 STag_surfxml_link_cb_list = main_STag_surfxml_link_cb_list;
357 ETag_surfxml_link_cb_list = main_ETag_surfxml_link_cb_list;
361 void STag_surfxml_route_c_multi(void)
363 surfxml_call_cb_functions(STag_surfxml_route_c_multi_cb_list);
366 void ETag_surfxml_route_c_multi(void)
368 surfxml_call_cb_functions(ETag_surfxml_route_c_multi_cb_list);
371 void STag_surfxml_cluster(void)
373 surfxml_call_cb_functions(STag_surfxml_cluster_cb_list);
376 void ETag_surfxml_cluster(void)
378 surfxml_call_cb_functions(ETag_surfxml_cluster_cb_list);
381 void STag_surfxml_trace(void)
383 surfxml_call_cb_functions(STag_surfxml_trace_cb_list);
386 void ETag_surfxml_trace(void)
388 surfxml_call_cb_functions(ETag_surfxml_trace_cb_list);
391 void STag_surfxml_trace_c_connect(void)
393 surfxml_call_cb_functions(STag_surfxml_trace_c_connect_cb_list);
396 void ETag_surfxml_trace_c_connect(void)
398 surfxml_call_cb_functions(ETag_surfxml_trace_c_connect_cb_list);
401 void STag_surfxml_random(void)
403 surfxml_call_cb_functions(STag_surfxml_random_cb_list);
406 void ETag_surfxml_random(void)
408 surfxml_call_cb_functions(ETag_surfxml_random_cb_list);
411 void surf_parse_open(const char *file)
413 static int warned = 0; /* warn only once */
417 ("Bypassing the XML parser since surf_parse_open received a NULL pointer. If it is not what you want, go fix your code.");
422 if (!surf_input_buffer_stack)
423 surf_input_buffer_stack = xbt_dynar_new(sizeof(YY_BUFFER_STATE), NULL);
424 if (!surf_file_to_parse_stack)
425 surf_file_to_parse_stack = xbt_dynar_new(sizeof(FILE *), NULL);
427 surf_file_to_parse = surf_fopen(file, "r");
428 xbt_assert1((surf_file_to_parse), "Unable to open \"%s\"\n", file);
429 surf_input_buffer = surf_parse__create_buffer(surf_file_to_parse, 10);
430 surf_parse__switch_to_buffer(surf_input_buffer);
431 surf_parse_lineno = 1;
434 void surf_parse_close(void)
436 if (surf_input_buffer_stack)
437 xbt_dynar_free(&surf_input_buffer_stack);
438 if (surf_file_to_parse_stack)
439 xbt_dynar_free(&surf_file_to_parse_stack);
441 if (surf_file_to_parse) {
442 surf_parse__delete_buffer(surf_input_buffer);
443 fclose(surf_file_to_parse);
448 static int _surf_parse(void)
450 return surf_parse_lex();
453 int_f_void_t surf_parse = _surf_parse;
455 void surf_parse_get_double(double *value, const char *string)
459 ret = sscanf(string, "%lg", value);
460 xbt_assert2((ret == 1), "Parse error line %d : %s not a number",
461 surf_parse_lineno, string);
464 void surf_parse_get_int(int *value, const char *string)
468 ret = sscanf(string, "%d", value);
469 xbt_assert2((ret == 1), "Parse error line %d : %s not a number",
470 surf_parse_lineno, string);
473 void surf_parse_get_trace(tmgr_trace_t * trace, const char *string)
475 if ((!string) || (strcmp(string, "") == 0))
478 *trace = tmgr_trace_new(string);
481 void parse_properties(void)
485 if(!current_property_set) current_property_set = xbt_dict_new();
487 value = xbt_strdup(A_surfxml_prop_value);
488 xbt_dict_set(current_property_set, A_surfxml_prop_id, value, free);
491 void surfxml_add_callback(xbt_dynar_t cb_list, void_f_void_t function)
493 xbt_dynar_push(cb_list, &function);
496 static XBT_INLINE void surfxml_call_cb_functions(xbt_dynar_t cb_list)
498 unsigned int iterator;
500 xbt_dynar_foreach(cb_list, iterator, fun){
501 DEBUG2("call %p %p",fun,*fun);
506 static void parse_route_set_endpoints(void) {
507 route_link_list = xbt_dynar_new(sizeof(char *), NULL);
510 static void init_data(void)
512 xbt_dict_free(&route_table);
513 xbt_dynar_free(&route_link_list);
514 route_table = xbt_dict_new();
516 route_multi_table = xbt_dict_new();
517 route_multi_elements = xbt_dynar_new(sizeof(char*), NULL);
518 traces_set_list = xbt_dict_new();
519 if (set_list == NULL) set_list = xbt_dict_new();
521 trace_connect_list_host_avail = xbt_dict_new();
522 trace_connect_list_power = xbt_dict_new();
523 trace_connect_list_link_avail = xbt_dict_new();
524 trace_connect_list_bandwidth = xbt_dict_new();
525 trace_connect_list_latency = xbt_dict_new();
527 random_data_list = xbt_dict_new();
528 surfxml_add_callback(STag_surfxml_prop_cb_list, &parse_properties);
529 surfxml_add_callback(ETag_surfxml_link_c_ctn_cb_list, &parse_route_elem);
530 surfxml_add_callback(STag_surfxml_route_cb_list, &parse_route_set_endpoints);
531 surfxml_add_callback(STag_surfxml_set_cb_list, &parse_sets);
532 surfxml_add_callback(STag_surfxml_route_c_multi_cb_list, &parse_route_multi_set_endpoints);
533 surfxml_add_callback(ETag_surfxml_route_c_multi_cb_list, &parse_route_multi_set_route);
534 surfxml_add_callback(STag_surfxml_foreach_cb_list, &parse_foreach);
535 surfxml_add_callback(STag_surfxml_cluster_cb_list, &parse_cluster);
536 surfxml_add_callback(STag_surfxml_trace_cb_list, &parse_trace_init);
537 surfxml_add_callback(ETag_surfxml_trace_cb_list, &parse_trace_finalize);
538 surfxml_add_callback(STag_surfxml_trace_c_connect_cb_list, &parse_trace_c_connect);
539 surfxml_add_callback(STag_surfxml_random_cb_list, &init_randomness);
540 surfxml_add_callback(ETag_surfxml_random_cb_list, &add_randomness);
543 static void free_data(void)
546 xbt_dict_cursor_t cursor = NULL;
548 xbt_dict_foreach(route_table, cursor, key, data) {
549 xbt_dynar_t links = (xbt_dynar_t)data;
550 xbt_dynar_free(&links);
552 xbt_dict_free(&route_table);
553 route_link_list = NULL;
555 xbt_dict_free(&trace_connect_list_host_avail);
556 xbt_dict_free(&trace_connect_list_power);
557 xbt_dict_free(&trace_connect_list_link_avail);
558 xbt_dict_free(&trace_connect_list_bandwidth);
559 xbt_dict_free(&trace_connect_list_latency);
560 xbt_dict_free(&traces_set_list);
563 void parse_platform_file(const char* file)
565 surf_parse_open(file);
567 xbt_assert1((!(*surf_parse)()), "Parse error in %s", file);
572 /* Functions to bypass route, host and link tags. Used by the foreach and route:multi tags */
574 static void parse_make_temporary_route(const char *src, const char *dst, int action)
577 surfxml_bufferstack = xbt_new0(char, 2048);
579 A_surfxml_route_action = action;
580 SURFXML_BUFFER_SET(route_src, src);
581 SURFXML_BUFFER_SET(route_dst, dst);
584 static void parse_change_cpu_data(const char* hostName, const char* surfxml_host_power, const char* surfxml_host_availability,
585 const char* surfxml_host_availability_file, const char* surfxml_host_state_file)
588 surfxml_bufferstack = xbt_new0(char, 2048);
590 SURFXML_BUFFER_SET(host_id, hostName);
591 SURFXML_BUFFER_SET(host_power, surfxml_host_power /*hostPower*/);
592 SURFXML_BUFFER_SET(host_availability, surfxml_host_availability);
593 SURFXML_BUFFER_SET(host_availability_file, surfxml_host_availability_file);
594 SURFXML_BUFFER_SET(host_state_file, surfxml_host_state_file);
597 static void parse_change_link_data(const char* linkName, const char* surfxml_link_bandwidth, const char* surfxml_link_bandwidth_file,
598 const char* surfxml_link_latency, const char* surfxml_link_latency_file, const char* surfxml_link_state_file)
601 surfxml_bufferstack = xbt_new0(char, 2048);
603 SURFXML_BUFFER_SET(link_id, linkName);
604 SURFXML_BUFFER_SET(link_bandwidth, surfxml_link_bandwidth);
605 SURFXML_BUFFER_SET(link_bandwidth_file, surfxml_link_bandwidth_file);
606 SURFXML_BUFFER_SET(link_latency, surfxml_link_latency);
607 SURFXML_BUFFER_SET(link_latency_file, surfxml_link_latency_file);
608 SURFXML_BUFFER_SET(link_state_file, surfxml_link_state_file);
612 * \brief Restores the original surfxml buffer
614 static void parse_restore_original_buffer(void)
616 free(surfxml_bufferstack);
617 surfxml_bufferstack = old_buff;
620 /* Functions for the sets and foreach tags */
622 static void parse_sets(void)
624 char *id, *suffix, *prefix, *radical;
626 xbt_dynar_t radical_ends;
627 xbt_dynar_t current_set;
631 id = xbt_strdup(A_surfxml_set_id);
632 prefix = xbt_strdup(A_surfxml_set_prefix);
633 suffix = xbt_strdup(A_surfxml_set_suffix);
634 radical = xbt_strdup(A_surfxml_set_radical);
636 xbt_assert1(!xbt_dict_get_or_null(set_list, id),
637 "Set '%s' declared several times in the platform file.",id);
638 radical_ends = xbt_str_split(radical, "-");
639 xbt_assert1((xbt_dynar_length(radical_ends)==2), "Radical must be in the form lvalue-rvalue! Provided value: %s", radical);
641 surf_parse_get_int(&start, xbt_dynar_get_as(radical_ends, 0, char*));
642 surf_parse_get_int(&end, xbt_dynar_get_as(radical_ends, 1, char*));
644 current_set = xbt_dynar_new(sizeof(char*), NULL);
647 for (i=start; i<=end; i++) {
648 value = bprintf("%s%d%s", prefix, i, suffix);
649 xbt_dynar_push(current_set, &value);
652 xbt_dict_set(set_list, id, current_set, NULL);
655 static const char* surfxml_host_power;
656 static const char* surfxml_host_availability;
657 static const char* surfxml_host_availability_file;
658 static const char* surfxml_host_state_file;
660 static void parse_host_foreach(void)
662 surfxml_host_power = A_surfxml_host_power;
663 surfxml_host_availability = A_surfxml_host_availability;
664 surfxml_host_availability_file = A_surfxml_host_availability_file;
665 surfxml_host_state_file = A_surfxml_host_state_file;
668 static void finalize_host_foreach(void)
670 xbt_dynar_t names = NULL;
671 unsigned int cpt = 0;
673 xbt_dict_cursor_t cursor = NULL;
676 xbt_dict_t cluster_host_props = current_property_set;
678 xbt_assert1((names = xbt_dict_get_or_null(set_list, foreach_set_name)),
679 "Set name '%s' reffered by foreach tag not found.", foreach_set_name);
681 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);
684 /* foreach name in set call the main host callback */
685 xbt_dynar_foreach (names, cpt, name) {
686 parse_change_cpu_data(name, surfxml_host_power, surfxml_host_availability,
687 surfxml_host_availability_file, surfxml_host_state_file);
688 surfxml_call_cb_functions(main_STag_surfxml_host_cb_list);
690 xbt_dict_foreach(cluster_host_props,cursor,key,data) {
691 xbt_dict_set(current_property_set, xbt_strdup(key), xbt_strdup(data), free);
694 surfxml_call_cb_functions(main_ETag_surfxml_host_cb_list);
695 free(surfxml_bufferstack);
698 current_property_set = xbt_dict_new();
700 surfxml_bufferstack = old_buff;
704 static const char* surfxml_link_bandwidth;
705 static const char* surfxml_link_bandwidth_file;
706 static const char* surfxml_link_latency;
707 static const char* surfxml_link_latency_file;
708 static const char* surfxml_link_state_file;
710 static void parse_link_foreach(void)
712 surfxml_link_bandwidth = A_surfxml_link_bandwidth;
713 surfxml_link_bandwidth_file = A_surfxml_link_bandwidth_file;
714 surfxml_link_latency = A_surfxml_link_latency;
715 surfxml_link_latency_file = A_surfxml_link_latency_file;
716 surfxml_link_state_file = A_surfxml_link_state_file;
719 static void finalize_link_foreach(void)
721 xbt_dynar_t names = NULL;
722 unsigned int cpt = 0;
724 xbt_dict_cursor_t cursor = NULL;
727 xbt_dict_t cluster_link_props = current_property_set;
729 xbt_assert1((names = xbt_dict_get_or_null(set_list, foreach_set_name)),
730 "Set name '%s' reffered by foreach tag not found.", foreach_set_name);
732 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);
735 /* for each name in set call the main link callback */
736 xbt_dynar_foreach (names, cpt, name) {
737 parse_change_link_data(name, surfxml_link_bandwidth, surfxml_link_bandwidth_file,
738 surfxml_link_latency, surfxml_link_latency_file, surfxml_link_state_file);
739 surfxml_call_cb_functions(main_STag_surfxml_link_cb_list);
741 xbt_dict_foreach(cluster_link_props,cursor,key,data) {
742 xbt_dict_set(current_property_set, xbt_strdup(key), xbt_strdup(data), free);
745 surfxml_call_cb_functions(main_ETag_surfxml_link_cb_list);
746 free(surfxml_bufferstack);
750 current_property_set = xbt_dict_new();
752 surfxml_bufferstack = old_buff;
755 static void parse_foreach(void)
757 /* save the host & link callbacks */
758 main_STag_surfxml_host_cb_list = STag_surfxml_host_cb_list;
759 main_ETag_surfxml_host_cb_list = ETag_surfxml_host_cb_list;
760 main_STag_surfxml_link_cb_list = STag_surfxml_link_cb_list;
761 main_ETag_surfxml_link_cb_list = ETag_surfxml_link_cb_list;
763 /* define host & link callbacks to be used only by the foreach tag */
764 STag_surfxml_host_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
765 ETag_surfxml_host_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
766 STag_surfxml_link_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
767 ETag_surfxml_link_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
769 surfxml_add_callback(STag_surfxml_host_cb_list, &parse_host_foreach);
770 surfxml_add_callback(ETag_surfxml_host_cb_list, &finalize_host_foreach);
771 surfxml_add_callback(STag_surfxml_link_cb_list, &parse_link_foreach);
772 surfxml_add_callback(ETag_surfxml_link_cb_list, &finalize_link_foreach);
775 foreach_set_name = xbt_strdup(A_surfxml_foreach_set_id);
778 /* Route:multi functions */
780 static int route_multi_size=0;
781 static char* src_name, *dst_name;
782 static int is_symmetric_route;
784 static void parse_route_elem(void)
788 val = xbt_strdup(A_surfxml_link_c_ctn_id);
790 xbt_dynar_push(route_link_list, &val);
793 static void parse_route_multi_set_endpoints(void)
795 src_name = xbt_strdup(A_surfxml_route_c_multi_src);
796 dst_name = xbt_strdup(A_surfxml_route_c_multi_dst);
797 route_action = A_surfxml_route_c_multi_action;
798 is_symmetric_route = A_surfxml_route_c_multi_symmetric;
801 route_link_list = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
804 static int contains(xbt_dynar_t list, const char* value)
808 xbt_dynar_foreach(list, cpt, val) {
809 if (strcmp(val, value) == 0)
816 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.
817 The decision is based upon the value of action specified in the xml route:multi attribute action
819 void manage_route(xbt_dict_t routing_table, const char *route_name, int action, int isMultiRoute)
825 /* get already existing list if it exists */
826 links = xbt_dict_get_or_null(routing_table, route_name);
827 DEBUG1("ROUTE: %s", route_name);
830 case A_surfxml_route_action_PREPEND: /* add existing links at the end; route_link_list + links */
831 xbt_dynar_foreach(links, cpt, value) {
832 xbt_dynar_push(route_link_list,&value);
834 xbt_dynar_free(&links);
836 case A_surfxml_route_action_POSTPEND: /* add existing links in front; links + route_link_list */
837 xbt_dynar_foreach(route_link_list, cpt, value) {
838 xbt_dynar_push(links,&value);
840 xbt_dynar_free(&route_link_list);
841 route_link_list = links;
843 case A_surfxml_route_action_OVERRIDE:
844 xbt_dynar_free(&links);
849 /* this is the final route; do not add if name is a set; add only if name is in set list */
851 xbt_dict_set(routing_table, route_name, route_link_list, NULL);
855 static void parse_route_multi_set_route(void)
859 route_name = bprintf("%s#%s#%d#%d#%d", src_name, dst_name, route_action, is_symmetric_route, route_multi_size);
861 xbt_dynar_push(route_multi_elements, &route_name);
864 xbt_dict_set(route_multi_table, route_name, route_link_list, NULL);
865 /* add symmetric if it is the case */
866 if (is_symmetric_route == 1) {
867 char * symmetric_name = bprintf("%s#%s#%d#%d#%d", dst_name, src_name, route_action, !is_symmetric_route, route_multi_size);
869 xbt_dict_set(route_multi_table, symmetric_name, route_link_list, NULL);
870 xbt_dynar_push(route_multi_elements, &symmetric_name);
871 is_symmetric_route = 0;
877 static void add_multi_links(const char* src, const char* dst, xbt_dynar_t links, const char* src_name, const char* dst_name)
881 parse_make_temporary_route(src_name, dst_name, route_action);
882 surfxml_call_cb_functions(STag_surfxml_route_cb_list);
883 DEBUG2("\tADDING ROUTE: %s -> %s", src_name, dst_name);
884 /* Build link list */
885 xbt_dynar_foreach(links, cpt, value) {
886 if (strcmp(value, src) == 0)
887 val = xbt_strdup(src_name);
888 else if (strcmp(value, dst) == 0)
889 val = xbt_strdup(dst_name);
890 else if (strcmp(value, "$dst") == 0)
891 val = xbt_strdup(dst_name);
892 else if (strcmp(value, "$src") == 0)
893 val = xbt_strdup(src_name);
895 val = xbt_strdup(value);
896 DEBUG1("\t\tELEMENT: %s", val);
897 xbt_dynar_push(route_link_list, &val);
899 surfxml_call_cb_functions(ETag_surfxml_route_cb_list);
900 free(surfxml_bufferstack);
903 static void convert_route_multi_to_routes(void)
905 xbt_dict_cursor_t cursor_w;
907 unsigned int cpt, cpt2, cursor;
908 char *src_host_name, *dst_host_name, *key, *src, *dst, *val, *key_w, *data_w;
911 xbt_dynar_t src_names = NULL, dst_names = NULL, links;
913 if (!route_multi_elements) return;
916 DEBUG1("%d", xbt_dict_length(workstation_set));
917 if (workstation_set != NULL && xbt_dict_length(workstation_set) > 0)
918 set = workstation_set;
921 old_buff = surfxml_bufferstack;
922 /* Get all routes in the exact order they were entered in the platform file */
923 xbt_dynar_foreach(route_multi_elements, cursor, key) {
924 /* Get links for the route */
925 links = (xbt_dynar_t)xbt_dict_get_or_null(route_multi_table, key);
926 keys = xbt_str_split_str(key, sep);
928 src = xbt_dynar_get_as(keys, 0, char*);
929 dst = xbt_dynar_get_as(keys, 1, char*);
930 route_action = atoi(xbt_dynar_get_as(keys, 2, char*));
931 symmetric = atoi(xbt_dynar_get_as(keys, 3, char*));
933 /* Create the dynar of src and dst hosts for the new routes */
934 /* NOTE: src and dst can be either set names or simple host names */
935 src_names = (xbt_dynar_t)xbt_dict_get_or_null(set_list, src);
936 dst_names = (xbt_dynar_t)xbt_dict_get_or_null(set_list, dst);
937 /* Add to dynar even if they are simple names */
938 if (src_names == NULL) {
939 src_names = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
940 val = xbt_strdup(src);
941 xbt_dynar_push(src_names, &val);
942 if (strcmp(val,"$*") != 0 && NULL == xbt_dict_get_or_null(set, val))
943 THROW3(unknown_error,0,"(In route:multi (%s -> %s) source %s does not exist (not a set or a host)", src, dst, src);
945 if (dst_names == NULL) {
946 dst_names = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
947 val = xbt_strdup(dst);
948 if (strcmp(val,"$*") != 0 && NULL == xbt_dict_get_or_null(set, val))
949 THROW3(unknown_error,0,"(In route:multi (%s -> %s) destination %s does not exist (not a set or a host)", src, dst, dst);
950 xbt_dynar_push(dst_names, &val);
953 /* Build the routes */
954 DEBUG2("ADDING MULTI ROUTE: %s -> %s", xbt_dynar_get_as(keys, 0, char*), xbt_dynar_get_as(keys, 1, char*));
955 xbt_dynar_foreach(src_names, cpt, src_host_name) {
956 xbt_dynar_foreach(dst_names, cpt2, dst_host_name) {
957 /* If dst is $* then set this route to have its dst point to all hosts */
958 if (strcmp(src_host_name,"$*") != 0 && strcmp(dst_host_name,"$*") == 0){
959 xbt_dict_foreach(set, cursor_w, key_w, data_w) {
960 //int n = xbt_dynar_member(src_names, (char*)key_w);
961 add_multi_links(src, dst, links, src_host_name, key_w);
964 /* If src is $* then set this route to have its dst point to all hosts */
965 if (strcmp(src_host_name,"$*") == 0 && strcmp(dst_host_name,"$*") != 0){
966 xbt_dict_foreach(set, cursor_w, key_w, data_w) {
967 // if (!symmetric || (symmetric && !contains(dst_names, key_w)))
968 add_multi_links(src, dst, links, key_w, dst_host_name);
971 /* if none of them are equal to $* */
972 if (strcmp(src_host_name,"$*") != 0 && strcmp(dst_host_name,"$*") != 0) {
973 add_multi_links(src, dst, links, src_host_name, dst_host_name);
978 surfxml_bufferstack = old_buff;
979 xbt_dict_free(&route_multi_table);
980 xbt_dynar_free(&route_multi_elements);
983 /* Cluster tag functions */
985 static void parse_cluster(void)
987 static int AX_ptr = 0;
988 static int surfxml_bufferstack_size = 2048;
990 char* cluster_id = A_surfxml_cluster_id;
991 char* cluster_prefix = A_surfxml_cluster_prefix;
992 char* cluster_suffix = A_surfxml_cluster_suffix;
993 char* cluster_radical = A_surfxml_cluster_radical;
994 char* cluster_power = A_surfxml_cluster_power;
995 char* cluster_bw = A_surfxml_cluster_bw;
996 char* cluster_lat = A_surfxml_cluster_lat;
997 char* cluster_bb_bw = A_surfxml_cluster_bb_bw;
998 char* cluster_bb_lat = A_surfxml_cluster_bb_lat;
1000 char* saved_buff = surfxml_bufferstack;
1002 char * backbone_name;
1004 surfxml_bufferstack = xbt_new0(char, surfxml_bufferstack_size);
1007 SURFXML_BUFFER_SET(set_id, cluster_id);
1008 SURFXML_BUFFER_SET(set_prefix, cluster_prefix);
1009 SURFXML_BUFFER_SET(set_suffix, cluster_suffix);
1010 SURFXML_BUFFER_SET(set_radical, cluster_radical);
1012 SURFXML_START_TAG(set);
1013 SURFXML_END_TAG(set);
1016 SURFXML_BUFFER_SET(foreach_set_id, cluster_id);
1018 SURFXML_START_TAG(foreach);
1020 /* Make host for the foreach */
1021 parse_change_cpu_data("$1", cluster_power, "1.0", "", "");
1022 A_surfxml_host_state = A_surfxml_host_state_ON;
1024 SURFXML_START_TAG(host);
1025 SURFXML_END_TAG(host);
1027 /* Make link for the foreach */
1028 parse_change_link_data("$1", cluster_bw, "", cluster_lat, "", "");
1029 A_surfxml_link_state = A_surfxml_link_state_ON;
1030 A_surfxml_link_sharing_policy = A_surfxml_link_sharing_policy_SHARED;
1032 SURFXML_START_TAG(link);
1033 SURFXML_END_TAG(link);
1035 SURFXML_END_TAG(foreach);
1037 /* Make backbone link */
1038 backbone_name = bprintf("%s_bb", cluster_id);
1039 parse_change_link_data(backbone_name, cluster_bb_bw, "", cluster_bb_lat, "", "");
1040 A_surfxml_link_state = A_surfxml_link_state_ON;
1041 A_surfxml_link_sharing_policy = A_surfxml_link_sharing_policy_FATPIPE;
1043 SURFXML_START_TAG(link);
1044 SURFXML_END_TAG(link);
1046 /* Make route multi with the outside world, i.e. cluster->$* */
1047 SURFXML_BUFFER_SET(route_c_multi_src, cluster_id);
1048 SURFXML_BUFFER_SET(route_c_multi_dst, "$*");
1049 A_surfxml_route_c_multi_symmetric = A_surfxml_route_c_multi_symmetric_NO;
1050 A_surfxml_route_c_multi_action = A_surfxml_route_c_multi_action_OVERRIDE;
1052 SURFXML_START_TAG(route_c_multi);
1054 SURFXML_BUFFER_SET(link_c_ctn_id, "$src");
1056 SURFXML_START_TAG(link_c_ctn);
1057 SURFXML_END_TAG(link_c_ctn);
1059 SURFXML_END_TAG(route_c_multi);
1061 /* Make route multi between cluster hosts, i.e. cluster->cluster */
1062 SURFXML_BUFFER_SET(route_c_multi_src, cluster_id);
1063 SURFXML_BUFFER_SET(route_c_multi_dst, cluster_id);
1064 A_surfxml_route_c_multi_action = A_surfxml_route_c_multi_action_POSTPEND;
1065 A_surfxml_route_c_multi_symmetric = A_surfxml_route_c_multi_symmetric_NO;
1067 SURFXML_START_TAG(route_c_multi);
1069 SURFXML_BUFFER_SET(link_c_ctn_id, backbone_name);
1071 SURFXML_START_TAG(link_c_ctn);
1072 SURFXML_END_TAG(link_c_ctn);
1074 SURFXML_END_TAG(route_c_multi);
1078 free(surfxml_bufferstack);
1079 surfxml_bufferstack = saved_buff;
1082 /* Trace management functions */
1084 static double trace_periodicity = -1.0;
1085 static char* trace_file = NULL;
1086 static char* trace_id;
1088 static void parse_trace_init(void)
1090 trace_id = strdup(A_surfxml_trace_id);
1091 trace_file = strdup(A_surfxml_trace_file);
1092 surf_parse_get_double(&trace_periodicity, A_surfxml_trace_periodicity);
1095 static void parse_trace_finalize(void)
1098 if (!trace_file || strcmp(trace_file,"") != 0) {
1099 surf_parse_get_trace(&trace, trace_file);
1102 if (strcmp(surfxml_pcdata, "") == 0) trace = NULL;
1104 trace = tmgr_trace_new_from_string(trace_id, surfxml_pcdata, trace_periodicity);
1106 xbt_dict_set(traces_set_list, trace_id, (void *)trace, NULL);
1109 static void parse_trace_c_connect(void)
1111 xbt_assert2(xbt_dict_get_or_null(traces_set_list, A_surfxml_trace_c_connect_trace),
1112 "Cannot connect trace %s to %s: trace unknown", A_surfxml_trace_c_connect_trace,A_surfxml_trace_c_connect_element);
1114 switch (A_surfxml_trace_c_connect_kind) {
1115 case A_surfxml_trace_c_connect_kind_HOST_AVAIL:
1116 xbt_dict_set(trace_connect_list_host_avail, A_surfxml_trace_c_connect_trace, xbt_strdup(A_surfxml_trace_c_connect_element), free);
1118 case A_surfxml_trace_c_connect_kind_POWER:
1119 xbt_dict_set(trace_connect_list_power, A_surfxml_trace_c_connect_trace, xbt_strdup(A_surfxml_trace_c_connect_element), free);
1121 case A_surfxml_trace_c_connect_kind_LINK_AVAIL:
1122 xbt_dict_set(trace_connect_list_link_avail, A_surfxml_trace_c_connect_trace, xbt_strdup(A_surfxml_trace_c_connect_element), free);
1124 case A_surfxml_trace_c_connect_kind_BANDWIDTH:
1125 xbt_dict_set(trace_connect_list_bandwidth, A_surfxml_trace_c_connect_trace, xbt_strdup(A_surfxml_trace_c_connect_element), free);
1127 case A_surfxml_trace_c_connect_kind_LATENCY:
1128 xbt_dict_set(trace_connect_list_latency, A_surfxml_trace_c_connect_trace, xbt_strdup(A_surfxml_trace_c_connect_element), free);
1131 xbt_die("kind of trace unknown");
1135 /* Random tag functions */
1137 double get_cpu_power(const char *power)
1139 double power_scale = 0.0;
1142 random_data_t random = NULL;
1143 /* randomness is inserted like this: power="$rand(my_random)" */
1144 if (((p = strstr(power, "$rand(")) != NULL) && ((q = strstr(power, ")")) != NULL)) {
1146 generator = xbt_malloc(q - (p + 6) + 1);
1147 memcpy(generator, p + 6, q - (p + 6));
1148 generator[q - (p + 6)] = '\0';
1149 xbt_assert1((random = xbt_dict_get_or_null(random_data_list, generator)),
1150 "Random generator %s undefined", generator);
1151 power_scale = random_generate(random);
1155 surf_parse_get_double(&power_scale, power);
1160 int random_min, random_max, random_mean, random_std_deviation, random_generator;
1163 static void init_randomness(void)
1165 random_id = A_surfxml_random_id;
1166 surf_parse_get_int(&random_min, A_surfxml_random_min);
1167 surf_parse_get_int(&random_max, A_surfxml_random_max);
1168 surf_parse_get_int(&random_mean, A_surfxml_random_mean);
1169 surf_parse_get_int(&random_std_deviation, A_surfxml_random_std_deviation);
1170 random_generator = A_surfxml_random_generator;
1173 static void add_randomness(void)
1175 /* If needed aditional properties can be added by using the prop tag */
1176 random_data_t random = random_new(random_generator, 0, random_min, random_max, random_mean, random_std_deviation);
1177 xbt_dict_set(random_data_list, random_id, (void *)random, NULL);