3 /* Copyright (c) 2004 Arnaud Legrand. All rights reserved. */
5 /* This program is free software; you can redistribute it and/or modify it
6 * under the terms of the license (GNU LGPL) which comes with this package. */
12 #include "surf/surfxml_parse_private.h"
13 #include "surf/surf_private.h"
15 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_parse, surf,
16 "Logging specific to the SURF parsing module");
19 #include "simgrid_dtd.c"
21 /* Initialize the parsing globals */
23 xbt_dict_t traces_set_list = NULL;
24 //xbt_dynar_t traces_connect_list = NULL;
25 xbt_dict_t trace_connect_list_host_avail = NULL;
26 xbt_dict_t trace_connect_list_power = NULL;
27 xbt_dict_t trace_connect_list_link_avail = NULL;
28 xbt_dict_t trace_connect_list_bandwidth = NULL;
29 xbt_dict_t trace_connect_list_latency = NULL;
31 /* This buffer is used to store the original buffer before substituing it by out own buffer. Usefull for the foreach tag */
33 /* Stores the set name reffered to by the foreach tag */
34 static const char* foreach_set_name;
35 static xbt_dynar_t main_STag_surfxml_host_cb_list = NULL;
36 static xbt_dynar_t main_ETag_surfxml_host_cb_list = NULL;
37 static xbt_dynar_t main_STag_surfxml_link_cb_list = NULL;
38 static xbt_dynar_t main_ETag_surfxml_link_cb_list = NULL;
40 /* make sure these symbols are defined as strong ones in this file so that the linked can resolve them */
41 xbt_dynar_t STag_surfxml_platform_cb_list = NULL;
42 xbt_dynar_t ETag_surfxml_platform_cb_list = NULL;
43 xbt_dynar_t STag_surfxml_host_cb_list = NULL;
44 xbt_dynar_t ETag_surfxml_host_cb_list = NULL;
45 xbt_dynar_t STag_surfxml_router_cb_list = NULL;
46 xbt_dynar_t ETag_surfxml_router_cb_list = NULL;
47 xbt_dynar_t STag_surfxml_link_cb_list = NULL;
48 xbt_dynar_t ETag_surfxml_link_cb_list = NULL;
49 xbt_dynar_t STag_surfxml_route_cb_list = NULL;
50 xbt_dynar_t ETag_surfxml_route_cb_list = NULL;
51 xbt_dynar_t STag_surfxml_link_c_ctn_cb_list = NULL;
52 xbt_dynar_t ETag_surfxml_link_c_ctn_cb_list = NULL;
53 xbt_dynar_t STag_surfxml_process_cb_list = NULL;
54 xbt_dynar_t ETag_surfxml_process_cb_list = NULL;
55 xbt_dynar_t STag_surfxml_argument_cb_list = NULL;
56 xbt_dynar_t ETag_surfxml_argument_cb_list = NULL;
57 xbt_dynar_t STag_surfxml_prop_cb_list = NULL;
58 xbt_dynar_t ETag_surfxml_prop_cb_list = NULL;
59 xbt_dynar_t STag_surfxml_set_cb_list = NULL;
60 xbt_dynar_t ETag_surfxml_set_cb_list = NULL;
61 xbt_dynar_t STag_surfxml_foreach_cb_list = NULL;
62 xbt_dynar_t ETag_surfxml_foreach_cb_list = NULL;
63 xbt_dynar_t STag_surfxml_route_c_multi_cb_list = NULL;
64 xbt_dynar_t ETag_surfxml_route_c_multi_cb_list = NULL;
65 xbt_dynar_t STag_surfxml_cluster_cb_list = NULL;
66 xbt_dynar_t ETag_surfxml_cluster_cb_list = NULL;
67 xbt_dynar_t STag_surfxml_trace_cb_list = NULL;
68 xbt_dynar_t ETag_surfxml_trace_cb_list = NULL;
69 xbt_dynar_t STag_surfxml_trace_c_connect_cb_list = NULL;
70 xbt_dynar_t ETag_surfxml_trace_c_connect_cb_list = NULL;
71 xbt_dynar_t STag_surfxml_random_cb_list = NULL;
72 xbt_dynar_t ETag_surfxml_random_cb_list = NULL;
74 /* Stores the sets defined in the XML */
75 xbt_dict_t set_list = NULL;
77 xbt_dict_t current_property_set = NULL;
79 /* For the route:multi tag */
80 xbt_dict_t route_table = NULL;
81 xbt_dict_t route_multi_table = NULL;
82 xbt_dynar_t route_multi_elements = NULL;
83 xbt_dynar_t route_link_list = NULL;
84 xbt_dynar_t links = NULL;
85 xbt_dynar_t keys = NULL;
87 xbt_dict_t random_data_list = NULL;
89 static xbt_dynar_t surf_input_buffer_stack = NULL;
90 static xbt_dynar_t surf_file_to_parse_stack = NULL;
92 static XBT_INLINE void surfxml_call_cb_functions(xbt_dynar_t);
94 YY_BUFFER_STATE surf_input_buffer;
95 FILE *surf_file_to_parse = NULL;
97 static void convert_route_multi_to_routes(void);
99 void surf_parse_free_callbacks(void)
101 xbt_dynar_free(&STag_surfxml_platform_cb_list);
102 xbt_dynar_free(&ETag_surfxml_platform_cb_list);
103 xbt_dynar_free(&STag_surfxml_host_cb_list);
104 xbt_dynar_free(&ETag_surfxml_host_cb_list);
105 xbt_dynar_free(&STag_surfxml_router_cb_list);
106 xbt_dynar_free(&ETag_surfxml_router_cb_list);
107 xbt_dynar_free(&STag_surfxml_link_cb_list);
108 xbt_dynar_free(&ETag_surfxml_link_cb_list);
109 xbt_dynar_free(&STag_surfxml_route_cb_list);
110 xbt_dynar_free(&ETag_surfxml_route_cb_list);
111 xbt_dynar_free(&STag_surfxml_link_c_ctn_cb_list);
112 xbt_dynar_free(&ETag_surfxml_link_c_ctn_cb_list);
113 xbt_dynar_free(&STag_surfxml_process_cb_list);
114 xbt_dynar_free(&ETag_surfxml_process_cb_list);
115 xbt_dynar_free(&STag_surfxml_argument_cb_list);
116 xbt_dynar_free(&ETag_surfxml_argument_cb_list);
117 xbt_dynar_free(&STag_surfxml_prop_cb_list);
118 xbt_dynar_free(&ETag_surfxml_prop_cb_list);
119 xbt_dynar_free(&STag_surfxml_set_cb_list);
120 xbt_dynar_free(&ETag_surfxml_set_cb_list);
121 xbt_dynar_free(&STag_surfxml_foreach_cb_list);
122 xbt_dynar_free(&ETag_surfxml_foreach_cb_list);
123 xbt_dynar_free(&STag_surfxml_route_c_multi_cb_list);
124 xbt_dynar_free(&ETag_surfxml_route_c_multi_cb_list);
125 xbt_dynar_free(&STag_surfxml_cluster_cb_list);
126 xbt_dynar_free(&ETag_surfxml_cluster_cb_list);
127 xbt_dynar_free(&STag_surfxml_trace_cb_list);
128 xbt_dynar_free(&ETag_surfxml_trace_cb_list);
129 xbt_dynar_free(&STag_surfxml_trace_c_connect_cb_list);
130 xbt_dynar_free(&ETag_surfxml_trace_c_connect_cb_list);
131 xbt_dynar_free(&STag_surfxml_random_cb_list);
132 xbt_dynar_free(&ETag_surfxml_random_cb_list);
135 void surf_parse_reset_parser(void)
137 surf_parse_free_callbacks();
138 STag_surfxml_platform_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
139 ETag_surfxml_platform_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
140 STag_surfxml_host_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
141 ETag_surfxml_host_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
142 STag_surfxml_router_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
143 ETag_surfxml_router_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
144 STag_surfxml_link_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
145 ETag_surfxml_link_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
146 STag_surfxml_route_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
147 ETag_surfxml_route_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
148 STag_surfxml_link_c_ctn_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
149 ETag_surfxml_link_c_ctn_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
150 STag_surfxml_process_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
151 ETag_surfxml_process_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
152 STag_surfxml_argument_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
153 ETag_surfxml_argument_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
154 STag_surfxml_prop_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
155 ETag_surfxml_prop_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
156 STag_surfxml_set_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
157 ETag_surfxml_set_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
158 STag_surfxml_foreach_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
159 ETag_surfxml_foreach_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
160 STag_surfxml_route_c_multi_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
161 ETag_surfxml_route_c_multi_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
162 STag_surfxml_cluster_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
163 ETag_surfxml_cluster_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
164 STag_surfxml_trace_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
165 ETag_surfxml_trace_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
166 STag_surfxml_trace_c_connect_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
167 ETag_surfxml_trace_c_connect_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
168 STag_surfxml_random_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
169 ETag_surfxml_random_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
172 void STag_surfxml_include(void)
174 xbt_dynar_push(surf_input_buffer_stack, &surf_input_buffer);
175 xbt_dynar_push(surf_file_to_parse_stack, &surf_file_to_parse);
177 surf_file_to_parse = surf_fopen(A_surfxml_include_file, "r");
178 xbt_assert1((surf_file_to_parse), "Unable to open \"%s\"\n",
179 A_surfxml_include_file);
180 surf_input_buffer = surf_parse__create_buffer(surf_file_to_parse, 10);
181 surf_parse__switch_to_buffer(surf_input_buffer);
186 void ETag_surfxml_include(void)
190 surf_parse__delete_buffer(surf_input_buffer);
191 fclose(surf_file_to_parse);
192 xbt_dynar_pop(surf_file_to_parse_stack, &surf_file_to_parse);
193 xbt_dynar_pop(surf_input_buffer_stack, &surf_input_buffer);
196 void STag_surfxml_platform(void)
198 double version = 0.0;
200 sscanf(A_surfxml_platform_version, "%lg", &version);
202 xbt_assert0((version >= 1.0), "******* BIG FAT WARNING *********\n "
203 "You're using an ancient XML file. "
204 "Since SimGrid 3.1, units are Bytes, Flops, and seconds "
205 "instead of MBytes, MFlops and seconds. "
206 "A script (surfxml_update.pl) to help you convert your old "
208 "is available in the contrib/platform_generation directory "
209 "of the simgrid repository. Please check also out the "
210 "SURF section of the ChangeLog for the 3.1 version. "
211 "Last, do not forget to also update your values for "
212 "the calls to MSG_task_create (if any).");
213 xbt_assert0((version >= 2.0), "******* BIG FAT WARNING *********\n "
214 "You're using an old XML file. "
215 "A script (surfxml_update.pl) to help you convert your old "
217 "is available in the contrib/platform_generation directory "
218 "of the simgrid repository.");
220 if (set_list == NULL) set_list = xbt_dict_new();
222 surfxml_call_cb_functions(STag_surfxml_platform_cb_list);
226 void ETag_surfxml_platform(void)
228 convert_route_multi_to_routes();
230 surfxml_call_cb_functions(ETag_surfxml_platform_cb_list);
232 xbt_dict_free(&random_data_list);
233 xbt_dict_free(&set_list);
237 void STag_surfxml_host(void)
239 surfxml_call_cb_functions(STag_surfxml_host_cb_list);
242 void ETag_surfxml_host(void)
244 surfxml_call_cb_functions(ETag_surfxml_host_cb_list);
247 void STag_surfxml_router(void)
249 surfxml_call_cb_functions(STag_surfxml_router_cb_list);
252 void ETag_surfxml_router(void)
254 surfxml_call_cb_functions(ETag_surfxml_router_cb_list);
257 void STag_surfxml_link(void)
259 surfxml_call_cb_functions(STag_surfxml_link_cb_list);
262 void ETag_surfxml_link(void)
264 surfxml_call_cb_functions(ETag_surfxml_link_cb_list);
267 void STag_surfxml_route(void)
269 surfxml_call_cb_functions(STag_surfxml_route_cb_list);
272 void ETag_surfxml_route(void)
274 surfxml_call_cb_functions(ETag_surfxml_route_cb_list);
277 void STag_surfxml_link_c_ctn(void)
279 surfxml_call_cb_functions(STag_surfxml_link_c_ctn_cb_list);
282 void ETag_surfxml_link_c_ctn(void)
284 surfxml_call_cb_functions(ETag_surfxml_link_c_ctn_cb_list);
287 void STag_surfxml_process(void)
289 surfxml_call_cb_functions(STag_surfxml_process_cb_list);
292 void ETag_surfxml_process(void)
294 surfxml_call_cb_functions(ETag_surfxml_process_cb_list);
297 void STag_surfxml_argument(void)
299 surfxml_call_cb_functions(STag_surfxml_argument_cb_list);
302 void ETag_surfxml_argument(void)
304 surfxml_call_cb_functions(ETag_surfxml_argument_cb_list);
307 void STag_surfxml_prop(void)
309 surfxml_call_cb_functions(STag_surfxml_prop_cb_list);
311 void ETag_surfxml_prop(void)
313 surfxml_call_cb_functions(ETag_surfxml_prop_cb_list);
316 void STag_surfxml_set(void)
318 surfxml_call_cb_functions(STag_surfxml_set_cb_list);
321 void ETag_surfxml_set(void)
323 surfxml_call_cb_functions(ETag_surfxml_set_cb_list);
326 void STag_surfxml_foreach(void)
328 /* Save the current buffer */
329 old_buff = surfxml_bufferstack;
330 surfxml_call_cb_functions(STag_surfxml_foreach_cb_list);
333 void ETag_surfxml_foreach(void)
335 surfxml_call_cb_functions(ETag_surfxml_foreach_cb_list);
337 /* free the temporary dynar and restore original */
338 xbt_dynar_free(&STag_surfxml_host_cb_list);
339 xbt_dynar_free(&ETag_surfxml_host_cb_list);
341 STag_surfxml_host_cb_list = main_STag_surfxml_host_cb_list;
342 ETag_surfxml_host_cb_list = main_ETag_surfxml_host_cb_list;
344 /* free the temporary dynar and restore original */
345 xbt_dynar_free(&STag_surfxml_link_cb_list);
346 xbt_dynar_free(&ETag_surfxml_link_cb_list);
348 STag_surfxml_link_cb_list = main_STag_surfxml_link_cb_list;
349 ETag_surfxml_link_cb_list = main_ETag_surfxml_link_cb_list;
353 void STag_surfxml_route_c_multi(void)
355 surfxml_call_cb_functions(STag_surfxml_route_c_multi_cb_list);
358 void ETag_surfxml_route_c_multi(void)
360 surfxml_call_cb_functions(ETag_surfxml_route_c_multi_cb_list);
363 void STag_surfxml_cluster(void)
365 surfxml_call_cb_functions(STag_surfxml_cluster_cb_list);
368 void ETag_surfxml_cluster(void)
370 surfxml_call_cb_functions(ETag_surfxml_cluster_cb_list);
373 void STag_surfxml_trace(void)
375 surfxml_call_cb_functions(STag_surfxml_trace_cb_list);
378 void ETag_surfxml_trace(void)
380 surfxml_call_cb_functions(ETag_surfxml_trace_cb_list);
383 void STag_surfxml_trace_c_connect(void)
385 surfxml_call_cb_functions(STag_surfxml_trace_c_connect_cb_list);
388 void ETag_surfxml_trace_c_connect(void)
390 surfxml_call_cb_functions(ETag_surfxml_trace_c_connect_cb_list);
393 void STag_surfxml_random(void)
395 surfxml_call_cb_functions(STag_surfxml_random_cb_list);
398 void ETag_surfxml_random(void)
400 surfxml_call_cb_functions(ETag_surfxml_random_cb_list);
403 void surf_parse_open(const char *file)
405 static int warned = 0; /* warn only once */
409 ("Bypassing the XML parser since surf_parse_open received a NULL pointer. If it is not what you want, go fix your code.");
414 if (!surf_input_buffer_stack)
415 surf_input_buffer_stack = xbt_dynar_new(sizeof(YY_BUFFER_STATE), NULL);
416 if (!surf_file_to_parse_stack)
417 surf_file_to_parse_stack = xbt_dynar_new(sizeof(FILE *), NULL);
419 surf_file_to_parse = surf_fopen(file, "r");
420 xbt_assert1((surf_file_to_parse), "Unable to open \"%s\"\n", file);
421 surf_input_buffer = surf_parse__create_buffer(surf_file_to_parse, 10);
422 surf_parse__switch_to_buffer(surf_input_buffer);
423 surf_parse_lineno = 1;
426 void surf_parse_close(void)
428 if (surf_input_buffer_stack)
429 xbt_dynar_free(&surf_input_buffer_stack);
430 if (surf_file_to_parse_stack)
431 xbt_dynar_free(&surf_file_to_parse_stack);
433 if (surf_file_to_parse) {
434 surf_parse__delete_buffer(surf_input_buffer);
435 fclose(surf_file_to_parse);
440 static int _surf_parse(void)
442 return surf_parse_lex();
445 int_f_void_t surf_parse = _surf_parse;
447 void surf_parse_get_double(double *value, const char *string)
451 ret = sscanf(string, "%lg", value);
452 xbt_assert2((ret == 1), "Parse error line %d : %s not a number",
453 surf_parse_lineno, string);
456 void surf_parse_get_int(int *value, const char *string)
460 ret = sscanf(string, "%d", value);
461 xbt_assert2((ret == 1), "Parse error line %d : %s not a number",
462 surf_parse_lineno, string);
465 void surf_parse_get_trace(tmgr_trace_t * trace, const char *string)
467 if ((!string) || (strcmp(string, "") == 0))
470 *trace = tmgr_trace_new(string);
473 void parse_properties(void)
477 if(!current_property_set) current_property_set = xbt_dict_new();
479 value = xbt_strdup(A_surfxml_prop_value);
480 xbt_dict_set(current_property_set, A_surfxml_prop_id, value, free);
483 void free_string(void *d)
488 void surfxml_add_callback(xbt_dynar_t cb_list, void_f_void_t function)
490 xbt_dynar_push(cb_list, &function);
493 static XBT_INLINE void surfxml_call_cb_functions(xbt_dynar_t cb_list)
495 unsigned int iterator;
497 xbt_dynar_foreach(cb_list, iterator, fun){
498 DEBUG2("call %p %p",fun,*fun);
505 xbt_dict_free(&route_table);
506 xbt_dynar_free(&route_link_list);
507 route_table = xbt_dict_new();
509 route_multi_table = xbt_dict_new();
510 route_multi_elements = xbt_dynar_new(sizeof(char*), NULL);
511 traces_set_list = xbt_dict_new();
513 trace_connect_list_host_avail = xbt_dict_new();
514 trace_connect_list_power = xbt_dict_new();
515 trace_connect_list_link_avail = xbt_dict_new();
516 trace_connect_list_bandwidth = xbt_dict_new();
517 trace_connect_list_latency = xbt_dict_new();
519 random_data_list = xbt_dict_new();
522 void parse_platform_file(const char* file)
524 surf_parse_open(file);
525 xbt_assert1((!(*surf_parse)()), "Parse error in %s", file);
529 /* Functions to bypass route, host and link tags. Used by the foreach and route:multi tags */
531 static void parse_make_temporary_route(const char *src, const char *dst, int action)
534 surfxml_bufferstack = xbt_new0(char, 2048);
536 A_surfxml_route_action = action;
537 SURFXML_BUFFER_SET(route_src, src);
538 SURFXML_BUFFER_SET(route_dst, dst);
541 static void parse_change_cpu_data(const char* hostName, const char* surfxml_host_power, const char* surfxml_host_availability,
542 const char* surfxml_host_availability_file, const char* surfxml_host_state_file)
545 surfxml_bufferstack = xbt_new0(char, 2048);
547 SURFXML_BUFFER_SET(host_id, hostName);
548 SURFXML_BUFFER_SET(host_power, surfxml_host_power /*hostPower*/);
549 SURFXML_BUFFER_SET(host_availability, surfxml_host_availability);
550 SURFXML_BUFFER_SET(host_availability_file, surfxml_host_availability_file);
551 SURFXML_BUFFER_SET(host_state_file, surfxml_host_state_file);
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;
584 xbt_dynar_t current_set;
588 id = xbt_strdup(A_surfxml_set_id);
589 prefix = xbt_strdup(A_surfxml_set_prefix);
590 suffix = xbt_strdup(A_surfxml_set_suffix);
591 radical = xbt_strdup(A_surfxml_set_radical);
593 xbt_assert1(!xbt_dict_get_or_null(set_list, id),
594 "Set '%s' declared several times in the platform file.",id);
595 radical_ends = xbt_str_split(radical, "-");
596 xbt_assert1((xbt_dynar_length(radical_ends)==2), "Radical must be in the form lvalue-rvalue! Provided value: %s", radical);
598 surf_parse_get_int(&start, xbt_dynar_get_as(radical_ends, 0, char*));
599 surf_parse_get_int(&end, xbt_dynar_get_as(radical_ends, 1, char*));
601 current_set = xbt_dynar_new(sizeof(char*), NULL);
604 for (i=start; i<end; i++) {
605 value = bprintf("%s%d%s", prefix, i, suffix);
606 xbt_dynar_push(current_set, &value);
609 xbt_dict_set(set_list, id, current_set, NULL);
612 static const char* surfxml_host_power;
613 static const char* surfxml_host_availability;
614 static const char* surfxml_host_availability_file;
615 static const char* surfxml_host_state_file;
617 static void parse_host_foreach(void)
619 surfxml_host_power = A_surfxml_host_power;
620 surfxml_host_availability = A_surfxml_host_availability;
621 surfxml_host_availability_file = A_surfxml_host_availability_file;
622 surfxml_host_state_file = A_surfxml_host_state_file;
625 static void finalize_host_foreach(void)
627 xbt_dynar_t names = NULL;
628 unsigned int cpt = 0;
630 xbt_dict_cursor_t cursor = NULL;
633 xbt_dict_t cluster_host_props = current_property_set;
635 xbt_assert1((names = xbt_dict_get_or_null(set_list, foreach_set_name)),
636 "Set name '%s' reffered by foreach tag not found.", foreach_set_name);
638 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);
641 /* foreach name in set call the main host callback */
642 xbt_dynar_foreach (names, cpt, name) {
643 parse_change_cpu_data(name, surfxml_host_power, surfxml_host_availability,
644 surfxml_host_availability_file, surfxml_host_state_file);
645 surfxml_call_cb_functions(main_STag_surfxml_host_cb_list);
647 xbt_dict_foreach(cluster_host_props,cursor,key,data) {
648 xbt_dict_set(current_property_set, xbt_strdup(key), xbt_strdup(data), free);
651 surfxml_call_cb_functions(main_ETag_surfxml_host_cb_list);
652 free(surfxml_bufferstack);
655 current_property_set = xbt_dict_new();
657 surfxml_bufferstack = old_buff;
661 static const char* surfxml_link_bandwidth;
662 static const char* surfxml_link_bandwidth_file;
663 static const char* surfxml_link_latency;
664 static const char* surfxml_link_latency_file;
665 static const char* surfxml_link_state_file;
667 static void parse_link_foreach(void)
669 surfxml_link_bandwidth = A_surfxml_link_bandwidth;
670 surfxml_link_bandwidth_file = A_surfxml_link_bandwidth_file;
671 surfxml_link_latency = A_surfxml_link_latency;
672 surfxml_link_latency_file = A_surfxml_link_latency_file;
673 surfxml_link_state_file = A_surfxml_link_state_file;
676 static void finalize_link_foreach(void)
678 xbt_dynar_t names = NULL;
679 unsigned int cpt = 0;
681 xbt_dict_cursor_t cursor = NULL;
684 xbt_dict_t cluster_link_props = current_property_set;
686 xbt_assert1((names = xbt_dict_get_or_null(set_list, foreach_set_name)),
687 "Set name '%s' reffered by foreach tag not found.", foreach_set_name);
689 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);
692 /* for each name in set call the main link callback */
693 xbt_dynar_foreach (names, cpt, name) {
694 parse_change_link_data(name, surfxml_link_bandwidth, surfxml_link_bandwidth_file,
695 surfxml_link_latency, surfxml_link_latency_file, surfxml_link_state_file);
696 surfxml_call_cb_functions(main_STag_surfxml_link_cb_list);
698 xbt_dict_foreach(cluster_link_props,cursor,key,data) {
699 xbt_dict_set(current_property_set, xbt_strdup(key), xbt_strdup(data), free);
702 surfxml_call_cb_functions(main_ETag_surfxml_link_cb_list);
703 free(surfxml_bufferstack);
707 current_property_set = xbt_dict_new();
709 surfxml_bufferstack = old_buff;
712 void parse_foreach(void)
714 /* save the host & link callbacks */
715 main_STag_surfxml_host_cb_list = STag_surfxml_host_cb_list;
716 main_ETag_surfxml_host_cb_list = ETag_surfxml_host_cb_list;
717 main_STag_surfxml_link_cb_list = STag_surfxml_link_cb_list;
718 main_ETag_surfxml_link_cb_list = ETag_surfxml_link_cb_list;
720 /* define host & link callbacks to be used only by the foreach tag */
721 STag_surfxml_host_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
722 ETag_surfxml_host_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
723 STag_surfxml_link_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
724 ETag_surfxml_link_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
726 surfxml_add_callback(STag_surfxml_host_cb_list, &parse_host_foreach);
727 surfxml_add_callback(ETag_surfxml_host_cb_list, &finalize_host_foreach);
728 surfxml_add_callback(STag_surfxml_link_cb_list, &parse_link_foreach);
729 surfxml_add_callback(ETag_surfxml_link_cb_list, &finalize_link_foreach);
732 foreach_set_name = xbt_strdup(A_surfxml_foreach_set_id);
735 /* Route:multi functions */
737 static int route_multi_size=0;
738 static char* src_name, *dst_name;
739 static int is_symmetric_route;
741 void parse_route_elem(void)
745 val = xbt_strdup(A_surfxml_link_c_ctn_id);
747 xbt_dynar_push(route_link_list, &val);
750 void parse_route_multi_set_endpoints(void)
752 src_name = xbt_strdup(A_surfxml_route_c_multi_src);
753 dst_name = xbt_strdup(A_surfxml_route_c_multi_dst);
754 route_action = A_surfxml_route_c_multi_action;
755 is_symmetric_route = A_surfxml_route_c_multi_symmetric;
758 route_link_list = xbt_dynar_new(sizeof(char *), &free_string);
761 static int contains(xbt_dynar_t list, const char* value)
765 xbt_dynar_foreach(list, cpt, val) {
766 if (strcmp(val, value) == 0)
773 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.
774 The decision is based upon the value of action specified in the xml route:multi attribute action
776 void manage_route(xbt_dict_t routing_table, const char *route_name, int action, int isMultiRoute)
782 /* get already existing list if it exists */
783 links = xbt_dict_get_or_null(routing_table, route_name);
784 DEBUG1("ROUTE: %s", route_name);
787 case A_surfxml_route_action_PREPEND: /* add existing links at the end; route_link_list + links */
788 xbt_dynar_foreach(links, cpt, value) {
789 xbt_dynar_push(route_link_list,&value);
792 case A_surfxml_route_action_POSTPEND: /* add existing links in front; links + route_link_list */
793 xbt_dynar_foreach(route_link_list, cpt, value) {
794 xbt_dynar_push(links,&value);
796 route_link_list = links;
798 case A_surfxml_route_action_OVERRIDE:
803 /* this is the final route; do not add if name is a set; add only if name is in set list */
805 xbt_dict_set(routing_table, route_name, route_link_list, NULL);
809 void parse_route_multi_set_route(void)
813 route_name = bprintf("%s#%s#%d#%d#%d", src_name, dst_name, route_action, is_symmetric_route, route_multi_size);
815 xbt_dynar_push(route_multi_elements, &route_name);
818 xbt_dict_set(route_multi_table, route_name, route_link_list, NULL);
819 /* add symmetric if it is the case */
820 if (is_symmetric_route == 1) {
821 char * symmetric_name = bprintf("%s#%s#%d#%d#%d", dst_name, src_name, route_action, !is_symmetric_route, route_multi_size);
823 xbt_dict_set(route_multi_table, symmetric_name, route_link_list, NULL);
824 xbt_dynar_push(route_multi_elements, &symmetric_name);
825 is_symmetric_route = 0;
831 static void add_multi_links(const char* src, const char* dst, xbt_dynar_t links, const char* src_name, const char* dst_name)
835 parse_make_temporary_route(src_name, dst_name, route_action);
836 surfxml_call_cb_functions(STag_surfxml_route_cb_list);
837 DEBUG2("\tADDING ROUTE: %s -> %s", src_name, dst_name);
838 /* Build link list */
839 xbt_dynar_foreach(links, cpt, value) {
840 if (strcmp(value, src) == 0)
841 val = xbt_strdup(src_name);
842 else if (strcmp(value, dst) == 0)
843 val = xbt_strdup(dst_name);
844 else if (strcmp(value, "$dst") == 0)
845 val = xbt_strdup(dst_name);
846 else if (strcmp(value, "$src") == 0)
847 val = xbt_strdup(src_name);
849 val = xbt_strdup(value);
850 DEBUG1("\t\tELEMENT: %s", val);
851 xbt_dynar_push(route_link_list, &val);
853 surfxml_call_cb_functions(ETag_surfxml_route_cb_list);
854 free(surfxml_bufferstack);
857 static void convert_route_multi_to_routes(void)
859 xbt_dict_cursor_t cursor_w;
861 unsigned int cpt, cpt2, cursor;
862 char *src_host_name, *dst_host_name, *key, *src, *dst, *val, *key_w, *data_w;
865 xbt_dynar_t src_names = NULL, dst_names = NULL, links;
867 if (!route_multi_elements) return;
870 DEBUG1("%d", xbt_dict_length(workstation_set));
871 if (workstation_set != NULL && xbt_dict_length(workstation_set) > 0)
872 set = workstation_set;
875 old_buff = surfxml_bufferstack;
876 /* Get all routes in the exact order they were entered in the platform file */
877 xbt_dynar_foreach(route_multi_elements, cursor, key) {
878 /* Get links for the route */
879 links = (xbt_dynar_t)xbt_dict_get_or_null(route_multi_table, key);
880 keys = xbt_str_split_str(key, sep);
882 src = xbt_dynar_get_as(keys, 0, char*);
883 dst = xbt_dynar_get_as(keys, 1, char*);
884 route_action = atoi(xbt_dynar_get_as(keys, 2, char*));
885 symmetric = atoi(xbt_dynar_get_as(keys, 3, char*));
887 /* Create the dynar of src and dst hosts for the new routes */
888 /* NOTE: src and dst can be either set names or simple host names */
889 src_names = (xbt_dynar_t)xbt_dict_get_or_null(set_list, src);
890 dst_names = (xbt_dynar_t)xbt_dict_get_or_null(set_list, dst);
891 /* Add to dynar even if they are simple names */
892 if (src_names == NULL) {
893 src_names = xbt_dynar_new(sizeof(char *), &free_string);
894 val = xbt_strdup(src);
895 xbt_dynar_push(src_names, &val);
896 if (strcmp(val,"$*") != 0 && NULL == xbt_dict_get_or_null(set, val))
897 THROW3(unknown_error,0,"(In route:multi (%s -> %s) source %s does not exist (not a set or a host)", src, dst, src);
899 if (dst_names == NULL) {
900 dst_names = xbt_dynar_new(sizeof(char *), &free_string);
901 val = xbt_strdup(dst);
902 if (strcmp(val,"$*") != 0 && NULL == xbt_dict_get_or_null(set, val))
903 THROW3(unknown_error,0,"(In route:multi (%s -> %s) destination %s does not exist (not a set or a host)", src, dst, dst);
904 xbt_dynar_push(dst_names, &val);
907 /* Build the routes */
908 DEBUG2("ADDING MULTI ROUTE: %s -> %s", xbt_dynar_get_as(keys, 0, char*), xbt_dynar_get_as(keys, 1, char*));
909 xbt_dynar_foreach(src_names, cpt, src_host_name) {
910 xbt_dynar_foreach(dst_names, cpt2, dst_host_name) {
911 /* If dst is $* then set this route to have its dst point to all hosts */
912 if (strcmp(src_host_name,"$*") != 0 && strcmp(dst_host_name,"$*") == 0){
913 xbt_dict_foreach(set, cursor_w, key_w, data_w) {
914 //int n = xbt_dynar_member(src_names, (char*)key_w);
915 add_multi_links(src, dst, links, src_host_name, key_w);
918 /* If src is $* then set this route to have its dst point to all hosts */
919 if (strcmp(src_host_name,"$*") == 0 && strcmp(dst_host_name,"$*") != 0){
920 xbt_dict_foreach(set, cursor_w, key_w, data_w) {
921 // if (!symmetric || (symmetric && !contains(dst_names, key_w)))
922 add_multi_links(src, dst, links, key_w, dst_host_name);
925 /* if none of them are equal to $* */
926 if (strcmp(src_host_name,"$*") != 0 && strcmp(dst_host_name,"$*") != 0) {
927 add_multi_links(src, dst, links, src_host_name, dst_host_name);
932 surfxml_bufferstack = old_buff;
933 xbt_dict_free(&route_multi_table);
934 xbt_dynar_free(&route_multi_elements);
937 /* Cluster tag functions */
939 void parse_cluster(void)
941 static int AX_ptr = 0;
942 static int surfxml_bufferstack_size = 2048;
944 char* cluster_id = A_surfxml_cluster_id;
945 char* cluster_prefix = A_surfxml_cluster_prefix;
946 char* cluster_suffix = A_surfxml_cluster_suffix;
947 char* cluster_radical = A_surfxml_cluster_radical;
948 char* cluster_power = A_surfxml_cluster_power;
949 char* cluster_bw = A_surfxml_cluster_bw;
950 char* cluster_lat = A_surfxml_cluster_lat;
951 char* cluster_bb_bw = A_surfxml_cluster_bb_bw;
952 char* cluster_bb_lat = A_surfxml_cluster_bb_lat;
954 char* saved_buff = surfxml_bufferstack;
956 char * backbone_name;
958 surfxml_bufferstack = xbt_new0(char, surfxml_bufferstack_size);
961 SURFXML_BUFFER_SET(set_id, cluster_id);
962 SURFXML_BUFFER_SET(set_prefix, cluster_prefix);
963 SURFXML_BUFFER_SET(set_suffix, cluster_suffix);
964 SURFXML_BUFFER_SET(set_radical, cluster_radical);
966 SURFXML_START_TAG(set);
967 SURFXML_END_TAG(set);
970 SURFXML_BUFFER_SET(foreach_set_id, cluster_id);
972 SURFXML_START_TAG(foreach);
974 /* Make host for the foreach */
975 parse_change_cpu_data("$1", cluster_power, "1.0", "", "");
976 A_surfxml_host_state = A_surfxml_host_state_ON;
978 SURFXML_START_TAG(host);
979 SURFXML_END_TAG(host);
981 /* Make link for the foreach */
982 parse_change_link_data("$1", cluster_bw, "", cluster_lat, "", "");
983 A_surfxml_link_state = A_surfxml_link_state_ON;
984 A_surfxml_link_sharing_policy = A_surfxml_link_sharing_policy_SHARED;
986 SURFXML_START_TAG(link);
987 SURFXML_END_TAG(link);
989 SURFXML_END_TAG(foreach);
991 /* Make backbone link */
992 backbone_name = bprintf("%s_bb", cluster_id);
993 parse_change_link_data(backbone_name, cluster_bb_bw, "", cluster_bb_lat, "", "");
994 A_surfxml_link_state = A_surfxml_link_state_ON;
995 A_surfxml_link_sharing_policy = A_surfxml_link_sharing_policy_FATPIPE;
997 SURFXML_START_TAG(link);
998 SURFXML_END_TAG(link);
1000 /* Make route multi with the outside world, i.e. cluster->$* */
1001 SURFXML_BUFFER_SET(route_c_multi_src, cluster_id);
1002 SURFXML_BUFFER_SET(route_c_multi_dst, "$*");
1003 A_surfxml_route_c_multi_symmetric = A_surfxml_route_c_multi_symmetric_NO;
1004 A_surfxml_route_c_multi_action = A_surfxml_route_c_multi_action_OVERRIDE;
1006 SURFXML_START_TAG(route_c_multi);
1008 SURFXML_BUFFER_SET(link_c_ctn_id, "$src");
1010 SURFXML_START_TAG(link_c_ctn);
1011 SURFXML_END_TAG(link_c_ctn);
1013 SURFXML_END_TAG(route_c_multi);
1015 /* Make route multi between cluster hosts, i.e. cluster->cluster */
1016 SURFXML_BUFFER_SET(route_c_multi_src, cluster_id);
1017 SURFXML_BUFFER_SET(route_c_multi_dst, cluster_id);
1018 A_surfxml_route_c_multi_action = A_surfxml_route_c_multi_action_POSTPEND;
1019 A_surfxml_route_c_multi_symmetric = A_surfxml_route_c_multi_symmetric_NO;
1021 SURFXML_START_TAG(route_c_multi);
1023 SURFXML_BUFFER_SET(link_c_ctn_id, backbone_name);
1025 SURFXML_START_TAG(link_c_ctn);
1026 SURFXML_END_TAG(link_c_ctn);
1028 SURFXML_END_TAG(route_c_multi);
1032 free(surfxml_bufferstack);
1033 surfxml_bufferstack = saved_buff;
1036 /* Trace management functions */
1038 static double trace_periodicity = -1.0;
1039 static char* trace_file = NULL;
1040 static char* trace_id;
1042 void parse_trace_init(void)
1044 trace_id = strdup(A_surfxml_trace_id);
1045 trace_file = strdup(A_surfxml_trace_file);
1046 surf_parse_get_double(&trace_periodicity, A_surfxml_trace_periodicity);
1049 void parse_trace_finalize(void)
1052 if (!trace_file || strcmp(trace_file,"") != 0) {
1053 surf_parse_get_trace(&trace, trace_file);
1056 if (strcmp(surfxml_pcdata, "") == 0) trace = NULL;
1058 trace = tmgr_trace_new_from_string(trace_id, surfxml_pcdata, trace_periodicity);
1060 xbt_dict_set(traces_set_list, trace_id, (void *)trace, NULL);
1063 void parse_trace_c_connect(void)
1065 xbt_assert2(xbt_dict_get_or_null(traces_set_list, A_surfxml_trace_c_connect_trace),
1066 "Cannot connect trace %s to %s: trace unknown", A_surfxml_trace_c_connect_trace,A_surfxml_trace_c_connect_element);
1068 switch (A_surfxml_trace_c_connect_kind) {
1069 case A_surfxml_trace_c_connect_kind_HOST_AVAIL:
1070 xbt_dict_set(trace_connect_list_host_avail, A_surfxml_trace_c_connect_element, xbt_strdup(A_surfxml_trace_c_connect_trace), free);
1072 case A_surfxml_trace_c_connect_kind_POWER:
1073 xbt_dict_set(trace_connect_list_power, A_surfxml_trace_c_connect_element, xbt_strdup(A_surfxml_trace_c_connect_trace), free);
1075 case A_surfxml_trace_c_connect_kind_LINK_AVAIL:
1076 xbt_dict_set(trace_connect_list_link_avail, A_surfxml_trace_c_connect_element, xbt_strdup(A_surfxml_trace_c_connect_trace), free);
1078 case A_surfxml_trace_c_connect_kind_BANDWIDTH:
1079 xbt_dict_set(trace_connect_list_bandwidth, A_surfxml_trace_c_connect_element, xbt_strdup(A_surfxml_trace_c_connect_trace), free);
1081 case A_surfxml_trace_c_connect_kind_LATENCY:
1082 xbt_dict_set(trace_connect_list_latency, A_surfxml_trace_c_connect_element, xbt_strdup(A_surfxml_trace_c_connect_trace), free);
1085 xbt_die("kind of trace unknown");
1089 /* Random tag functions */
1091 double get_cpu_power(const char *power)
1093 double power_scale = 0.0;
1096 random_data_t random = NULL;
1097 /* randomness is inserted like this: power="$rand(my_random)" */
1098 if (((p = strstr(power, "$rand(")) != NULL) && ((q = strstr(power, ")")) != NULL)) {
1100 generator = xbt_malloc(q - (p + 6) + 1);
1101 memcpy(generator, p + 6, q - (p + 6));
1102 generator[q - (p + 6)] = '\0';
1103 xbt_assert1((random = xbt_dict_get_or_null(random_data_list, generator)),
1104 "Random generator %s undefined", generator);
1105 power_scale = random_generate(random);
1109 surf_parse_get_double(&power_scale, power);
1114 int random_min, random_max, random_mean, random_std_deviation, random_generator;
1117 void init_randomness(void)
1119 random_id = A_surfxml_random_id;
1120 surf_parse_get_int(&random_min, A_surfxml_random_min);
1121 surf_parse_get_int(&random_max, A_surfxml_random_max);
1122 surf_parse_get_int(&random_mean, A_surfxml_random_mean);
1123 surf_parse_get_int(&random_std_deviation, A_surfxml_random_std_deviation);
1124 random_generator = A_surfxml_random_generator;
1127 void add_randomness(void)
1129 /* If needed aditional properties can be added by using the prop tag */
1130 random_data_t random = random_new(random_generator, 0, random_min, random_max, random_mean, random_std_deviation);
1131 xbt_dict_set(random_data_list, random_id, (void *)random, NULL);