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);
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);
529 SURFXML_BUFFER_SET(route_impact_on_src, "0.0");
530 SURFXML_BUFFER_SET(route_impact_on_dst, "0.0");
531 SURFXML_BUFFER_SET(route_impact_on_src_with_other_recv, "0.0");
532 SURFXML_BUFFER_SET(route_impact_on_dst_with_other_send, "0.0");
535 static void parse_change_cpu_data(const char* hostName, const char* surfxml_host_power, const char* surfxml_host_availability,
536 const char* surfxml_host_availability_file, const char* surfxml_host_state_file,
537 const char* surfxml_host_interference_send, const char* surfxml_host_interference_recv,
538 const char* surfxml_host_interference_send_recv, const char* surfxml_host_max_outgoing_rate)
541 surfxml_bufferstack = xbt_new0(char, 2048);
543 SURFXML_BUFFER_SET(host_id, hostName);
544 SURFXML_BUFFER_SET(host_power, surfxml_host_power /*hostPower*/);
545 SURFXML_BUFFER_SET(host_availability, surfxml_host_availability);
546 SURFXML_BUFFER_SET(host_availability_file, surfxml_host_availability_file);
547 SURFXML_BUFFER_SET(host_state_file, surfxml_host_state_file);
548 SURFXML_BUFFER_SET(host_interference_send, surfxml_host_interference_send);
549 SURFXML_BUFFER_SET(host_interference_recv, surfxml_host_interference_recv);
550 SURFXML_BUFFER_SET(host_interference_send_recv, surfxml_host_interference_send_recv);
551 SURFXML_BUFFER_SET(host_max_outgoing_rate, surfxml_host_max_outgoing_rate);
554 static void parse_change_link_data(const char* linkName, const char* surfxml_link_bandwidth, const char* surfxml_link_bandwidth_file,
555 const char* surfxml_link_latency, const char* surfxml_link_latency_file, const char* surfxml_link_state_file)
558 surfxml_bufferstack = xbt_new0(char, 2048);
560 SURFXML_BUFFER_SET(link_id, linkName);
561 SURFXML_BUFFER_SET(link_bandwidth, surfxml_link_bandwidth);
562 SURFXML_BUFFER_SET(link_bandwidth_file, surfxml_link_bandwidth_file);
563 SURFXML_BUFFER_SET(link_latency, surfxml_link_latency);
564 SURFXML_BUFFER_SET(link_latency_file, surfxml_link_latency_file);
565 SURFXML_BUFFER_SET(link_state_file, surfxml_link_state_file);
569 * \brief Restores the original surfxml buffer
571 static void parse_restore_original_buffer(void)
573 free(surfxml_bufferstack);
574 surfxml_bufferstack = old_buff;
577 /* Functions for the sets and foreach tags */
579 void parse_sets(void)
581 char *id, *suffix, *prefix, *radical;
583 xbt_dynar_t radical_ends;
585 id = xbt_strdup(A_surfxml_set_id);
586 prefix = xbt_strdup(A_surfxml_set_prefix);
587 suffix = xbt_strdup(A_surfxml_set_suffix);
588 radical = xbt_strdup(A_surfxml_set_radical);
590 xbt_assert1(!xbt_dict_get_or_null(set_list, id),
591 "Set '%s' declared several times in the platform file.",id);
592 radical_ends = xbt_str_split(radical, "-");
593 xbt_assert1((xbt_dynar_length(radical_ends)==2), "Radical must be in the form lvalue-rvalue! Provided value: %s", radical);
595 surf_parse_get_int(&start, xbt_dynar_get_as(radical_ends, 0, char*));
596 surf_parse_get_int(&end, xbt_dynar_get_as(radical_ends, 1, char*));
598 xbt_dynar_t current_set = xbt_dynar_new(sizeof(char*), NULL);
602 for (i=start; i<end; i++) {
603 value = bprintf("%s%d%s", prefix, i, suffix);
604 xbt_dynar_push(current_set, &value);
607 xbt_dict_set(set_list, id, current_set, NULL);
610 static void parse_host_foreach(void)
612 xbt_dynar_t names = NULL;
613 unsigned int cpt = 0;
616 xbt_assert1((names = xbt_dict_get_or_null(set_list, foreach_set_name)),
617 "Set name '%s' reffered by foreach tag not found.", foreach_set_name);
619 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);
621 const char* surfxml_host_power = A_surfxml_host_power;
622 const char* surfxml_host_availability = A_surfxml_host_availability;
623 const char* surfxml_host_availability_file = A_surfxml_host_availability_file;
624 const char* surfxml_host_state_file = A_surfxml_host_state_file;
625 const char* surfxml_host_interference_send = A_surfxml_host_interference_send;
626 const char* surfxml_host_interference_recv = A_surfxml_host_interference_recv;
627 const char *surfxml_host_interference_send_recv = A_surfxml_host_interference_send_recv;
628 const char* surfxml_host_max_outgoing_rate = A_surfxml_host_max_outgoing_rate;
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_host_interference_send, surfxml_host_interference_recv,
635 surfxml_host_interference_send_recv, surfxml_host_max_outgoing_rate);
636 surfxml_call_cb_functions(main_STag_surfxml_host_cb_list);
637 surfxml_call_cb_functions(main_ETag_surfxml_host_cb_list);
638 free(surfxml_bufferstack);
641 surfxml_bufferstack = old_buff;
645 static void parse_link_foreach(void)
647 xbt_dynar_t names = NULL;
648 unsigned int cpt = 0;
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_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);
656 const char* surfxml_link_bandwidth = A_surfxml_link_bandwidth;
657 const char* surfxml_link_bandwidth_file = A_surfxml_link_bandwidth_file;
658 const char* surfxml_link_latency = A_surfxml_link_latency;
659 const char* surfxml_link_latency_file = A_surfxml_link_latency_file;
660 const char* surfxml_link_state_file = A_surfxml_link_state_file;
661 /* for each name in set call the main link callback */
662 xbt_dynar_foreach (names, cpt, name) {
663 parse_change_link_data(name, surfxml_link_bandwidth, surfxml_link_bandwidth_file,
664 surfxml_link_latency, surfxml_link_latency_file, surfxml_link_state_file);
665 surfxml_call_cb_functions(main_STag_surfxml_link_cb_list);
666 surfxml_call_cb_functions(main_ETag_surfxml_link_cb_list);
667 free(surfxml_bufferstack);
671 surfxml_bufferstack = old_buff;
674 void parse_foreach(void)
676 /* save the host & link callbacks */
677 main_STag_surfxml_host_cb_list = STag_surfxml_host_cb_list;
678 main_ETag_surfxml_host_cb_list = ETag_surfxml_host_cb_list;
679 main_STag_surfxml_link_cb_list = STag_surfxml_link_cb_list;
680 main_ETag_surfxml_link_cb_list = ETag_surfxml_link_cb_list;
682 /* define host & link callbacks to be used only by the foreach tag */
683 STag_surfxml_host_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
684 ETag_surfxml_host_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
685 STag_surfxml_link_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
686 ETag_surfxml_link_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
688 surfxml_add_callback(STag_surfxml_host_cb_list, &parse_host_foreach);
689 surfxml_add_callback(STag_surfxml_link_cb_list, &parse_link_foreach);
692 foreach_set_name = xbt_strdup(A_surfxml_foreach_set_id);
695 /* Route:multi functions */
697 static int route_multi_size=0;
698 static char* src_name, *dst_name;
699 static int is_symmetric_route;
701 void parse_route_elem(void)
705 val = xbt_strdup(A_surfxml_link_c_ctn_id);
707 xbt_dynar_push(route_link_list, &val);
710 void parse_route_multi_set_endpoints(void)
712 src_name = xbt_strdup(A_surfxml_route_c_multi_src);
713 dst_name = xbt_strdup(A_surfxml_route_c_multi_dst);
714 route_action = A_surfxml_route_c_multi_action;
715 is_symmetric_route = A_surfxml_route_c_multi_symmetric;
718 route_link_list = xbt_dynar_new(sizeof(char *), &free_string);
721 static int contains(xbt_dynar_t list, const char* value)
725 xbt_dynar_foreach(list, cpt, val) {
726 if (strcmp(val, value) == 0)
733 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.
734 The decision is based upon the value of action specified in the xml route:multi attribute action
736 void manage_route(xbt_dict_t routing_table, const char *route_name, int action, int isMultiRoute)
742 /* get already existing list if it exists */
743 links = xbt_dict_get_or_null(routing_table, route_name);
744 DEBUG1("ROUTE: %s", route_name);
747 case A_surfxml_route_c_multi_action_PREPEND: /* add existing links at the end; route_link_list + links */
748 xbt_dynar_foreach(links, cpt, value) {
749 xbt_dynar_push(route_link_list,&value);
752 case A_surfxml_route_c_multi_action_POSTPEND: /* add existing links in front; links + route_link_list */
753 xbt_dynar_foreach(route_link_list, cpt, value) {
754 xbt_dynar_push(links,&value);
756 route_link_list = links;
758 case A_surfxml_route_c_multi_action_OVERRIDE:
763 /* this is the final route; do not add if name is a set; add only if name is in set list */
765 xbt_dict_set(routing_table, route_name, route_link_list, NULL);
769 void parse_route_multi_set_route(void)
773 route_name = bprintf("%s#%s#%d#%d#%d", src_name, dst_name, route_action, is_symmetric_route, route_multi_size);
775 xbt_dynar_push(route_multi_elements, &route_name);
778 xbt_dict_set(route_multi_table, route_name, route_link_list, NULL);
779 /* add symmetric if it is the case */
780 if (is_symmetric_route == 1) {
781 char * symmetric_name = bprintf("%s#%s#%d#%d#%d", dst_name, src_name, route_action, !is_symmetric_route, route_multi_size);
783 xbt_dict_set(route_multi_table, symmetric_name, route_link_list, NULL);
784 xbt_dynar_push(route_multi_elements, &symmetric_name);
785 is_symmetric_route = 0;
791 static void add_multi_links(const char* src, const char* dst, xbt_dynar_t links, const char* src_name, const char* dst_name)
795 parse_make_temporary_route(src_name, dst_name, route_action);
796 surfxml_call_cb_functions(STag_surfxml_route_cb_list);
797 DEBUG2("\tADDING ROUTE: %s -> %s", src_name, dst_name);
798 /* Build link list */
799 xbt_dynar_foreach(links, cpt, value) {
800 if (strcmp(value, src) == 0)
801 val = xbt_strdup(src_name);
802 else if (strcmp(value, dst) == 0)
803 val = xbt_strdup(dst_name);
804 else if (strcmp(value, "$dst") == 0)
805 val = xbt_strdup(dst_name);
806 else if (strcmp(value, "$src") == 0)
807 val = xbt_strdup(src_name);
809 val = xbt_strdup(value);
810 DEBUG1("\t\tELEMENT: %s", val);
811 xbt_dynar_push(route_link_list, &val);
813 surfxml_call_cb_functions(ETag_surfxml_route_cb_list);
814 free(surfxml_bufferstack);
817 static void convert_route_multi_to_routes(void)
819 xbt_dict_cursor_t cursor_w;
821 unsigned int cpt, cpt2, cursor;
822 char *src_host_name, *dst_host_name, *key, *src, *dst, *val, *key_w, *data_w;
824 xbt_dynar_t src_names = NULL, dst_names = NULL, links;
826 if (!route_multi_elements) return;
828 old_buff = surfxml_bufferstack;
829 /* Get all routes in the exact order they were entered in the platform file */
830 xbt_dynar_foreach(route_multi_elements, cursor, key) {
831 /* Get links for the route */
832 links = (xbt_dynar_t)xbt_dict_get_or_null(route_multi_table, key);
833 keys = xbt_str_split_str(key, sep);
835 src = xbt_dynar_get_as(keys, 0, char*);
836 dst = xbt_dynar_get_as(keys, 1, char*);
837 route_action = atoi(xbt_dynar_get_as(keys, 2, char*));
838 symmetric = atoi(xbt_dynar_get_as(keys, 3, char*));
840 /* Create the dynar of src and dst hosts for the new routes */
841 /* NOTE: src and dst can be either set names or simple host names */
842 src_names = (xbt_dynar_t)xbt_dict_get_or_null(set_list, src);
843 dst_names = (xbt_dynar_t)xbt_dict_get_or_null(set_list, dst);
844 /* Add to dynar even if they are simple names */
845 if (src_names == NULL) {
846 src_names = xbt_dynar_new(sizeof(char *), &free_string);
847 val = xbt_strdup(src);
848 xbt_dynar_push(src_names, &val);
850 if (dst_names == NULL) {
851 dst_names = xbt_dynar_new(sizeof(char *), &free_string);
852 val = xbt_strdup(dst);
853 xbt_dynar_push(dst_names, &val);
855 /* Build the routes */
856 DEBUG2("ADDING MULTI ROUTE: %s -> %s", xbt_dynar_get_as(keys, 0, char*), xbt_dynar_get_as(keys, 1, char*));
857 xbt_dynar_foreach(src_names, cpt, src_host_name) {
858 xbt_dynar_foreach(dst_names, cpt2, dst_host_name) {
859 /* If dst is $* then set this route to have its dst point to all hosts */
860 if (strcmp(src_host_name,"$*") != 0 && strcmp(dst_host_name,"$*") == 0){
861 xbt_dict_foreach(workstation_set, cursor_w, key_w, data_w) {
862 //int n = xbt_dynar_member(src_names, (char*)key_w);
863 add_multi_links(src, dst, links, src_host_name, key_w);
866 /* If src is $* then set this route to have its dst point to all hosts */
867 if (strcmp(src_host_name,"$*") == 0 && strcmp(dst_host_name,"$*") != 0){
868 xbt_dict_foreach(workstation_set, cursor_w, key_w, data_w) {
869 // if (!symmetric || (symmetric && !contains(dst_names, key_w)))
870 add_multi_links(src, dst, links, key_w, dst_host_name);
873 /* if none of them are equal to $* */
874 if (strcmp(src_host_name,"$*") != 0 && strcmp(dst_host_name,"$*") != 0) {
875 add_multi_links(src, dst, links, src_host_name, dst_host_name);
880 surfxml_bufferstack = old_buff;
881 xbt_dict_free(&route_multi_table);
882 xbt_dynar_free(&route_multi_elements);
885 /* Cluster tag functions */
887 void parse_cluster(void)
889 static int AX_ptr = 0;
890 static int surfxml_bufferstack_size = 2048;
892 char* cluster_id = A_surfxml_cluster_id;
893 char* cluster_prefix = A_surfxml_cluster_prefix;
894 char* cluster_suffix = A_surfxml_cluster_suffix;
895 char* cluster_radical = A_surfxml_cluster_radical;
896 char* cluster_power = A_surfxml_cluster_power;
897 char* cluster_bw = A_surfxml_cluster_bw;
898 char* cluster_lat = A_surfxml_cluster_lat;
899 char* cluster_bb_bw = A_surfxml_cluster_bb_bw;
900 char* cluster_bb_lat = A_surfxml_cluster_bb_lat;
902 char* saved_buff = surfxml_bufferstack;
903 surfxml_bufferstack = xbt_new0(char, surfxml_bufferstack_size);
906 SURFXML_BUFFER_SET(set_id, cluster_id);
907 SURFXML_BUFFER_SET(set_prefix, cluster_prefix);
908 SURFXML_BUFFER_SET(set_suffix, cluster_suffix);
909 SURFXML_BUFFER_SET(set_radical, cluster_radical);
911 SURFXML_START_TAG(set);
912 SURFXML_END_TAG(set);
915 SURFXML_BUFFER_SET(foreach_set_id, cluster_id);
917 SURFXML_START_TAG(foreach);
919 /* Make host for the foreach */
920 parse_change_cpu_data("$1", cluster_power, "1.0", "", "", "1.0", "1.0", "1.0","-1.0");
921 A_surfxml_host_state = A_surfxml_host_state_ON;
923 SURFXML_START_TAG(host);
924 SURFXML_END_TAG(host);
926 /* Make link for the foreach */
927 parse_change_link_data("$1", cluster_bw, "", cluster_lat, "", "");
928 A_surfxml_link_state = A_surfxml_link_state_ON;
929 A_surfxml_link_sharing_policy = A_surfxml_link_sharing_policy_SHARED;
931 SURFXML_START_TAG(link);
932 SURFXML_END_TAG(link);
934 SURFXML_END_TAG(foreach);
936 /* Make backbone link */
937 char * backbone_name = bprintf("%s_bb", cluster_id);
938 parse_change_link_data(backbone_name, cluster_bb_bw, "", cluster_bb_lat, "", "");
939 A_surfxml_link_state = A_surfxml_link_state_ON;
940 A_surfxml_link_sharing_policy = A_surfxml_link_sharing_policy_FATPIPE;
942 SURFXML_START_TAG(link);
943 SURFXML_END_TAG(link);
945 /* Make route multi with the outside world, i.e. cluster->$* */
946 SURFXML_BUFFER_SET(route_c_multi_src, cluster_id);
947 SURFXML_BUFFER_SET(route_c_multi_dst, "$*");
948 A_surfxml_route_c_multi_symmetric = A_surfxml_route_c_multi_symmetric_NO;
949 A_surfxml_route_c_multi_action = A_surfxml_route_c_multi_action_OVERRIDE;
951 SURFXML_START_TAG(route_c_multi);
953 SURFXML_BUFFER_SET(link_c_ctn_id, "$src");
955 SURFXML_START_TAG(link_c_ctn);
956 SURFXML_END_TAG(link_c_ctn);
958 SURFXML_END_TAG(route_c_multi);
960 /* Make route multi between cluster hosts, i.e. cluster->cluster */
961 SURFXML_BUFFER_SET(route_c_multi_src, cluster_id);
962 SURFXML_BUFFER_SET(route_c_multi_dst, cluster_id);
963 A_surfxml_route_c_multi_action = A_surfxml_route_c_multi_action_POSTPEND;
964 A_surfxml_route_c_multi_symmetric = A_surfxml_route_c_multi_symmetric_NO;
966 SURFXML_START_TAG(route_c_multi);
968 SURFXML_BUFFER_SET(link_c_ctn_id, backbone_name);
970 SURFXML_START_TAG(link_c_ctn);
971 SURFXML_END_TAG(link_c_ctn);
973 SURFXML_END_TAG(route_c_multi);
977 free(surfxml_bufferstack);
978 surfxml_bufferstack = saved_buff;
981 /* Trace management functions */
983 static double trace_periodicity = -1.0;
984 static char* trace_file = NULL;
985 static char* trace_id;
987 void parse_trace_init(void)
989 trace_id = strdup(A_surfxml_trace_id);
990 trace_file = strdup(A_surfxml_trace_file);
991 surf_parse_get_double(&trace_periodicity, A_surfxml_trace_periodicity);
994 void parse_trace_finalize(void)
997 if (!trace_file || strcmp(trace_file,"") != 0) {
998 surf_parse_get_trace(&trace, trace_file);
1001 if (strcmp(surfxml_pcdata, "") == 0) trace = NULL;
1003 trace = tmgr_trace_new_from_string(trace_id, surfxml_pcdata, trace_periodicity);
1005 xbt_dict_set(traces_set_list, trace_id, (void *)trace, NULL);
1008 void parse_trace_c_connect(void)
1010 xbt_assert1(xbt_dict_get_or_null(traces_set_list, A_surfxml_trace_c_connect_trace_id),
1011 "Trace %s undefined", A_surfxml_trace_c_connect_trace_id);
1012 char* trace_connect = bprintf("%s#%d#%d#%s", A_surfxml_trace_c_connect_trace_id, A_surfxml_trace_c_connect_element,
1013 A_surfxml_trace_c_connect_kind, A_surfxml_trace_c_connect_connector_id);
1014 xbt_dynar_push(traces_connect_list, &trace_connect);
1017 /* Random tag functions */
1019 double get_cpu_power(const char *power)
1021 double power_scale = 0.0;
1024 random_data_t random = NULL;
1025 /* randomness is inserted like this: power="$rand(my_random)" */
1026 if (((p = strstr(power, "$rand(")) != NULL) && ((q = strstr(power, ")")) != NULL)) {
1028 generator = xbt_malloc(q - (p + 6) + 1);
1029 memcpy(generator, p + 6, q - (p + 6));
1030 generator[q - (p + 6)] = '\0';
1031 xbt_assert1((random = xbt_dict_get_or_null(random_data_list, generator)),
1032 "Random generator %s undefined", generator);
1033 power_scale = random_generate(random);
1037 surf_parse_get_double(&power_scale, power);
1042 int random_min, random_max, random_mean, random_std_deviation, random_generator;
1045 void init_randomness(void)
1047 random_id = A_surfxml_random_id;
1048 surf_parse_get_int(&random_min, A_surfxml_random_min);
1049 surf_parse_get_int(&random_max, A_surfxml_random_max);
1050 surf_parse_get_int(&random_mean, A_surfxml_random_mean);
1051 surf_parse_get_int(&random_std_deviation, A_surfxml_random_std_deviation);
1052 random_generator = A_surfxml_random_generator;
1055 void add_randomness(void)
1057 /* If needed aditional properties can be added by using the prop tag */
1058 random_data_t random = random_new(random_generator, random_min, random_max, random_mean, random_std_deviation);
1059 xbt_dict_set(random_data_list, random_id, (void *)random, NULL);