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 /* This buffer is used to store the original buffer before substituing it by out own buffer. Usefull for the foreach tag */
22 /* Stores the set name reffered to by the foreach tag */
23 static const char* foreach_set_name;
24 static xbt_dynar_t main_STag_surfxml_host_cb_list = NULL;
25 static xbt_dynar_t main_ETag_surfxml_host_cb_list = NULL;
26 static xbt_dynar_t main_STag_surfxml_link_cb_list = NULL;
27 static xbt_dynar_t main_ETag_surfxml_link_cb_list = NULL;
29 /* make sure these symbols are defined as strong ones in this file so that the linked can resolve them */
30 xbt_dynar_t STag_surfxml_platform_cb_list = NULL;
31 xbt_dynar_t ETag_surfxml_platform_cb_list = NULL;
32 xbt_dynar_t STag_surfxml_host_cb_list = NULL;
33 xbt_dynar_t ETag_surfxml_host_cb_list = NULL;
34 xbt_dynar_t STag_surfxml_router_cb_list = NULL;
35 xbt_dynar_t ETag_surfxml_router_cb_list = NULL;
36 xbt_dynar_t STag_surfxml_link_cb_list = NULL;
37 xbt_dynar_t ETag_surfxml_link_cb_list = NULL;
38 xbt_dynar_t STag_surfxml_route_cb_list = NULL;
39 xbt_dynar_t ETag_surfxml_route_cb_list = NULL;
40 xbt_dynar_t STag_surfxml_link_c_ctn_cb_list = NULL;
41 xbt_dynar_t ETag_surfxml_link_c_ctn_cb_list = NULL;
42 xbt_dynar_t STag_surfxml_process_cb_list = NULL;
43 xbt_dynar_t ETag_surfxml_process_cb_list = NULL;
44 xbt_dynar_t STag_surfxml_argument_cb_list = NULL;
45 xbt_dynar_t ETag_surfxml_argument_cb_list = NULL;
46 xbt_dynar_t STag_surfxml_prop_cb_list = NULL;
47 xbt_dynar_t ETag_surfxml_prop_cb_list = NULL;
48 xbt_dynar_t STag_surfxml_set_cb_list = NULL;
49 xbt_dynar_t ETag_surfxml_set_cb_list = NULL;
50 xbt_dynar_t STag_surfxml_foreach_cb_list = NULL;
51 xbt_dynar_t ETag_surfxml_foreach_cb_list = NULL;
52 xbt_dynar_t STag_surfxml_route_c_multi_cb_list = NULL;
53 xbt_dynar_t ETag_surfxml_route_c_multi_cb_list = NULL;
54 xbt_dynar_t STag_surfxml_cluster_cb_list = NULL;
55 xbt_dynar_t ETag_surfxml_cluster_cb_list = NULL;
56 xbt_dynar_t STag_surfxml_trace_cb_list = NULL;
57 xbt_dynar_t ETag_surfxml_trace_cb_list = NULL;
58 xbt_dynar_t STag_surfxml_trace_c_connect_cb_list = NULL;
59 xbt_dynar_t ETag_surfxml_trace_c_connect_cb_list = NULL;
60 xbt_dynar_t STag_surfxml_random_cb_list = NULL;
61 xbt_dynar_t ETag_surfxml_random_cb_list = NULL;
63 /* Stores the sets defined in the XML */
64 xbt_dict_t set_list = NULL;
66 xbt_dict_t current_property_set = NULL;
68 /* For the route:multi tag */
69 xbt_dict_t route_table = NULL;
70 xbt_dict_t route_multi_table = NULL;
71 xbt_dynar_t route_multi_elements = NULL;
72 xbt_dynar_t route_link_list = NULL;
73 xbt_dynar_t links = NULL;
74 xbt_dynar_t keys = NULL;
76 xbt_dict_t random_data_list = NULL;
78 static xbt_dynar_t surf_input_buffer_stack = NULL;
79 static xbt_dynar_t surf_file_to_parse_stack = NULL;
81 static XBT_INLINE void surfxml_call_cb_functions(xbt_dynar_t);
83 YY_BUFFER_STATE surf_input_buffer;
84 FILE *surf_file_to_parse = NULL;
86 static void convert_route_multi_to_routes(void);
88 void surf_parse_free_callbacks(void)
90 xbt_dynar_free(&STag_surfxml_platform_cb_list);
91 xbt_dynar_free(&ETag_surfxml_platform_cb_list);
92 xbt_dynar_free(&STag_surfxml_host_cb_list);
93 xbt_dynar_free(&ETag_surfxml_host_cb_list);
94 xbt_dynar_free(&STag_surfxml_router_cb_list);
95 xbt_dynar_free(&ETag_surfxml_router_cb_list);
96 xbt_dynar_free(&STag_surfxml_link_cb_list);
97 xbt_dynar_free(&ETag_surfxml_link_cb_list);
98 xbt_dynar_free(&STag_surfxml_route_cb_list);
99 xbt_dynar_free(&ETag_surfxml_route_cb_list);
100 xbt_dynar_free(&STag_surfxml_link_c_ctn_cb_list);
101 xbt_dynar_free(&ETag_surfxml_link_c_ctn_cb_list);
102 xbt_dynar_free(&STag_surfxml_process_cb_list);
103 xbt_dynar_free(&ETag_surfxml_process_cb_list);
104 xbt_dynar_free(&STag_surfxml_argument_cb_list);
105 xbt_dynar_free(&ETag_surfxml_argument_cb_list);
106 xbt_dynar_free(&STag_surfxml_prop_cb_list);
107 xbt_dynar_free(&ETag_surfxml_prop_cb_list);
108 xbt_dynar_free(&STag_surfxml_set_cb_list);
109 xbt_dynar_free(&ETag_surfxml_set_cb_list);
110 xbt_dynar_free(&STag_surfxml_foreach_cb_list);
111 xbt_dynar_free(&ETag_surfxml_foreach_cb_list);
112 xbt_dynar_free(&STag_surfxml_route_c_multi_cb_list);
113 xbt_dynar_free(&ETag_surfxml_route_c_multi_cb_list);
114 xbt_dynar_free(&STag_surfxml_cluster_cb_list);
115 xbt_dynar_free(&ETag_surfxml_cluster_cb_list);
116 xbt_dynar_free(&STag_surfxml_trace_cb_list);
117 xbt_dynar_free(&ETag_surfxml_trace_cb_list);
118 xbt_dynar_free(&STag_surfxml_trace_c_connect_cb_list);
119 xbt_dynar_free(&ETag_surfxml_trace_c_connect_cb_list);
120 xbt_dynar_free(&STag_surfxml_random_cb_list);
121 xbt_dynar_free(&ETag_surfxml_random_cb_list);
124 void surf_parse_reset_parser(void)
126 surf_parse_free_callbacks();
127 STag_surfxml_platform_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
128 ETag_surfxml_platform_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
129 STag_surfxml_host_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
130 ETag_surfxml_host_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
131 STag_surfxml_router_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
132 ETag_surfxml_router_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
133 STag_surfxml_link_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
134 ETag_surfxml_link_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
135 STag_surfxml_route_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
136 ETag_surfxml_route_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
137 STag_surfxml_link_c_ctn_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
138 ETag_surfxml_link_c_ctn_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
139 STag_surfxml_process_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
140 ETag_surfxml_process_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
141 STag_surfxml_argument_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
142 ETag_surfxml_argument_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
143 STag_surfxml_prop_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
144 ETag_surfxml_prop_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
145 STag_surfxml_set_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
146 ETag_surfxml_set_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
147 STag_surfxml_foreach_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
148 ETag_surfxml_foreach_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
149 STag_surfxml_route_c_multi_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
150 ETag_surfxml_route_c_multi_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
151 STag_surfxml_cluster_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
152 ETag_surfxml_cluster_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
153 STag_surfxml_trace_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
154 ETag_surfxml_trace_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
155 STag_surfxml_trace_c_connect_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
156 ETag_surfxml_trace_c_connect_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
157 STag_surfxml_random_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
158 ETag_surfxml_random_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
161 void STag_surfxml_include(void)
163 xbt_dynar_push(surf_input_buffer_stack, &surf_input_buffer);
164 xbt_dynar_push(surf_file_to_parse_stack, &surf_file_to_parse);
166 surf_file_to_parse = surf_fopen(A_surfxml_include_file, "r");
167 xbt_assert1((surf_file_to_parse), "Unable to open \"%s\"\n",
168 A_surfxml_include_file);
169 surf_input_buffer = surf_parse__create_buffer(surf_file_to_parse, 10);
170 surf_parse__switch_to_buffer(surf_input_buffer);
175 void ETag_surfxml_include(void)
179 surf_parse__delete_buffer(surf_input_buffer);
180 fclose(surf_file_to_parse);
181 xbt_dynar_pop(surf_file_to_parse_stack, &surf_file_to_parse);
182 xbt_dynar_pop(surf_input_buffer_stack, &surf_input_buffer);
185 void STag_surfxml_platform(void)
187 double version = 0.0;
189 sscanf(A_surfxml_platform_version, "%lg", &version);
191 xbt_assert0((version >= 1.0), "******* BIG FAT WARNING *********\n "
192 "You're using an ancient XML file. "
193 "Since SimGrid 3.1, units are Bytes, Flops, and seconds "
194 "instead of MBytes, MFlops and seconds. "
195 "A script (surfxml_update.pl) to help you convert your old "
197 "is available in the contrib/platform_generation directory "
198 "of the simgrid repository. Please check also out the "
199 "SURF section of the ChangeLog for the 3.1 version. "
200 "Last, do not forget to also update your values for "
201 "the calls to MSG_task_create (if any).");
202 xbt_assert0((version >= 2.0), "******* BIG FAT WARNING *********\n "
203 "You're using an old XML file. "
204 "A script (surfxml_update.pl) to help you convert your old "
206 "is available in the contrib/platform_generation directory "
207 "of the simgrid repository.");
209 if (set_list == NULL) set_list = xbt_dict_new();
211 surfxml_call_cb_functions(STag_surfxml_platform_cb_list);
215 void ETag_surfxml_platform(void)
217 convert_route_multi_to_routes();
219 surfxml_call_cb_functions(ETag_surfxml_platform_cb_list);
221 xbt_dict_free(&random_data_list);
222 xbt_dict_free(&set_list);
226 void STag_surfxml_host(void)
228 surfxml_call_cb_functions(STag_surfxml_host_cb_list);
231 void ETag_surfxml_host(void)
233 surfxml_call_cb_functions(ETag_surfxml_host_cb_list);
236 void STag_surfxml_router(void)
238 surfxml_call_cb_functions(STag_surfxml_router_cb_list);
241 void ETag_surfxml_router(void)
243 surfxml_call_cb_functions(ETag_surfxml_router_cb_list);
246 void STag_surfxml_link(void)
248 surfxml_call_cb_functions(STag_surfxml_link_cb_list);
251 void ETag_surfxml_link(void)
253 surfxml_call_cb_functions(ETag_surfxml_link_cb_list);
256 void STag_surfxml_route(void)
258 surfxml_call_cb_functions(STag_surfxml_route_cb_list);
261 void ETag_surfxml_route(void)
263 surfxml_call_cb_functions(ETag_surfxml_route_cb_list);
266 void STag_surfxml_link_c_ctn(void)
268 surfxml_call_cb_functions(STag_surfxml_link_c_ctn_cb_list);
271 void ETag_surfxml_link_c_ctn(void)
273 surfxml_call_cb_functions(ETag_surfxml_link_c_ctn_cb_list);
276 void STag_surfxml_process(void)
278 surfxml_call_cb_functions(STag_surfxml_process_cb_list);
281 void ETag_surfxml_process(void)
283 surfxml_call_cb_functions(ETag_surfxml_process_cb_list);
286 void STag_surfxml_argument(void)
288 surfxml_call_cb_functions(STag_surfxml_argument_cb_list);
291 void ETag_surfxml_argument(void)
293 surfxml_call_cb_functions(ETag_surfxml_argument_cb_list);
296 void STag_surfxml_prop(void)
298 surfxml_call_cb_functions(STag_surfxml_prop_cb_list);
300 void ETag_surfxml_prop(void)
302 surfxml_call_cb_functions(ETag_surfxml_prop_cb_list);
305 void STag_surfxml_set(void)
307 surfxml_call_cb_functions(STag_surfxml_set_cb_list);
310 void ETag_surfxml_set(void)
312 surfxml_call_cb_functions(ETag_surfxml_set_cb_list);
315 void STag_surfxml_foreach(void)
317 /* Save the current buffer */
318 old_buff = surfxml_bufferstack;
319 surfxml_call_cb_functions(STag_surfxml_foreach_cb_list);
322 void ETag_surfxml_foreach(void)
324 surfxml_call_cb_functions(ETag_surfxml_foreach_cb_list);
326 /* free the temporary dynar and restore original */
327 xbt_dynar_free(&STag_surfxml_host_cb_list);
328 xbt_dynar_free(&ETag_surfxml_host_cb_list);
330 STag_surfxml_host_cb_list = main_STag_surfxml_host_cb_list;
331 ETag_surfxml_host_cb_list = main_ETag_surfxml_host_cb_list;
333 /* free the temporary dynar and restore original */
334 xbt_dynar_free(&STag_surfxml_link_cb_list);
335 xbt_dynar_free(&ETag_surfxml_link_cb_list);
337 STag_surfxml_link_cb_list = main_STag_surfxml_link_cb_list;
338 ETag_surfxml_link_cb_list = main_ETag_surfxml_link_cb_list;
342 void STag_surfxml_route_c_multi(void)
344 surfxml_call_cb_functions(STag_surfxml_route_c_multi_cb_list);
347 void ETag_surfxml_route_c_multi(void)
349 surfxml_call_cb_functions(ETag_surfxml_route_c_multi_cb_list);
352 void STag_surfxml_cluster(void)
354 surfxml_call_cb_functions(STag_surfxml_cluster_cb_list);
357 void ETag_surfxml_cluster(void)
359 surfxml_call_cb_functions(ETag_surfxml_cluster_cb_list);
362 void STag_surfxml_trace(void)
364 surfxml_call_cb_functions(STag_surfxml_trace_cb_list);
367 void ETag_surfxml_trace(void)
369 surfxml_call_cb_functions(ETag_surfxml_trace_cb_list);
372 void STag_surfxml_trace_c_connect(void)
374 surfxml_call_cb_functions(STag_surfxml_trace_c_connect_cb_list);
377 void ETag_surfxml_trace_c_connect(void)
379 surfxml_call_cb_functions(ETag_surfxml_trace_c_connect_cb_list);
382 void STag_surfxml_random(void)
384 surfxml_call_cb_functions(STag_surfxml_random_cb_list);
387 void ETag_surfxml_random(void)
389 surfxml_call_cb_functions(ETag_surfxml_random_cb_list);
392 void surf_parse_open(const char *file)
394 static int warned = 0; /* warn only once */
398 ("Bypassing the XML parser since surf_parse_open received a NULL pointer. If it is not what you want, go fix your code.");
403 if (!surf_input_buffer_stack)
404 surf_input_buffer_stack = xbt_dynar_new(sizeof(YY_BUFFER_STATE), NULL);
405 if (!surf_file_to_parse_stack)
406 surf_file_to_parse_stack = xbt_dynar_new(sizeof(FILE *), NULL);
408 surf_file_to_parse = surf_fopen(file, "r");
409 xbt_assert1((surf_file_to_parse), "Unable to open \"%s\"\n", file);
410 surf_input_buffer = surf_parse__create_buffer(surf_file_to_parse, 10);
411 surf_parse__switch_to_buffer(surf_input_buffer);
412 surf_parse_lineno = 1;
415 void surf_parse_close(void)
417 if (surf_input_buffer_stack)
418 xbt_dynar_free(&surf_input_buffer_stack);
419 if (surf_file_to_parse_stack)
420 xbt_dynar_free(&surf_file_to_parse_stack);
422 if (surf_file_to_parse) {
423 surf_parse__delete_buffer(surf_input_buffer);
424 fclose(surf_file_to_parse);
429 static int _surf_parse(void)
431 return surf_parse_lex();
434 int_f_void_t surf_parse = _surf_parse;
436 void surf_parse_get_double(double *value, const char *string)
440 ret = sscanf(string, "%lg", value);
441 xbt_assert2((ret == 1), "Parse error line %d : %s not a number",
442 surf_parse_lineno, string);
445 void surf_parse_get_int(int *value, const char *string)
449 ret = sscanf(string, "%d", value);
450 xbt_assert2((ret == 1), "Parse error line %d : %s not a number",
451 surf_parse_lineno, string);
454 void surf_parse_get_trace(tmgr_trace_t * trace, const char *string)
456 if ((!string) || (strcmp(string, "") == 0))
459 *trace = tmgr_trace_new(string);
462 void parse_properties(void)
466 if(!current_property_set) current_property_set = xbt_dict_new();
468 value = xbt_strdup(A_surfxml_prop_value);
469 xbt_dict_set(current_property_set, A_surfxml_prop_id, value, free);
473 void free_string(void *d)
478 void surfxml_add_callback(xbt_dynar_t cb_list, void_f_void_t function)
480 xbt_dynar_push(cb_list, &function);
483 static XBT_INLINE void surfxml_call_cb_functions(xbt_dynar_t cb_list)
485 unsigned int iterator;
487 xbt_dynar_foreach(cb_list, iterator, fun){
488 DEBUG2("call %p %p",fun,*fun);
495 xbt_dict_free(&route_table);
496 xbt_dynar_free(&route_link_list);
497 route_table = xbt_dict_new_ext(202400);
499 route_multi_table = xbt_dict_new();
500 route_multi_elements = xbt_dynar_new(sizeof(char*), NULL);
501 traces_set_list = xbt_dict_new();
502 traces_connect_list = xbt_dynar_new(sizeof(char*), NULL);
503 random_data_list = xbt_dict_new();
507 void parse_platform_file(const char* file)
509 surf_parse_open(file);
510 xbt_assert1((!(*surf_parse)()), "Parse error in %s", file);
514 /* Functions to bypass route, host and link tags. Used by the foreach and route:multi tags */
516 static void parse_make_temporary_route(const char *src, const char *dst, int action)
519 surfxml_bufferstack = xbt_new0(char, 2048);
521 A_surfxml_route_action = action;
522 SURFXML_BUFFER_SET(route_src, src);
523 SURFXML_BUFFER_SET(route_dst, dst);
524 SURFXML_BUFFER_SET(route_impact_on_src, "0.0");
525 SURFXML_BUFFER_SET(route_impact_on_dst, "0.0");
526 SURFXML_BUFFER_SET(route_impact_on_src_with_other_recv, "0.0");
527 SURFXML_BUFFER_SET(route_impact_on_dst_with_other_send, "0.0");
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,
532 const char* surfxml_host_interference_send, const char* surfxml_host_interference_recv,
533 const char* surfxml_host_interference_send_recv, const char* surfxml_host_max_outgoing_rate)
536 surfxml_bufferstack = xbt_new0(char, 2048);
538 SURFXML_BUFFER_SET(host_id, hostName);
539 SURFXML_BUFFER_SET(host_power, surfxml_host_power /*hostPower*/);
540 SURFXML_BUFFER_SET(host_availability, surfxml_host_availability);
541 SURFXML_BUFFER_SET(host_availability_file, surfxml_host_availability_file);
542 SURFXML_BUFFER_SET(host_state_file, surfxml_host_state_file);
543 SURFXML_BUFFER_SET(host_interference_send, surfxml_host_interference_send);
544 SURFXML_BUFFER_SET(host_interference_recv, surfxml_host_interference_recv);
545 SURFXML_BUFFER_SET(host_interference_send_recv, surfxml_host_interference_send_recv);
546 SURFXML_BUFFER_SET(host_max_outgoing_rate, surfxml_host_max_outgoing_rate);
549 static void parse_change_link_data(const char* linkName, const char* surfxml_link_bandwidth, const char* surfxml_link_bandwidth_file,
550 const char* surfxml_link_latency, const char* surfxml_link_latency_file, const char* surfxml_link_state_file)
553 surfxml_bufferstack = xbt_new0(char, 2048);
555 SURFXML_BUFFER_SET(link_id, linkName);
556 SURFXML_BUFFER_SET(link_bandwidth, surfxml_link_bandwidth);
557 SURFXML_BUFFER_SET(link_bandwidth_file, surfxml_link_bandwidth_file);
558 SURFXML_BUFFER_SET(link_latency, surfxml_link_latency);
559 SURFXML_BUFFER_SET(link_latency_file, surfxml_link_latency_file);
560 SURFXML_BUFFER_SET(link_state_file, surfxml_link_state_file);
564 * \brief Restores the original surfxml buffer
566 static void parse_restore_original_buffer(void)
568 free(surfxml_bufferstack);
569 surfxml_bufferstack = old_buff;
572 /* Functions for the sets and foreach tags */
574 void parse_sets(void)
576 char *id, *suffix, *prefix, *radical;
578 xbt_dynar_t radical_ends;
580 id = xbt_strdup(A_surfxml_set_id);
581 prefix = xbt_strdup(A_surfxml_set_prefix);
582 suffix = xbt_strdup(A_surfxml_set_suffix);
583 radical = xbt_strdup(A_surfxml_set_radical);
585 xbt_assert1(!xbt_dict_get_or_null(set_list, id),
586 "Set '%s' declared several times in the platform file.",id);
587 radical_ends = xbt_str_split(radical, "-");
588 xbt_assert1((xbt_dynar_length(radical_ends)==2), "Radical must be in the form lvalue-rvalue! Provided value: %s", radical);
590 surf_parse_get_int(&start, xbt_dynar_get_as(radical_ends, 0, char*));
591 surf_parse_get_int(&end, xbt_dynar_get_as(radical_ends, 1, char*));
593 xbt_dynar_t current_set = xbt_dynar_new(sizeof(char*), NULL);
597 for (i=start; i<end; i++) {
598 value = bprintf("%s%d%s", prefix, i, suffix);
599 xbt_dynar_push(current_set, &value);
602 xbt_dict_set(set_list, id, current_set, NULL);
605 static void parse_host_foreach(void)
607 xbt_dynar_t names = NULL;
608 unsigned int cpt = 0;
611 xbt_assert1((names = xbt_dict_get_or_null(set_list, foreach_set_name)),
612 "Set name '%s' reffered by foreach tag not found.", foreach_set_name);
614 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);
616 const char* surfxml_host_power = A_surfxml_host_power;
617 const char* surfxml_host_availability = A_surfxml_host_availability;
618 const char* surfxml_host_availability_file = A_surfxml_host_availability_file;
619 const char* surfxml_host_state_file = A_surfxml_host_state_file;
620 const char* surfxml_host_interference_send = A_surfxml_host_interference_send;
621 const char* surfxml_host_interference_recv = A_surfxml_host_interference_recv;
622 const char *surfxml_host_interference_send_recv = A_surfxml_host_interference_send_recv;
623 const char* surfxml_host_max_outgoing_rate = A_surfxml_host_max_outgoing_rate;
625 /* foreach name in set call the main host callback */
626 xbt_dynar_foreach (names, cpt, name) {
627 parse_change_cpu_data(name, surfxml_host_power, surfxml_host_availability,
628 surfxml_host_availability_file, surfxml_host_state_file,
629 surfxml_host_interference_send, surfxml_host_interference_recv,
630 surfxml_host_interference_send_recv, surfxml_host_max_outgoing_rate);
631 surfxml_call_cb_functions(main_STag_surfxml_host_cb_list);
632 surfxml_call_cb_functions(main_ETag_surfxml_host_cb_list);
633 free(surfxml_bufferstack);
636 surfxml_bufferstack = old_buff;
640 static void parse_link_foreach(void)
642 xbt_dynar_t names = NULL;
643 unsigned int cpt = 0;
646 xbt_assert1((names = xbt_dict_get_or_null(set_list, foreach_set_name)),
647 "Set name '%s' reffered by foreach tag not found.", foreach_set_name);
649 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);
651 const char* surfxml_link_bandwidth = A_surfxml_link_bandwidth;
652 const char* surfxml_link_bandwidth_file = A_surfxml_link_bandwidth_file;
653 const char* surfxml_link_latency = A_surfxml_link_latency;
654 const char* surfxml_link_latency_file = A_surfxml_link_latency_file;
655 const char* surfxml_link_state_file = A_surfxml_link_state_file;
656 /* for each name in set call the main link callback */
657 xbt_dynar_foreach (names, cpt, name) {
658 parse_change_link_data(name, surfxml_link_bandwidth, surfxml_link_bandwidth_file,
659 surfxml_link_latency, surfxml_link_latency_file, surfxml_link_state_file);
660 surfxml_call_cb_functions(main_STag_surfxml_link_cb_list);
661 surfxml_call_cb_functions(main_ETag_surfxml_link_cb_list);
662 free(surfxml_bufferstack);
666 surfxml_bufferstack = old_buff;
669 void parse_foreach(void)
671 /* save the host & link callbacks */
672 main_STag_surfxml_host_cb_list = STag_surfxml_host_cb_list;
673 main_ETag_surfxml_host_cb_list = ETag_surfxml_host_cb_list;
674 main_STag_surfxml_link_cb_list = STag_surfxml_link_cb_list;
675 main_ETag_surfxml_link_cb_list = ETag_surfxml_link_cb_list;
677 /* define host & link callbacks to be used only by the foreach tag */
678 STag_surfxml_host_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
679 ETag_surfxml_host_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
680 STag_surfxml_link_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
681 ETag_surfxml_link_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
683 surfxml_add_callback(STag_surfxml_host_cb_list, &parse_host_foreach);
684 surfxml_add_callback(STag_surfxml_link_cb_list, &parse_link_foreach);
687 foreach_set_name = xbt_strdup(A_surfxml_foreach_set_id);
690 /* Route:multi functions */
692 static int route_multi_size=0;
693 static char* src_name, *dst_name;
694 static int is_symmetric_route;
696 void parse_route_elem(void)
700 val = xbt_strdup(A_surfxml_link_c_ctn_id);
702 xbt_dynar_push(route_link_list, &val);
705 void parse_route_multi_set_endpoints(void)
707 src_name = xbt_strdup(A_surfxml_route_c_multi_src);
708 dst_name = xbt_strdup(A_surfxml_route_c_multi_dst);
709 route_action = A_surfxml_route_c_multi_action;
710 is_symmetric_route = A_surfxml_route_c_multi_symmetric;
713 route_link_list = xbt_dynar_new(sizeof(char *), &free_string);
716 static int contains(xbt_dynar_t list, const char* value)
720 xbt_dynar_foreach(list, cpt, val) {
721 if (strcmp(val, value) == 0)
728 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.
729 The decision is based upon the value of action specified in the xml route:multi attribute action
731 void manage_route(xbt_dict_t routing_table, const char *route_name, int action, int isMultiRoute)
737 /* get already existing list if it exists */
738 links = xbt_dict_get_or_null(routing_table, route_name);
739 DEBUG1("ROUTE: %s", route_name);
742 case A_surfxml_route_c_multi_action_PREPEND: /* add existing links at the end; route_link_list + links */
743 xbt_dynar_foreach(links, cpt, value) {
744 xbt_dynar_push(route_link_list,&value);
747 case A_surfxml_route_c_multi_action_POSTPEND: /* add existing links in front; links + route_link_list */
748 xbt_dynar_foreach(route_link_list, cpt, value) {
749 xbt_dynar_push(links,&value);
751 route_link_list = links;
753 case A_surfxml_route_c_multi_action_OVERRIDE:
758 /* this is the final route; do not add if name is a set; add only if name is in set list */
760 xbt_dict_set(routing_table, route_name, route_link_list, NULL);
764 void parse_route_multi_set_route(void)
768 route_name = bprintf("%s#%s#%d#%d#%d", src_name, dst_name, route_action, is_symmetric_route, route_multi_size);
770 xbt_dynar_push(route_multi_elements, &route_name);
773 xbt_dict_set(route_multi_table, route_name, route_link_list, NULL);
774 /* add symmetric if it is the case */
775 if (is_symmetric_route == 1) {
776 char * symmetric_name = bprintf("%s#%s#%d#%d#%d", dst_name, src_name, route_action, !is_symmetric_route, route_multi_size);
778 xbt_dict_set(route_multi_table, symmetric_name, route_link_list, NULL);
779 xbt_dynar_push(route_multi_elements, &symmetric_name);
780 is_symmetric_route = 0;
786 static void add_multi_links(const char* src, const char* dst, xbt_dynar_t links, const char* src_name, const char* dst_name)
790 parse_make_temporary_route(src_name, dst_name, route_action);
791 surfxml_call_cb_functions(STag_surfxml_route_cb_list);
792 DEBUG2("\tADDING ROUTE: %s -> %s", src_name, dst_name);
793 /* Build link list */
794 xbt_dynar_foreach(links, cpt, value) {
795 if (strcmp(value, src) == 0)
796 val = xbt_strdup(src_name);
797 else if (strcmp(value, dst) == 0)
798 val = xbt_strdup(dst_name);
799 else if (strcmp(value, "$dst") == 0)
800 val = xbt_strdup(dst_name);
801 else if (strcmp(value, "$src") == 0)
802 val = xbt_strdup(src_name);
804 val = xbt_strdup(value);
805 xbt_dynar_push(route_link_list, &val);
807 surfxml_call_cb_functions(ETag_surfxml_route_cb_list);
808 free(surfxml_bufferstack);
811 static void convert_route_multi_to_routes(void)
813 xbt_dict_cursor_t cursor_w;
815 unsigned int cpt, cpt2, cursor;
816 char *src_host_name, *dst_host_name, *key, *src, *dst, *val, *key_w, *data_w;
818 xbt_dynar_t src_names = NULL, dst_names = NULL, links;
820 if (!route_multi_elements) return;
822 old_buff = surfxml_bufferstack;
823 /* Get all routes in the exact order they were entered in the platform file */
824 xbt_dynar_foreach(route_multi_elements, cursor, key) {
825 /* Get links for the route */
826 links = (xbt_dynar_t)xbt_dict_get_or_null(route_multi_table, key);
827 keys = xbt_str_split_str(key, sep);
829 src = xbt_dynar_get_as(keys, 0, char*);
830 dst = xbt_dynar_get_as(keys, 1, char*);
831 route_action = atoi(xbt_dynar_get_as(keys, 2, char*));
832 symmetric = atoi(xbt_dynar_get_as(keys, 3, char*));
834 /* Create the dynar of src and dst hosts for the new routes */
835 /* NOTE: src and dst can be either set names or simple host names */
836 src_names = (xbt_dynar_t)xbt_dict_get_or_null(set_list, src);
837 dst_names = (xbt_dynar_t)xbt_dict_get_or_null(set_list, dst);
838 /* Add to dynar even if they are simple names */
839 if (src_names == NULL) {
840 src_names = xbt_dynar_new(sizeof(char *), &free_string);
841 val = xbt_strdup(src);
842 xbt_dynar_push(src_names, &val);
844 if (dst_names == NULL) {
845 dst_names = xbt_dynar_new(sizeof(char *), &free_string);
846 val = xbt_strdup(dst);
847 xbt_dynar_push(dst_names, &val);
849 /* Build the routes */
850 DEBUG2("ADDING MULTI ROUTE: %s -> %s", xbt_dynar_get_as(keys, 0, char*), xbt_dynar_get_as(keys, 1, char*));
851 xbt_dynar_foreach(src_names, cpt, src_host_name) {
852 xbt_dynar_foreach(dst_names, cpt2, dst_host_name) {
853 /* If dst is $* then set this route to have its dst point to all hosts */
854 if (strcmp(src_host_name,"$*") != 0 && strcmp(dst_host_name,"$*") == 0){
855 xbt_dict_foreach(workstation_set, cursor_w, key_w, data_w) {
856 //int n = xbt_dynar_member(src_names, (char*)key_w);
857 add_multi_links(src, dst, links, src_host_name, key_w);
860 /* If src is $* then set this route to have its dst point to all hosts */
861 if (strcmp(src_host_name,"$*") == 0 && strcmp(dst_host_name,"$*") != 0){
862 xbt_dict_foreach(workstation_set, cursor_w, key_w, data_w) {
863 // if (!symmetric || (symmetric && !contains(dst_names, key_w)))
864 add_multi_links(src, dst, links, key_w, dst_host_name);
867 /* if none of them are equal to $* */
868 if (strcmp(src_host_name,"$*") != 0 && strcmp(dst_host_name,"$*") != 0) {
869 add_multi_links(src, dst, links, src_host_name, dst_host_name);
874 surfxml_bufferstack = old_buff;
875 xbt_dict_free(&route_multi_table);
876 xbt_dynar_free(&route_multi_elements);
879 /* Cluster tag functions */
881 void parse_cluster(void)
883 static int AX_ptr = 0;
884 static int surfxml_bufferstack_size = 2048;
886 char* cluster_id = A_surfxml_cluster_id;
887 char* cluster_prefix = A_surfxml_cluster_prefix;
888 char* cluster_suffix = A_surfxml_cluster_suffix;
889 char* cluster_radical = A_surfxml_cluster_radical;
890 char* cluster_power = A_surfxml_cluster_power;
891 char* cluster_bw = A_surfxml_cluster_bw;
892 char* cluster_lat = A_surfxml_cluster_lat;
893 char* cluster_bb_bw = A_surfxml_cluster_bb_bw;
894 char* cluster_bb_lat = A_surfxml_cluster_bb_lat;
896 char* saved_buff = surfxml_bufferstack;
897 surfxml_bufferstack = xbt_new0(char, surfxml_bufferstack_size);
900 SURFXML_BUFFER_SET(set_id, cluster_id);
901 SURFXML_BUFFER_SET(set_prefix, cluster_prefix);
902 SURFXML_BUFFER_SET(set_suffix, cluster_suffix);
903 SURFXML_BUFFER_SET(set_radical, cluster_radical);
905 SURFXML_START_TAG(set);
906 SURFXML_END_TAG(set);
909 SURFXML_BUFFER_SET(foreach_set_id, cluster_id);
911 SURFXML_START_TAG(foreach);
913 /* Make host for the foreach */
914 parse_change_cpu_data("$1", cluster_power, "1.0", "", "", "1.0", "1.0", "1.0","-1.0");
915 A_surfxml_host_state = A_surfxml_host_state_ON;
917 SURFXML_START_TAG(host);
918 SURFXML_END_TAG(host);
920 /* Make link for the foreach */
921 parse_change_link_data("$1", cluster_bw, "", cluster_lat, "", "");
922 A_surfxml_link_state = A_surfxml_link_state_ON;
923 A_surfxml_link_sharing_policy = A_surfxml_link_sharing_policy_SHARED;
925 SURFXML_START_TAG(link);
926 SURFXML_END_TAG(link);
928 SURFXML_END_TAG(foreach);
930 /* Make backbone link */
931 char * backbone_name = bprintf("%s_bb", cluster_id);
932 parse_change_link_data(backbone_name, cluster_bb_bw, "", cluster_bb_lat, "", "");
933 A_surfxml_link_state = A_surfxml_link_state_ON;
934 A_surfxml_link_sharing_policy = A_surfxml_link_sharing_policy_SHARED;
936 SURFXML_START_TAG(link);
937 SURFXML_END_TAG(link);
939 /* Make route multi with the outside world, i.e. cluster->$* */
940 SURFXML_BUFFER_SET(route_c_multi_src, cluster_id);
941 SURFXML_BUFFER_SET(route_c_multi_dst, "$*");
942 A_surfxml_route_c_multi_symmetric = A_surfxml_route_c_multi_symmetric_NO;
943 A_surfxml_route_c_multi_action = A_surfxml_route_c_multi_action_OVERRIDE;
945 SURFXML_START_TAG(route_c_multi);
947 SURFXML_BUFFER_SET(link_c_ctn_id, "$src");
949 SURFXML_START_TAG(link_c_ctn);
950 SURFXML_END_TAG(link_c_ctn);
952 SURFXML_END_TAG(route_c_multi);
954 /* Make route multi between cluster hosts, i.e. cluster->cluster */
955 SURFXML_BUFFER_SET(route_c_multi_src, cluster_id);
956 SURFXML_BUFFER_SET(route_c_multi_dst, cluster_id);
957 A_surfxml_route_c_multi_action = A_surfxml_route_c_multi_action_POSTPEND;
958 A_surfxml_route_c_multi_symmetric = A_surfxml_route_c_multi_symmetric_NO;
960 SURFXML_START_TAG(route_c_multi);
962 SURFXML_BUFFER_SET(link_c_ctn_id, backbone_name);
964 SURFXML_START_TAG(link_c_ctn);
965 SURFXML_END_TAG(link_c_ctn);
967 SURFXML_END_TAG(route_c_multi);
971 free(surfxml_bufferstack);
972 surfxml_bufferstack = saved_buff;
975 /* Trace management functions */
977 static double trace_periodicity = -1.0;
978 static char* trace_file = NULL;
979 static char* trace_id;
981 void parse_trace_init(void)
983 trace_id = strdup(A_surfxml_trace_id);
984 trace_file = strdup(A_surfxml_trace_file);
985 surf_parse_get_double(&trace_periodicity, A_surfxml_trace_periodicity);
988 void parse_trace_finalize(void)
991 if (!trace_file || strcmp(trace_file,"") != 0) {
992 surf_parse_get_trace(&trace, trace_file);
995 if (strcmp(surfxml_pcdata, "") == 0) trace = NULL;
997 trace = tmgr_trace_new_from_string(trace_id, surfxml_pcdata, trace_periodicity);
999 xbt_dict_set(traces_set_list, trace_id, (void *)trace, NULL);
1002 void parse_trace_c_connect(void)
1004 xbt_assert1(xbt_dict_get_or_null(traces_set_list, A_surfxml_trace_c_connect_trace_id),
1005 "Trace %s undefined", A_surfxml_trace_c_connect_trace_id);
1006 char* trace_connect = bprintf("%s#%d#%d#%s", A_surfxml_trace_c_connect_trace_id, A_surfxml_trace_c_connect_element,
1007 A_surfxml_trace_c_connect_kind, A_surfxml_trace_c_connect_connector_id);
1008 xbt_dynar_push(traces_connect_list, &trace_connect);
1011 /* Random tag functions */
1013 double get_cpu_power(const char *power)
1015 double power_scale = 0.0;
1018 random_data_t random = NULL;
1019 /* randomness is inserted like this: power="$rand(my_random)" */
1020 if (((p = strstr(power, "$rand(")) != NULL) && ((q = strstr(power, ")")) != NULL)) {
1022 generator = xbt_malloc(q - (p + 6) + 1);
1023 memcpy(generator, p + 6, q - (p + 6));
1024 generator[q - (p + 6)] = '\0';
1025 xbt_assert1((random = xbt_dict_get_or_null(random_data_list, generator)),
1026 "Random generator %s undefined", generator);
1027 power_scale = random_generate(random);
1031 surf_parse_get_double(&power_scale, power);
1036 int random_min, random_max, random_mean, random_std_deviation, random_generator;
1039 void init_randomness(void)
1041 random_id = A_surfxml_random_id;
1042 surf_parse_get_int(&random_min, A_surfxml_random_min);
1043 surf_parse_get_int(&random_max, A_surfxml_random_max);
1044 surf_parse_get_int(&random_mean, A_surfxml_random_mean);
1045 surf_parse_get_int(&random_std_deviation, A_surfxml_random_std_deviation);
1046 random_generator = A_surfxml_random_generator;
1049 void add_randomness(void)
1051 /* If needed aditional properties can be added by using the prop tag */
1052 random_data_t random = random_new(random_generator, random_min, random_max, random_mean, random_std_deviation);
1053 xbt_dict_set(random_data_list, random_id, (void *)random, NULL);