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);
100 void surf_parse_free_callbacks(void)
102 xbt_dynar_free(&STag_surfxml_platform_cb_list);
103 xbt_dynar_free(&ETag_surfxml_platform_cb_list);
104 xbt_dynar_free(&STag_surfxml_host_cb_list);
105 xbt_dynar_free(&ETag_surfxml_host_cb_list);
106 xbt_dynar_free(&STag_surfxml_router_cb_list);
107 xbt_dynar_free(&ETag_surfxml_router_cb_list);
108 xbt_dynar_free(&STag_surfxml_link_cb_list);
109 xbt_dynar_free(&ETag_surfxml_link_cb_list);
110 xbt_dynar_free(&STag_surfxml_route_cb_list);
111 xbt_dynar_free(&ETag_surfxml_route_cb_list);
112 xbt_dynar_free(&STag_surfxml_link_c_ctn_cb_list);
113 xbt_dynar_free(&ETag_surfxml_link_c_ctn_cb_list);
114 xbt_dynar_free(&STag_surfxml_process_cb_list);
115 xbt_dynar_free(&ETag_surfxml_process_cb_list);
116 xbt_dynar_free(&STag_surfxml_argument_cb_list);
117 xbt_dynar_free(&ETag_surfxml_argument_cb_list);
118 xbt_dynar_free(&STag_surfxml_prop_cb_list);
119 xbt_dynar_free(&ETag_surfxml_prop_cb_list);
120 xbt_dynar_free(&STag_surfxml_set_cb_list);
121 xbt_dynar_free(&ETag_surfxml_set_cb_list);
122 xbt_dynar_free(&STag_surfxml_foreach_cb_list);
123 xbt_dynar_free(&ETag_surfxml_foreach_cb_list);
124 xbt_dynar_free(&STag_surfxml_route_c_multi_cb_list);
125 xbt_dynar_free(&ETag_surfxml_route_c_multi_cb_list);
126 xbt_dynar_free(&STag_surfxml_cluster_cb_list);
127 xbt_dynar_free(&ETag_surfxml_cluster_cb_list);
128 xbt_dynar_free(&STag_surfxml_trace_cb_list);
129 xbt_dynar_free(&ETag_surfxml_trace_cb_list);
130 xbt_dynar_free(&STag_surfxml_trace_c_connect_cb_list);
131 xbt_dynar_free(&ETag_surfxml_trace_c_connect_cb_list);
132 xbt_dynar_free(&STag_surfxml_random_cb_list);
133 xbt_dynar_free(&ETag_surfxml_random_cb_list);
136 void surf_parse_reset_parser(void)
138 surf_parse_free_callbacks();
139 STag_surfxml_platform_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
140 ETag_surfxml_platform_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
141 STag_surfxml_host_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
142 ETag_surfxml_host_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
143 STag_surfxml_router_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
144 ETag_surfxml_router_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
145 STag_surfxml_link_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
146 ETag_surfxml_link_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
147 STag_surfxml_route_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
148 ETag_surfxml_route_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
149 STag_surfxml_link_c_ctn_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
150 ETag_surfxml_link_c_ctn_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
151 STag_surfxml_process_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
152 ETag_surfxml_process_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
153 STag_surfxml_argument_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
154 ETag_surfxml_argument_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
155 STag_surfxml_prop_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
156 ETag_surfxml_prop_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
157 STag_surfxml_set_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
158 ETag_surfxml_set_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
159 STag_surfxml_foreach_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
160 ETag_surfxml_foreach_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
161 STag_surfxml_route_c_multi_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
162 ETag_surfxml_route_c_multi_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
163 STag_surfxml_cluster_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
164 ETag_surfxml_cluster_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
165 STag_surfxml_trace_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
166 ETag_surfxml_trace_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
167 STag_surfxml_trace_c_connect_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
168 ETag_surfxml_trace_c_connect_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
169 STag_surfxml_random_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
170 ETag_surfxml_random_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
173 void STag_surfxml_include(void)
175 xbt_dynar_push(surf_input_buffer_stack, &surf_input_buffer);
176 xbt_dynar_push(surf_file_to_parse_stack, &surf_file_to_parse);
178 surf_file_to_parse = surf_fopen(A_surfxml_include_file, "r");
179 xbt_assert1((surf_file_to_parse), "Unable to open \"%s\"\n",
180 A_surfxml_include_file);
181 surf_input_buffer = surf_parse__create_buffer(surf_file_to_parse, 10);
182 surf_parse__switch_to_buffer(surf_input_buffer);
187 void ETag_surfxml_include(void)
191 surf_parse__delete_buffer(surf_input_buffer);
192 fclose(surf_file_to_parse);
193 xbt_dynar_pop(surf_file_to_parse_stack, &surf_file_to_parse);
194 xbt_dynar_pop(surf_input_buffer_stack, &surf_input_buffer);
197 void STag_surfxml_platform(void)
199 double version = 0.0;
201 sscanf(A_surfxml_platform_version, "%lg", &version);
203 xbt_assert0((version >= 1.0), "******* BIG FAT WARNING *********\n "
204 "You're using an ancient XML file. "
205 "Since SimGrid 3.1, units are Bytes, Flops, and seconds "
206 "instead of MBytes, MFlops and seconds. "
207 "A script (surfxml_update.pl) to help you convert your old "
209 "is available in the contrib/platform_generation directory "
210 "of the simgrid repository. Please check also out the "
211 "SURF section of the ChangeLog for the 3.1 version. "
212 "Last, do not forget to also update your values for "
213 "the calls to MSG_task_create (if any).");
214 xbt_assert0((version >= 2.0), "******* BIG FAT WARNING *********\n "
215 "You're using an old XML file. "
216 "A script (surfxml_update.pl) to help you convert your old "
218 "is available in the contrib/platform_generation directory "
219 "of the simgrid repository.");
221 if (set_list == NULL) set_list = xbt_dict_new();
223 surfxml_call_cb_functions(STag_surfxml_platform_cb_list);
227 void ETag_surfxml_platform(void)
229 convert_route_multi_to_routes();
231 surfxml_call_cb_functions(ETag_surfxml_platform_cb_list);
233 xbt_dict_free(&random_data_list);
234 xbt_dict_free(&set_list);
237 void STag_surfxml_host(void)
239 surfxml_call_cb_functions(STag_surfxml_host_cb_list);
242 void ETag_surfxml_host(void)
244 surfxml_call_cb_functions(ETag_surfxml_host_cb_list);
247 void STag_surfxml_router(void)
249 surfxml_call_cb_functions(STag_surfxml_router_cb_list);
252 void ETag_surfxml_router(void)
254 surfxml_call_cb_functions(ETag_surfxml_router_cb_list);
257 void STag_surfxml_link(void)
259 surfxml_call_cb_functions(STag_surfxml_link_cb_list);
262 void ETag_surfxml_link(void)
264 surfxml_call_cb_functions(ETag_surfxml_link_cb_list);
267 void STag_surfxml_route(void)
269 surfxml_call_cb_functions(STag_surfxml_route_cb_list);
272 void ETag_surfxml_route(void)
274 surfxml_call_cb_functions(ETag_surfxml_route_cb_list);
277 void STag_surfxml_link_c_ctn(void)
279 surfxml_call_cb_functions(STag_surfxml_link_c_ctn_cb_list);
282 void ETag_surfxml_link_c_ctn(void)
284 surfxml_call_cb_functions(ETag_surfxml_link_c_ctn_cb_list);
287 void STag_surfxml_process(void)
289 surfxml_call_cb_functions(STag_surfxml_process_cb_list);
292 void ETag_surfxml_process(void)
294 surfxml_call_cb_functions(ETag_surfxml_process_cb_list);
297 void STag_surfxml_argument(void)
299 surfxml_call_cb_functions(STag_surfxml_argument_cb_list);
302 void ETag_surfxml_argument(void)
304 surfxml_call_cb_functions(ETag_surfxml_argument_cb_list);
307 void STag_surfxml_prop(void)
309 surfxml_call_cb_functions(STag_surfxml_prop_cb_list);
311 void ETag_surfxml_prop(void)
313 surfxml_call_cb_functions(ETag_surfxml_prop_cb_list);
316 void STag_surfxml_set(void)
318 surfxml_call_cb_functions(STag_surfxml_set_cb_list);
321 void ETag_surfxml_set(void)
323 surfxml_call_cb_functions(ETag_surfxml_set_cb_list);
326 void STag_surfxml_foreach(void)
328 /* Save the current buffer */
329 old_buff = surfxml_bufferstack;
330 surfxml_call_cb_functions(STag_surfxml_foreach_cb_list);
333 void ETag_surfxml_foreach(void)
335 surfxml_call_cb_functions(ETag_surfxml_foreach_cb_list);
337 /* free the temporary dynar and restore original */
338 xbt_dynar_free(&STag_surfxml_host_cb_list);
339 xbt_dynar_free(&ETag_surfxml_host_cb_list);
341 STag_surfxml_host_cb_list = main_STag_surfxml_host_cb_list;
342 ETag_surfxml_host_cb_list = main_ETag_surfxml_host_cb_list;
344 /* free the temporary dynar and restore original */
345 xbt_dynar_free(&STag_surfxml_link_cb_list);
346 xbt_dynar_free(&ETag_surfxml_link_cb_list);
348 STag_surfxml_link_cb_list = main_STag_surfxml_link_cb_list;
349 ETag_surfxml_link_cb_list = main_ETag_surfxml_link_cb_list;
353 void STag_surfxml_route_c_multi(void)
355 surfxml_call_cb_functions(STag_surfxml_route_c_multi_cb_list);
358 void ETag_surfxml_route_c_multi(void)
360 surfxml_call_cb_functions(ETag_surfxml_route_c_multi_cb_list);
363 void STag_surfxml_cluster(void)
365 surfxml_call_cb_functions(STag_surfxml_cluster_cb_list);
368 void ETag_surfxml_cluster(void)
370 surfxml_call_cb_functions(ETag_surfxml_cluster_cb_list);
373 void STag_surfxml_trace(void)
375 surfxml_call_cb_functions(STag_surfxml_trace_cb_list);
378 void ETag_surfxml_trace(void)
380 surfxml_call_cb_functions(ETag_surfxml_trace_cb_list);
383 void STag_surfxml_trace_c_connect(void)
385 surfxml_call_cb_functions(STag_surfxml_trace_c_connect_cb_list);
388 void ETag_surfxml_trace_c_connect(void)
390 surfxml_call_cb_functions(ETag_surfxml_trace_c_connect_cb_list);
393 void STag_surfxml_random(void)
395 surfxml_call_cb_functions(STag_surfxml_random_cb_list);
398 void ETag_surfxml_random(void)
400 surfxml_call_cb_functions(ETag_surfxml_random_cb_list);
403 void surf_parse_open(const char *file)
405 static int warned = 0; /* warn only once */
409 ("Bypassing the XML parser since surf_parse_open received a NULL pointer. If it is not what you want, go fix your code.");
414 if (!surf_input_buffer_stack)
415 surf_input_buffer_stack = xbt_dynar_new(sizeof(YY_BUFFER_STATE), NULL);
416 if (!surf_file_to_parse_stack)
417 surf_file_to_parse_stack = xbt_dynar_new(sizeof(FILE *), NULL);
419 surf_file_to_parse = surf_fopen(file, "r");
420 xbt_assert1((surf_file_to_parse), "Unable to open \"%s\"\n", file);
421 surf_input_buffer = surf_parse__create_buffer(surf_file_to_parse, 10);
422 surf_parse__switch_to_buffer(surf_input_buffer);
423 surf_parse_lineno = 1;
426 void surf_parse_close(void)
428 if (surf_input_buffer_stack)
429 xbt_dynar_free(&surf_input_buffer_stack);
430 if (surf_file_to_parse_stack)
431 xbt_dynar_free(&surf_file_to_parse_stack);
433 if (surf_file_to_parse) {
434 surf_parse__delete_buffer(surf_input_buffer);
435 fclose(surf_file_to_parse);
440 static int _surf_parse(void)
442 return surf_parse_lex();
445 int_f_void_t surf_parse = _surf_parse;
447 void surf_parse_get_double(double *value, const char *string)
451 ret = sscanf(string, "%lg", value);
452 xbt_assert2((ret == 1), "Parse error line %d : %s not a number",
453 surf_parse_lineno, string);
456 void surf_parse_get_int(int *value, const char *string)
460 ret = sscanf(string, "%d", value);
461 xbt_assert2((ret == 1), "Parse error line %d : %s not a number",
462 surf_parse_lineno, string);
465 void surf_parse_get_trace(tmgr_trace_t * trace, const char *string)
467 if ((!string) || (strcmp(string, "") == 0))
470 *trace = tmgr_trace_new(string);
473 void parse_properties(void)
477 if(!current_property_set) current_property_set = xbt_dict_new();
479 value = xbt_strdup(A_surfxml_prop_value);
480 xbt_dict_set(current_property_set, A_surfxml_prop_id, value, free);
483 void surfxml_add_callback(xbt_dynar_t cb_list, void_f_void_t function)
485 xbt_dynar_push(cb_list, &function);
488 static XBT_INLINE void surfxml_call_cb_functions(xbt_dynar_t cb_list)
490 unsigned int iterator;
492 xbt_dynar_foreach(cb_list, iterator, fun){
493 DEBUG2("call %p %p",fun,*fun);
498 static void parse_route_set_endpoints(void) {
499 route_link_list = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
502 static void init_data(void)
504 xbt_dict_free(&route_table);
505 xbt_dynar_free(&route_link_list);
506 route_table = xbt_dict_new();
508 route_multi_table = xbt_dict_new();
509 route_multi_elements = xbt_dynar_new(sizeof(char*), NULL);
510 traces_set_list = xbt_dict_new();
512 trace_connect_list_host_avail = xbt_dict_new();
513 trace_connect_list_power = xbt_dict_new();
514 trace_connect_list_link_avail = xbt_dict_new();
515 trace_connect_list_bandwidth = xbt_dict_new();
516 trace_connect_list_latency = xbt_dict_new();
518 random_data_list = xbt_dict_new();
519 surfxml_add_callback(ETag_surfxml_link_c_ctn_cb_list, &parse_route_elem);
520 surfxml_add_callback(STag_surfxml_route_cb_list, &parse_route_set_endpoints);
523 static void free_data(void)
526 xbt_dict_cursor_t cursor = NULL;
528 xbt_dict_foreach(route_table, cursor, key, data) {
529 xbt_dynar_t links = (xbt_dynar_t)data;
530 xbt_dynar_free(&links);
532 xbt_dict_free(&route_table);
533 route_link_list = NULL;
536 void parse_platform_file(const char* file)
538 surf_parse_open(file);
540 xbt_assert1((!(*surf_parse)()), "Parse error in %s", file);
545 /* Functions to bypass route, host and link tags. Used by the foreach and route:multi tags */
547 static void parse_make_temporary_route(const char *src, const char *dst, int action)
550 surfxml_bufferstack = xbt_new0(char, 2048);
552 A_surfxml_route_action = action;
553 SURFXML_BUFFER_SET(route_src, src);
554 SURFXML_BUFFER_SET(route_dst, dst);
557 static void parse_change_cpu_data(const char* hostName, const char* surfxml_host_power, const char* surfxml_host_availability,
558 const char* surfxml_host_availability_file, const char* surfxml_host_state_file)
561 surfxml_bufferstack = xbt_new0(char, 2048);
563 SURFXML_BUFFER_SET(host_id, hostName);
564 SURFXML_BUFFER_SET(host_power, surfxml_host_power /*hostPower*/);
565 SURFXML_BUFFER_SET(host_availability, surfxml_host_availability);
566 SURFXML_BUFFER_SET(host_availability_file, surfxml_host_availability_file);
567 SURFXML_BUFFER_SET(host_state_file, surfxml_host_state_file);
570 static void parse_change_link_data(const char* linkName, const char* surfxml_link_bandwidth, const char* surfxml_link_bandwidth_file,
571 const char* surfxml_link_latency, const char* surfxml_link_latency_file, const char* surfxml_link_state_file)
574 surfxml_bufferstack = xbt_new0(char, 2048);
576 SURFXML_BUFFER_SET(link_id, linkName);
577 SURFXML_BUFFER_SET(link_bandwidth, surfxml_link_bandwidth);
578 SURFXML_BUFFER_SET(link_bandwidth_file, surfxml_link_bandwidth_file);
579 SURFXML_BUFFER_SET(link_latency, surfxml_link_latency);
580 SURFXML_BUFFER_SET(link_latency_file, surfxml_link_latency_file);
581 SURFXML_BUFFER_SET(link_state_file, surfxml_link_state_file);
585 * \brief Restores the original surfxml buffer
587 static void parse_restore_original_buffer(void)
589 free(surfxml_bufferstack);
590 surfxml_bufferstack = old_buff;
593 /* Functions for the sets and foreach tags */
595 void parse_sets(void)
597 char *id, *suffix, *prefix, *radical;
599 xbt_dynar_t radical_ends;
600 xbt_dynar_t current_set;
604 id = xbt_strdup(A_surfxml_set_id);
605 prefix = xbt_strdup(A_surfxml_set_prefix);
606 suffix = xbt_strdup(A_surfxml_set_suffix);
607 radical = xbt_strdup(A_surfxml_set_radical);
609 xbt_assert1(!xbt_dict_get_or_null(set_list, id),
610 "Set '%s' declared several times in the platform file.",id);
611 radical_ends = xbt_str_split(radical, "-");
612 xbt_assert1((xbt_dynar_length(radical_ends)==2), "Radical must be in the form lvalue-rvalue! Provided value: %s", radical);
614 surf_parse_get_int(&start, xbt_dynar_get_as(radical_ends, 0, char*));
615 surf_parse_get_int(&end, xbt_dynar_get_as(radical_ends, 1, char*));
617 current_set = xbt_dynar_new(sizeof(char*), NULL);
620 for (i=start; i<=end; i++) {
621 value = bprintf("%s%d%s", prefix, i, suffix);
622 xbt_dynar_push(current_set, &value);
625 xbt_dict_set(set_list, id, current_set, NULL);
628 static const char* surfxml_host_power;
629 static const char* surfxml_host_availability;
630 static const char* surfxml_host_availability_file;
631 static const char* surfxml_host_state_file;
633 static void parse_host_foreach(void)
635 surfxml_host_power = A_surfxml_host_power;
636 surfxml_host_availability = A_surfxml_host_availability;
637 surfxml_host_availability_file = A_surfxml_host_availability_file;
638 surfxml_host_state_file = A_surfxml_host_state_file;
641 static void finalize_host_foreach(void)
643 xbt_dynar_t names = NULL;
644 unsigned int cpt = 0;
646 xbt_dict_cursor_t cursor = NULL;
649 xbt_dict_t cluster_host_props = current_property_set;
651 xbt_assert1((names = xbt_dict_get_or_null(set_list, foreach_set_name)),
652 "Set name '%s' reffered by foreach tag not found.", foreach_set_name);
654 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);
657 /* foreach name in set call the main host callback */
658 xbt_dynar_foreach (names, cpt, name) {
659 parse_change_cpu_data(name, surfxml_host_power, surfxml_host_availability,
660 surfxml_host_availability_file, surfxml_host_state_file);
661 surfxml_call_cb_functions(main_STag_surfxml_host_cb_list);
663 xbt_dict_foreach(cluster_host_props,cursor,key,data) {
664 xbt_dict_set(current_property_set, xbt_strdup(key), xbt_strdup(data), free);
667 surfxml_call_cb_functions(main_ETag_surfxml_host_cb_list);
668 free(surfxml_bufferstack);
671 current_property_set = xbt_dict_new();
673 surfxml_bufferstack = old_buff;
677 static const char* surfxml_link_bandwidth;
678 static const char* surfxml_link_bandwidth_file;
679 static const char* surfxml_link_latency;
680 static const char* surfxml_link_latency_file;
681 static const char* surfxml_link_state_file;
683 static void parse_link_foreach(void)
685 surfxml_link_bandwidth = A_surfxml_link_bandwidth;
686 surfxml_link_bandwidth_file = A_surfxml_link_bandwidth_file;
687 surfxml_link_latency = A_surfxml_link_latency;
688 surfxml_link_latency_file = A_surfxml_link_latency_file;
689 surfxml_link_state_file = A_surfxml_link_state_file;
692 static void finalize_link_foreach(void)
694 xbt_dynar_t names = NULL;
695 unsigned int cpt = 0;
697 xbt_dict_cursor_t cursor = NULL;
700 xbt_dict_t cluster_link_props = current_property_set;
702 xbt_assert1((names = xbt_dict_get_or_null(set_list, foreach_set_name)),
703 "Set name '%s' reffered by foreach tag not found.", foreach_set_name);
705 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);
708 /* for each name in set call the main link callback */
709 xbt_dynar_foreach (names, cpt, name) {
710 parse_change_link_data(name, surfxml_link_bandwidth, surfxml_link_bandwidth_file,
711 surfxml_link_latency, surfxml_link_latency_file, surfxml_link_state_file);
712 surfxml_call_cb_functions(main_STag_surfxml_link_cb_list);
714 xbt_dict_foreach(cluster_link_props,cursor,key,data) {
715 xbt_dict_set(current_property_set, xbt_strdup(key), xbt_strdup(data), free);
718 surfxml_call_cb_functions(main_ETag_surfxml_link_cb_list);
719 free(surfxml_bufferstack);
723 current_property_set = xbt_dict_new();
725 surfxml_bufferstack = old_buff;
728 void parse_foreach(void)
730 /* save the host & link callbacks */
731 main_STag_surfxml_host_cb_list = STag_surfxml_host_cb_list;
732 main_ETag_surfxml_host_cb_list = ETag_surfxml_host_cb_list;
733 main_STag_surfxml_link_cb_list = STag_surfxml_link_cb_list;
734 main_ETag_surfxml_link_cb_list = ETag_surfxml_link_cb_list;
736 /* define host & link callbacks to be used only by the foreach tag */
737 STag_surfxml_host_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
738 ETag_surfxml_host_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
739 STag_surfxml_link_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
740 ETag_surfxml_link_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
742 surfxml_add_callback(STag_surfxml_host_cb_list, &parse_host_foreach);
743 surfxml_add_callback(ETag_surfxml_host_cb_list, &finalize_host_foreach);
744 surfxml_add_callback(STag_surfxml_link_cb_list, &parse_link_foreach);
745 surfxml_add_callback(ETag_surfxml_link_cb_list, &finalize_link_foreach);
748 foreach_set_name = xbt_strdup(A_surfxml_foreach_set_id);
751 /* Route:multi functions */
753 static int route_multi_size=0;
754 static char* src_name, *dst_name;
755 static int is_symmetric_route;
757 static void parse_route_elem(void)
761 val = xbt_strdup(A_surfxml_link_c_ctn_id);
763 xbt_dynar_push(route_link_list, &val);
766 void parse_route_multi_set_endpoints(void)
768 src_name = xbt_strdup(A_surfxml_route_c_multi_src);
769 dst_name = xbt_strdup(A_surfxml_route_c_multi_dst);
770 route_action = A_surfxml_route_c_multi_action;
771 is_symmetric_route = A_surfxml_route_c_multi_symmetric;
774 route_link_list = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
777 static int contains(xbt_dynar_t list, const char* value)
781 xbt_dynar_foreach(list, cpt, val) {
782 if (strcmp(val, value) == 0)
789 This function is used to append or override the contents of an alread existing route in the case a new one with its name is found.
790 The decision is based upon the value of action specified in the xml route:multi attribute action
792 void manage_route(xbt_dict_t routing_table, const char *route_name, int action, int isMultiRoute)
798 /* get already existing list if it exists */
799 links = xbt_dict_get_or_null(routing_table, route_name);
800 DEBUG1("ROUTE: %s", route_name);
803 case A_surfxml_route_action_PREPEND: /* add existing links at the end; route_link_list + links */
804 xbt_dynar_foreach(links, cpt, value) {
805 xbt_dynar_push(route_link_list,&value);
808 case A_surfxml_route_action_POSTPEND: /* add existing links in front; links + route_link_list */
809 xbt_dynar_foreach(route_link_list, cpt, value) {
810 xbt_dynar_push(links,&value);
812 xbt_dynar_free(&route_link_list);
813 route_link_list = links;
815 case A_surfxml_route_action_OVERRIDE:
820 /* this is the final route; do not add if name is a set; add only if name is in set list */
822 xbt_dict_set(routing_table, route_name, route_link_list, NULL);
826 void parse_route_multi_set_route(void)
830 route_name = bprintf("%s#%s#%d#%d#%d", src_name, dst_name, route_action, is_symmetric_route, route_multi_size);
832 xbt_dynar_push(route_multi_elements, &route_name);
835 xbt_dict_set(route_multi_table, route_name, route_link_list, NULL);
836 /* add symmetric if it is the case */
837 if (is_symmetric_route == 1) {
838 char * symmetric_name = bprintf("%s#%s#%d#%d#%d", dst_name, src_name, route_action, !is_symmetric_route, route_multi_size);
840 xbt_dict_set(route_multi_table, symmetric_name, route_link_list, NULL);
841 xbt_dynar_push(route_multi_elements, &symmetric_name);
842 is_symmetric_route = 0;
848 static void add_multi_links(const char* src, const char* dst, xbt_dynar_t links, const char* src_name, const char* dst_name)
852 parse_make_temporary_route(src_name, dst_name, route_action);
853 surfxml_call_cb_functions(STag_surfxml_route_cb_list);
854 DEBUG2("\tADDING ROUTE: %s -> %s", src_name, dst_name);
855 /* Build link list */
856 xbt_dynar_foreach(links, cpt, value) {
857 if (strcmp(value, src) == 0)
858 val = xbt_strdup(src_name);
859 else if (strcmp(value, dst) == 0)
860 val = xbt_strdup(dst_name);
861 else if (strcmp(value, "$dst") == 0)
862 val = xbt_strdup(dst_name);
863 else if (strcmp(value, "$src") == 0)
864 val = xbt_strdup(src_name);
866 val = xbt_strdup(value);
867 DEBUG1("\t\tELEMENT: %s", val);
868 xbt_dynar_push(route_link_list, &val);
870 surfxml_call_cb_functions(ETag_surfxml_route_cb_list);
871 free(surfxml_bufferstack);
874 static void convert_route_multi_to_routes(void)
876 xbt_dict_cursor_t cursor_w;
878 unsigned int cpt, cpt2, cursor;
879 char *src_host_name, *dst_host_name, *key, *src, *dst, *val, *key_w, *data_w;
882 xbt_dynar_t src_names = NULL, dst_names = NULL, links;
884 if (!route_multi_elements) return;
887 DEBUG1("%d", xbt_dict_length(workstation_set));
888 if (workstation_set != NULL && xbt_dict_length(workstation_set) > 0)
889 set = workstation_set;
892 old_buff = surfxml_bufferstack;
893 /* Get all routes in the exact order they were entered in the platform file */
894 xbt_dynar_foreach(route_multi_elements, cursor, key) {
895 /* Get links for the route */
896 links = (xbt_dynar_t)xbt_dict_get_or_null(route_multi_table, key);
897 keys = xbt_str_split_str(key, sep);
899 src = xbt_dynar_get_as(keys, 0, char*);
900 dst = xbt_dynar_get_as(keys, 1, char*);
901 route_action = atoi(xbt_dynar_get_as(keys, 2, char*));
902 symmetric = atoi(xbt_dynar_get_as(keys, 3, char*));
904 /* Create the dynar of src and dst hosts for the new routes */
905 /* NOTE: src and dst can be either set names or simple host names */
906 src_names = (xbt_dynar_t)xbt_dict_get_or_null(set_list, src);
907 dst_names = (xbt_dynar_t)xbt_dict_get_or_null(set_list, dst);
908 /* Add to dynar even if they are simple names */
909 if (src_names == NULL) {
910 src_names = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
911 val = xbt_strdup(src);
912 xbt_dynar_push(src_names, &val);
913 if (strcmp(val,"$*") != 0 && NULL == xbt_dict_get_or_null(set, val))
914 THROW3(unknown_error,0,"(In route:multi (%s -> %s) source %s does not exist (not a set or a host)", src, dst, src);
916 if (dst_names == NULL) {
917 dst_names = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
918 val = xbt_strdup(dst);
919 if (strcmp(val,"$*") != 0 && NULL == xbt_dict_get_or_null(set, val))
920 THROW3(unknown_error,0,"(In route:multi (%s -> %s) destination %s does not exist (not a set or a host)", src, dst, dst);
921 xbt_dynar_push(dst_names, &val);
924 /* Build the routes */
925 DEBUG2("ADDING MULTI ROUTE: %s -> %s", xbt_dynar_get_as(keys, 0, char*), xbt_dynar_get_as(keys, 1, char*));
926 xbt_dynar_foreach(src_names, cpt, src_host_name) {
927 xbt_dynar_foreach(dst_names, cpt2, dst_host_name) {
928 /* If dst is $* then set this route to have its dst point to all hosts */
929 if (strcmp(src_host_name,"$*") != 0 && strcmp(dst_host_name,"$*") == 0){
930 xbt_dict_foreach(set, cursor_w, key_w, data_w) {
931 //int n = xbt_dynar_member(src_names, (char*)key_w);
932 add_multi_links(src, dst, links, src_host_name, key_w);
935 /* If src is $* then set this route to have its dst point to all hosts */
936 if (strcmp(src_host_name,"$*") == 0 && strcmp(dst_host_name,"$*") != 0){
937 xbt_dict_foreach(set, cursor_w, key_w, data_w) {
938 // if (!symmetric || (symmetric && !contains(dst_names, key_w)))
939 add_multi_links(src, dst, links, key_w, dst_host_name);
942 /* if none of them are equal to $* */
943 if (strcmp(src_host_name,"$*") != 0 && strcmp(dst_host_name,"$*") != 0) {
944 add_multi_links(src, dst, links, src_host_name, dst_host_name);
949 surfxml_bufferstack = old_buff;
950 xbt_dict_free(&route_multi_table);
951 xbt_dynar_free(&route_multi_elements);
954 /* Cluster tag functions */
956 void parse_cluster(void)
958 static int AX_ptr = 0;
959 static int surfxml_bufferstack_size = 2048;
961 char* cluster_id = A_surfxml_cluster_id;
962 char* cluster_prefix = A_surfxml_cluster_prefix;
963 char* cluster_suffix = A_surfxml_cluster_suffix;
964 char* cluster_radical = A_surfxml_cluster_radical;
965 char* cluster_power = A_surfxml_cluster_power;
966 char* cluster_bw = A_surfxml_cluster_bw;
967 char* cluster_lat = A_surfxml_cluster_lat;
968 char* cluster_bb_bw = A_surfxml_cluster_bb_bw;
969 char* cluster_bb_lat = A_surfxml_cluster_bb_lat;
971 char* saved_buff = surfxml_bufferstack;
973 char * backbone_name;
975 surfxml_bufferstack = xbt_new0(char, surfxml_bufferstack_size);
978 SURFXML_BUFFER_SET(set_id, cluster_id);
979 SURFXML_BUFFER_SET(set_prefix, cluster_prefix);
980 SURFXML_BUFFER_SET(set_suffix, cluster_suffix);
981 SURFXML_BUFFER_SET(set_radical, cluster_radical);
983 SURFXML_START_TAG(set);
984 SURFXML_END_TAG(set);
987 SURFXML_BUFFER_SET(foreach_set_id, cluster_id);
989 SURFXML_START_TAG(foreach);
991 /* Make host for the foreach */
992 parse_change_cpu_data("$1", cluster_power, "1.0", "", "");
993 A_surfxml_host_state = A_surfxml_host_state_ON;
995 SURFXML_START_TAG(host);
996 SURFXML_END_TAG(host);
998 /* Make link for the foreach */
999 parse_change_link_data("$1", cluster_bw, "", cluster_lat, "", "");
1000 A_surfxml_link_state = A_surfxml_link_state_ON;
1001 A_surfxml_link_sharing_policy = A_surfxml_link_sharing_policy_SHARED;
1003 SURFXML_START_TAG(link);
1004 SURFXML_END_TAG(link);
1006 SURFXML_END_TAG(foreach);
1008 /* Make backbone link */
1009 backbone_name = bprintf("%s_bb", cluster_id);
1010 parse_change_link_data(backbone_name, cluster_bb_bw, "", cluster_bb_lat, "", "");
1011 A_surfxml_link_state = A_surfxml_link_state_ON;
1012 A_surfxml_link_sharing_policy = A_surfxml_link_sharing_policy_FATPIPE;
1014 SURFXML_START_TAG(link);
1015 SURFXML_END_TAG(link);
1017 /* Make route multi with the outside world, i.e. cluster->$* */
1018 SURFXML_BUFFER_SET(route_c_multi_src, cluster_id);
1019 SURFXML_BUFFER_SET(route_c_multi_dst, "$*");
1020 A_surfxml_route_c_multi_symmetric = A_surfxml_route_c_multi_symmetric_NO;
1021 A_surfxml_route_c_multi_action = A_surfxml_route_c_multi_action_OVERRIDE;
1023 SURFXML_START_TAG(route_c_multi);
1025 SURFXML_BUFFER_SET(link_c_ctn_id, "$src");
1027 SURFXML_START_TAG(link_c_ctn);
1028 SURFXML_END_TAG(link_c_ctn);
1030 SURFXML_END_TAG(route_c_multi);
1032 /* Make route multi between cluster hosts, i.e. cluster->cluster */
1033 SURFXML_BUFFER_SET(route_c_multi_src, cluster_id);
1034 SURFXML_BUFFER_SET(route_c_multi_dst, cluster_id);
1035 A_surfxml_route_c_multi_action = A_surfxml_route_c_multi_action_POSTPEND;
1036 A_surfxml_route_c_multi_symmetric = A_surfxml_route_c_multi_symmetric_NO;
1038 SURFXML_START_TAG(route_c_multi);
1040 SURFXML_BUFFER_SET(link_c_ctn_id, backbone_name);
1042 SURFXML_START_TAG(link_c_ctn);
1043 SURFXML_END_TAG(link_c_ctn);
1045 SURFXML_END_TAG(route_c_multi);
1049 free(surfxml_bufferstack);
1050 surfxml_bufferstack = saved_buff;
1053 /* Trace management functions */
1055 static double trace_periodicity = -1.0;
1056 static char* trace_file = NULL;
1057 static char* trace_id;
1059 void parse_trace_init(void)
1061 trace_id = strdup(A_surfxml_trace_id);
1062 trace_file = strdup(A_surfxml_trace_file);
1063 surf_parse_get_double(&trace_periodicity, A_surfxml_trace_periodicity);
1066 void parse_trace_finalize(void)
1069 if (!trace_file || strcmp(trace_file,"") != 0) {
1070 surf_parse_get_trace(&trace, trace_file);
1073 if (strcmp(surfxml_pcdata, "") == 0) trace = NULL;
1075 trace = tmgr_trace_new_from_string(trace_id, surfxml_pcdata, trace_periodicity);
1077 xbt_dict_set(traces_set_list, trace_id, (void *)trace, NULL);
1080 void parse_trace_c_connect(void)
1082 xbt_assert2(xbt_dict_get_or_null(traces_set_list, A_surfxml_trace_c_connect_trace),
1083 "Cannot connect trace %s to %s: trace unknown", A_surfxml_trace_c_connect_trace,A_surfxml_trace_c_connect_element);
1085 switch (A_surfxml_trace_c_connect_kind) {
1086 case A_surfxml_trace_c_connect_kind_HOST_AVAIL:
1087 xbt_dict_set(trace_connect_list_host_avail, A_surfxml_trace_c_connect_trace, xbt_strdup(A_surfxml_trace_c_connect_element), free);
1089 case A_surfxml_trace_c_connect_kind_POWER:
1090 xbt_dict_set(trace_connect_list_power, A_surfxml_trace_c_connect_trace, 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, A_surfxml_trace_c_connect_trace, xbt_strdup(A_surfxml_trace_c_connect_element), free);
1095 case A_surfxml_trace_c_connect_kind_BANDWIDTH:
1096 xbt_dict_set(trace_connect_list_bandwidth, A_surfxml_trace_c_connect_trace, xbt_strdup(A_surfxml_trace_c_connect_element), free);
1098 case A_surfxml_trace_c_connect_kind_LATENCY:
1099 xbt_dict_set(trace_connect_list_latency, A_surfxml_trace_c_connect_trace, xbt_strdup(A_surfxml_trace_c_connect_element), free);
1102 xbt_die("kind of trace unknown");
1106 /* Random tag functions */
1108 double get_cpu_power(const char *power)
1110 double power_scale = 0.0;
1113 random_data_t random = NULL;
1114 /* randomness is inserted like this: power="$rand(my_random)" */
1115 if (((p = strstr(power, "$rand(")) != NULL) && ((q = strstr(power, ")")) != NULL)) {
1117 generator = xbt_malloc(q - (p + 6) + 1);
1118 memcpy(generator, p + 6, q - (p + 6));
1119 generator[q - (p + 6)] = '\0';
1120 xbt_assert1((random = xbt_dict_get_or_null(random_data_list, generator)),
1121 "Random generator %s undefined", generator);
1122 power_scale = random_generate(random);
1126 surf_parse_get_double(&power_scale, power);
1131 int random_min, random_max, random_mean, random_std_deviation, random_generator;
1134 void init_randomness(void)
1136 random_id = A_surfxml_random_id;
1137 surf_parse_get_int(&random_min, A_surfxml_random_min);
1138 surf_parse_get_int(&random_max, A_surfxml_random_max);
1139 surf_parse_get_int(&random_mean, A_surfxml_random_mean);
1140 surf_parse_get_int(&random_std_deviation, A_surfxml_random_std_deviation);
1141 random_generator = A_surfxml_random_generator;
1144 void add_randomness(void)
1146 /* If needed aditional properties can be added by using the prop tag */
1147 random_data_t random = random_new(random_generator, 0, random_min, random_max, random_mean, random_std_deviation);
1148 xbt_dict_set(random_data_list, random_id, (void *)random, NULL);