Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
4c38b8d28a02cafe05f008b5891d1e7780863c72
[simgrid.git] / src / surf / surfxml_parse.c
1 /* Copyright (c) 2006, 2007, 2008, 2009, 2010, 2011. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 #include "xbt/misc.h"
8 #include "xbt/log.h"
9 #include "xbt/str.h"
10 #include "xbt/dict.h"
11 #include "surf/surfxml_parse_private.h"
12 #include "surf/surf_private.h"
13
14 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_parse, surf,
15                                 "Logging specific to the SURF parsing module");
16 #undef CLEANUP
17 int ETag_surfxml_include_state(void);
18
19 #include "simgrid_dtd.c"
20
21 /*
22  * Helping functions
23  */
24 static void surf_parse_error(char *msg) {
25   xbt_die("Parse error on line %d: %s\n", surf_parse_lineno, msg);
26 }
27
28 void surf_parse_get_double(double *value, const char *string) {
29   int ret = sscanf(string, "%lg", value);
30   if (ret != 1)
31     surf_parse_error(bprintf("%s is not a double", string));
32 }
33
34 void surf_parse_get_int(int *value, const char *string) {
35   int ret = sscanf(string, "%d", value);
36   if (ret != 1)
37     surf_parse_error(bprintf("%s is not an integer", string));
38 }
39
40
41 /*
42  * All the callback lists that can be overiden anywhere.
43  * (this list should probably be reduced to the bare minimum to allow the models to work)
44  */
45
46 /* make sure these symbols are defined as strong ones in this file so that the linker can resolve them */
47 xbt_dynar_t STag_surfxml_platform_cb_list = NULL;
48 xbt_dynar_t ETag_surfxml_platform_cb_list = NULL;
49 xbt_dynar_t STag_surfxml_host_cb_list = NULL;
50 xbt_dynar_t ETag_surfxml_host_cb_list = NULL;
51 xbt_dynar_t STag_surfxml_router_cb_list = NULL;
52 xbt_dynar_t ETag_surfxml_router_cb_list = NULL;
53 xbt_dynar_t STag_surfxml_link_cb_list = NULL;
54 xbt_dynar_t ETag_surfxml_link_cb_list = NULL;
55 xbt_dynar_t STag_surfxml_route_cb_list = NULL;
56 xbt_dynar_t ETag_surfxml_route_cb_list = NULL;
57 xbt_dynar_t STag_surfxml_link_ctn_cb_list = NULL;
58 xbt_dynar_t ETag_surfxml_link_ctn_cb_list = NULL;
59 xbt_dynar_t STag_surfxml_process_cb_list = NULL;
60 xbt_dynar_t ETag_surfxml_process_cb_list = NULL;
61 xbt_dynar_t STag_surfxml_argument_cb_list = NULL;
62 xbt_dynar_t ETag_surfxml_argument_cb_list = NULL;
63 xbt_dynar_t STag_surfxml_prop_cb_list = NULL;
64 xbt_dynar_t ETag_surfxml_prop_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_peer_cb_list = NULL;
68 xbt_dynar_t ETag_surfxml_peer_cb_list = NULL;
69 xbt_dynar_t STag_surfxml_trace_cb_list = NULL;
70 xbt_dynar_t ETag_surfxml_trace_cb_list = NULL;
71 xbt_dynar_t STag_surfxml_trace_connect_cb_list = NULL;
72 xbt_dynar_t ETag_surfxml_trace_connect_cb_list = NULL;
73 xbt_dynar_t STag_surfxml_random_cb_list = NULL;
74 xbt_dynar_t ETag_surfxml_random_cb_list = NULL;
75 xbt_dynar_t STag_surfxml_AS_cb_list = NULL;
76 xbt_dynar_t ETag_surfxml_AS_cb_list = NULL;
77 xbt_dynar_t STag_surfxml_ASroute_cb_list = NULL;
78 xbt_dynar_t ETag_surfxml_ASroute_cb_list = NULL;
79 xbt_dynar_t STag_surfxml_bypassRoute_cb_list = NULL;
80 xbt_dynar_t ETag_surfxml_bypassRoute_cb_list = NULL;
81 xbt_dynar_t STag_surfxml_config_cb_list = NULL;
82 xbt_dynar_t ETag_surfxml_config_cb_list = NULL;
83 xbt_dynar_t STag_surfxml_include_cb_list = NULL;
84 xbt_dynar_t ETag_surfxml_include_cb_list = NULL;
85
86 /* The default current property receiver. Setup in the corresponding opening callbacks. */
87 xbt_dict_t current_property_set = NULL;
88 /* dictionary of random generator data */
89 xbt_dict_t random_data_list = NULL;
90
91 /* Call all the callbacks of a specific SAX event */
92 static XBT_INLINE void surfxml_call_cb_functions(xbt_dynar_t);
93
94 YY_BUFFER_STATE surf_input_buffer;
95 FILE *surf_file_to_parse = NULL;
96
97 static void parse_Stag_trace(void);
98 static void parse_Etag_trace(void);
99 static void parse_Stag_trace_connect(void);
100
101 static void init_randomness(void);
102 static void add_randomness(void);
103
104 /*
105  * Stuff relative to the <include> tag
106  */
107
108 static xbt_dynar_t surf_input_buffer_stack = NULL;
109 static xbt_dynar_t surf_file_to_parse_stack = NULL;
110
111 void STag_surfxml_include(void)
112 {
113   XBT_INFO("STag_surfxml_include '%s'",A_surfxml_include_file);
114   xbt_dynar_push(surf_file_to_parse_stack, &surf_file_to_parse); //save old filename
115
116   surf_file_to_parse = surf_fopen(A_surfxml_include_file, "r"); // read new filename
117   xbt_assert((surf_file_to_parse), "Unable to open \"%s\"\n",
118               A_surfxml_include_file);
119   xbt_dynar_push(surf_input_buffer_stack,&surf_input_buffer);
120   surf_input_buffer = surf_parse__create_buffer(surf_file_to_parse, YY_BUF_SIZE);
121   surf_parse_push_buffer_state(surf_input_buffer);
122   fflush(NULL);
123 }
124
125 void ETag_surfxml_include(void) {
126 /* Nothing to do when done with reading the include tag.
127  * Instead, the handling should be deferred until the EOF of current buffer -- see below */
128 }
129
130 /** @brief When reaching EOF, check whether we are in an include tag, and behave accordingly if yes
131  *
132  * This function is called automatically by sedding the parser in buildtools/Cmake/MaintainerMode.cmake
133  * Every FAIL on "Premature EOF" is preceded by a call to this function, which role is to restore the
134  * previous buffer if we reached the EOF /of an include file/. Its return code is used to avoid the
135  * error message in that case.
136  *
137  * Yeah, that's terribly hackish, but it works. A better solution should be dealed with in flexml
138  * directly: a command line flag could instruct it to do the correct thing when #include is encountered
139  * on a line.
140  */
141 int ETag_surfxml_include_state(void)
142 {
143   fflush(NULL);
144   XBT_INFO("ETag_surfxml_include_state '%s'",A_surfxml_include_file);
145   if(xbt_dynar_length(surf_input_buffer_stack)!= 0)
146           return 1;
147   fclose(surf_file_to_parse);
148   xbt_dynar_pop(surf_file_to_parse_stack, &surf_file_to_parse);
149   surf_parse_pop_buffer_state();
150   xbt_dynar_pop(surf_input_buffer_stack,surf_input_buffer);
151   return 0;
152 }
153
154
155 void surf_parse_init_callbacks(void)
156 {
157           STag_surfxml_platform_cb_list =
158               xbt_dynar_new(sizeof(void_f_void_t), NULL);
159           ETag_surfxml_platform_cb_list =
160               xbt_dynar_new(sizeof(void_f_void_t), NULL);
161           STag_surfxml_host_cb_list = xbt_dynar_new(sizeof(void_f_void_t), NULL);
162           ETag_surfxml_host_cb_list = xbt_dynar_new(sizeof(void_f_void_t), NULL);
163           STag_surfxml_router_cb_list = xbt_dynar_new(sizeof(void_f_void_t), NULL);
164           ETag_surfxml_router_cb_list = xbt_dynar_new(sizeof(void_f_void_t), NULL);
165           STag_surfxml_link_cb_list = xbt_dynar_new(sizeof(void_f_void_t), NULL);
166           ETag_surfxml_link_cb_list = xbt_dynar_new(sizeof(void_f_void_t), NULL);
167           STag_surfxml_route_cb_list = xbt_dynar_new(sizeof(void_f_void_t), NULL);
168           ETag_surfxml_route_cb_list = xbt_dynar_new(sizeof(void_f_void_t), NULL);
169           STag_surfxml_link_ctn_cb_list =
170               xbt_dynar_new(sizeof(void_f_void_t), NULL);
171           ETag_surfxml_link_ctn_cb_list =
172               xbt_dynar_new(sizeof(void_f_void_t), NULL);
173           STag_surfxml_process_cb_list =
174               xbt_dynar_new(sizeof(void_f_void_t), NULL);
175           ETag_surfxml_process_cb_list =
176               xbt_dynar_new(sizeof(void_f_void_t), NULL);
177           STag_surfxml_argument_cb_list =
178               xbt_dynar_new(sizeof(void_f_void_t), NULL);
179           ETag_surfxml_argument_cb_list =
180               xbt_dynar_new(sizeof(void_f_void_t), NULL);
181           STag_surfxml_prop_cb_list = xbt_dynar_new(sizeof(void_f_void_t), NULL);
182           ETag_surfxml_prop_cb_list = xbt_dynar_new(sizeof(void_f_void_t), NULL);
183           STag_surfxml_trace_cb_list = xbt_dynar_new(sizeof(void_f_void_t), NULL);
184           ETag_surfxml_trace_cb_list = xbt_dynar_new(sizeof(void_f_void_t), NULL);
185           STag_surfxml_trace_connect_cb_list =
186               xbt_dynar_new(sizeof(void_f_void_t), NULL);
187           ETag_surfxml_trace_connect_cb_list =
188               xbt_dynar_new(sizeof(void_f_void_t), NULL);
189           STag_surfxml_random_cb_list = xbt_dynar_new(sizeof(void_f_void_t), NULL);
190           ETag_surfxml_random_cb_list = xbt_dynar_new(sizeof(void_f_void_t), NULL);
191           STag_surfxml_AS_cb_list = xbt_dynar_new(sizeof(void_f_void_t), NULL);
192           ETag_surfxml_AS_cb_list = xbt_dynar_new(sizeof(void_f_void_t), NULL);
193           STag_surfxml_ASroute_cb_list =
194               xbt_dynar_new(sizeof(void_f_void_t), NULL);
195           ETag_surfxml_ASroute_cb_list =
196               xbt_dynar_new(sizeof(void_f_void_t), NULL);
197           STag_surfxml_bypassRoute_cb_list =
198               xbt_dynar_new(sizeof(void_f_void_t), NULL);
199           ETag_surfxml_bypassRoute_cb_list =
200               xbt_dynar_new(sizeof(void_f_void_t), NULL);
201           STag_surfxml_cluster_cb_list =
202               xbt_dynar_new(sizeof(void_f_void_t), NULL);
203           ETag_surfxml_cluster_cb_list =
204               xbt_dynar_new(sizeof(void_f_void_t), NULL);
205           STag_surfxml_peer_cb_list =
206               xbt_dynar_new(sizeof(void_f_void_t), NULL);
207           ETag_surfxml_peer_cb_list =
208               xbt_dynar_new(sizeof(void_f_void_t), NULL);
209           STag_surfxml_config_cb_list =
210                           xbt_dynar_new(sizeof(void_f_void_t), NULL);
211           ETag_surfxml_config_cb_list =
212                           xbt_dynar_new(sizeof(void_f_void_t), NULL);
213           STag_surfxml_include_cb_list =
214                           xbt_dynar_new(sizeof(void_f_void_t), NULL);
215           ETag_surfxml_include_cb_list =
216                           xbt_dynar_new(sizeof(void_f_void_t), NULL);
217 }
218
219 void surf_parse_reset_callbacks(void)
220 {
221         surf_parse_free_callbacks();
222         surf_parse_init_callbacks();
223 }
224
225 void surf_parse_free_callbacks(void)
226 {
227   xbt_dynar_free(&STag_surfxml_platform_cb_list);
228   xbt_dynar_free(&ETag_surfxml_platform_cb_list);
229   xbt_dynar_free(&STag_surfxml_host_cb_list);
230   xbt_dynar_free(&ETag_surfxml_host_cb_list);
231   xbt_dynar_free(&STag_surfxml_router_cb_list);
232   xbt_dynar_free(&ETag_surfxml_router_cb_list);
233   xbt_dynar_free(&STag_surfxml_link_cb_list);
234   xbt_dynar_free(&ETag_surfxml_link_cb_list);
235   xbt_dynar_free(&STag_surfxml_route_cb_list);
236   xbt_dynar_free(&ETag_surfxml_route_cb_list);
237   xbt_dynar_free(&STag_surfxml_link_ctn_cb_list);
238   xbt_dynar_free(&ETag_surfxml_link_ctn_cb_list);
239   xbt_dynar_free(&STag_surfxml_process_cb_list);
240   xbt_dynar_free(&ETag_surfxml_process_cb_list);
241   xbt_dynar_free(&STag_surfxml_argument_cb_list);
242   xbt_dynar_free(&ETag_surfxml_argument_cb_list);
243   xbt_dynar_free(&STag_surfxml_prop_cb_list);
244   xbt_dynar_free(&ETag_surfxml_prop_cb_list);
245   xbt_dynar_free(&STag_surfxml_trace_cb_list);
246   xbt_dynar_free(&ETag_surfxml_trace_cb_list);
247   xbt_dynar_free(&STag_surfxml_trace_connect_cb_list);
248   xbt_dynar_free(&ETag_surfxml_trace_connect_cb_list);
249   xbt_dynar_free(&STag_surfxml_random_cb_list);
250   xbt_dynar_free(&ETag_surfxml_random_cb_list);
251   xbt_dynar_free(&STag_surfxml_AS_cb_list);
252   xbt_dynar_free(&ETag_surfxml_AS_cb_list);
253   xbt_dynar_free(&STag_surfxml_ASroute_cb_list);
254   xbt_dynar_free(&ETag_surfxml_ASroute_cb_list);
255   xbt_dynar_free(&STag_surfxml_bypassRoute_cb_list);
256   xbt_dynar_free(&ETag_surfxml_bypassRoute_cb_list);
257   xbt_dynar_free(&STag_surfxml_cluster_cb_list);
258   xbt_dynar_free(&ETag_surfxml_cluster_cb_list);
259   xbt_dynar_free(&STag_surfxml_peer_cb_list);
260   xbt_dynar_free(&ETag_surfxml_peer_cb_list);
261   xbt_dynar_free(&STag_surfxml_config_cb_list);
262   xbt_dynar_free(&ETag_surfxml_config_cb_list);
263   xbt_dynar_free(&STag_surfxml_include_cb_list);
264   xbt_dynar_free(&ETag_surfxml_include_cb_list);
265 }
266
267 /* Stag and Etag parse functions */
268
269 void STag_surfxml_platform(void)
270 {
271   double version;
272   surf_parse_get_double(&version, A_surfxml_platform_version);
273
274   xbt_assert((version >= 1.0), "******* BIG FAT WARNING *********\n "
275       "You're using an ancient XML file.\n"
276       "Since SimGrid 3.1, units are Bytes, Flops, and seconds "
277       "instead of MBytes, MFlops and seconds.\n"
278
279       "Use simgrid_update_xml to update your file automatically. "
280       "This program is installed automatically with SimGrid, or "
281       "available in the tools/ directory of the source archive.\n"
282
283       "Please check also out the SURF section of the ChangeLog for "
284       "the 3.1 version for more information. \n"
285
286       "Last, do not forget to also update your values for "
287       "the calls to MSG_task_create (if any).");
288   xbt_assert((version >= 3.0), "******* BIG FAT WARNING *********\n "
289       "You're using an old XML file.\n"
290       "Use simgrid_update_xml to update your file automatically. "
291       "This program is installed automatically with SimGrid, or "
292       "available in the tools/ directory of the source archive.");
293
294   surfxml_call_cb_functions(STag_surfxml_platform_cb_list);
295
296 }
297
298 void STag_surfxml_host(void){
299 //      XBT_INFO("STag_surfxml_host [%s]",A_surfxml_host_id);
300         struct_host = xbt_new0(s_hostSG_t, 1);
301         struct_host->V_host_id = xbt_strdup(A_surfxml_host_id);
302         struct_host->V_host_power_peak = get_cpu_power(A_surfxml_host_power);
303         surf_parse_get_double(&(struct_host->V_host_power_scale), A_surfxml_host_availability);
304         surf_parse_get_int(&(struct_host->V_host_core),A_surfxml_host_core);
305         struct_host->V_host_power_trace = tmgr_trace_new(A_surfxml_host_availability_file);
306         struct_host->V_host_state_trace = tmgr_trace_new(A_surfxml_host_state_file);
307         xbt_assert((A_surfxml_host_state == A_surfxml_host_state_ON) ||
308                           (A_surfxml_host_state == A_surfxml_host_state_OFF), "Invalid state");
309         if (A_surfxml_host_state == A_surfxml_host_state_ON)
310                 struct_host->V_host_state_initial = SURF_RESOURCE_ON;
311         if (A_surfxml_host_state == A_surfxml_host_state_OFF)
312                 struct_host->V_host_state_initial = SURF_RESOURCE_OFF;
313         struct_host->V_host_coord = xbt_strdup(A_surfxml_host_coordinates);
314
315         surfxml_call_cb_functions(STag_surfxml_host_cb_list);
316 }
317 void STag_surfxml_host_cluster(void){
318         surfxml_call_cb_functions(STag_surfxml_host_cb_list);
319 }
320 void ETag_surfxml_host(void){
321         surfxml_call_cb_functions(ETag_surfxml_host_cb_list);
322 //      xbt_free(struct_host->V_host_id);
323 }
324
325 void STag_surfxml_router(void){
326         struct_router = xbt_new0(s_router_t, 1);
327         struct_router->V_router_id = xbt_strdup(A_surfxml_router_id);
328         struct_router->V_router_coord = xbt_strdup(A_surfxml_router_coordinates);
329         surfxml_call_cb_functions(STag_surfxml_router_cb_list);
330 }
331 void ETag_surfxml_router(void){
332         surfxml_call_cb_functions(ETag_surfxml_router_cb_list);
333         xbt_free(struct_router->V_router_id);
334         xbt_free(struct_router->V_router_coord);
335         xbt_free(struct_router);
336 }
337
338 void STag_surfxml_cluster(void){
339   surf_parse_models_setup(); /* ensure that the models are created after the last <config> tag. See comment in simgrid.dtd */
340
341         struct_cluster = xbt_new0(s_cluster_t, 1);
342         struct_cluster->V_cluster_id = xbt_strdup(A_surfxml_cluster_id);
343         struct_cluster->V_cluster_prefix = xbt_strdup(A_surfxml_cluster_prefix);
344         struct_cluster->V_cluster_suffix = xbt_strdup(A_surfxml_cluster_suffix);
345         struct_cluster->V_cluster_radical = xbt_strdup(A_surfxml_cluster_radical);
346         surf_parse_get_double(&struct_cluster->S_cluster_power,A_surfxml_cluster_power);
347         surf_parse_get_int(&struct_cluster->S_cluster_core,A_surfxml_cluster_core);
348         surf_parse_get_double(&struct_cluster->S_cluster_bw,A_surfxml_cluster_bw);
349         surf_parse_get_double(&struct_cluster->S_cluster_lat,A_surfxml_cluster_lat);
350         if(strcmp(A_surfxml_cluster_bb_bw,""))
351                 surf_parse_get_double(&struct_cluster->S_cluster_bb_bw,A_surfxml_cluster_bb_bw);
352         if(strcmp(A_surfxml_cluster_bb_lat,""))
353                 surf_parse_get_double(&struct_cluster->S_cluster_bb_lat,A_surfxml_cluster_bb_lat);
354         if(!strcmp(A_surfxml_cluster_router_id,""))
355                 struct_cluster->S_cluster_router_id = bprintf("%s%s_router%s",
356                                 struct_cluster->V_cluster_prefix,
357                                 struct_cluster->V_cluster_id,
358                                 struct_cluster->V_cluster_suffix);
359         else
360                 struct_cluster->S_cluster_router_id = xbt_strdup(A_surfxml_cluster_router_id);
361
362         struct_cluster->V_cluster_sharing_policy = AX_surfxml_cluster_sharing_policy;
363         struct_cluster->V_cluster_bb_sharing_policy = AX_surfxml_cluster_bb_sharing_policy;
364
365         surfxml_call_cb_functions(STag_surfxml_cluster_cb_list);
366 }
367 void ETag_surfxml_cluster(void){
368         surfxml_call_cb_functions(ETag_surfxml_cluster_cb_list);
369         xbt_free(struct_cluster->V_cluster_id);
370         xbt_free(struct_cluster->V_cluster_prefix);
371         xbt_free(struct_cluster->V_cluster_suffix);
372         xbt_free(struct_cluster->V_cluster_radical);
373         struct_cluster->S_cluster_power = 0;
374         struct_cluster->S_cluster_core = 0;
375         struct_cluster->S_cluster_bw = 0;
376         struct_cluster->S_cluster_lat = 0;
377         struct_cluster->S_cluster_bb_bw = 0;
378         struct_cluster->S_cluster_bb_lat = 0;
379         xbt_free(struct_cluster->S_cluster_router_id);
380         struct_cluster->V_cluster_sharing_policy = 0;
381         struct_cluster->V_cluster_bb_sharing_policy = 0;
382         xbt_free(struct_cluster);
383 }
384
385 void STag_surfxml_peer(void){
386   surf_parse_models_setup(); /* ensure that the models are created after the last <config> tag. See comment in simgrid.dtd */
387
388         struct_peer = xbt_new0(s_peer_t, 1);
389         struct_peer->V_peer_id = xbt_strdup(A_surfxml_peer_id);
390         struct_peer->V_peer_power = xbt_strdup(A_surfxml_peer_power);
391         struct_peer->V_peer_bw_in = xbt_strdup(A_surfxml_peer_bw_in);
392         struct_peer->V_peer_bw_out = xbt_strdup(A_surfxml_peer_bw_out);
393         struct_peer->V_peer_lat = xbt_strdup(A_surfxml_peer_lat);
394         struct_peer->V_peer_coord = xbt_strdup(A_surfxml_peer_coordinates);
395         struct_peer->V_peer_availability_trace = xbt_strdup(A_surfxml_peer_availability_file);
396         struct_peer->V_peer_state_trace = xbt_strdup(A_surfxml_peer_state_file);
397         surfxml_call_cb_functions(STag_surfxml_peer_cb_list);
398 }
399 void ETag_surfxml_peer(void){
400         surfxml_call_cb_functions(ETag_surfxml_peer_cb_list);
401         xbt_free(struct_peer->V_peer_id);
402         xbt_free(struct_peer->V_peer_power);
403         xbt_free(struct_peer->V_peer_bw_in);
404         xbt_free(struct_peer->V_peer_bw_out);
405         xbt_free(struct_peer->V_peer_lat);
406         xbt_free(struct_peer->V_peer_coord);
407         xbt_free(struct_peer->V_peer_availability_trace);
408         xbt_free(struct_peer->V_peer_state_trace);
409         xbt_free(struct_peer);
410 }
411 void STag_surfxml_link(void){
412         struct_lnk = xbt_new0(s_link_t, 1);
413         struct_lnk->V_link_id = xbt_strdup(A_surfxml_link_id);
414         surf_parse_get_double(&(struct_lnk->V_link_bandwidth),A_surfxml_link_bandwidth);
415         struct_lnk->V_link_bandwidth_file = tmgr_trace_new(A_surfxml_link_bandwidth_file);
416         surf_parse_get_double(&(struct_lnk->V_link_latency),A_surfxml_link_latency);
417         struct_lnk->V_link_latency_file = tmgr_trace_new(A_surfxml_link_latency_file);
418         xbt_assert((A_surfxml_link_state == A_surfxml_link_state_ON) ||
419                           (A_surfxml_link_state == A_surfxml_link_state_OFF), "Invalid state");
420         if (A_surfxml_link_state == A_surfxml_link_state_ON)
421                 struct_lnk->V_link_state = SURF_RESOURCE_ON;
422         if (A_surfxml_link_state == A_surfxml_link_state_OFF)
423                 struct_lnk->V_link_state = SURF_RESOURCE_OFF;
424         struct_lnk->V_link_state_file = tmgr_trace_new(A_surfxml_link_state_file);
425         struct_lnk->V_link_sharing_policy = A_surfxml_link_sharing_policy;
426
427         if (A_surfxml_link_sharing_policy == A_surfxml_link_sharing_policy_SHARED)
428                 struct_lnk->V_policy_initial_link = SURF_LINK_SHARED;
429         else
430         {
431          if (A_surfxml_link_sharing_policy == A_surfxml_link_sharing_policy_FATPIPE)
432                  struct_lnk->V_policy_initial_link = SURF_LINK_FATPIPE;
433          else if (A_surfxml_link_sharing_policy == A_surfxml_link_sharing_policy_FULLDUPLEX)
434                  struct_lnk->V_policy_initial_link = SURF_LINK_FULLDUPLEX;
435         }
436
437         surfxml_call_cb_functions(STag_surfxml_link_cb_list);
438 }
439 void STag_surfxml_link_cluster(void){
440         surfxml_call_cb_functions(STag_surfxml_link_cb_list);
441 }
442 void ETag_surfxml_link(void){
443         surfxml_call_cb_functions(ETag_surfxml_link_cb_list);
444 }
445
446 void STag_surfxml_route(void){
447         surfxml_call_cb_functions(STag_surfxml_route_cb_list);
448 }
449 void STag_surfxml_link_ctn(void){
450         surfxml_call_cb_functions(STag_surfxml_link_ctn_cb_list);
451 }
452 void STag_surfxml_process(void){
453         surfxml_call_cb_functions(STag_surfxml_process_cb_list);
454 }
455 void STag_surfxml_argument(void){
456         surfxml_call_cb_functions(STag_surfxml_argument_cb_list);
457 }
458 void STag_surfxml_prop(void){
459         surfxml_call_cb_functions(STag_surfxml_prop_cb_list);
460 }
461 void STag_surfxml_trace(void){
462         surfxml_call_cb_functions(STag_surfxml_trace_cb_list);
463 }
464 void STag_surfxml_trace_connect(void){
465         surfxml_call_cb_functions(STag_surfxml_trace_connect_cb_list);
466 }
467 void STag_surfxml_AS(void){
468   surf_parse_models_setup(); /* ensure that the models are created after the last <config> tag. See comment in simgrid.dtd */
469
470         surfxml_call_cb_functions(STag_surfxml_AS_cb_list);
471 }
472 void STag_surfxml_ASroute(void){
473         surfxml_call_cb_functions(STag_surfxml_ASroute_cb_list);
474 }
475 void STag_surfxml_bypassRoute(void){
476         surfxml_call_cb_functions(STag_surfxml_bypassRoute_cb_list);
477 }
478 void STag_surfxml_config(void){
479         surfxml_call_cb_functions(STag_surfxml_config_cb_list);
480 }
481 void STag_surfxml_random(void){
482         surfxml_call_cb_functions(STag_surfxml_random_cb_list);
483 }
484
485 #define parse_method(type,name) \
486 void type##Tag_surfxml_##name(void) \
487 { surfxml_call_cb_functions(type##Tag_surfxml_##name##_cb_list); }
488 parse_method(E, platform);
489 parse_method(E, route);
490 parse_method(E, link_ctn);
491 parse_method(E, process);
492 parse_method(E, argument);
493 parse_method(E, prop);
494 parse_method(E, trace);
495 parse_method(E, trace_connect);
496 parse_method(E, random);
497 parse_method(E, AS);
498 parse_method(E, ASroute);
499 parse_method(E, bypassRoute);
500 parse_method(E, config);
501
502 /* Open and Close parse file */
503
504 void surf_parse_open(const char *file)
505 {
506   static int warned = 0;        /* warn only once */
507   if (!file) {
508     if (!warned) {
509       XBT_WARN
510           ("Bypassing the XML parser since surf_parse_open received a NULL pointer. "
511               "If it is not what you want, go fix your code.");
512       warned = 1;
513     }
514     return;
515   }
516
517   if (!surf_input_buffer_stack)
518     surf_input_buffer_stack = xbt_dynar_new(sizeof(YY_BUFFER_STATE), NULL);
519   if (!surf_file_to_parse_stack)
520     surf_file_to_parse_stack = xbt_dynar_new(sizeof(FILE *), NULL);
521
522   surf_file_to_parse = surf_fopen(file, "r");
523   xbt_assert((surf_file_to_parse), "Unable to open \"%s\"\n", file);
524   surf_input_buffer = surf_parse__create_buffer(surf_file_to_parse, YY_BUF_SIZE);
525   surf_parse__switch_to_buffer(surf_input_buffer);
526   surf_parse_lineno = 1;
527 }
528
529 void surf_parse_close(void)
530 {
531   if (surf_input_buffer_stack)
532     xbt_dynar_free(&surf_input_buffer_stack);
533   if (surf_file_to_parse_stack)
534     xbt_dynar_free(&surf_file_to_parse_stack);
535
536   if (surf_file_to_parse) {
537     surf_parse__delete_buffer(surf_input_buffer);
538     fclose(surf_file_to_parse);
539     surf_file_to_parse = NULL; //Must be reset for Bypass
540   }
541 }
542
543 /* Call the lexer to parse the currently opened file. This pointer to function enables bypassing of the parser */
544
545 static int _surf_parse(void) {
546   return surf_parse_lex();
547 }
548 int_f_void_t surf_parse = _surf_parse;
549
550
551 /* Aux parse functions */
552
553 void surfxml_add_callback(xbt_dynar_t cb_list, void_f_void_t function)
554 {
555   xbt_dynar_push(cb_list, &function);
556 }
557
558 void surfxml_del_callback(xbt_dynar_t cb_list, void_f_void_t function)
559 {
560   xbt_ex_t e;
561   unsigned int it=0;
562   void_f_void_t null_f=NULL;
563
564   TRY {
565     it = xbt_dynar_search(cb_list,&function);
566   }
567   CATCH(e) {
568     if (e.category == not_found_error) {
569       xbt_ex_free(e);
570       xbt_die("Trying to remove a callback that is not here! This should not happen");
571     }
572     RETHROW;
573   }
574
575   xbt_dynar_replace(cb_list, it,&null_f);
576 }
577
578 static XBT_INLINE void surfxml_call_cb_functions(xbt_dynar_t cb_list)
579 {
580   unsigned int iterator;
581   void_f_void_t fun;
582   xbt_dynar_foreach(cb_list, iterator, fun) {
583     if (fun) (*fun) ();
584   }
585 }
586
587
588 /* Prop tag functions */
589
590 void parse_properties(const char* prop_id, const char* prop_value)
591 {
592     char *value = NULL;
593         if (!current_property_set)
594             current_property_set = xbt_dict_new();      // Maybe, it should raise an error
595         if(!strcmp(prop_id,"coordinates")){
596                 if(!strcmp(prop_value,"yes") && !COORD_HOST_LEVEL)
597                   {
598                             XBT_INFO("Configuration change: Set '%s' to '%s'", prop_id, prop_value);
599                                 COORD_HOST_LEVEL = xbt_lib_add_level(host_lib,xbt_dynar_free_voidp);
600                                 COORD_ASR_LEVEL  = xbt_lib_add_level(as_router_lib,xbt_dynar_free_voidp);
601                   }
602                 if(strcmp(A_surfxml_prop_value,"yes"))
603                           xbt_die("Setting XML prop coordinates must be \"yes\"");
604           }
605         else{
606                   value = xbt_strdup(prop_value);
607                   xbt_dict_set(current_property_set, prop_id, value, free);
608          }
609 }
610
611 /**
612  * With XML parser
613  */
614 void parse_properties_XML(void)
615 {
616   parse_properties(A_surfxml_prop_id, A_surfxml_prop_value);
617 }
618
619
620 /* Random tag functions */
621
622 double get_cpu_power(const char *power)
623 {
624   double power_scale = 0.0;
625   const char *p, *q;
626   char *generator;
627   random_data_t random = NULL;
628   /* randomness is inserted like this: power="$rand(my_random)" */
629   if (((p = strstr(power, "$rand(")) != NULL)
630       && ((q = strstr(power, ")")) != NULL)) {
631     if (p < q) {
632       generator = xbt_malloc(q - (p + 6) + 1);
633       memcpy(generator, p + 6, q - (p + 6));
634       generator[q - (p + 6)] = '\0';
635       random = xbt_dict_get_or_null(random_data_list, generator);
636       xbt_assert(random, "Random generator %s undefined", generator);
637       power_scale = random_generate(random);
638     }
639   } else {
640     surf_parse_get_double(&power_scale, power);
641   }
642   return power_scale;
643 }
644
645 double random_min, random_max, random_mean, random_std_deviation,
646     random_generator;
647 char *random_id;
648
649 static void init_randomness(void)
650 {
651   random_id = A_surfxml_random_id;
652   surf_parse_get_double(&random_min, A_surfxml_random_min);
653   surf_parse_get_double(&random_max, A_surfxml_random_max);
654   surf_parse_get_double(&random_mean, A_surfxml_random_mean);
655   surf_parse_get_double(&random_std_deviation,
656                         A_surfxml_random_std_deviation);
657   random_generator = A_surfxml_random_generator;
658 }
659
660 static void add_randomness(void)
661 {
662   /* If needed aditional properties can be added by using the prop tag */
663   random_data_t random =
664       random_new(random_generator, 0, random_min, random_max, random_mean,
665                  random_std_deviation);
666   xbt_dict_set(random_data_list, random_id, (void *) random,
667                &xbt_free_ref);
668 }
669
670 /**
671  * create CPU resource via CPU Model
672  */
673 void* surf_host_create_resource(char *name, double power_peak,
674                                double power_scale,
675                                tmgr_trace_t power_trace, int core,
676                                e_surf_resource_state_t state_initial,
677                                tmgr_trace_t state_trace,
678                                xbt_dict_t cpu_properties)
679 {
680   return surf_cpu_model->extension.cpu.create_resource(name, power_peak,
681                                                        power_scale,
682                                                        power_trace,
683                                                        core,
684                                                        state_initial,
685                                                        state_trace,
686                                                        cpu_properties);
687 }
688
689 /**
690  * create CPU resource via worsktation_ptask_L07 model
691  */
692
693 void* surf_wsL07_host_create_resource(char *name, double power_peak,
694                                      double power_scale,
695                                      tmgr_trace_t power_trace,
696                                      e_surf_resource_state_t state_initial,
697                                      tmgr_trace_t state_trace,
698                                      xbt_dict_t cpu_properties)
699 {
700   return surf_workstation_model->extension.workstation.cpu_create_resource(name,
701                                                                     power_peak,
702                                                                     power_scale,
703                                                                     power_trace,
704                                                                     state_initial,
705                                                                     state_trace,
706                                                                     cpu_properties);
707 }
708
709 /**
710  * create link resource via network Model
711  */
712 void* surf_link_create_resource(char *name,
713                                double bw_initial,
714                                tmgr_trace_t bw_trace,
715                                double lat_initial,
716                                tmgr_trace_t lat_trace,
717                                e_surf_resource_state_t
718                                state_initial,
719                                tmgr_trace_t state_trace,
720                                e_surf_link_sharing_policy_t policy,
721                                xbt_dict_t properties)
722 {
723   return surf_network_model->extension.network.create_resource(name,
724                                                                bw_initial,
725                                                                bw_trace,
726                                                                lat_initial,
727                                                                lat_trace,
728                                                                state_initial,
729                                                                state_trace,
730                                                                policy,
731                                                                properties);
732 }
733
734 /**
735  * create link resource via workstation_ptask_L07 model
736  */
737
738 void* surf_wsL07_link_create_resource(char *name,
739                                      double bw_initial,
740                                      tmgr_trace_t bw_trace,
741                                      double lat_initial,
742                                      tmgr_trace_t lat_trace,
743                                      e_surf_resource_state_t
744                                      state_initial,
745                                      tmgr_trace_t state_trace,
746                                      e_surf_link_sharing_policy_t
747                                      policy, xbt_dict_t properties)
748 {
749   return surf_workstation_model->extension.workstation.
750       link_create_resource(name, bw_initial, bw_trace, lat_initial,
751                            lat_trace, state_initial, state_trace, policy,
752                            properties);
753 }
754
755 /**
756  *
757  *init new routing model component
758  */
759
760 void surf_AS_new(const char *AS_id, const char *AS_mode)
761 {
762   routing_AS_init(AS_id, AS_mode);
763 }
764
765 void surf_AS_finalize(const char *AS_id)
766 {
767   routing_AS_end(AS_id);
768 }
769
770 /*
771  * add host to the network element list
772  */
773 void surf_route_add_host(const char *host_id)
774 {
775   routing_add_host(host_id);
776 }
777
778 /**
779  * set route
780  */
781 void surf_routing_add_route(const char *src_id, const char *dst_id,
782                             xbt_dynar_t links_id)
783 {
784   unsigned int i;
785   const char *link_id;
786   routing_set_route(src_id, dst_id);
787   xbt_dynar_foreach(links_id, i, link_id) {
788     routing_add_link(link_id);
789   }
790
791   //store the route
792   routing_store_route();
793 }
794
795 /**
796  * Add Traces
797  */
798 void surf_add_host_traces(void)
799 {
800   return surf_cpu_model->extension.cpu.add_traces();
801 }
802
803 void surf_add_link_traces(void)
804 {
805   return surf_network_model->extension.network.add_traces();
806 }
807
808 void surf_wsL07_add_traces(void)
809 {
810   return surf_workstation_model->extension.workstation.add_traces();
811 }