Logo AND Algorithmique Numérique Distribuée

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