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_foreach(set_list, cursor, key, data) {
556 xbt_dynar_t set = (xbt_dynar_t)data;
558 unsigned int cpt = 0;
560 xbt_dynar_foreach (set, cpt, name) free(name);
561 xbt_dynar_free(&set);
563 xbt_dict_free(&set_list);
565 xbt_dict_free(&trace_connect_list_host_avail);
566 xbt_dict_free(&trace_connect_list_power);
567 xbt_dict_free(&trace_connect_list_link_avail);
568 xbt_dict_free(&trace_connect_list_bandwidth);
569 xbt_dict_free(&trace_connect_list_latency);
570 xbt_dict_free(&traces_set_list);
573 void parse_platform_file(const char* file)
575 surf_parse_open(file);
577 xbt_assert1((!(*surf_parse)()), "Parse error in %s", file);
582 /* Functions to bypass route, host and link tags. Used by the foreach and route:multi tags */
584 static void parse_make_temporary_route(const char *src, const char *dst, int action)
587 surfxml_bufferstack = xbt_new0(char, 2048);
589 A_surfxml_route_action = action;
590 SURFXML_BUFFER_SET(route_src, src);
591 SURFXML_BUFFER_SET(route_dst, dst);
594 static void parse_change_cpu_data(const char* hostName, const char* surfxml_host_power, const char* surfxml_host_availability,
595 const char* surfxml_host_availability_file, const char* surfxml_host_state_file)
598 surfxml_bufferstack = xbt_new0(char, 2048);
600 SURFXML_BUFFER_SET(host_id, hostName);
601 SURFXML_BUFFER_SET(host_power, surfxml_host_power /*hostPower*/);
602 SURFXML_BUFFER_SET(host_availability, surfxml_host_availability);
603 SURFXML_BUFFER_SET(host_availability_file, surfxml_host_availability_file);
604 SURFXML_BUFFER_SET(host_state_file, surfxml_host_state_file);
607 static void parse_change_link_data(const char* linkName, const char* surfxml_link_bandwidth, const char* surfxml_link_bandwidth_file,
608 const char* surfxml_link_latency, const char* surfxml_link_latency_file, const char* surfxml_link_state_file)
611 surfxml_bufferstack = xbt_new0(char, 2048);
613 SURFXML_BUFFER_SET(link_id, linkName);
614 SURFXML_BUFFER_SET(link_bandwidth, surfxml_link_bandwidth);
615 SURFXML_BUFFER_SET(link_bandwidth_file, surfxml_link_bandwidth_file);
616 SURFXML_BUFFER_SET(link_latency, surfxml_link_latency);
617 SURFXML_BUFFER_SET(link_latency_file, surfxml_link_latency_file);
618 SURFXML_BUFFER_SET(link_state_file, surfxml_link_state_file);
622 * \brief Restores the original surfxml buffer
624 static void parse_restore_original_buffer(void)
626 free(surfxml_bufferstack);
627 surfxml_bufferstack = old_buff;
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_ends;
637 xbt_dynar_t current_set;
641 id = xbt_strdup(A_surfxml_set_id);
642 prefix = xbt_strdup(A_surfxml_set_prefix);
643 suffix = xbt_strdup(A_surfxml_set_suffix);
644 radical = xbt_strdup(A_surfxml_set_radical);
646 xbt_assert1(!xbt_dict_get_or_null(set_list, id),
647 "Set '%s' declared several times in the platform file.",id);
648 radical_ends = xbt_str_split(radical, "-");
649 xbt_assert1((xbt_dynar_length(radical_ends)==2), "Radical must be in the form lvalue-rvalue! Provided value: %s", radical);
651 surf_parse_get_int(&start, xbt_dynar_get_as(radical_ends, 0, char*));
652 surf_parse_get_int(&end, xbt_dynar_get_as(radical_ends, 1, char*));
654 current_set = xbt_dynar_new(sizeof(char*), NULL);
657 for (i=start; i<=end; i++) {
658 value = bprintf("%s%d%s", prefix, i, suffix);
659 xbt_dynar_push(current_set, &value);
662 xbt_dict_set(set_list, id, current_set, NULL);
665 static const char* surfxml_host_power;
666 static const char* surfxml_host_availability;
667 static const char* surfxml_host_availability_file;
668 static const char* surfxml_host_state_file;
670 static void parse_host_foreach(void)
672 surfxml_host_power = A_surfxml_host_power;
673 surfxml_host_availability = A_surfxml_host_availability;
674 surfxml_host_availability_file = A_surfxml_host_availability_file;
675 surfxml_host_state_file = A_surfxml_host_state_file;
678 static void finalize_host_foreach(void)
680 xbt_dynar_t names = NULL;
681 unsigned int cpt = 0;
683 xbt_dict_cursor_t cursor = NULL;
686 xbt_dict_t cluster_host_props = current_property_set;
688 xbt_assert1((names = xbt_dict_get_or_null(set_list, foreach_set_name)),
689 "Set name '%s' reffered by foreach tag not found.", foreach_set_name);
691 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);
694 /* foreach name in set call the main host callback */
695 xbt_dynar_foreach (names, cpt, name) {
696 parse_change_cpu_data(name, surfxml_host_power, surfxml_host_availability,
697 surfxml_host_availability_file, surfxml_host_state_file);
698 surfxml_call_cb_functions(main_STag_surfxml_host_cb_list);
700 xbt_dict_foreach(cluster_host_props,cursor,key,data) {
701 xbt_dict_set(current_property_set, xbt_strdup(key), xbt_strdup(data), free);
704 surfxml_call_cb_functions(main_ETag_surfxml_host_cb_list);
705 free(surfxml_bufferstack);
708 current_property_set = xbt_dict_new();
710 surfxml_bufferstack = old_buff;
714 static const char* surfxml_link_bandwidth;
715 static const char* surfxml_link_bandwidth_file;
716 static const char* surfxml_link_latency;
717 static const char* surfxml_link_latency_file;
718 static const char* surfxml_link_state_file;
720 static void parse_link_foreach(void)
722 surfxml_link_bandwidth = A_surfxml_link_bandwidth;
723 surfxml_link_bandwidth_file = A_surfxml_link_bandwidth_file;
724 surfxml_link_latency = A_surfxml_link_latency;
725 surfxml_link_latency_file = A_surfxml_link_latency_file;
726 surfxml_link_state_file = A_surfxml_link_state_file;
729 static void finalize_link_foreach(void)
731 xbt_dynar_t names = NULL;
732 unsigned int cpt = 0;
734 xbt_dict_cursor_t cursor = NULL;
737 xbt_dict_t cluster_link_props = current_property_set;
739 xbt_assert1((names = xbt_dict_get_or_null(set_list, foreach_set_name)),
740 "Set name '%s' reffered by foreach tag not found.", foreach_set_name);
742 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);
745 /* for each name in set call the main link callback */
746 xbt_dynar_foreach (names, cpt, name) {
747 parse_change_link_data(name, surfxml_link_bandwidth, surfxml_link_bandwidth_file,
748 surfxml_link_latency, surfxml_link_latency_file, surfxml_link_state_file);
749 surfxml_call_cb_functions(main_STag_surfxml_link_cb_list);
751 xbt_dict_foreach(cluster_link_props,cursor,key,data) {
752 xbt_dict_set(current_property_set, xbt_strdup(key), xbt_strdup(data), free);
755 surfxml_call_cb_functions(main_ETag_surfxml_link_cb_list);
756 free(surfxml_bufferstack);
760 current_property_set = xbt_dict_new();
762 surfxml_bufferstack = old_buff;
765 static void parse_foreach(void)
767 /* save the host & link callbacks */
768 main_STag_surfxml_host_cb_list = STag_surfxml_host_cb_list;
769 main_ETag_surfxml_host_cb_list = ETag_surfxml_host_cb_list;
770 main_STag_surfxml_link_cb_list = STag_surfxml_link_cb_list;
771 main_ETag_surfxml_link_cb_list = ETag_surfxml_link_cb_list;
773 /* define host & link callbacks to be used only by the foreach tag */
774 STag_surfxml_host_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
775 ETag_surfxml_host_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
776 STag_surfxml_link_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
777 ETag_surfxml_link_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
779 surfxml_add_callback(STag_surfxml_host_cb_list, &parse_host_foreach);
780 surfxml_add_callback(ETag_surfxml_host_cb_list, &finalize_host_foreach);
781 surfxml_add_callback(STag_surfxml_link_cb_list, &parse_link_foreach);
782 surfxml_add_callback(ETag_surfxml_link_cb_list, &finalize_link_foreach);
785 foreach_set_name = xbt_strdup(A_surfxml_foreach_set_id);
788 /* Route:multi functions */
790 static int route_multi_size=0;
791 static char* src_name, *dst_name;
792 static int is_symmetric_route;
794 static void parse_route_elem(void)
798 val = xbt_strdup(A_surfxml_link_c_ctn_id);
800 xbt_dynar_push(route_link_list, &val);
803 static void parse_route_multi_set_endpoints(void)
805 src_name = xbt_strdup(A_surfxml_route_c_multi_src);
806 dst_name = xbt_strdup(A_surfxml_route_c_multi_dst);
807 route_action = A_surfxml_route_c_multi_action;
808 is_symmetric_route = A_surfxml_route_c_multi_symmetric;
811 route_link_list = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
814 static int contains(xbt_dynar_t list, const char* value)
818 xbt_dynar_foreach(list, cpt, val) {
819 if (strcmp(val, value) == 0)
826 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.
827 The decision is based upon the value of action specified in the xml route:multi attribute action
829 void manage_route(xbt_dict_t routing_table, const char *route_name, int action, int isMultiRoute)
835 /* get already existing list if it exists */
836 links = xbt_dict_get_or_null(routing_table, route_name);
837 DEBUG1("ROUTE: %s", route_name);
840 case A_surfxml_route_action_PREPEND: /* add existing links at the end; route_link_list + links */
841 xbt_dynar_foreach(links, cpt, value) {
842 xbt_dynar_push(route_link_list,&value);
844 xbt_dynar_free(&links);
846 case A_surfxml_route_action_POSTPEND: /* add existing links in front; links + route_link_list */
847 xbt_dynar_foreach(route_link_list, cpt, value) {
848 xbt_dynar_push(links,&value);
850 xbt_dynar_free(&route_link_list);
851 route_link_list = links;
853 case A_surfxml_route_action_OVERRIDE:
854 xbt_dynar_free(&links);
859 /* this is the final route; do not add if name is a set; add only if name is in set list */
861 xbt_dict_set(routing_table, route_name, route_link_list, NULL);
865 static void parse_route_multi_set_route(void)
869 route_name = bprintf("%s#%s#%d#%d#%d", src_name, dst_name, route_action, is_symmetric_route, route_multi_size);
871 xbt_dynar_push(route_multi_elements, &route_name);
874 xbt_dict_set(route_multi_table, route_name, route_link_list, NULL);
875 /* add symmetric if it is the case */
876 if (is_symmetric_route == 1) {
877 char * symmetric_name = bprintf("%s#%s#%d#%d#%d", dst_name, src_name, route_action, !is_symmetric_route, route_multi_size);
879 xbt_dict_set(route_multi_table, symmetric_name, route_link_list, NULL);
880 xbt_dynar_push(route_multi_elements, &symmetric_name);
881 is_symmetric_route = 0;
887 static void add_multi_links(const char* src, const char* dst, xbt_dynar_t links, const char* src_name, const char* dst_name)
891 parse_make_temporary_route(src_name, dst_name, route_action);
892 surfxml_call_cb_functions(STag_surfxml_route_cb_list);
893 DEBUG2("\tADDING ROUTE: %s -> %s", src_name, dst_name);
894 /* Build link list */
895 xbt_dynar_foreach(links, cpt, value) {
896 if (strcmp(value, src) == 0)
897 val = xbt_strdup(src_name);
898 else if (strcmp(value, dst) == 0)
899 val = xbt_strdup(dst_name);
900 else if (strcmp(value, "$dst") == 0)
901 val = xbt_strdup(dst_name);
902 else if (strcmp(value, "$src") == 0)
903 val = xbt_strdup(src_name);
905 val = xbt_strdup(value);
906 DEBUG1("\t\tELEMENT: %s", val);
907 xbt_dynar_push(route_link_list, &val);
909 surfxml_call_cb_functions(ETag_surfxml_route_cb_list);
910 free(surfxml_bufferstack);
913 static void convert_route_multi_to_routes(void)
915 xbt_dict_cursor_t cursor_w;
917 unsigned int cpt, cpt2, cursor;
918 char *src_host_name, *dst_host_name, *key, *src, *dst, *val, *key_w, *data_w;
921 xbt_dynar_t src_names = NULL, dst_names = NULL, links;
923 if (!route_multi_elements) return;
926 DEBUG1("%d", xbt_dict_length(workstation_set));
927 if (workstation_set != NULL && xbt_dict_length(workstation_set) > 0)
928 set = workstation_set;
931 old_buff = surfxml_bufferstack;
932 /* Get all routes in the exact order they were entered in the platform file */
933 xbt_dynar_foreach(route_multi_elements, cursor, key) {
934 /* Get links for the route */
935 links = (xbt_dynar_t)xbt_dict_get_or_null(route_multi_table, key);
936 keys = xbt_str_split_str(key, sep);
938 src = xbt_dynar_get_as(keys, 0, char*);
939 dst = xbt_dynar_get_as(keys, 1, char*);
940 route_action = atoi(xbt_dynar_get_as(keys, 2, char*));
941 symmetric = atoi(xbt_dynar_get_as(keys, 3, char*));
943 /* Create the dynar of src and dst hosts for the new routes */
944 /* NOTE: src and dst can be either set names or simple host names */
945 src_names = (xbt_dynar_t)xbt_dict_get_or_null(set_list, src);
946 dst_names = (xbt_dynar_t)xbt_dict_get_or_null(set_list, dst);
947 /* Add to dynar even if they are simple names */
948 if (src_names == NULL) {
949 src_names = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
950 val = xbt_strdup(src);
951 xbt_dynar_push(src_names, &val);
952 if (strcmp(val,"$*") != 0 && NULL == xbt_dict_get_or_null(set, val))
953 THROW3(unknown_error,0,"(In route:multi (%s -> %s) source %s does not exist (not a set or a host)", src, dst, src);
955 if (dst_names == NULL) {
956 dst_names = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
957 val = xbt_strdup(dst);
958 if (strcmp(val,"$*") != 0 && NULL == xbt_dict_get_or_null(set, val))
959 THROW3(unknown_error,0,"(In route:multi (%s -> %s) destination %s does not exist (not a set or a host)", src, dst, dst);
960 xbt_dynar_push(dst_names, &val);
963 /* Build the routes */
964 DEBUG2("ADDING MULTI ROUTE: %s -> %s", xbt_dynar_get_as(keys, 0, char*), xbt_dynar_get_as(keys, 1, char*));
965 xbt_dynar_foreach(src_names, cpt, src_host_name) {
966 xbt_dynar_foreach(dst_names, cpt2, dst_host_name) {
967 /* If dst is $* then set this route to have its dst point to all hosts */
968 if (strcmp(src_host_name,"$*") != 0 && strcmp(dst_host_name,"$*") == 0){
969 xbt_dict_foreach(set, cursor_w, key_w, data_w) {
970 //int n = xbt_dynar_member(src_names, (char*)key_w);
971 add_multi_links(src, dst, links, src_host_name, key_w);
974 /* If src is $* then set this route to have its dst point to all hosts */
975 if (strcmp(src_host_name,"$*") == 0 && strcmp(dst_host_name,"$*") != 0){
976 xbt_dict_foreach(set, cursor_w, key_w, data_w) {
977 // if (!symmetric || (symmetric && !contains(dst_names, key_w)))
978 add_multi_links(src, dst, links, key_w, dst_host_name);
981 /* if none of them are equal to $* */
982 if (strcmp(src_host_name,"$*") != 0 && strcmp(dst_host_name,"$*") != 0) {
983 add_multi_links(src, dst, links, src_host_name, dst_host_name);
988 surfxml_bufferstack = old_buff;
989 xbt_dict_free(&route_multi_table);
990 xbt_dynar_free(&route_multi_elements);
993 /* Cluster tag functions */
995 static void parse_cluster(void)
997 static int AX_ptr = 0;
998 static int surfxml_bufferstack_size = 2048;
1000 char* cluster_id = A_surfxml_cluster_id;
1001 char* cluster_prefix = A_surfxml_cluster_prefix;
1002 char* cluster_suffix = A_surfxml_cluster_suffix;
1003 char* cluster_radical = A_surfxml_cluster_radical;
1004 char* cluster_power = A_surfxml_cluster_power;
1005 char* cluster_bw = A_surfxml_cluster_bw;
1006 char* cluster_lat = A_surfxml_cluster_lat;
1007 char* cluster_bb_bw = A_surfxml_cluster_bb_bw;
1008 char* cluster_bb_lat = A_surfxml_cluster_bb_lat;
1010 char* saved_buff = surfxml_bufferstack;
1012 char * backbone_name;
1014 surfxml_bufferstack = xbt_new0(char, surfxml_bufferstack_size);
1017 SURFXML_BUFFER_SET(set_id, cluster_id);
1018 SURFXML_BUFFER_SET(set_prefix, cluster_prefix);
1019 SURFXML_BUFFER_SET(set_suffix, cluster_suffix);
1020 SURFXML_BUFFER_SET(set_radical, cluster_radical);
1022 SURFXML_START_TAG(set);
1023 SURFXML_END_TAG(set);
1026 SURFXML_BUFFER_SET(foreach_set_id, cluster_id);
1028 SURFXML_START_TAG(foreach);
1030 /* Make host for the foreach */
1031 parse_change_cpu_data("$1", cluster_power, "1.0", "", "");
1032 A_surfxml_host_state = A_surfxml_host_state_ON;
1034 SURFXML_START_TAG(host);
1035 SURFXML_END_TAG(host);
1037 /* Make link for the foreach */
1038 parse_change_link_data("$1", cluster_bw, "", cluster_lat, "", "");
1039 A_surfxml_link_state = A_surfxml_link_state_ON;
1040 A_surfxml_link_sharing_policy = A_surfxml_link_sharing_policy_SHARED;
1042 SURFXML_START_TAG(link);
1043 SURFXML_END_TAG(link);
1045 SURFXML_END_TAG(foreach);
1047 /* Make backbone link */
1048 backbone_name = bprintf("%s_bb", cluster_id);
1049 parse_change_link_data(backbone_name, cluster_bb_bw, "", cluster_bb_lat, "", "");
1050 A_surfxml_link_state = A_surfxml_link_state_ON;
1051 A_surfxml_link_sharing_policy = A_surfxml_link_sharing_policy_FATPIPE;
1053 SURFXML_START_TAG(link);
1054 SURFXML_END_TAG(link);
1056 /* Make route multi with the outside world, i.e. cluster->$* */
1057 SURFXML_BUFFER_SET(route_c_multi_src, cluster_id);
1058 SURFXML_BUFFER_SET(route_c_multi_dst, "$*");
1059 A_surfxml_route_c_multi_symmetric = A_surfxml_route_c_multi_symmetric_NO;
1060 A_surfxml_route_c_multi_action = A_surfxml_route_c_multi_action_OVERRIDE;
1062 SURFXML_START_TAG(route_c_multi);
1064 SURFXML_BUFFER_SET(link_c_ctn_id, "$src");
1066 SURFXML_START_TAG(link_c_ctn);
1067 SURFXML_END_TAG(link_c_ctn);
1069 SURFXML_END_TAG(route_c_multi);
1071 /* Make route multi between cluster hosts, i.e. cluster->cluster */
1072 SURFXML_BUFFER_SET(route_c_multi_src, cluster_id);
1073 SURFXML_BUFFER_SET(route_c_multi_dst, cluster_id);
1074 A_surfxml_route_c_multi_action = A_surfxml_route_c_multi_action_POSTPEND;
1075 A_surfxml_route_c_multi_symmetric = A_surfxml_route_c_multi_symmetric_NO;
1077 SURFXML_START_TAG(route_c_multi);
1079 SURFXML_BUFFER_SET(link_c_ctn_id, backbone_name);
1081 SURFXML_START_TAG(link_c_ctn);
1082 SURFXML_END_TAG(link_c_ctn);
1084 SURFXML_END_TAG(route_c_multi);
1088 free(surfxml_bufferstack);
1089 surfxml_bufferstack = saved_buff;
1092 /* Trace management functions */
1094 static double trace_periodicity = -1.0;
1095 static char* trace_file = NULL;
1096 static char* trace_id;
1098 static void parse_trace_init(void)
1100 trace_id = strdup(A_surfxml_trace_id);
1101 trace_file = strdup(A_surfxml_trace_file);
1102 surf_parse_get_double(&trace_periodicity, A_surfxml_trace_periodicity);
1105 static void parse_trace_finalize(void)
1108 if (!trace_file || strcmp(trace_file,"") != 0) {
1109 surf_parse_get_trace(&trace, trace_file);
1112 if (strcmp(surfxml_pcdata, "") == 0) trace = NULL;
1114 trace = tmgr_trace_new_from_string(trace_id, surfxml_pcdata, trace_periodicity);
1116 xbt_dict_set(traces_set_list, trace_id, (void *)trace, NULL);
1119 static void parse_trace_c_connect(void)
1121 xbt_assert2(xbt_dict_get_or_null(traces_set_list, A_surfxml_trace_c_connect_trace),
1122 "Cannot connect trace %s to %s: trace unknown", A_surfxml_trace_c_connect_trace,A_surfxml_trace_c_connect_element);
1124 switch (A_surfxml_trace_c_connect_kind) {
1125 case A_surfxml_trace_c_connect_kind_HOST_AVAIL:
1126 xbt_dict_set(trace_connect_list_host_avail, A_surfxml_trace_c_connect_trace, xbt_strdup(A_surfxml_trace_c_connect_element), free);
1128 case A_surfxml_trace_c_connect_kind_POWER:
1129 xbt_dict_set(trace_connect_list_power, A_surfxml_trace_c_connect_trace, xbt_strdup(A_surfxml_trace_c_connect_element), free);
1131 case A_surfxml_trace_c_connect_kind_LINK_AVAIL:
1132 xbt_dict_set(trace_connect_list_link_avail, A_surfxml_trace_c_connect_trace, xbt_strdup(A_surfxml_trace_c_connect_element), free);
1134 case A_surfxml_trace_c_connect_kind_BANDWIDTH:
1135 xbt_dict_set(trace_connect_list_bandwidth, A_surfxml_trace_c_connect_trace, xbt_strdup(A_surfxml_trace_c_connect_element), free);
1137 case A_surfxml_trace_c_connect_kind_LATENCY:
1138 xbt_dict_set(trace_connect_list_latency, A_surfxml_trace_c_connect_trace, xbt_strdup(A_surfxml_trace_c_connect_element), free);
1141 xbt_die("kind of trace unknown");
1145 /* Random tag functions */
1147 double get_cpu_power(const char *power)
1149 double power_scale = 0.0;
1152 random_data_t random = NULL;
1153 /* randomness is inserted like this: power="$rand(my_random)" */
1154 if (((p = strstr(power, "$rand(")) != NULL) && ((q = strstr(power, ")")) != NULL)) {
1156 generator = xbt_malloc(q - (p + 6) + 1);
1157 memcpy(generator, p + 6, q - (p + 6));
1158 generator[q - (p + 6)] = '\0';
1159 xbt_assert1((random = xbt_dict_get_or_null(random_data_list, generator)),
1160 "Random generator %s undefined", generator);
1161 power_scale = random_generate(random);
1165 surf_parse_get_double(&power_scale, power);
1170 int random_min, random_max, random_mean, random_std_deviation, random_generator;
1173 static void init_randomness(void)
1175 random_id = A_surfxml_random_id;
1176 surf_parse_get_int(&random_min, A_surfxml_random_min);
1177 surf_parse_get_int(&random_max, A_surfxml_random_max);
1178 surf_parse_get_int(&random_mean, A_surfxml_random_mean);
1179 surf_parse_get_int(&random_std_deviation, A_surfxml_random_std_deviation);
1180 random_generator = A_surfxml_random_generator;
1183 static void add_randomness(void)
1185 /* If needed aditional properties can be added by using the prop tag */
1186 random_data_t random = random_new(random_generator, 0, random_min, random_max, random_mean, random_std_deviation);
1187 xbt_dict_set(random_data_list, random_id, (void *)random, NULL);