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. */
11 #include "surf/surfxml_parse_private.h"
12 #include "surf/surf_private.h"
14 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_parse, surf,
15 "Logging specific to the SURF parsing module");
20 /* Initialize the parsing globals */
22 xbt_dict_t traces_set_list = NULL;
23 xbt_dynar_t traces_connect_list = NULL;
25 /* This buffer is used to store the original buffer before substituing it by out own buffer. Usefull for the foreach tag */
27 /* Stores the set name reffered to by the foreach tag */
28 static const char* foreach_set_name;
29 static xbt_dynar_t main_STag_surfxml_host_cb_list = NULL;
30 static xbt_dynar_t main_ETag_surfxml_host_cb_list = NULL;
31 static xbt_dynar_t main_STag_surfxml_link_cb_list = NULL;
32 static xbt_dynar_t main_ETag_surfxml_link_cb_list = NULL;
34 /* make sure these symbols are defined as strong ones in this file so that the linked can resolve them */
35 xbt_dynar_t STag_surfxml_platform_cb_list = NULL;
36 xbt_dynar_t ETag_surfxml_platform_cb_list = NULL;
37 xbt_dynar_t STag_surfxml_host_cb_list = NULL;
38 xbt_dynar_t ETag_surfxml_host_cb_list = NULL;
39 xbt_dynar_t STag_surfxml_router_cb_list = NULL;
40 xbt_dynar_t ETag_surfxml_router_cb_list = NULL;
41 xbt_dynar_t STag_surfxml_link_cb_list = NULL;
42 xbt_dynar_t ETag_surfxml_link_cb_list = NULL;
43 xbt_dynar_t STag_surfxml_route_cb_list = NULL;
44 xbt_dynar_t ETag_surfxml_route_cb_list = NULL;
45 xbt_dynar_t STag_surfxml_link_c_ctn_cb_list = NULL;
46 xbt_dynar_t ETag_surfxml_link_c_ctn_cb_list = NULL;
47 xbt_dynar_t STag_surfxml_process_cb_list = NULL;
48 xbt_dynar_t ETag_surfxml_process_cb_list = NULL;
49 xbt_dynar_t STag_surfxml_argument_cb_list = NULL;
50 xbt_dynar_t ETag_surfxml_argument_cb_list = NULL;
51 xbt_dynar_t STag_surfxml_prop_cb_list = NULL;
52 xbt_dynar_t ETag_surfxml_prop_cb_list = NULL;
53 xbt_dynar_t STag_surfxml_set_cb_list = NULL;
54 xbt_dynar_t ETag_surfxml_set_cb_list = NULL;
55 xbt_dynar_t STag_surfxml_foreach_cb_list = NULL;
56 xbt_dynar_t ETag_surfxml_foreach_cb_list = NULL;
57 xbt_dynar_t STag_surfxml_route_c_multi_cb_list = NULL;
58 xbt_dynar_t ETag_surfxml_route_c_multi_cb_list = NULL;
59 xbt_dynar_t STag_surfxml_cluster_cb_list = NULL;
60 xbt_dynar_t ETag_surfxml_cluster_cb_list = NULL;
61 xbt_dynar_t STag_surfxml_trace_cb_list = NULL;
62 xbt_dynar_t ETag_surfxml_trace_cb_list = NULL;
63 xbt_dynar_t STag_surfxml_trace_c_connect_cb_list = NULL;
64 xbt_dynar_t ETag_surfxml_trace_c_connect_cb_list = NULL;
65 xbt_dynar_t STag_surfxml_random_cb_list = NULL;
66 xbt_dynar_t ETag_surfxml_random_cb_list = NULL;
68 /* Stores the sets defined in the XML */
69 xbt_dict_t set_list = NULL;
71 xbt_dict_t current_property_set = NULL;
73 /* For the route:multi tag */
74 xbt_dict_t route_table = NULL;
75 xbt_dict_t route_multi_table = NULL;
76 xbt_dynar_t route_multi_elements = NULL;
77 xbt_dynar_t route_link_list = NULL;
78 xbt_dynar_t links = NULL;
79 xbt_dynar_t keys = NULL;
81 xbt_dict_t random_data_list = NULL;
83 static xbt_dynar_t surf_input_buffer_stack = NULL;
84 static xbt_dynar_t surf_file_to_parse_stack = NULL;
86 static XBT_INLINE void surfxml_call_cb_functions(xbt_dynar_t);
88 YY_BUFFER_STATE surf_input_buffer;
89 FILE *surf_file_to_parse = NULL;
91 static void convert_route_multi_to_routes(void);
93 void surf_parse_free_callbacks(void)
95 xbt_dynar_free(&STag_surfxml_platform_cb_list);
96 xbt_dynar_free(&ETag_surfxml_platform_cb_list);
97 xbt_dynar_free(&STag_surfxml_host_cb_list);
98 xbt_dynar_free(&ETag_surfxml_host_cb_list);
99 xbt_dynar_free(&STag_surfxml_router_cb_list);
100 xbt_dynar_free(&ETag_surfxml_router_cb_list);
101 xbt_dynar_free(&STag_surfxml_link_cb_list);
102 xbt_dynar_free(&ETag_surfxml_link_cb_list);
103 xbt_dynar_free(&STag_surfxml_route_cb_list);
104 xbt_dynar_free(&ETag_surfxml_route_cb_list);
105 xbt_dynar_free(&STag_surfxml_link_c_ctn_cb_list);
106 xbt_dynar_free(&ETag_surfxml_link_c_ctn_cb_list);
107 xbt_dynar_free(&STag_surfxml_process_cb_list);
108 xbt_dynar_free(&ETag_surfxml_process_cb_list);
109 xbt_dynar_free(&STag_surfxml_argument_cb_list);
110 xbt_dynar_free(&ETag_surfxml_argument_cb_list);
111 xbt_dynar_free(&STag_surfxml_prop_cb_list);
112 xbt_dynar_free(&ETag_surfxml_prop_cb_list);
113 xbt_dynar_free(&STag_surfxml_set_cb_list);
114 xbt_dynar_free(&ETag_surfxml_set_cb_list);
115 xbt_dynar_free(&STag_surfxml_foreach_cb_list);
116 xbt_dynar_free(&ETag_surfxml_foreach_cb_list);
117 xbt_dynar_free(&STag_surfxml_route_c_multi_cb_list);
118 xbt_dynar_free(&ETag_surfxml_route_c_multi_cb_list);
119 xbt_dynar_free(&STag_surfxml_cluster_cb_list);
120 xbt_dynar_free(&ETag_surfxml_cluster_cb_list);
121 xbt_dynar_free(&STag_surfxml_trace_cb_list);
122 xbt_dynar_free(&ETag_surfxml_trace_cb_list);
123 xbt_dynar_free(&STag_surfxml_trace_c_connect_cb_list);
124 xbt_dynar_free(&ETag_surfxml_trace_c_connect_cb_list);
125 xbt_dynar_free(&STag_surfxml_random_cb_list);
126 xbt_dynar_free(&ETag_surfxml_random_cb_list);
129 void surf_parse_reset_parser(void)
131 surf_parse_free_callbacks();
132 STag_surfxml_platform_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
133 ETag_surfxml_platform_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
134 STag_surfxml_host_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
135 ETag_surfxml_host_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
136 STag_surfxml_router_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
137 ETag_surfxml_router_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
138 STag_surfxml_link_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
139 ETag_surfxml_link_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
140 STag_surfxml_route_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
141 ETag_surfxml_route_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
142 STag_surfxml_link_c_ctn_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
143 ETag_surfxml_link_c_ctn_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
144 STag_surfxml_process_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
145 ETag_surfxml_process_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
146 STag_surfxml_argument_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
147 ETag_surfxml_argument_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
148 STag_surfxml_prop_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
149 ETag_surfxml_prop_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
150 STag_surfxml_set_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
151 ETag_surfxml_set_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
152 STag_surfxml_foreach_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
153 ETag_surfxml_foreach_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
154 STag_surfxml_route_c_multi_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
155 ETag_surfxml_route_c_multi_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
156 STag_surfxml_cluster_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
157 ETag_surfxml_cluster_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
158 STag_surfxml_trace_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
159 ETag_surfxml_trace_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
160 STag_surfxml_trace_c_connect_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
161 ETag_surfxml_trace_c_connect_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
162 STag_surfxml_random_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
163 ETag_surfxml_random_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
166 void STag_surfxml_include(void)
168 xbt_dynar_push(surf_input_buffer_stack, &surf_input_buffer);
169 xbt_dynar_push(surf_file_to_parse_stack, &surf_file_to_parse);
171 surf_file_to_parse = surf_fopen(A_surfxml_include_file, "r");
172 xbt_assert1((surf_file_to_parse), "Unable to open \"%s\"\n",
173 A_surfxml_include_file);
174 surf_input_buffer = surf_parse__create_buffer(surf_file_to_parse, 10);
175 surf_parse__switch_to_buffer(surf_input_buffer);
180 void ETag_surfxml_include(void)
184 surf_parse__delete_buffer(surf_input_buffer);
185 fclose(surf_file_to_parse);
186 xbt_dynar_pop(surf_file_to_parse_stack, &surf_file_to_parse);
187 xbt_dynar_pop(surf_input_buffer_stack, &surf_input_buffer);
190 void STag_surfxml_platform(void)
192 double version = 0.0;
194 sscanf(A_surfxml_platform_version, "%lg", &version);
196 xbt_assert0((version >= 1.0), "******* BIG FAT WARNING *********\n "
197 "You're using an ancient XML file. "
198 "Since SimGrid 3.1, units are Bytes, Flops, and seconds "
199 "instead of MBytes, MFlops and seconds. "
200 "A script (surfxml_update.pl) to help you convert your old "
202 "is available in the contrib/platform_generation directory "
203 "of the simgrid repository. Please check also out the "
204 "SURF section of the ChangeLog for the 3.1 version. "
205 "Last, do not forget to also update your values for "
206 "the calls to MSG_task_create (if any).");
207 xbt_assert0((version >= 2.0), "******* BIG FAT WARNING *********\n "
208 "You're using an old XML file. "
209 "A script (surfxml_update.pl) to help you convert your old "
211 "is available in the contrib/platform_generation directory "
212 "of the simgrid repository.");
214 if (set_list == NULL) set_list = xbt_dict_new();
216 surfxml_call_cb_functions(STag_surfxml_platform_cb_list);
220 void ETag_surfxml_platform(void)
222 convert_route_multi_to_routes();
224 surfxml_call_cb_functions(ETag_surfxml_platform_cb_list);
226 xbt_dict_free(&random_data_list);
227 xbt_dict_free(&set_list);
231 void STag_surfxml_host(void)
233 surfxml_call_cb_functions(STag_surfxml_host_cb_list);
236 void ETag_surfxml_host(void)
238 surfxml_call_cb_functions(ETag_surfxml_host_cb_list);
241 void STag_surfxml_router(void)
243 surfxml_call_cb_functions(STag_surfxml_router_cb_list);
246 void ETag_surfxml_router(void)
248 surfxml_call_cb_functions(ETag_surfxml_router_cb_list);
251 void STag_surfxml_link(void)
253 surfxml_call_cb_functions(STag_surfxml_link_cb_list);
256 void ETag_surfxml_link(void)
258 surfxml_call_cb_functions(ETag_surfxml_link_cb_list);
261 void STag_surfxml_route(void)
263 surfxml_call_cb_functions(STag_surfxml_route_cb_list);
266 void ETag_surfxml_route(void)
268 surfxml_call_cb_functions(ETag_surfxml_route_cb_list);
271 void STag_surfxml_link_c_ctn(void)
273 surfxml_call_cb_functions(STag_surfxml_link_c_ctn_cb_list);
276 void ETag_surfxml_link_c_ctn(void)
278 surfxml_call_cb_functions(ETag_surfxml_link_c_ctn_cb_list);
281 void STag_surfxml_process(void)
283 surfxml_call_cb_functions(STag_surfxml_process_cb_list);
286 void ETag_surfxml_process(void)
288 surfxml_call_cb_functions(ETag_surfxml_process_cb_list);
291 void STag_surfxml_argument(void)
293 surfxml_call_cb_functions(STag_surfxml_argument_cb_list);
296 void ETag_surfxml_argument(void)
298 surfxml_call_cb_functions(ETag_surfxml_argument_cb_list);
301 void STag_surfxml_prop(void)
303 surfxml_call_cb_functions(STag_surfxml_prop_cb_list);
305 void ETag_surfxml_prop(void)
307 surfxml_call_cb_functions(ETag_surfxml_prop_cb_list);
310 void STag_surfxml_set(void)
312 surfxml_call_cb_functions(STag_surfxml_set_cb_list);
315 void ETag_surfxml_set(void)
317 surfxml_call_cb_functions(ETag_surfxml_set_cb_list);
320 void STag_surfxml_foreach(void)
322 /* Save the current buffer */
323 old_buff = surfxml_bufferstack;
324 surfxml_call_cb_functions(STag_surfxml_foreach_cb_list);
327 void ETag_surfxml_foreach(void)
329 surfxml_call_cb_functions(ETag_surfxml_foreach_cb_list);
331 /* free the temporary dynar and restore original */
332 xbt_dynar_free(&STag_surfxml_host_cb_list);
333 xbt_dynar_free(&ETag_surfxml_host_cb_list);
335 STag_surfxml_host_cb_list = main_STag_surfxml_host_cb_list;
336 ETag_surfxml_host_cb_list = main_ETag_surfxml_host_cb_list;
338 /* free the temporary dynar and restore original */
339 xbt_dynar_free(&STag_surfxml_link_cb_list);
340 xbt_dynar_free(&ETag_surfxml_link_cb_list);
342 STag_surfxml_link_cb_list = main_STag_surfxml_link_cb_list;
343 ETag_surfxml_link_cb_list = main_ETag_surfxml_link_cb_list;
347 void STag_surfxml_route_c_multi(void)
349 surfxml_call_cb_functions(STag_surfxml_route_c_multi_cb_list);
352 void ETag_surfxml_route_c_multi(void)
354 surfxml_call_cb_functions(ETag_surfxml_route_c_multi_cb_list);
357 void STag_surfxml_cluster(void)
359 surfxml_call_cb_functions(STag_surfxml_cluster_cb_list);
362 void ETag_surfxml_cluster(void)
364 surfxml_call_cb_functions(ETag_surfxml_cluster_cb_list);
367 void STag_surfxml_trace(void)
369 surfxml_call_cb_functions(STag_surfxml_trace_cb_list);
372 void ETag_surfxml_trace(void)
374 surfxml_call_cb_functions(ETag_surfxml_trace_cb_list);
377 void STag_surfxml_trace_c_connect(void)
379 surfxml_call_cb_functions(STag_surfxml_trace_c_connect_cb_list);
382 void ETag_surfxml_trace_c_connect(void)
384 surfxml_call_cb_functions(ETag_surfxml_trace_c_connect_cb_list);
387 void STag_surfxml_random(void)
389 surfxml_call_cb_functions(STag_surfxml_random_cb_list);
392 void ETag_surfxml_random(void)
394 surfxml_call_cb_functions(ETag_surfxml_random_cb_list);
397 void surf_parse_open(const char *file)
399 static int warned = 0; /* warn only once */
403 ("Bypassing the XML parser since surf_parse_open received a NULL pointer. If it is not what you want, go fix your code.");
408 if (!surf_input_buffer_stack)
409 surf_input_buffer_stack = xbt_dynar_new(sizeof(YY_BUFFER_STATE), NULL);
410 if (!surf_file_to_parse_stack)
411 surf_file_to_parse_stack = xbt_dynar_new(sizeof(FILE *), NULL);
413 surf_file_to_parse = surf_fopen(file, "r");
414 xbt_assert1((surf_file_to_parse), "Unable to open \"%s\"\n", file);
415 surf_input_buffer = surf_parse__create_buffer(surf_file_to_parse, 10);
416 surf_parse__switch_to_buffer(surf_input_buffer);
417 surf_parse_lineno = 1;
420 void surf_parse_close(void)
422 if (surf_input_buffer_stack)
423 xbt_dynar_free(&surf_input_buffer_stack);
424 if (surf_file_to_parse_stack)
425 xbt_dynar_free(&surf_file_to_parse_stack);
427 if (surf_file_to_parse) {
428 surf_parse__delete_buffer(surf_input_buffer);
429 fclose(surf_file_to_parse);
434 static int _surf_parse(void)
436 return surf_parse_lex();
439 int_f_void_t surf_parse = _surf_parse;
441 void surf_parse_get_double(double *value, const char *string)
445 ret = sscanf(string, "%lg", value);
446 xbt_assert2((ret == 1), "Parse error line %d : %s not a number",
447 surf_parse_lineno, string);
450 void surf_parse_get_int(int *value, const char *string)
454 ret = sscanf(string, "%d", value);
455 xbt_assert2((ret == 1), "Parse error line %d : %s not a number",
456 surf_parse_lineno, string);
459 void surf_parse_get_trace(tmgr_trace_t * trace, const char *string)
461 if ((!string) || (strcmp(string, "") == 0))
464 *trace = tmgr_trace_new(string);
467 void parse_properties(void)
471 if(!current_property_set) current_property_set = xbt_dict_new();
473 value = xbt_strdup(A_surfxml_prop_value);
474 xbt_dict_set(current_property_set, A_surfxml_prop_id, value, free);
477 void free_string(void *d)
482 void surfxml_add_callback(xbt_dynar_t cb_list, void_f_void_t function)
484 xbt_dynar_push(cb_list, &function);
487 static XBT_INLINE void surfxml_call_cb_functions(xbt_dynar_t cb_list)
489 unsigned int iterator;
491 xbt_dynar_foreach(cb_list, iterator, fun){
492 DEBUG2("call %p %p",fun,*fun);
499 xbt_dict_free(&route_table);
500 xbt_dynar_free(&route_link_list);
501 route_table = xbt_dict_new();
503 route_multi_table = xbt_dict_new();
504 route_multi_elements = xbt_dynar_new(sizeof(char*), NULL);
505 traces_set_list = xbt_dict_new();
506 traces_connect_list = xbt_dynar_new(sizeof(char*), NULL);
507 random_data_list = xbt_dict_new();
511 void parse_platform_file(const char* file)
513 surf_parse_open(file);
514 xbt_assert1((!(*surf_parse)()), "Parse error in %s", file);
518 /* Functions to bypass route, host and link tags. Used by the foreach and route:multi tags */
520 static void parse_make_temporary_route(const char *src, const char *dst, int action)
523 surfxml_bufferstack = xbt_new0(char, 2048);
525 A_surfxml_route_action = action;
526 SURFXML_BUFFER_SET(route_src, src);
527 SURFXML_BUFFER_SET(route_dst, dst);
530 static void parse_change_cpu_data(const char* hostName, const char* surfxml_host_power, const char* surfxml_host_availability,
531 const char* surfxml_host_availability_file, const char* surfxml_host_state_file)
534 surfxml_bufferstack = xbt_new0(char, 2048);
536 SURFXML_BUFFER_SET(host_id, hostName);
537 SURFXML_BUFFER_SET(host_power, surfxml_host_power /*hostPower*/);
538 SURFXML_BUFFER_SET(host_availability, surfxml_host_availability);
539 SURFXML_BUFFER_SET(host_availability_file, surfxml_host_availability_file);
540 SURFXML_BUFFER_SET(host_state_file, surfxml_host_state_file);
543 static void parse_change_link_data(const char* linkName, const char* surfxml_link_bandwidth, const char* surfxml_link_bandwidth_file,
544 const char* surfxml_link_latency, const char* surfxml_link_latency_file, const char* surfxml_link_state_file)
547 surfxml_bufferstack = xbt_new0(char, 2048);
549 SURFXML_BUFFER_SET(link_id, linkName);
550 SURFXML_BUFFER_SET(link_bandwidth, surfxml_link_bandwidth);
551 SURFXML_BUFFER_SET(link_bandwidth_file, surfxml_link_bandwidth_file);
552 SURFXML_BUFFER_SET(link_latency, surfxml_link_latency);
553 SURFXML_BUFFER_SET(link_latency_file, surfxml_link_latency_file);
554 SURFXML_BUFFER_SET(link_state_file, surfxml_link_state_file);
558 * \brief Restores the original surfxml buffer
560 static void parse_restore_original_buffer(void)
562 free(surfxml_bufferstack);
563 surfxml_bufferstack = old_buff;
566 /* Functions for the sets and foreach tags */
568 void parse_sets(void)
570 char *id, *suffix, *prefix, *radical;
572 xbt_dynar_t radical_ends;
573 xbt_dynar_t current_set;
577 id = xbt_strdup(A_surfxml_set_id);
578 prefix = xbt_strdup(A_surfxml_set_prefix);
579 suffix = xbt_strdup(A_surfxml_set_suffix);
580 radical = xbt_strdup(A_surfxml_set_radical);
582 xbt_assert1(!xbt_dict_get_or_null(set_list, id),
583 "Set '%s' declared several times in the platform file.",id);
584 radical_ends = xbt_str_split(radical, "-");
585 xbt_assert1((xbt_dynar_length(radical_ends)==2), "Radical must be in the form lvalue-rvalue! Provided value: %s", radical);
587 surf_parse_get_int(&start, xbt_dynar_get_as(radical_ends, 0, char*));
588 surf_parse_get_int(&end, xbt_dynar_get_as(radical_ends, 1, char*));
590 current_set = xbt_dynar_new(sizeof(char*), NULL);
593 for (i=start; i<end; i++) {
594 value = bprintf("%s%d%s", prefix, i, suffix);
595 xbt_dynar_push(current_set, &value);
598 xbt_dict_set(set_list, id, current_set, NULL);
601 static const char* surfxml_host_power;
602 static const char* surfxml_host_availability;
603 static const char* surfxml_host_availability_file;
604 static const char* surfxml_host_state_file;
606 static void parse_host_foreach(void)
608 surfxml_host_power = A_surfxml_host_power;
609 surfxml_host_availability = A_surfxml_host_availability;
610 surfxml_host_availability_file = A_surfxml_host_availability_file;
611 surfxml_host_state_file = A_surfxml_host_state_file;
614 static void finalize_host_foreach(void)
616 xbt_dynar_t names = NULL;
617 unsigned int cpt = 0;
619 xbt_dict_cursor_t cursor = NULL;
622 xbt_dict_t cluster_host_props = current_property_set;
624 xbt_assert1((names = xbt_dict_get_or_null(set_list, foreach_set_name)),
625 "Set name '%s' reffered by foreach tag not found.", foreach_set_name);
627 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);
630 /* foreach name in set call the main host callback */
631 xbt_dynar_foreach (names, cpt, name) {
632 parse_change_cpu_data(name, surfxml_host_power, surfxml_host_availability,
633 surfxml_host_availability_file, surfxml_host_state_file);
634 surfxml_call_cb_functions(main_STag_surfxml_host_cb_list);
636 xbt_dict_foreach(cluster_host_props,cursor,key,data) {
637 xbt_dict_set(current_property_set, xbt_strdup(key), xbt_strdup(data), free);
640 surfxml_call_cb_functions(main_ETag_surfxml_host_cb_list);
641 free(surfxml_bufferstack);
644 current_property_set = xbt_dict_new();
646 surfxml_bufferstack = old_buff;
650 static const char* surfxml_link_bandwidth;
651 static const char* surfxml_link_bandwidth_file;
652 static const char* surfxml_link_latency;
653 static const char* surfxml_link_latency_file;
654 static const char* surfxml_link_state_file;
656 static void parse_link_foreach(void)
658 surfxml_link_bandwidth = A_surfxml_link_bandwidth;
659 surfxml_link_bandwidth_file = A_surfxml_link_bandwidth_file;
660 surfxml_link_latency = A_surfxml_link_latency;
661 surfxml_link_latency_file = A_surfxml_link_latency_file;
662 surfxml_link_state_file = A_surfxml_link_state_file;
665 static void finalize_link_foreach(void)
667 xbt_dynar_t names = NULL;
668 unsigned int cpt = 0;
670 xbt_dict_cursor_t cursor = NULL;
673 xbt_dict_t cluster_link_props = current_property_set;
675 xbt_assert1((names = xbt_dict_get_or_null(set_list, foreach_set_name)),
676 "Set name '%s' reffered by foreach tag not found.", foreach_set_name);
678 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);
681 /* for each name in set call the main link callback */
682 xbt_dynar_foreach (names, cpt, name) {
683 parse_change_link_data(name, surfxml_link_bandwidth, surfxml_link_bandwidth_file,
684 surfxml_link_latency, surfxml_link_latency_file, surfxml_link_state_file);
685 surfxml_call_cb_functions(main_STag_surfxml_link_cb_list);
687 xbt_dict_foreach(cluster_link_props,cursor,key,data) {
688 xbt_dict_set(current_property_set, xbt_strdup(key), xbt_strdup(data), free);
691 surfxml_call_cb_functions(main_ETag_surfxml_link_cb_list);
692 free(surfxml_bufferstack);
696 current_property_set = xbt_dict_new();
698 surfxml_bufferstack = old_buff;
701 void parse_foreach(void)
703 /* save the host & link callbacks */
704 main_STag_surfxml_host_cb_list = STag_surfxml_host_cb_list;
705 main_ETag_surfxml_host_cb_list = ETag_surfxml_host_cb_list;
706 main_STag_surfxml_link_cb_list = STag_surfxml_link_cb_list;
707 main_ETag_surfxml_link_cb_list = ETag_surfxml_link_cb_list;
709 /* define host & link callbacks to be used only by the foreach tag */
710 STag_surfxml_host_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
711 ETag_surfxml_host_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
712 STag_surfxml_link_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
713 ETag_surfxml_link_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
715 surfxml_add_callback(STag_surfxml_host_cb_list, &parse_host_foreach);
716 surfxml_add_callback(ETag_surfxml_host_cb_list, &finalize_host_foreach);
717 surfxml_add_callback(STag_surfxml_link_cb_list, &parse_link_foreach);
718 surfxml_add_callback(ETag_surfxml_link_cb_list, &finalize_link_foreach);
721 foreach_set_name = xbt_strdup(A_surfxml_foreach_set_id);
724 /* Route:multi functions */
726 static int route_multi_size=0;
727 static char* src_name, *dst_name;
728 static int is_symmetric_route;
730 void parse_route_elem(void)
734 val = xbt_strdup(A_surfxml_link_c_ctn_id);
736 xbt_dynar_push(route_link_list, &val);
739 void parse_route_multi_set_endpoints(void)
741 src_name = xbt_strdup(A_surfxml_route_c_multi_src);
742 dst_name = xbt_strdup(A_surfxml_route_c_multi_dst);
743 route_action = A_surfxml_route_c_multi_action;
744 is_symmetric_route = A_surfxml_route_c_multi_symmetric;
747 route_link_list = xbt_dynar_new(sizeof(char *), &free_string);
750 static int contains(xbt_dynar_t list, const char* value)
754 xbt_dynar_foreach(list, cpt, val) {
755 if (strcmp(val, value) == 0)
762 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.
763 The decision is based upon the value of action specified in the xml route:multi attribute action
765 void manage_route(xbt_dict_t routing_table, const char *route_name, int action, int isMultiRoute)
771 /* get already existing list if it exists */
772 links = xbt_dict_get_or_null(routing_table, route_name);
773 DEBUG1("ROUTE: %s", route_name);
776 case A_surfxml_route_action_PREPEND: /* add existing links at the end; route_link_list + links */
777 xbt_dynar_foreach(links, cpt, value) {
778 xbt_dynar_push(route_link_list,&value);
781 case A_surfxml_route_action_POSTPEND: /* add existing links in front; links + route_link_list */
782 xbt_dynar_foreach(route_link_list, cpt, value) {
783 xbt_dynar_push(links,&value);
785 route_link_list = links;
787 case A_surfxml_route_action_OVERRIDE:
792 /* this is the final route; do not add if name is a set; add only if name is in set list */
794 xbt_dict_set(routing_table, route_name, route_link_list, NULL);
798 void parse_route_multi_set_route(void)
802 route_name = bprintf("%s#%s#%d#%d#%d", src_name, dst_name, route_action, is_symmetric_route, route_multi_size);
804 xbt_dynar_push(route_multi_elements, &route_name);
807 xbt_dict_set(route_multi_table, route_name, route_link_list, NULL);
808 /* add symmetric if it is the case */
809 if (is_symmetric_route == 1) {
810 char * symmetric_name = bprintf("%s#%s#%d#%d#%d", dst_name, src_name, route_action, !is_symmetric_route, route_multi_size);
812 xbt_dict_set(route_multi_table, symmetric_name, route_link_list, NULL);
813 xbt_dynar_push(route_multi_elements, &symmetric_name);
814 is_symmetric_route = 0;
820 static void add_multi_links(const char* src, const char* dst, xbt_dynar_t links, const char* src_name, const char* dst_name)
824 parse_make_temporary_route(src_name, dst_name, route_action);
825 surfxml_call_cb_functions(STag_surfxml_route_cb_list);
826 DEBUG2("\tADDING ROUTE: %s -> %s", src_name, dst_name);
827 /* Build link list */
828 xbt_dynar_foreach(links, cpt, value) {
829 if (strcmp(value, src) == 0)
830 val = xbt_strdup(src_name);
831 else if (strcmp(value, dst) == 0)
832 val = xbt_strdup(dst_name);
833 else if (strcmp(value, "$dst") == 0)
834 val = xbt_strdup(dst_name);
835 else if (strcmp(value, "$src") == 0)
836 val = xbt_strdup(src_name);
838 val = xbt_strdup(value);
839 DEBUG1("\t\tELEMENT: %s", val);
840 xbt_dynar_push(route_link_list, &val);
842 surfxml_call_cb_functions(ETag_surfxml_route_cb_list);
843 free(surfxml_bufferstack);
846 static void convert_route_multi_to_routes(void)
848 xbt_dict_cursor_t cursor_w;
850 unsigned int cpt, cpt2, cursor;
851 char *src_host_name, *dst_host_name, *key, *src, *dst, *val, *key_w, *data_w;
853 xbt_dynar_t src_names = NULL, dst_names = NULL, links;
855 if (!route_multi_elements) return;
857 xbt_dict_t set = cpu_set;
858 if (workstation_set != NULL)
859 set = workstation_set;
862 old_buff = surfxml_bufferstack;
863 /* Get all routes in the exact order they were entered in the platform file */
864 xbt_dynar_foreach(route_multi_elements, cursor, key) {
865 /* Get links for the route */
866 links = (xbt_dynar_t)xbt_dict_get_or_null(route_multi_table, key);
867 keys = xbt_str_split_str(key, sep);
869 src = xbt_dynar_get_as(keys, 0, char*);
870 dst = xbt_dynar_get_as(keys, 1, char*);
871 route_action = atoi(xbt_dynar_get_as(keys, 2, char*));
872 symmetric = atoi(xbt_dynar_get_as(keys, 3, char*));
874 /* Create the dynar of src and dst hosts for the new routes */
875 /* NOTE: src and dst can be either set names or simple host names */
876 src_names = (xbt_dynar_t)xbt_dict_get_or_null(set_list, src);
877 dst_names = (xbt_dynar_t)xbt_dict_get_or_null(set_list, dst);
878 /* Add to dynar even if they are simple names */
879 if (src_names == NULL) {
880 src_names = xbt_dynar_new(sizeof(char *), &free_string);
881 val = xbt_strdup(src);
882 xbt_dynar_push(src_names, &val);
884 if (dst_names == NULL) {
885 dst_names = xbt_dynar_new(sizeof(char *), &free_string);
886 val = xbt_strdup(dst);
887 xbt_dynar_push(dst_names, &val);
889 /* Build the routes */
890 DEBUG2("ADDING MULTI ROUTE: %s -> %s", xbt_dynar_get_as(keys, 0, char*), xbt_dynar_get_as(keys, 1, char*));
891 xbt_dynar_foreach(src_names, cpt, src_host_name) {
892 xbt_dynar_foreach(dst_names, cpt2, dst_host_name) {
893 /* If dst is $* then set this route to have its dst point to all hosts */
894 if (strcmp(src_host_name,"$*") != 0 && strcmp(dst_host_name,"$*") == 0){
895 xbt_dict_foreach(set, cursor_w, key_w, data_w) {
896 //int n = xbt_dynar_member(src_names, (char*)key_w);
897 add_multi_links(src, dst, links, src_host_name, key_w);
900 /* If src 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 // if (!symmetric || (symmetric && !contains(dst_names, key_w)))
904 add_multi_links(src, dst, links, key_w, dst_host_name);
907 /* if none of them are equal to $* */
908 if (strcmp(src_host_name,"$*") != 0 && strcmp(dst_host_name,"$*") != 0) {
909 add_multi_links(src, dst, links, src_host_name, dst_host_name);
914 surfxml_bufferstack = old_buff;
915 xbt_dict_free(&route_multi_table);
916 xbt_dynar_free(&route_multi_elements);
919 /* Cluster tag functions */
921 void parse_cluster(void)
923 static int AX_ptr = 0;
924 static int surfxml_bufferstack_size = 2048;
926 char* cluster_id = A_surfxml_cluster_id;
927 char* cluster_prefix = A_surfxml_cluster_prefix;
928 char* cluster_suffix = A_surfxml_cluster_suffix;
929 char* cluster_radical = A_surfxml_cluster_radical;
930 char* cluster_power = A_surfxml_cluster_power;
931 char* cluster_bw = A_surfxml_cluster_bw;
932 char* cluster_lat = A_surfxml_cluster_lat;
933 char* cluster_bb_bw = A_surfxml_cluster_bb_bw;
934 char* cluster_bb_lat = A_surfxml_cluster_bb_lat;
936 char* saved_buff = surfxml_bufferstack;
938 char * backbone_name;
940 surfxml_bufferstack = xbt_new0(char, surfxml_bufferstack_size);
943 SURFXML_BUFFER_SET(set_id, cluster_id);
944 SURFXML_BUFFER_SET(set_prefix, cluster_prefix);
945 SURFXML_BUFFER_SET(set_suffix, cluster_suffix);
946 SURFXML_BUFFER_SET(set_radical, cluster_radical);
948 SURFXML_START_TAG(set);
949 SURFXML_END_TAG(set);
952 SURFXML_BUFFER_SET(foreach_set_id, cluster_id);
954 SURFXML_START_TAG(foreach);
956 /* Make host for the foreach */
957 parse_change_cpu_data("$1", cluster_power, "1.0", "", "");
958 A_surfxml_host_state = A_surfxml_host_state_ON;
960 SURFXML_START_TAG(host);
961 SURFXML_END_TAG(host);
963 /* Make link for the foreach */
964 parse_change_link_data("$1", cluster_bw, "", cluster_lat, "", "");
965 A_surfxml_link_state = A_surfxml_link_state_ON;
966 A_surfxml_link_sharing_policy = A_surfxml_link_sharing_policy_SHARED;
968 SURFXML_START_TAG(link);
969 SURFXML_END_TAG(link);
971 SURFXML_END_TAG(foreach);
973 /* Make backbone link */
974 backbone_name = bprintf("%s_bb", cluster_id);
975 parse_change_link_data(backbone_name, cluster_bb_bw, "", cluster_bb_lat, "", "");
976 A_surfxml_link_state = A_surfxml_link_state_ON;
977 A_surfxml_link_sharing_policy = A_surfxml_link_sharing_policy_FATPIPE;
979 SURFXML_START_TAG(link);
980 SURFXML_END_TAG(link);
982 /* Make route multi with the outside world, i.e. cluster->$* */
983 SURFXML_BUFFER_SET(route_c_multi_src, cluster_id);
984 SURFXML_BUFFER_SET(route_c_multi_dst, "$*");
985 A_surfxml_route_c_multi_symmetric = A_surfxml_route_c_multi_symmetric_NO;
986 A_surfxml_route_c_multi_action = A_surfxml_route_c_multi_action_OVERRIDE;
988 SURFXML_START_TAG(route_c_multi);
990 SURFXML_BUFFER_SET(link_c_ctn_id, "$src");
992 SURFXML_START_TAG(link_c_ctn);
993 SURFXML_END_TAG(link_c_ctn);
995 SURFXML_END_TAG(route_c_multi);
997 /* Make route multi between cluster hosts, i.e. cluster->cluster */
998 SURFXML_BUFFER_SET(route_c_multi_src, cluster_id);
999 SURFXML_BUFFER_SET(route_c_multi_dst, cluster_id);
1000 A_surfxml_route_c_multi_action = A_surfxml_route_c_multi_action_POSTPEND;
1001 A_surfxml_route_c_multi_symmetric = A_surfxml_route_c_multi_symmetric_NO;
1003 SURFXML_START_TAG(route_c_multi);
1005 SURFXML_BUFFER_SET(link_c_ctn_id, backbone_name);
1007 SURFXML_START_TAG(link_c_ctn);
1008 SURFXML_END_TAG(link_c_ctn);
1010 SURFXML_END_TAG(route_c_multi);
1014 free(surfxml_bufferstack);
1015 surfxml_bufferstack = saved_buff;
1018 /* Trace management functions */
1020 static double trace_periodicity = -1.0;
1021 static char* trace_file = NULL;
1022 static char* trace_id;
1024 void parse_trace_init(void)
1026 trace_id = strdup(A_surfxml_trace_id);
1027 trace_file = strdup(A_surfxml_trace_file);
1028 surf_parse_get_double(&trace_periodicity, A_surfxml_trace_periodicity);
1031 void parse_trace_finalize(void)
1034 if (!trace_file || strcmp(trace_file,"") != 0) {
1035 surf_parse_get_trace(&trace, trace_file);
1038 if (strcmp(surfxml_pcdata, "") == 0) trace = NULL;
1040 trace = tmgr_trace_new_from_string(trace_id, surfxml_pcdata, trace_periodicity);
1042 xbt_dict_set(traces_set_list, trace_id, (void *)trace, NULL);
1045 void parse_trace_c_connect(void)
1047 char* trace_connect;
1048 xbt_assert1(xbt_dict_get_or_null(traces_set_list, A_surfxml_trace_c_connect_trace_id),
1049 "Trace %s undefined", A_surfxml_trace_c_connect_trace_id);
1050 trace_connect = bprintf("%s#%d#%d#%s", A_surfxml_trace_c_connect_trace_id, A_surfxml_trace_c_connect_element,
1051 A_surfxml_trace_c_connect_kind, A_surfxml_trace_c_connect_connector_id);
1052 xbt_dynar_push(traces_connect_list, &trace_connect);
1055 /* Random tag functions */
1057 double get_cpu_power(const char *power)
1059 double power_scale = 0.0;
1062 random_data_t random = NULL;
1063 /* randomness is inserted like this: power="$rand(my_random)" */
1064 if (((p = strstr(power, "$rand(")) != NULL) && ((q = strstr(power, ")")) != NULL)) {
1066 generator = xbt_malloc(q - (p + 6) + 1);
1067 memcpy(generator, p + 6, q - (p + 6));
1068 generator[q - (p + 6)] = '\0';
1069 xbt_assert1((random = xbt_dict_get_or_null(random_data_list, generator)),
1070 "Random generator %s undefined", generator);
1071 power_scale = random_generate(random);
1075 surf_parse_get_double(&power_scale, power);
1080 int random_min, random_max, random_mean, random_std_deviation, random_generator;
1083 void init_randomness(void)
1085 random_id = A_surfxml_random_id;
1086 surf_parse_get_int(&random_min, A_surfxml_random_min);
1087 surf_parse_get_int(&random_max, A_surfxml_random_max);
1088 surf_parse_get_int(&random_mean, A_surfxml_random_mean);
1089 surf_parse_get_int(&random_std_deviation, A_surfxml_random_std_deviation);
1090 random_generator = A_surfxml_random_generator;
1093 void add_randomness(void)
1095 /* If needed aditional properties can be added by using the prop tag */
1096 random_data_t random = random_new(random_generator, random_min, random_max, random_mean, random_std_deviation);
1097 xbt_dict_set(random_data_list, random_id, (void *)random, NULL);