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_dynar_free(&route_link_list);
233 xbt_dict_free(&random_data_list);
234 xbt_dict_free(&set_list);
238 void STag_surfxml_host(void)
240 surfxml_call_cb_functions(STag_surfxml_host_cb_list);
243 void ETag_surfxml_host(void)
245 surfxml_call_cb_functions(ETag_surfxml_host_cb_list);
248 void STag_surfxml_router(void)
250 surfxml_call_cb_functions(STag_surfxml_router_cb_list);
253 void ETag_surfxml_router(void)
255 surfxml_call_cb_functions(ETag_surfxml_router_cb_list);
258 void STag_surfxml_link(void)
260 surfxml_call_cb_functions(STag_surfxml_link_cb_list);
263 void ETag_surfxml_link(void)
265 surfxml_call_cb_functions(ETag_surfxml_link_cb_list);
268 void STag_surfxml_route(void)
270 surfxml_call_cb_functions(STag_surfxml_route_cb_list);
273 void ETag_surfxml_route(void)
275 surfxml_call_cb_functions(ETag_surfxml_route_cb_list);
278 void STag_surfxml_link_c_ctn(void)
280 surfxml_call_cb_functions(STag_surfxml_link_c_ctn_cb_list);
283 void ETag_surfxml_link_c_ctn(void)
285 surfxml_call_cb_functions(ETag_surfxml_link_c_ctn_cb_list);
288 void STag_surfxml_process(void)
290 surfxml_call_cb_functions(STag_surfxml_process_cb_list);
293 void ETag_surfxml_process(void)
295 surfxml_call_cb_functions(ETag_surfxml_process_cb_list);
298 void STag_surfxml_argument(void)
300 surfxml_call_cb_functions(STag_surfxml_argument_cb_list);
303 void ETag_surfxml_argument(void)
305 surfxml_call_cb_functions(ETag_surfxml_argument_cb_list);
308 void STag_surfxml_prop(void)
310 surfxml_call_cb_functions(STag_surfxml_prop_cb_list);
312 void ETag_surfxml_prop(void)
314 surfxml_call_cb_functions(ETag_surfxml_prop_cb_list);
317 void STag_surfxml_set(void)
319 surfxml_call_cb_functions(STag_surfxml_set_cb_list);
322 void ETag_surfxml_set(void)
324 surfxml_call_cb_functions(ETag_surfxml_set_cb_list);
327 void STag_surfxml_foreach(void)
329 /* Save the current buffer */
330 old_buff = surfxml_bufferstack;
331 surfxml_call_cb_functions(STag_surfxml_foreach_cb_list);
334 void ETag_surfxml_foreach(void)
336 surfxml_call_cb_functions(ETag_surfxml_foreach_cb_list);
338 /* free the temporary dynar and restore original */
339 xbt_dynar_free(&STag_surfxml_host_cb_list);
340 xbt_dynar_free(&ETag_surfxml_host_cb_list);
342 STag_surfxml_host_cb_list = main_STag_surfxml_host_cb_list;
343 ETag_surfxml_host_cb_list = main_ETag_surfxml_host_cb_list;
345 /* free the temporary dynar and restore original */
346 xbt_dynar_free(&STag_surfxml_link_cb_list);
347 xbt_dynar_free(&ETag_surfxml_link_cb_list);
349 STag_surfxml_link_cb_list = main_STag_surfxml_link_cb_list;
350 ETag_surfxml_link_cb_list = main_ETag_surfxml_link_cb_list;
354 void STag_surfxml_route_c_multi(void)
356 surfxml_call_cb_functions(STag_surfxml_route_c_multi_cb_list);
359 void ETag_surfxml_route_c_multi(void)
361 surfxml_call_cb_functions(ETag_surfxml_route_c_multi_cb_list);
364 void STag_surfxml_cluster(void)
366 surfxml_call_cb_functions(STag_surfxml_cluster_cb_list);
369 void ETag_surfxml_cluster(void)
371 surfxml_call_cb_functions(ETag_surfxml_cluster_cb_list);
374 void STag_surfxml_trace(void)
376 surfxml_call_cb_functions(STag_surfxml_trace_cb_list);
379 void ETag_surfxml_trace(void)
381 surfxml_call_cb_functions(ETag_surfxml_trace_cb_list);
384 void STag_surfxml_trace_c_connect(void)
386 surfxml_call_cb_functions(STag_surfxml_trace_c_connect_cb_list);
389 void ETag_surfxml_trace_c_connect(void)
391 surfxml_call_cb_functions(ETag_surfxml_trace_c_connect_cb_list);
394 void STag_surfxml_random(void)
396 surfxml_call_cb_functions(STag_surfxml_random_cb_list);
399 void ETag_surfxml_random(void)
401 surfxml_call_cb_functions(ETag_surfxml_random_cb_list);
404 void surf_parse_open(const char *file)
406 static int warned = 0; /* warn only once */
410 ("Bypassing the XML parser since surf_parse_open received a NULL pointer. If it is not what you want, go fix your code.");
415 if (!surf_input_buffer_stack)
416 surf_input_buffer_stack = xbt_dynar_new(sizeof(YY_BUFFER_STATE), NULL);
417 if (!surf_file_to_parse_stack)
418 surf_file_to_parse_stack = xbt_dynar_new(sizeof(FILE *), NULL);
420 surf_file_to_parse = surf_fopen(file, "r");
421 xbt_assert1((surf_file_to_parse), "Unable to open \"%s\"\n", file);
422 surf_input_buffer = surf_parse__create_buffer(surf_file_to_parse, 10);
423 surf_parse__switch_to_buffer(surf_input_buffer);
424 surf_parse_lineno = 1;
427 void surf_parse_close(void)
429 if (surf_input_buffer_stack)
430 xbt_dynar_free(&surf_input_buffer_stack);
431 if (surf_file_to_parse_stack)
432 xbt_dynar_free(&surf_file_to_parse_stack);
434 if (surf_file_to_parse) {
435 surf_parse__delete_buffer(surf_input_buffer);
436 fclose(surf_file_to_parse);
441 static int _surf_parse(void)
443 return surf_parse_lex();
446 int_f_void_t surf_parse = _surf_parse;
448 void surf_parse_get_double(double *value, const char *string)
452 ret = sscanf(string, "%lg", value);
453 xbt_assert2((ret == 1), "Parse error line %d : %s not a number",
454 surf_parse_lineno, string);
457 void surf_parse_get_int(int *value, const char *string)
461 ret = sscanf(string, "%d", value);
462 xbt_assert2((ret == 1), "Parse error line %d : %s not a number",
463 surf_parse_lineno, string);
466 void surf_parse_get_trace(tmgr_trace_t * trace, const char *string)
468 if ((!string) || (strcmp(string, "") == 0))
471 *trace = tmgr_trace_new(string);
474 void parse_properties(void)
478 if(!current_property_set) current_property_set = xbt_dict_new();
480 value = xbt_strdup(A_surfxml_prop_value);
481 xbt_dict_set(current_property_set, A_surfxml_prop_id, value, free);
484 void surfxml_add_callback(xbt_dynar_t cb_list, void_f_void_t function)
486 xbt_dynar_push(cb_list, &function);
489 static XBT_INLINE void surfxml_call_cb_functions(xbt_dynar_t cb_list)
491 unsigned int iterator;
493 xbt_dynar_foreach(cb_list, iterator, fun){
494 DEBUG2("call %p %p",fun,*fun);
501 xbt_dict_free(&route_table);
502 xbt_dynar_free(&route_link_list);
503 route_table = xbt_dict_new();
505 route_multi_table = xbt_dict_new();
506 route_multi_elements = xbt_dynar_new(sizeof(char*), NULL);
507 traces_set_list = xbt_dict_new();
509 trace_connect_list_host_avail = xbt_dict_new();
510 trace_connect_list_power = xbt_dict_new();
511 trace_connect_list_link_avail = xbt_dict_new();
512 trace_connect_list_bandwidth = xbt_dict_new();
513 trace_connect_list_latency = xbt_dict_new();
515 random_data_list = xbt_dict_new();
518 void parse_platform_file(const char* file)
520 surf_parse_open(file);
521 xbt_assert1((!(*surf_parse)()), "Parse error in %s", file);
525 /* Functions to bypass route, host and link tags. Used by the foreach and route:multi tags */
527 static void parse_make_temporary_route(const char *src, const char *dst, int action)
530 surfxml_bufferstack = xbt_new0(char, 2048);
532 A_surfxml_route_action = action;
533 SURFXML_BUFFER_SET(route_src, src);
534 SURFXML_BUFFER_SET(route_dst, dst);
537 static void parse_change_cpu_data(const char* hostName, const char* surfxml_host_power, const char* surfxml_host_availability,
538 const char* surfxml_host_availability_file, const char* surfxml_host_state_file)
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);
550 static void parse_change_link_data(const char* linkName, const char* surfxml_link_bandwidth, const char* surfxml_link_bandwidth_file,
551 const char* surfxml_link_latency, const char* surfxml_link_latency_file, const char* surfxml_link_state_file)
554 surfxml_bufferstack = xbt_new0(char, 2048);
556 SURFXML_BUFFER_SET(link_id, linkName);
557 SURFXML_BUFFER_SET(link_bandwidth, surfxml_link_bandwidth);
558 SURFXML_BUFFER_SET(link_bandwidth_file, surfxml_link_bandwidth_file);
559 SURFXML_BUFFER_SET(link_latency, surfxml_link_latency);
560 SURFXML_BUFFER_SET(link_latency_file, surfxml_link_latency_file);
561 SURFXML_BUFFER_SET(link_state_file, surfxml_link_state_file);
565 * \brief Restores the original surfxml buffer
567 static void parse_restore_original_buffer(void)
569 free(surfxml_bufferstack);
570 surfxml_bufferstack = old_buff;
573 /* Functions for the sets and foreach tags */
575 void parse_sets(void)
577 char *id, *suffix, *prefix, *radical;
579 xbt_dynar_t radical_ends;
580 xbt_dynar_t current_set;
584 id = xbt_strdup(A_surfxml_set_id);
585 prefix = xbt_strdup(A_surfxml_set_prefix);
586 suffix = xbt_strdup(A_surfxml_set_suffix);
587 radical = xbt_strdup(A_surfxml_set_radical);
589 xbt_assert1(!xbt_dict_get_or_null(set_list, id),
590 "Set '%s' declared several times in the platform file.",id);
591 radical_ends = xbt_str_split(radical, "-");
592 xbt_assert1((xbt_dynar_length(radical_ends)==2), "Radical must be in the form lvalue-rvalue! Provided value: %s", radical);
594 surf_parse_get_int(&start, xbt_dynar_get_as(radical_ends, 0, char*));
595 surf_parse_get_int(&end, xbt_dynar_get_as(radical_ends, 1, char*));
597 current_set = xbt_dynar_new(sizeof(char*), NULL);
600 for (i=start; i<=end; i++) {
601 value = bprintf("%s%d%s", prefix, i, suffix);
602 xbt_dynar_push(current_set, &value);
605 xbt_dict_set(set_list, id, current_set, NULL);
608 static const char* surfxml_host_power;
609 static const char* surfxml_host_availability;
610 static const char* surfxml_host_availability_file;
611 static const char* surfxml_host_state_file;
613 static void parse_host_foreach(void)
615 surfxml_host_power = A_surfxml_host_power;
616 surfxml_host_availability = A_surfxml_host_availability;
617 surfxml_host_availability_file = A_surfxml_host_availability_file;
618 surfxml_host_state_file = A_surfxml_host_state_file;
621 static void finalize_host_foreach(void)
623 xbt_dynar_t names = NULL;
624 unsigned int cpt = 0;
626 xbt_dict_cursor_t cursor = NULL;
629 xbt_dict_t cluster_host_props = current_property_set;
631 xbt_assert1((names = xbt_dict_get_or_null(set_list, foreach_set_name)),
632 "Set name '%s' reffered by foreach tag not found.", foreach_set_name);
634 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);
637 /* foreach name in set call the main host callback */
638 xbt_dynar_foreach (names, cpt, name) {
639 parse_change_cpu_data(name, surfxml_host_power, surfxml_host_availability,
640 surfxml_host_availability_file, surfxml_host_state_file);
641 surfxml_call_cb_functions(main_STag_surfxml_host_cb_list);
643 xbt_dict_foreach(cluster_host_props,cursor,key,data) {
644 xbt_dict_set(current_property_set, xbt_strdup(key), xbt_strdup(data), free);
647 surfxml_call_cb_functions(main_ETag_surfxml_host_cb_list);
648 free(surfxml_bufferstack);
651 current_property_set = xbt_dict_new();
653 surfxml_bufferstack = old_buff;
657 static const char* surfxml_link_bandwidth;
658 static const char* surfxml_link_bandwidth_file;
659 static const char* surfxml_link_latency;
660 static const char* surfxml_link_latency_file;
661 static const char* surfxml_link_state_file;
663 static void parse_link_foreach(void)
665 surfxml_link_bandwidth = A_surfxml_link_bandwidth;
666 surfxml_link_bandwidth_file = A_surfxml_link_bandwidth_file;
667 surfxml_link_latency = A_surfxml_link_latency;
668 surfxml_link_latency_file = A_surfxml_link_latency_file;
669 surfxml_link_state_file = A_surfxml_link_state_file;
672 static void finalize_link_foreach(void)
674 xbt_dynar_t names = NULL;
675 unsigned int cpt = 0;
677 xbt_dict_cursor_t cursor = NULL;
680 xbt_dict_t cluster_link_props = current_property_set;
682 xbt_assert1((names = xbt_dict_get_or_null(set_list, foreach_set_name)),
683 "Set name '%s' reffered by foreach tag not found.", foreach_set_name);
685 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);
688 /* for each name in set call the main link callback */
689 xbt_dynar_foreach (names, cpt, name) {
690 parse_change_link_data(name, surfxml_link_bandwidth, surfxml_link_bandwidth_file,
691 surfxml_link_latency, surfxml_link_latency_file, surfxml_link_state_file);
692 surfxml_call_cb_functions(main_STag_surfxml_link_cb_list);
694 xbt_dict_foreach(cluster_link_props,cursor,key,data) {
695 xbt_dict_set(current_property_set, xbt_strdup(key), xbt_strdup(data), free);
698 surfxml_call_cb_functions(main_ETag_surfxml_link_cb_list);
699 free(surfxml_bufferstack);
703 current_property_set = xbt_dict_new();
705 surfxml_bufferstack = old_buff;
708 void parse_foreach(void)
710 /* save the host & link callbacks */
711 main_STag_surfxml_host_cb_list = STag_surfxml_host_cb_list;
712 main_ETag_surfxml_host_cb_list = ETag_surfxml_host_cb_list;
713 main_STag_surfxml_link_cb_list = STag_surfxml_link_cb_list;
714 main_ETag_surfxml_link_cb_list = ETag_surfxml_link_cb_list;
716 /* define host & link callbacks to be used only by the foreach tag */
717 STag_surfxml_host_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
718 ETag_surfxml_host_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
719 STag_surfxml_link_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
720 ETag_surfxml_link_cb_list = xbt_dynar_new(sizeof(void_f_void_t),NULL);
722 surfxml_add_callback(STag_surfxml_host_cb_list, &parse_host_foreach);
723 surfxml_add_callback(ETag_surfxml_host_cb_list, &finalize_host_foreach);
724 surfxml_add_callback(STag_surfxml_link_cb_list, &parse_link_foreach);
725 surfxml_add_callback(ETag_surfxml_link_cb_list, &finalize_link_foreach);
728 foreach_set_name = xbt_strdup(A_surfxml_foreach_set_id);
731 /* Route:multi functions */
733 static int route_multi_size=0;
734 static char* src_name, *dst_name;
735 static int is_symmetric_route;
737 void parse_route_elem(void)
741 val = xbt_strdup(A_surfxml_link_c_ctn_id);
743 xbt_dynar_push(route_link_list, &val);
746 void parse_route_multi_set_endpoints(void)
748 src_name = xbt_strdup(A_surfxml_route_c_multi_src);
749 dst_name = xbt_strdup(A_surfxml_route_c_multi_dst);
750 route_action = A_surfxml_route_c_multi_action;
751 is_symmetric_route = A_surfxml_route_c_multi_symmetric;
754 route_link_list = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
757 static int contains(xbt_dynar_t list, const char* value)
761 xbt_dynar_foreach(list, cpt, val) {
762 if (strcmp(val, value) == 0)
769 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.
770 The decision is based upon the value of action specified in the xml route:multi attribute action
772 void manage_route(xbt_dict_t routing_table, const char *route_name, int action, int isMultiRoute)
778 /* get already existing list if it exists */
779 links = xbt_dict_get_or_null(routing_table, route_name);
780 DEBUG1("ROUTE: %s", route_name);
783 case A_surfxml_route_action_PREPEND: /* add existing links at the end; route_link_list + links */
784 xbt_dynar_foreach(links, cpt, value) {
785 xbt_dynar_push(route_link_list,&value);
788 case A_surfxml_route_action_POSTPEND: /* add existing links in front; links + route_link_list */
789 xbt_dynar_foreach(route_link_list, cpt, value) {
790 xbt_dynar_push(links,&value);
792 route_link_list = links;
794 case A_surfxml_route_action_OVERRIDE:
799 /* this is the final route; do not add if name is a set; add only if name is in set list */
801 xbt_dict_set(routing_table, route_name, route_link_list, NULL);
805 void parse_route_multi_set_route(void)
809 route_name = bprintf("%s#%s#%d#%d#%d", src_name, dst_name, route_action, is_symmetric_route, route_multi_size);
811 xbt_dynar_push(route_multi_elements, &route_name);
814 xbt_dict_set(route_multi_table, route_name, route_link_list, NULL);
815 /* add symmetric if it is the case */
816 if (is_symmetric_route == 1) {
817 char * symmetric_name = bprintf("%s#%s#%d#%d#%d", dst_name, src_name, route_action, !is_symmetric_route, route_multi_size);
819 xbt_dict_set(route_multi_table, symmetric_name, route_link_list, NULL);
820 xbt_dynar_push(route_multi_elements, &symmetric_name);
821 is_symmetric_route = 0;
827 static void add_multi_links(const char* src, const char* dst, xbt_dynar_t links, const char* src_name, const char* dst_name)
831 parse_make_temporary_route(src_name, dst_name, route_action);
832 surfxml_call_cb_functions(STag_surfxml_route_cb_list);
833 DEBUG2("\tADDING ROUTE: %s -> %s", src_name, dst_name);
834 /* Build link list */
835 xbt_dynar_foreach(links, cpt, value) {
836 if (strcmp(value, src) == 0)
837 val = xbt_strdup(src_name);
838 else if (strcmp(value, dst) == 0)
839 val = xbt_strdup(dst_name);
840 else if (strcmp(value, "$dst") == 0)
841 val = xbt_strdup(dst_name);
842 else if (strcmp(value, "$src") == 0)
843 val = xbt_strdup(src_name);
845 val = xbt_strdup(value);
846 DEBUG1("\t\tELEMENT: %s", val);
847 xbt_dynar_push(route_link_list, &val);
849 surfxml_call_cb_functions(ETag_surfxml_route_cb_list);
850 free(surfxml_bufferstack);
853 static void convert_route_multi_to_routes(void)
855 xbt_dict_cursor_t cursor_w;
857 unsigned int cpt, cpt2, cursor;
858 char *src_host_name, *dst_host_name, *key, *src, *dst, *val, *key_w, *data_w;
861 xbt_dynar_t src_names = NULL, dst_names = NULL, links;
863 if (!route_multi_elements) return;
866 DEBUG1("%d", xbt_dict_length(workstation_set));
867 if (workstation_set != NULL && xbt_dict_length(workstation_set) > 0)
868 set = workstation_set;
871 old_buff = surfxml_bufferstack;
872 /* Get all routes in the exact order they were entered in the platform file */
873 xbt_dynar_foreach(route_multi_elements, cursor, key) {
874 /* Get links for the route */
875 links = (xbt_dynar_t)xbt_dict_get_or_null(route_multi_table, key);
876 keys = xbt_str_split_str(key, sep);
878 src = xbt_dynar_get_as(keys, 0, char*);
879 dst = xbt_dynar_get_as(keys, 1, char*);
880 route_action = atoi(xbt_dynar_get_as(keys, 2, char*));
881 symmetric = atoi(xbt_dynar_get_as(keys, 3, char*));
883 /* Create the dynar of src and dst hosts for the new routes */
884 /* NOTE: src and dst can be either set names or simple host names */
885 src_names = (xbt_dynar_t)xbt_dict_get_or_null(set_list, src);
886 dst_names = (xbt_dynar_t)xbt_dict_get_or_null(set_list, dst);
887 /* Add to dynar even if they are simple names */
888 if (src_names == NULL) {
889 src_names = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
890 val = xbt_strdup(src);
891 xbt_dynar_push(src_names, &val);
892 if (strcmp(val,"$*") != 0 && NULL == xbt_dict_get_or_null(set, val))
893 THROW3(unknown_error,0,"(In route:multi (%s -> %s) source %s does not exist (not a set or a host)", src, dst, src);
895 if (dst_names == NULL) {
896 dst_names = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
897 val = xbt_strdup(dst);
898 if (strcmp(val,"$*") != 0 && NULL == xbt_dict_get_or_null(set, val))
899 THROW3(unknown_error,0,"(In route:multi (%s -> %s) destination %s does not exist (not a set or a host)", src, dst, dst);
900 xbt_dynar_push(dst_names, &val);
903 /* Build the routes */
904 DEBUG2("ADDING MULTI ROUTE: %s -> %s", xbt_dynar_get_as(keys, 0, char*), xbt_dynar_get_as(keys, 1, char*));
905 xbt_dynar_foreach(src_names, cpt, src_host_name) {
906 xbt_dynar_foreach(dst_names, cpt2, dst_host_name) {
907 /* If dst is $* then set this route to have its dst point to all hosts */
908 if (strcmp(src_host_name,"$*") != 0 && strcmp(dst_host_name,"$*") == 0){
909 xbt_dict_foreach(set, cursor_w, key_w, data_w) {
910 //int n = xbt_dynar_member(src_names, (char*)key_w);
911 add_multi_links(src, dst, links, src_host_name, key_w);
914 /* If src is $* then set this route to have its dst point to all hosts */
915 if (strcmp(src_host_name,"$*") == 0 && strcmp(dst_host_name,"$*") != 0){
916 xbt_dict_foreach(set, cursor_w, key_w, data_w) {
917 // if (!symmetric || (symmetric && !contains(dst_names, key_w)))
918 add_multi_links(src, dst, links, key_w, dst_host_name);
921 /* if none of them are equal to $* */
922 if (strcmp(src_host_name,"$*") != 0 && strcmp(dst_host_name,"$*") != 0) {
923 add_multi_links(src, dst, links, src_host_name, dst_host_name);
928 surfxml_bufferstack = old_buff;
929 xbt_dict_free(&route_multi_table);
930 xbt_dynar_free(&route_multi_elements);
933 /* Cluster tag functions */
935 void parse_cluster(void)
937 static int AX_ptr = 0;
938 static int surfxml_bufferstack_size = 2048;
940 char* cluster_id = A_surfxml_cluster_id;
941 char* cluster_prefix = A_surfxml_cluster_prefix;
942 char* cluster_suffix = A_surfxml_cluster_suffix;
943 char* cluster_radical = A_surfxml_cluster_radical;
944 char* cluster_power = A_surfxml_cluster_power;
945 char* cluster_bw = A_surfxml_cluster_bw;
946 char* cluster_lat = A_surfxml_cluster_lat;
947 char* cluster_bb_bw = A_surfxml_cluster_bb_bw;
948 char* cluster_bb_lat = A_surfxml_cluster_bb_lat;
950 char* saved_buff = surfxml_bufferstack;
952 char * backbone_name;
954 surfxml_bufferstack = xbt_new0(char, surfxml_bufferstack_size);
957 SURFXML_BUFFER_SET(set_id, cluster_id);
958 SURFXML_BUFFER_SET(set_prefix, cluster_prefix);
959 SURFXML_BUFFER_SET(set_suffix, cluster_suffix);
960 SURFXML_BUFFER_SET(set_radical, cluster_radical);
962 SURFXML_START_TAG(set);
963 SURFXML_END_TAG(set);
966 SURFXML_BUFFER_SET(foreach_set_id, cluster_id);
968 SURFXML_START_TAG(foreach);
970 /* Make host for the foreach */
971 parse_change_cpu_data("$1", cluster_power, "1.0", "", "");
972 A_surfxml_host_state = A_surfxml_host_state_ON;
974 SURFXML_START_TAG(host);
975 SURFXML_END_TAG(host);
977 /* Make link for the foreach */
978 parse_change_link_data("$1", cluster_bw, "", cluster_lat, "", "");
979 A_surfxml_link_state = A_surfxml_link_state_ON;
980 A_surfxml_link_sharing_policy = A_surfxml_link_sharing_policy_SHARED;
982 SURFXML_START_TAG(link);
983 SURFXML_END_TAG(link);
985 SURFXML_END_TAG(foreach);
987 /* Make backbone link */
988 backbone_name = bprintf("%s_bb", cluster_id);
989 parse_change_link_data(backbone_name, cluster_bb_bw, "", cluster_bb_lat, "", "");
990 A_surfxml_link_state = A_surfxml_link_state_ON;
991 A_surfxml_link_sharing_policy = A_surfxml_link_sharing_policy_FATPIPE;
993 SURFXML_START_TAG(link);
994 SURFXML_END_TAG(link);
996 /* Make route multi with the outside world, i.e. cluster->$* */
997 SURFXML_BUFFER_SET(route_c_multi_src, cluster_id);
998 SURFXML_BUFFER_SET(route_c_multi_dst, "$*");
999 A_surfxml_route_c_multi_symmetric = A_surfxml_route_c_multi_symmetric_NO;
1000 A_surfxml_route_c_multi_action = A_surfxml_route_c_multi_action_OVERRIDE;
1002 SURFXML_START_TAG(route_c_multi);
1004 SURFXML_BUFFER_SET(link_c_ctn_id, "$src");
1006 SURFXML_START_TAG(link_c_ctn);
1007 SURFXML_END_TAG(link_c_ctn);
1009 SURFXML_END_TAG(route_c_multi);
1011 /* Make route multi between cluster hosts, i.e. cluster->cluster */
1012 SURFXML_BUFFER_SET(route_c_multi_src, cluster_id);
1013 SURFXML_BUFFER_SET(route_c_multi_dst, cluster_id);
1014 A_surfxml_route_c_multi_action = A_surfxml_route_c_multi_action_POSTPEND;
1015 A_surfxml_route_c_multi_symmetric = A_surfxml_route_c_multi_symmetric_NO;
1017 SURFXML_START_TAG(route_c_multi);
1019 SURFXML_BUFFER_SET(link_c_ctn_id, backbone_name);
1021 SURFXML_START_TAG(link_c_ctn);
1022 SURFXML_END_TAG(link_c_ctn);
1024 SURFXML_END_TAG(route_c_multi);
1028 free(surfxml_bufferstack);
1029 surfxml_bufferstack = saved_buff;
1032 /* Trace management functions */
1034 static double trace_periodicity = -1.0;
1035 static char* trace_file = NULL;
1036 static char* trace_id;
1038 void parse_trace_init(void)
1040 trace_id = strdup(A_surfxml_trace_id);
1041 trace_file = strdup(A_surfxml_trace_file);
1042 surf_parse_get_double(&trace_periodicity, A_surfxml_trace_periodicity);
1045 void parse_trace_finalize(void)
1048 if (!trace_file || strcmp(trace_file,"") != 0) {
1049 surf_parse_get_trace(&trace, trace_file);
1052 if (strcmp(surfxml_pcdata, "") == 0) trace = NULL;
1054 trace = tmgr_trace_new_from_string(trace_id, surfxml_pcdata, trace_periodicity);
1056 xbt_dict_set(traces_set_list, trace_id, (void *)trace, NULL);
1059 void parse_trace_c_connect(void)
1061 xbt_assert2(xbt_dict_get_or_null(traces_set_list, A_surfxml_trace_c_connect_trace),
1062 "Cannot connect trace %s to %s: trace unknown", A_surfxml_trace_c_connect_trace,A_surfxml_trace_c_connect_element);
1064 switch (A_surfxml_trace_c_connect_kind) {
1065 case A_surfxml_trace_c_connect_kind_HOST_AVAIL:
1066 xbt_dict_set(trace_connect_list_host_avail, A_surfxml_trace_c_connect_trace, xbt_strdup(A_surfxml_trace_c_connect_element), free);
1068 case A_surfxml_trace_c_connect_kind_POWER:
1069 xbt_dict_set(trace_connect_list_power, A_surfxml_trace_c_connect_trace, xbt_strdup(A_surfxml_trace_c_connect_element), free);
1071 case A_surfxml_trace_c_connect_kind_LINK_AVAIL:
1072 xbt_dict_set(trace_connect_list_link_avail, A_surfxml_trace_c_connect_trace, xbt_strdup(A_surfxml_trace_c_connect_element), free);
1074 case A_surfxml_trace_c_connect_kind_BANDWIDTH:
1075 xbt_dict_set(trace_connect_list_bandwidth, A_surfxml_trace_c_connect_trace, xbt_strdup(A_surfxml_trace_c_connect_element), free);
1077 case A_surfxml_trace_c_connect_kind_LATENCY:
1078 xbt_dict_set(trace_connect_list_latency, A_surfxml_trace_c_connect_trace, xbt_strdup(A_surfxml_trace_c_connect_element), free);
1081 xbt_die("kind of trace unknown");
1085 /* Random tag functions */
1087 double get_cpu_power(const char *power)
1089 double power_scale = 0.0;
1092 random_data_t random = NULL;
1093 /* randomness is inserted like this: power="$rand(my_random)" */
1094 if (((p = strstr(power, "$rand(")) != NULL) && ((q = strstr(power, ")")) != NULL)) {
1096 generator = xbt_malloc(q - (p + 6) + 1);
1097 memcpy(generator, p + 6, q - (p + 6));
1098 generator[q - (p + 6)] = '\0';
1099 xbt_assert1((random = xbt_dict_get_or_null(random_data_list, generator)),
1100 "Random generator %s undefined", generator);
1101 power_scale = random_generate(random);
1105 surf_parse_get_double(&power_scale, power);
1110 int random_min, random_max, random_mean, random_std_deviation, random_generator;
1113 void init_randomness(void)
1115 random_id = A_surfxml_random_id;
1116 surf_parse_get_int(&random_min, A_surfxml_random_min);
1117 surf_parse_get_int(&random_max, A_surfxml_random_max);
1118 surf_parse_get_int(&random_mean, A_surfxml_random_mean);
1119 surf_parse_get_int(&random_std_deviation, A_surfxml_random_std_deviation);
1120 random_generator = A_surfxml_random_generator;
1123 void add_randomness(void)
1125 /* If needed aditional properties can be added by using the prop tag */
1126 random_data_t random = random_new(random_generator, 0, random_min, random_max, random_mean, random_std_deviation);
1127 xbt_dict_set(random_data_list, random_id, (void *)random, NULL);