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;
26 /* This buffer is used to store the original buffer before substituing it by out own buffer. Usefull for the foreach tag */
28 /* Stores the set name reffered to by the foreach tag */
29 static const char* foreach_set_name;
30 static xbt_dynar_t main_STag_surfxml_host_cb_list = NULL;
31 static xbt_dynar_t main_ETag_surfxml_host_cb_list = NULL;
32 static xbt_dynar_t main_STag_surfxml_link_cb_list = NULL;
33 static xbt_dynar_t main_ETag_surfxml_link_cb_list = NULL;
35 /* make sure these symbols are defined as strong ones in this file so that the linked can resolve them */
36 xbt_dynar_t STag_surfxml_platform_cb_list = NULL;
37 xbt_dynar_t ETag_surfxml_platform_cb_list = NULL;
38 xbt_dynar_t STag_surfxml_host_cb_list = NULL;
39 xbt_dynar_t ETag_surfxml_host_cb_list = NULL;
40 xbt_dynar_t STag_surfxml_router_cb_list = NULL;
41 xbt_dynar_t ETag_surfxml_router_cb_list = NULL;
42 xbt_dynar_t STag_surfxml_link_cb_list = NULL;
43 xbt_dynar_t ETag_surfxml_link_cb_list = NULL;
44 xbt_dynar_t STag_surfxml_route_cb_list = NULL;
45 xbt_dynar_t ETag_surfxml_route_cb_list = NULL;
46 xbt_dynar_t STag_surfxml_link_c_ctn_cb_list = NULL;
47 xbt_dynar_t ETag_surfxml_link_c_ctn_cb_list = NULL;
48 xbt_dynar_t STag_surfxml_process_cb_list = NULL;
49 xbt_dynar_t ETag_surfxml_process_cb_list = NULL;
50 xbt_dynar_t STag_surfxml_argument_cb_list = NULL;
51 xbt_dynar_t ETag_surfxml_argument_cb_list = NULL;
52 xbt_dynar_t STag_surfxml_prop_cb_list = NULL;
53 xbt_dynar_t ETag_surfxml_prop_cb_list = NULL;
54 xbt_dynar_t STag_surfxml_set_cb_list = NULL;
55 xbt_dynar_t ETag_surfxml_set_cb_list = NULL;
56 xbt_dynar_t STag_surfxml_foreach_cb_list = NULL;
57 xbt_dynar_t ETag_surfxml_foreach_cb_list = NULL;
58 xbt_dynar_t STag_surfxml_route_c_multi_cb_list = NULL;
59 xbt_dynar_t ETag_surfxml_route_c_multi_cb_list = NULL;
60 xbt_dynar_t STag_surfxml_cluster_cb_list = NULL;
61 xbt_dynar_t ETag_surfxml_cluster_cb_list = NULL;
62 xbt_dynar_t STag_surfxml_trace_cb_list = NULL;
63 xbt_dynar_t ETag_surfxml_trace_cb_list = NULL;
64 xbt_dynar_t STag_surfxml_trace_c_connect_cb_list = NULL;
65 xbt_dynar_t ETag_surfxml_trace_c_connect_cb_list = NULL;
66 xbt_dynar_t STag_surfxml_random_cb_list = NULL;
67 xbt_dynar_t ETag_surfxml_random_cb_list = NULL;
69 /* Stores the sets defined in the XML */
70 xbt_dict_t set_list = NULL;
72 xbt_dict_t current_property_set = NULL;
74 /* For the route:multi tag */
75 xbt_dict_t route_table = NULL;
76 xbt_dict_t route_multi_table = NULL;
77 xbt_dynar_t route_multi_elements = NULL;
78 xbt_dynar_t route_link_list = NULL;
79 xbt_dynar_t links = NULL;
80 xbt_dynar_t keys = NULL;
82 xbt_dict_t random_data_list = NULL;
84 static xbt_dynar_t surf_input_buffer_stack = NULL;
85 static xbt_dynar_t surf_file_to_parse_stack = NULL;
87 static XBT_INLINE void surfxml_call_cb_functions(xbt_dynar_t);
89 YY_BUFFER_STATE surf_input_buffer;
90 FILE *surf_file_to_parse = NULL;
92 static void convert_route_multi_to_routes(void);
94 void surf_parse_free_callbacks(void)
96 xbt_dynar_free(&STag_surfxml_platform_cb_list);
97 xbt_dynar_free(&ETag_surfxml_platform_cb_list);
98 xbt_dynar_free(&STag_surfxml_host_cb_list);
99 xbt_dynar_free(&ETag_surfxml_host_cb_list);
100 xbt_dynar_free(&STag_surfxml_router_cb_list);
101 xbt_dynar_free(&ETag_surfxml_router_cb_list);
102 xbt_dynar_free(&STag_surfxml_link_cb_list);
103 xbt_dynar_free(&ETag_surfxml_link_cb_list);
104 xbt_dynar_free(&STag_surfxml_route_cb_list);
105 xbt_dynar_free(&ETag_surfxml_route_cb_list);
106 xbt_dynar_free(&STag_surfxml_link_c_ctn_cb_list);
107 xbt_dynar_free(&ETag_surfxml_link_c_ctn_cb_list);
108 xbt_dynar_free(&STag_surfxml_process_cb_list);
109 xbt_dynar_free(&ETag_surfxml_process_cb_list);
110 xbt_dynar_free(&STag_surfxml_argument_cb_list);
111 xbt_dynar_free(&ETag_surfxml_argument_cb_list);
112 xbt_dynar_free(&STag_surfxml_prop_cb_list);
113 xbt_dynar_free(&ETag_surfxml_prop_cb_list);
114 xbt_dynar_free(&STag_surfxml_set_cb_list);
115 xbt_dynar_free(&ETag_surfxml_set_cb_list);
116 xbt_dynar_free(&STag_surfxml_foreach_cb_list);
117 xbt_dynar_free(&ETag_surfxml_foreach_cb_list);
118 xbt_dynar_free(&STag_surfxml_route_c_multi_cb_list);
119 xbt_dynar_free(&ETag_surfxml_route_c_multi_cb_list);
120 xbt_dynar_free(&STag_surfxml_cluster_cb_list);
121 xbt_dynar_free(&ETag_surfxml_cluster_cb_list);
122 xbt_dynar_free(&STag_surfxml_trace_cb_list);
123 xbt_dynar_free(&ETag_surfxml_trace_cb_list);
124 xbt_dynar_free(&STag_surfxml_trace_c_connect_cb_list);
125 xbt_dynar_free(&ETag_surfxml_trace_c_connect_cb_list);
126 xbt_dynar_free(&STag_surfxml_random_cb_list);
127 xbt_dynar_free(&ETag_surfxml_random_cb_list);
130 void surf_parse_reset_parser(void)
132 surf_parse_free_callbacks();
133 STag_surfxml_platform_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
134 ETag_surfxml_platform_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
135 STag_surfxml_host_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
136 ETag_surfxml_host_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
137 STag_surfxml_router_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
138 ETag_surfxml_router_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
139 STag_surfxml_link_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
140 ETag_surfxml_link_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
141 STag_surfxml_route_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
142 ETag_surfxml_route_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
143 STag_surfxml_link_c_ctn_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
144 ETag_surfxml_link_c_ctn_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
145 STag_surfxml_process_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
146 ETag_surfxml_process_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
147 STag_surfxml_argument_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
148 ETag_surfxml_argument_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
149 STag_surfxml_prop_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
150 ETag_surfxml_prop_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
151 STag_surfxml_set_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
152 ETag_surfxml_set_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
153 STag_surfxml_foreach_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
154 ETag_surfxml_foreach_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
155 STag_surfxml_route_c_multi_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
156 ETag_surfxml_route_c_multi_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
157 STag_surfxml_cluster_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
158 ETag_surfxml_cluster_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
159 STag_surfxml_trace_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
160 ETag_surfxml_trace_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
161 STag_surfxml_trace_c_connect_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
162 ETag_surfxml_trace_c_connect_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
163 STag_surfxml_random_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
164 ETag_surfxml_random_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
167 void STag_surfxml_include(void)
169 xbt_dynar_push(surf_input_buffer_stack, &surf_input_buffer);
170 xbt_dynar_push(surf_file_to_parse_stack, &surf_file_to_parse);
172 surf_file_to_parse = surf_fopen(A_surfxml_include_file, "r");
173 xbt_assert1((surf_file_to_parse), "Unable to open \"%s\"\n",
174 A_surfxml_include_file);
175 surf_input_buffer = surf_parse__create_buffer(surf_file_to_parse, 10);
176 surf_parse__switch_to_buffer(surf_input_buffer);
181 void ETag_surfxml_include(void)
185 surf_parse__delete_buffer(surf_input_buffer);
186 fclose(surf_file_to_parse);
187 xbt_dynar_pop(surf_file_to_parse_stack, &surf_file_to_parse);
188 xbt_dynar_pop(surf_input_buffer_stack, &surf_input_buffer);
191 void STag_surfxml_platform(void)
193 double version = 0.0;
195 sscanf(A_surfxml_platform_version, "%lg", &version);
197 xbt_assert0((version >= 1.0), "******* BIG FAT WARNING *********\n "
198 "You're using an ancient XML file. "
199 "Since SimGrid 3.1, units are Bytes, Flops, and seconds "
200 "instead of MBytes, MFlops and seconds. "
201 "A script (surfxml_update.pl) to help you convert your old "
203 "is available in the contrib/platform_generation directory "
204 "of the simgrid repository. Please check also out the "
205 "SURF section of the ChangeLog for the 3.1 version. "
206 "Last, do not forget to also update your values for "
207 "the calls to MSG_task_create (if any).");
208 xbt_assert0((version >= 2.0), "******* BIG FAT WARNING *********\n "
209 "You're using an old XML file. "
210 "A script (surfxml_update.pl) to help you convert your old "
212 "is available in the contrib/platform_generation directory "
213 "of the simgrid repository.");
215 if (set_list == NULL) set_list = xbt_dict_new();
217 surfxml_call_cb_functions(STag_surfxml_platform_cb_list);
221 void ETag_surfxml_platform(void)
223 convert_route_multi_to_routes();
225 surfxml_call_cb_functions(ETag_surfxml_platform_cb_list);
227 xbt_dict_free(&random_data_list);
228 xbt_dict_free(&set_list);
232 void STag_surfxml_host(void)
234 surfxml_call_cb_functions(STag_surfxml_host_cb_list);
237 void ETag_surfxml_host(void)
239 surfxml_call_cb_functions(ETag_surfxml_host_cb_list);
242 void STag_surfxml_router(void)
244 surfxml_call_cb_functions(STag_surfxml_router_cb_list);
247 void ETag_surfxml_router(void)
249 surfxml_call_cb_functions(ETag_surfxml_router_cb_list);
252 void STag_surfxml_link(void)
254 surfxml_call_cb_functions(STag_surfxml_link_cb_list);
257 void ETag_surfxml_link(void)
259 surfxml_call_cb_functions(ETag_surfxml_link_cb_list);
262 void STag_surfxml_route(void)
264 surfxml_call_cb_functions(STag_surfxml_route_cb_list);
267 void ETag_surfxml_route(void)
269 surfxml_call_cb_functions(ETag_surfxml_route_cb_list);
272 void STag_surfxml_link_c_ctn(void)
274 surfxml_call_cb_functions(STag_surfxml_link_c_ctn_cb_list);
277 void ETag_surfxml_link_c_ctn(void)
279 surfxml_call_cb_functions(ETag_surfxml_link_c_ctn_cb_list);
282 void STag_surfxml_process(void)
284 surfxml_call_cb_functions(STag_surfxml_process_cb_list);
287 void ETag_surfxml_process(void)
289 surfxml_call_cb_functions(ETag_surfxml_process_cb_list);
292 void STag_surfxml_argument(void)
294 surfxml_call_cb_functions(STag_surfxml_argument_cb_list);
297 void ETag_surfxml_argument(void)
299 surfxml_call_cb_functions(ETag_surfxml_argument_cb_list);
302 void STag_surfxml_prop(void)
304 surfxml_call_cb_functions(STag_surfxml_prop_cb_list);
306 void ETag_surfxml_prop(void)
308 surfxml_call_cb_functions(ETag_surfxml_prop_cb_list);
311 void STag_surfxml_set(void)
313 surfxml_call_cb_functions(STag_surfxml_set_cb_list);
316 void ETag_surfxml_set(void)
318 surfxml_call_cb_functions(ETag_surfxml_set_cb_list);
321 void STag_surfxml_foreach(void)
323 /* Save the current buffer */
324 old_buff = surfxml_bufferstack;
325 surfxml_call_cb_functions(STag_surfxml_foreach_cb_list);
328 void ETag_surfxml_foreach(void)
330 surfxml_call_cb_functions(ETag_surfxml_foreach_cb_list);
332 /* free the temporary dynar and restore original */
333 xbt_dynar_free(&STag_surfxml_host_cb_list);
334 xbt_dynar_free(&ETag_surfxml_host_cb_list);
336 STag_surfxml_host_cb_list = main_STag_surfxml_host_cb_list;
337 ETag_surfxml_host_cb_list = main_ETag_surfxml_host_cb_list;
339 /* free the temporary dynar and restore original */
340 xbt_dynar_free(&STag_surfxml_link_cb_list);
341 xbt_dynar_free(&ETag_surfxml_link_cb_list);
343 STag_surfxml_link_cb_list = main_STag_surfxml_link_cb_list;
344 ETag_surfxml_link_cb_list = main_ETag_surfxml_link_cb_list;
348 void STag_surfxml_route_c_multi(void)
350 surfxml_call_cb_functions(STag_surfxml_route_c_multi_cb_list);
353 void ETag_surfxml_route_c_multi(void)
355 surfxml_call_cb_functions(ETag_surfxml_route_c_multi_cb_list);
358 void STag_surfxml_cluster(void)
360 surfxml_call_cb_functions(STag_surfxml_cluster_cb_list);
363 void ETag_surfxml_cluster(void)
365 surfxml_call_cb_functions(ETag_surfxml_cluster_cb_list);
368 void STag_surfxml_trace(void)
370 surfxml_call_cb_functions(STag_surfxml_trace_cb_list);
373 void ETag_surfxml_trace(void)
375 surfxml_call_cb_functions(ETag_surfxml_trace_cb_list);
378 void STag_surfxml_trace_c_connect(void)
380 surfxml_call_cb_functions(STag_surfxml_trace_c_connect_cb_list);
383 void ETag_surfxml_trace_c_connect(void)
385 surfxml_call_cb_functions(ETag_surfxml_trace_c_connect_cb_list);
388 void STag_surfxml_random(void)
390 surfxml_call_cb_functions(STag_surfxml_random_cb_list);
393 void ETag_surfxml_random(void)
395 surfxml_call_cb_functions(ETag_surfxml_random_cb_list);
398 void surf_parse_open(const char *file)
400 static int warned = 0; /* warn only once */
404 ("Bypassing the XML parser since surf_parse_open received a NULL pointer. If it is not what you want, go fix your code.");
409 if (!surf_input_buffer_stack)
410 surf_input_buffer_stack = xbt_dynar_new(sizeof(YY_BUFFER_STATE), NULL);
411 if (!surf_file_to_parse_stack)
412 surf_file_to_parse_stack = xbt_dynar_new(sizeof(FILE *), NULL);
414 surf_file_to_parse = surf_fopen(file, "r");
415 xbt_assert1((surf_file_to_parse), "Unable to open \"%s\"\n", file);
416 surf_input_buffer = surf_parse__create_buffer(surf_file_to_parse, 10);
417 surf_parse__switch_to_buffer(surf_input_buffer);
418 surf_parse_lineno = 1;
421 void surf_parse_close(void)
423 if (surf_input_buffer_stack)
424 xbt_dynar_free(&surf_input_buffer_stack);
425 if (surf_file_to_parse_stack)
426 xbt_dynar_free(&surf_file_to_parse_stack);
428 if (surf_file_to_parse) {
429 surf_parse__delete_buffer(surf_input_buffer);
430 fclose(surf_file_to_parse);
435 static int _surf_parse(void)
437 return surf_parse_lex();
440 int_f_void_t surf_parse = _surf_parse;
442 void surf_parse_get_double(double *value, const char *string)
446 ret = sscanf(string, "%lg", value);
447 xbt_assert2((ret == 1), "Parse error line %d : %s not a number",
448 surf_parse_lineno, string);
451 void surf_parse_get_int(int *value, const char *string)
455 ret = sscanf(string, "%d", value);
456 xbt_assert2((ret == 1), "Parse error line %d : %s not a number",
457 surf_parse_lineno, string);
460 void surf_parse_get_trace(tmgr_trace_t * trace, const char *string)
462 if ((!string) || (strcmp(string, "") == 0))
465 *trace = tmgr_trace_new(string);
468 void parse_properties(void)
472 if(!current_property_set) current_property_set = xbt_dict_new();
474 value = xbt_strdup(A_surfxml_prop_value);
475 xbt_dict_set(current_property_set, A_surfxml_prop_id, value, free);
478 void free_string(void *d)
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);
500 xbt_dict_free(&route_table);
501 xbt_dynar_free(&route_link_list);
502 route_table = xbt_dict_new();
504 route_multi_table = xbt_dict_new();
505 route_multi_elements = xbt_dynar_new(sizeof(char*), NULL);
506 traces_set_list = xbt_dict_new();
507 traces_connect_list = xbt_dynar_new(sizeof(char*), NULL);
508 random_data_list = xbt_dict_new();
512 void parse_platform_file(const char* file)
514 surf_parse_open(file);
515 xbt_assert1((!(*surf_parse)()), "Parse error in %s", file);
519 /* Functions to bypass route, host and link tags. Used by the foreach and route:multi tags */
521 static void parse_make_temporary_route(const char *src, const char *dst, int action)
524 surfxml_bufferstack = xbt_new0(char, 2048);
526 A_surfxml_route_action = action;
527 SURFXML_BUFFER_SET(route_src, src);
528 SURFXML_BUFFER_SET(route_dst, dst);
531 static void parse_change_cpu_data(const char* hostName, const char* surfxml_host_power, const char* surfxml_host_availability,
532 const char* surfxml_host_availability_file, const char* surfxml_host_state_file)
535 surfxml_bufferstack = xbt_new0(char, 2048);
537 SURFXML_BUFFER_SET(host_id, hostName);
538 SURFXML_BUFFER_SET(host_power, surfxml_host_power /*hostPower*/);
539 SURFXML_BUFFER_SET(host_availability, surfxml_host_availability);
540 SURFXML_BUFFER_SET(host_availability_file, surfxml_host_availability_file);
541 SURFXML_BUFFER_SET(host_state_file, surfxml_host_state_file);
544 static void parse_change_link_data(const char* linkName, const char* surfxml_link_bandwidth, const char* surfxml_link_bandwidth_file,
545 const char* surfxml_link_latency, const char* surfxml_link_latency_file, const char* surfxml_link_state_file)
548 surfxml_bufferstack = xbt_new0(char, 2048);
550 SURFXML_BUFFER_SET(link_id, linkName);
551 SURFXML_BUFFER_SET(link_bandwidth, surfxml_link_bandwidth);
552 SURFXML_BUFFER_SET(link_bandwidth_file, surfxml_link_bandwidth_file);
553 SURFXML_BUFFER_SET(link_latency, surfxml_link_latency);
554 SURFXML_BUFFER_SET(link_latency_file, surfxml_link_latency_file);
555 SURFXML_BUFFER_SET(link_state_file, surfxml_link_state_file);
559 * \brief Restores the original surfxml buffer
561 static void parse_restore_original_buffer(void)
563 free(surfxml_bufferstack);
564 surfxml_bufferstack = old_buff;
567 /* Functions for the sets and foreach tags */
569 void parse_sets(void)
571 char *id, *suffix, *prefix, *radical;
573 xbt_dynar_t radical_ends;
574 xbt_dynar_t current_set;
578 id = xbt_strdup(A_surfxml_set_id);
579 prefix = xbt_strdup(A_surfxml_set_prefix);
580 suffix = xbt_strdup(A_surfxml_set_suffix);
581 radical = xbt_strdup(A_surfxml_set_radical);
583 xbt_assert1(!xbt_dict_get_or_null(set_list, id),
584 "Set '%s' declared several times in the platform file.",id);
585 radical_ends = xbt_str_split(radical, "-");
586 xbt_assert1((xbt_dynar_length(radical_ends)==2), "Radical must be in the form lvalue-rvalue! Provided value: %s", radical);
588 surf_parse_get_int(&start, xbt_dynar_get_as(radical_ends, 0, char*));
589 surf_parse_get_int(&end, xbt_dynar_get_as(radical_ends, 1, char*));
591 current_set = xbt_dynar_new(sizeof(char*), NULL);
594 for (i=start; i<end; i++) {
595 value = bprintf("%s%d%s", prefix, i, suffix);
596 xbt_dynar_push(current_set, &value);
599 xbt_dict_set(set_list, id, current_set, NULL);
602 static const char* surfxml_host_power;
603 static const char* surfxml_host_availability;
604 static const char* surfxml_host_availability_file;
605 static const char* surfxml_host_state_file;
607 static void parse_host_foreach(void)
609 surfxml_host_power = A_surfxml_host_power;
610 surfxml_host_availability = A_surfxml_host_availability;
611 surfxml_host_availability_file = A_surfxml_host_availability_file;
612 surfxml_host_state_file = A_surfxml_host_state_file;
615 static void finalize_host_foreach(void)
617 xbt_dynar_t names = NULL;
618 unsigned int cpt = 0;
620 xbt_dict_cursor_t cursor = NULL;
623 xbt_dict_t cluster_host_props = current_property_set;
625 xbt_assert1((names = xbt_dict_get_or_null(set_list, foreach_set_name)),
626 "Set name '%s' reffered by foreach tag not found.", foreach_set_name);
628 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);
631 /* foreach name in set call the main host callback */
632 xbt_dynar_foreach (names, cpt, name) {
633 parse_change_cpu_data(name, surfxml_host_power, surfxml_host_availability,
634 surfxml_host_availability_file, surfxml_host_state_file);
635 surfxml_call_cb_functions(main_STag_surfxml_host_cb_list);
637 xbt_dict_foreach(cluster_host_props,cursor,key,data) {
638 xbt_dict_set(current_property_set, xbt_strdup(key), xbt_strdup(data), free);
641 surfxml_call_cb_functions(main_ETag_surfxml_host_cb_list);
642 free(surfxml_bufferstack);
645 current_property_set = xbt_dict_new();
647 surfxml_bufferstack = old_buff;
651 static const char* surfxml_link_bandwidth;
652 static const char* surfxml_link_bandwidth_file;
653 static const char* surfxml_link_latency;
654 static const char* surfxml_link_latency_file;
655 static const char* surfxml_link_state_file;
657 static void parse_link_foreach(void)
659 surfxml_link_bandwidth = A_surfxml_link_bandwidth;
660 surfxml_link_bandwidth_file = A_surfxml_link_bandwidth_file;
661 surfxml_link_latency = A_surfxml_link_latency;
662 surfxml_link_latency_file = A_surfxml_link_latency_file;
663 surfxml_link_state_file = A_surfxml_link_state_file;
666 static void finalize_link_foreach(void)
668 xbt_dynar_t names = NULL;
669 unsigned int cpt = 0;
671 xbt_dict_cursor_t cursor = NULL;
674 xbt_dict_t cluster_link_props = current_property_set;
676 xbt_assert1((names = xbt_dict_get_or_null(set_list, foreach_set_name)),
677 "Set name '%s' reffered by foreach tag not found.", foreach_set_name);
679 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);
682 /* for each name in set call the main link callback */
683 xbt_dynar_foreach (names, cpt, name) {
684 parse_change_link_data(name, surfxml_link_bandwidth, surfxml_link_bandwidth_file,
685 surfxml_link_latency, surfxml_link_latency_file, surfxml_link_state_file);
686 surfxml_call_cb_functions(main_STag_surfxml_link_cb_list);
688 xbt_dict_foreach(cluster_link_props,cursor,key,data) {
689 xbt_dict_set(current_property_set, xbt_strdup(key), xbt_strdup(data), free);
692 surfxml_call_cb_functions(main_ETag_surfxml_link_cb_list);
693 free(surfxml_bufferstack);
697 current_property_set = xbt_dict_new();
699 surfxml_bufferstack = old_buff;
702 void parse_foreach(void)
704 /* save the host & link callbacks */
705 main_STag_surfxml_host_cb_list = STag_surfxml_host_cb_list;
706 main_ETag_surfxml_host_cb_list = ETag_surfxml_host_cb_list;
707 main_STag_surfxml_link_cb_list = STag_surfxml_link_cb_list;
708 main_ETag_surfxml_link_cb_list = ETag_surfxml_link_cb_list;
710 /* define host & link callbacks to be used only by the foreach tag */
711 STag_surfxml_host_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
712 ETag_surfxml_host_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
713 STag_surfxml_link_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
714 ETag_surfxml_link_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
716 surfxml_add_callback(STag_surfxml_host_cb_list, &parse_host_foreach);
717 surfxml_add_callback(ETag_surfxml_host_cb_list, &finalize_host_foreach);
718 surfxml_add_callback(STag_surfxml_link_cb_list, &parse_link_foreach);
719 surfxml_add_callback(ETag_surfxml_link_cb_list, &finalize_link_foreach);
722 foreach_set_name = xbt_strdup(A_surfxml_foreach_set_id);
725 /* Route:multi functions */
727 static int route_multi_size=0;
728 static char* src_name, *dst_name;
729 static int is_symmetric_route;
731 void parse_route_elem(void)
735 val = xbt_strdup(A_surfxml_link_c_ctn_id);
737 xbt_dynar_push(route_link_list, &val);
740 void parse_route_multi_set_endpoints(void)
742 src_name = xbt_strdup(A_surfxml_route_c_multi_src);
743 dst_name = xbt_strdup(A_surfxml_route_c_multi_dst);
744 route_action = A_surfxml_route_c_multi_action;
745 is_symmetric_route = A_surfxml_route_c_multi_symmetric;
748 route_link_list = xbt_dynar_new(sizeof(char *), &free_string);
751 static int contains(xbt_dynar_t list, const char* value)
755 xbt_dynar_foreach(list, cpt, val) {
756 if (strcmp(val, value) == 0)
763 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.
764 The decision is based upon the value of action specified in the xml route:multi attribute action
766 void manage_route(xbt_dict_t routing_table, const char *route_name, int action, int isMultiRoute)
772 /* get already existing list if it exists */
773 links = xbt_dict_get_or_null(routing_table, route_name);
774 DEBUG1("ROUTE: %s", route_name);
777 case A_surfxml_route_action_PREPEND: /* add existing links at the end; route_link_list + links */
778 xbt_dynar_foreach(links, cpt, value) {
779 xbt_dynar_push(route_link_list,&value);
782 case A_surfxml_route_action_POSTPEND: /* add existing links in front; links + route_link_list */
783 xbt_dynar_foreach(route_link_list, cpt, value) {
784 xbt_dynar_push(links,&value);
786 route_link_list = links;
788 case A_surfxml_route_action_OVERRIDE:
793 /* this is the final route; do not add if name is a set; add only if name is in set list */
795 xbt_dict_set(routing_table, route_name, route_link_list, NULL);
799 void parse_route_multi_set_route(void)
803 route_name = bprintf("%s#%s#%d#%d#%d", src_name, dst_name, route_action, is_symmetric_route, route_multi_size);
805 xbt_dynar_push(route_multi_elements, &route_name);
808 xbt_dict_set(route_multi_table, route_name, route_link_list, NULL);
809 /* add symmetric if it is the case */
810 if (is_symmetric_route == 1) {
811 char * symmetric_name = bprintf("%s#%s#%d#%d#%d", dst_name, src_name, route_action, !is_symmetric_route, route_multi_size);
813 xbt_dict_set(route_multi_table, symmetric_name, route_link_list, NULL);
814 xbt_dynar_push(route_multi_elements, &symmetric_name);
815 is_symmetric_route = 0;
821 static void add_multi_links(const char* src, const char* dst, xbt_dynar_t links, const char* src_name, const char* dst_name)
825 parse_make_temporary_route(src_name, dst_name, route_action);
826 surfxml_call_cb_functions(STag_surfxml_route_cb_list);
827 DEBUG2("\tADDING ROUTE: %s -> %s", src_name, dst_name);
828 /* Build link list */
829 xbt_dynar_foreach(links, cpt, value) {
830 if (strcmp(value, src) == 0)
831 val = xbt_strdup(src_name);
832 else if (strcmp(value, dst) == 0)
833 val = xbt_strdup(dst_name);
834 else if (strcmp(value, "$dst") == 0)
835 val = xbt_strdup(dst_name);
836 else if (strcmp(value, "$src") == 0)
837 val = xbt_strdup(src_name);
839 val = xbt_strdup(value);
840 DEBUG1("\t\tELEMENT: %s", val);
841 xbt_dynar_push(route_link_list, &val);
843 surfxml_call_cb_functions(ETag_surfxml_route_cb_list);
844 free(surfxml_bufferstack);
847 static void convert_route_multi_to_routes(void)
849 xbt_dict_cursor_t cursor_w;
851 unsigned int cpt, cpt2, cursor;
852 char *src_host_name, *dst_host_name, *key, *src, *dst, *val, *key_w, *data_w;
854 xbt_dynar_t src_names = NULL, dst_names = NULL, links;
856 if (!route_multi_elements) return;
858 xbt_dict_t set = cpu_set;
859 DEBUG1("%d", xbt_dict_length(workstation_set));
860 if (workstation_set != NULL && xbt_dict_length(workstation_set) > 0)
861 set = workstation_set;
864 old_buff = surfxml_bufferstack;
865 /* Get all routes in the exact order they were entered in the platform file */
866 xbt_dynar_foreach(route_multi_elements, cursor, key) {
867 /* Get links for the route */
868 links = (xbt_dynar_t)xbt_dict_get_or_null(route_multi_table, key);
869 keys = xbt_str_split_str(key, sep);
871 src = xbt_dynar_get_as(keys, 0, char*);
872 dst = xbt_dynar_get_as(keys, 1, char*);
873 route_action = atoi(xbt_dynar_get_as(keys, 2, char*));
874 symmetric = atoi(xbt_dynar_get_as(keys, 3, char*));
876 /* Create the dynar of src and dst hosts for the new routes */
877 /* NOTE: src and dst can be either set names or simple host names */
878 src_names = (xbt_dynar_t)xbt_dict_get_or_null(set_list, src);
879 dst_names = (xbt_dynar_t)xbt_dict_get_or_null(set_list, dst);
880 /* Add to dynar even if they are simple names */
881 if (src_names == NULL) {
882 src_names = xbt_dynar_new(sizeof(char *), &free_string);
883 val = xbt_strdup(src);
884 xbt_dynar_push(src_names, &val);
885 if (strcmp(val,"$*") != 0 && NULL == xbt_dict_get_or_null(set, val))
886 THROW3(unknown_error,0,"(In route:multi (%s -> %s) source %s does not exist (not a set or a host)", src, dst, src);
888 if (dst_names == NULL) {
889 dst_names = xbt_dynar_new(sizeof(char *), &free_string);
890 val = xbt_strdup(dst);
891 if (strcmp(val,"$*") != 0 && NULL == xbt_dict_get_or_null(set, val))
892 THROW3(unknown_error,0,"(In route:multi (%s -> %s) destination %s does not exist (not a set or a host)", src, dst, dst);
893 xbt_dynar_push(dst_names, &val);
896 /* Build the routes */
897 DEBUG2("ADDING MULTI ROUTE: %s -> %s", xbt_dynar_get_as(keys, 0, char*), xbt_dynar_get_as(keys, 1, char*));
898 xbt_dynar_foreach(src_names, cpt, src_host_name) {
899 xbt_dynar_foreach(dst_names, cpt2, dst_host_name) {
900 /* If dst is $* then set this route to have its dst point to all hosts */
901 if (strcmp(src_host_name,"$*") != 0 && strcmp(dst_host_name,"$*") == 0){
902 xbt_dict_foreach(set, cursor_w, key_w, data_w) {
903 //int n = xbt_dynar_member(src_names, (char*)key_w);
904 add_multi_links(src, dst, links, src_host_name, key_w);
907 /* If src is $* then set this route to have its dst point to all hosts */
908 if (strcmp(src_host_name,"$*") == 0 && strcmp(dst_host_name,"$*") != 0){
909 xbt_dict_foreach(set, cursor_w, key_w, data_w) {
910 // if (!symmetric || (symmetric && !contains(dst_names, key_w)))
911 add_multi_links(src, dst, links, key_w, dst_host_name);
914 /* if none of them are equal to $* */
915 if (strcmp(src_host_name,"$*") != 0 && strcmp(dst_host_name,"$*") != 0) {
916 add_multi_links(src, dst, links, src_host_name, dst_host_name);
921 surfxml_bufferstack = old_buff;
922 xbt_dict_free(&route_multi_table);
923 xbt_dynar_free(&route_multi_elements);
926 /* Cluster tag functions */
928 void parse_cluster(void)
930 static int AX_ptr = 0;
931 static int surfxml_bufferstack_size = 2048;
933 char* cluster_id = A_surfxml_cluster_id;
934 char* cluster_prefix = A_surfxml_cluster_prefix;
935 char* cluster_suffix = A_surfxml_cluster_suffix;
936 char* cluster_radical = A_surfxml_cluster_radical;
937 char* cluster_power = A_surfxml_cluster_power;
938 char* cluster_bw = A_surfxml_cluster_bw;
939 char* cluster_lat = A_surfxml_cluster_lat;
940 char* cluster_bb_bw = A_surfxml_cluster_bb_bw;
941 char* cluster_bb_lat = A_surfxml_cluster_bb_lat;
943 char* saved_buff = surfxml_bufferstack;
945 char * backbone_name;
947 surfxml_bufferstack = xbt_new0(char, surfxml_bufferstack_size);
950 SURFXML_BUFFER_SET(set_id, cluster_id);
951 SURFXML_BUFFER_SET(set_prefix, cluster_prefix);
952 SURFXML_BUFFER_SET(set_suffix, cluster_suffix);
953 SURFXML_BUFFER_SET(set_radical, cluster_radical);
955 SURFXML_START_TAG(set);
956 SURFXML_END_TAG(set);
959 SURFXML_BUFFER_SET(foreach_set_id, cluster_id);
961 SURFXML_START_TAG(foreach);
963 /* Make host for the foreach */
964 parse_change_cpu_data("$1", cluster_power, "1.0", "", "");
965 A_surfxml_host_state = A_surfxml_host_state_ON;
967 SURFXML_START_TAG(host);
968 SURFXML_END_TAG(host);
970 /* Make link for the foreach */
971 parse_change_link_data("$1", cluster_bw, "", cluster_lat, "", "");
972 A_surfxml_link_state = A_surfxml_link_state_ON;
973 A_surfxml_link_sharing_policy = A_surfxml_link_sharing_policy_SHARED;
975 SURFXML_START_TAG(link);
976 SURFXML_END_TAG(link);
978 SURFXML_END_TAG(foreach);
980 /* Make backbone link */
981 backbone_name = bprintf("%s_bb", cluster_id);
982 parse_change_link_data(backbone_name, cluster_bb_bw, "", cluster_bb_lat, "", "");
983 A_surfxml_link_state = A_surfxml_link_state_ON;
984 A_surfxml_link_sharing_policy = A_surfxml_link_sharing_policy_FATPIPE;
986 SURFXML_START_TAG(link);
987 SURFXML_END_TAG(link);
989 /* Make route multi with the outside world, i.e. cluster->$* */
990 SURFXML_BUFFER_SET(route_c_multi_src, cluster_id);
991 SURFXML_BUFFER_SET(route_c_multi_dst, "$*");
992 A_surfxml_route_c_multi_symmetric = A_surfxml_route_c_multi_symmetric_NO;
993 A_surfxml_route_c_multi_action = A_surfxml_route_c_multi_action_OVERRIDE;
995 SURFXML_START_TAG(route_c_multi);
997 SURFXML_BUFFER_SET(link_c_ctn_id, "$src");
999 SURFXML_START_TAG(link_c_ctn);
1000 SURFXML_END_TAG(link_c_ctn);
1002 SURFXML_END_TAG(route_c_multi);
1004 /* Make route multi between cluster hosts, i.e. cluster->cluster */
1005 SURFXML_BUFFER_SET(route_c_multi_src, cluster_id);
1006 SURFXML_BUFFER_SET(route_c_multi_dst, cluster_id);
1007 A_surfxml_route_c_multi_action = A_surfxml_route_c_multi_action_POSTPEND;
1008 A_surfxml_route_c_multi_symmetric = A_surfxml_route_c_multi_symmetric_NO;
1010 SURFXML_START_TAG(route_c_multi);
1012 SURFXML_BUFFER_SET(link_c_ctn_id, backbone_name);
1014 SURFXML_START_TAG(link_c_ctn);
1015 SURFXML_END_TAG(link_c_ctn);
1017 SURFXML_END_TAG(route_c_multi);
1021 free(surfxml_bufferstack);
1022 surfxml_bufferstack = saved_buff;
1025 /* Trace management functions */
1027 static double trace_periodicity = -1.0;
1028 static char* trace_file = NULL;
1029 static char* trace_id;
1031 void parse_trace_init(void)
1033 trace_id = strdup(A_surfxml_trace_id);
1034 trace_file = strdup(A_surfxml_trace_file);
1035 surf_parse_get_double(&trace_periodicity, A_surfxml_trace_periodicity);
1038 void parse_trace_finalize(void)
1041 if (!trace_file || strcmp(trace_file,"") != 0) {
1042 surf_parse_get_trace(&trace, trace_file);
1045 if (strcmp(surfxml_pcdata, "") == 0) trace = NULL;
1047 trace = tmgr_trace_new_from_string(trace_id, surfxml_pcdata, trace_periodicity);
1049 xbt_dict_set(traces_set_list, trace_id, (void *)trace, NULL);
1052 void parse_trace_c_connect(void)
1054 char* trace_connect;
1055 xbt_assert1(xbt_dict_get_or_null(traces_set_list, A_surfxml_trace_c_connect_trace_id),
1056 "Trace %s undefined", A_surfxml_trace_c_connect_trace_id);
1057 trace_connect = bprintf("%s#%d#%d#%s", A_surfxml_trace_c_connect_trace_id, A_surfxml_trace_c_connect_element,
1058 A_surfxml_trace_c_connect_kind, A_surfxml_trace_c_connect_connector_id);
1059 xbt_dynar_push(traces_connect_list, &trace_connect);
1062 /* Random tag functions */
1064 double get_cpu_power(const char *power)
1066 double power_scale = 0.0;
1069 random_data_t random = NULL;
1070 /* randomness is inserted like this: power="$rand(my_random)" */
1071 if (((p = strstr(power, "$rand(")) != NULL) && ((q = strstr(power, ")")) != NULL)) {
1073 generator = xbt_malloc(q - (p + 6) + 1);
1074 memcpy(generator, p + 6, q - (p + 6));
1075 generator[q - (p + 6)] = '\0';
1076 xbt_assert1((random = xbt_dict_get_or_null(random_data_list, generator)),
1077 "Random generator %s undefined", generator);
1078 power_scale = random_generate(random);
1082 surf_parse_get_double(&power_scale, power);
1087 int random_min, random_max, random_mean, random_std_deviation, random_generator;
1090 void init_randomness(void)
1092 random_id = A_surfxml_random_id;
1093 surf_parse_get_int(&random_min, A_surfxml_random_min);
1094 surf_parse_get_int(&random_max, A_surfxml_random_max);
1095 surf_parse_get_int(&random_mean, A_surfxml_random_mean);
1096 surf_parse_get_int(&random_std_deviation, A_surfxml_random_std_deviation);
1097 random_generator = A_surfxml_random_generator;
1100 void add_randomness(void)
1102 /* If needed aditional properties can be added by using the prop tag */
1103 random_data_t random = random_new(random_generator, random_min, random_max, random_mean, random_std_deviation);
1104 xbt_dict_set(random_data_list, random_id, (void *)random, NULL);