Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
0099307608d75036c3575f5ecea5dadb852d93e3
[simgrid.git] / src / surf / surfxml_parse.c
1 /* Copyright (c) 2006, 2007, 2008, 2009, 2010. 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 char *platform_filename;
22
23 /* Initialize the parsing globals */
24 xbt_dict_t traces_set_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;
30
31 /* This buffer is used to store the original buffer before substituting it by out own buffer. Useful for the foreach tag */
32 static xbt_dynar_t surfxml_bufferstack_stack = NULL;
33 int surfxml_bufferstack_size = 2048;
34
35 static char *old_buff = NULL;
36 static void surf_parse_error(char *msg);
37
38 unsigned int surfxml_buffer_stack_stack_ptr;
39 unsigned int surfxml_buffer_stack_stack[1024];
40
41
42 void surfxml_bufferstack_push(int new)
43 {
44   if (!new)
45     old_buff = surfxml_bufferstack;
46   else {
47     xbt_dynar_push(surfxml_bufferstack_stack, &surfxml_bufferstack);
48     surfxml_bufferstack = xbt_new0(char, surfxml_bufferstack_size);
49   }
50 }
51
52 void surfxml_bufferstack_pop(int new)
53 {
54   if (!new)
55     surfxml_bufferstack = old_buff;
56   else {
57     free(surfxml_bufferstack);
58     xbt_dynar_pop(surfxml_bufferstack_stack, &surfxml_bufferstack);
59   }
60 }
61
62 /* make sure these symbols are defined as strong ones in this file so that the linked can resolve them */
63 xbt_dynar_t STag_surfxml_platform_cb_list = NULL;
64 xbt_dynar_t ETag_surfxml_platform_cb_list = NULL;
65 xbt_dynar_t STag_surfxml_host_cb_list = NULL;
66 xbt_dynar_t ETag_surfxml_host_cb_list = NULL;
67 xbt_dynar_t STag_surfxml_router_cb_list = NULL;
68 xbt_dynar_t ETag_surfxml_router_cb_list = NULL;
69 xbt_dynar_t STag_surfxml_link_cb_list = NULL;
70 xbt_dynar_t ETag_surfxml_link_cb_list = NULL;
71 xbt_dynar_t STag_surfxml_route_cb_list = NULL;
72 xbt_dynar_t ETag_surfxml_route_cb_list = NULL;
73 xbt_dynar_t STag_surfxml_link_ctn_cb_list = NULL;
74 xbt_dynar_t ETag_surfxml_link_ctn_cb_list = NULL;
75 xbt_dynar_t STag_surfxml_process_cb_list = NULL;
76 xbt_dynar_t ETag_surfxml_process_cb_list = NULL;
77 xbt_dynar_t STag_surfxml_argument_cb_list = NULL;
78 xbt_dynar_t ETag_surfxml_argument_cb_list = NULL;
79 xbt_dynar_t STag_surfxml_prop_cb_list = NULL;
80 xbt_dynar_t ETag_surfxml_prop_cb_list = NULL;
81 xbt_dynar_t STag_surfxml_cluster_cb_list = NULL;
82 xbt_dynar_t ETag_surfxml_cluster_cb_list = NULL;
83 xbt_dynar_t STag_surfxml_peer_cb_list = NULL;
84 xbt_dynar_t ETag_surfxml_peer_cb_list = NULL;
85 xbt_dynar_t STag_surfxml_trace_cb_list = NULL;
86 xbt_dynar_t ETag_surfxml_trace_cb_list = NULL;
87 xbt_dynar_t STag_surfxml_trace_connect_cb_list = NULL;
88 xbt_dynar_t ETag_surfxml_trace_connect_cb_list = NULL;
89 xbt_dynar_t STag_surfxml_random_cb_list = NULL;
90 xbt_dynar_t ETag_surfxml_random_cb_list = NULL;
91 xbt_dynar_t STag_surfxml_AS_cb_list = NULL;
92 xbt_dynar_t ETag_surfxml_AS_cb_list = NULL;
93 xbt_dynar_t STag_surfxml_ASroute_cb_list = NULL;
94 xbt_dynar_t ETag_surfxml_ASroute_cb_list = NULL;
95 xbt_dynar_t STag_surfxml_bypassRoute_cb_list = NULL;
96 xbt_dynar_t ETag_surfxml_bypassRoute_cb_list = NULL;
97 xbt_dynar_t STag_surfxml_config_cb_list = NULL;
98 xbt_dynar_t ETag_surfxml_config_cb_list = NULL;
99 xbt_dynar_t STag_surfxml_include_cb_list = NULL;
100 xbt_dynar_t ETag_surfxml_include_cb_list = NULL;
101
102 /* store the current property set for any tag */
103 xbt_dict_t current_property_set = NULL;
104 /* dictionary of random generator data */
105 xbt_dict_t random_data_list = NULL;
106
107 static XBT_INLINE void surfxml_call_cb_functions(xbt_dynar_t);
108
109 YY_BUFFER_STATE surf_input_buffer;
110 FILE *surf_file_to_parse = NULL;
111
112 static void surf_parse_error(char *msg);
113
114 static void parse_Stag_trace(void);
115 static void parse_Etag_trace(void);
116 static void parse_Stag_trace_connect(void);
117
118 static void init_randomness(void);
119 static void add_randomness(void);
120
121 static xbt_dynar_t surf_input_buffer_stack = NULL;
122 static xbt_dynar_t surf_file_to_parse_stack = NULL;
123
124 void STag_surfxml_include(void)
125 {
126   XBT_INFO("STag_surfxml_include '%s'",A_surfxml_include_file);
127   xbt_dynar_push(surf_file_to_parse_stack, &surf_file_to_parse); //save old filename
128
129   surf_file_to_parse = surf_fopen(A_surfxml_include_file, "r"); // read new filename
130   xbt_assert((surf_file_to_parse), "Unable to open \"%s\"\n",
131               A_surfxml_include_file);
132   xbt_dynar_push(surf_input_buffer_stack,&surf_input_buffer);
133   surf_input_buffer = surf_parse__create_buffer(surf_file_to_parse, YY_BUF_SIZE);
134   surf_parse_push_buffer_state(surf_input_buffer);
135   fflush(NULL);
136 }
137
138 void ETag_surfxml_include(void)
139 {
140 //      XBT_INFO("ETag_surfxml_include '%s'",A_surfxml_include_file);
141 //      fflush(NULL);
142 //      fclose(surf_file_to_parse);
143 //      xbt_dynar_pop(surf_file_to_parse_stack, &surf_file_to_parse); // restore old filename
144 //      surf_parse_pop_buffer_state();
145 //      xbt_dynar_pop(surf_input_buffer_stack,&surf_input_buffer);
146 }
147
148 /*
149  * Return 1 if tag include is opened
150  */
151 int ETag_surfxml_include_state(void)
152 {
153   fflush(NULL);
154   XBT_INFO("ETag_surfxml_include_state '%s'",A_surfxml_include_file);
155   if(xbt_dynar_length(surf_input_buffer_stack)!= 0)
156           return 1;
157   fclose(surf_file_to_parse);
158   xbt_dynar_pop(surf_file_to_parse_stack, &surf_file_to_parse);
159   surf_parse_pop_buffer_state();
160   xbt_dynar_pop(surf_input_buffer_stack,surf_input_buffer);
161   return 0;
162 }
163
164
165 void surf_parse_init_callbacks(void)
166 {
167           STag_surfxml_platform_cb_list =
168               xbt_dynar_new(sizeof(void_f_void_t), NULL);
169           ETag_surfxml_platform_cb_list =
170               xbt_dynar_new(sizeof(void_f_void_t), NULL);
171           STag_surfxml_host_cb_list = xbt_dynar_new(sizeof(void_f_void_t), NULL);
172           ETag_surfxml_host_cb_list = xbt_dynar_new(sizeof(void_f_void_t), NULL);
173           STag_surfxml_router_cb_list = xbt_dynar_new(sizeof(void_f_void_t), NULL);
174           ETag_surfxml_router_cb_list = xbt_dynar_new(sizeof(void_f_void_t), NULL);
175           STag_surfxml_link_cb_list = xbt_dynar_new(sizeof(void_f_void_t), NULL);
176           ETag_surfxml_link_cb_list = xbt_dynar_new(sizeof(void_f_void_t), NULL);
177           STag_surfxml_route_cb_list = xbt_dynar_new(sizeof(void_f_void_t), NULL);
178           ETag_surfxml_route_cb_list = xbt_dynar_new(sizeof(void_f_void_t), NULL);
179           STag_surfxml_link_ctn_cb_list =
180               xbt_dynar_new(sizeof(void_f_void_t), NULL);
181           ETag_surfxml_link_ctn_cb_list =
182               xbt_dynar_new(sizeof(void_f_void_t), NULL);
183           STag_surfxml_process_cb_list =
184               xbt_dynar_new(sizeof(void_f_void_t), NULL);
185           ETag_surfxml_process_cb_list =
186               xbt_dynar_new(sizeof(void_f_void_t), NULL);
187           STag_surfxml_argument_cb_list =
188               xbt_dynar_new(sizeof(void_f_void_t), NULL);
189           ETag_surfxml_argument_cb_list =
190               xbt_dynar_new(sizeof(void_f_void_t), NULL);
191           STag_surfxml_prop_cb_list = xbt_dynar_new(sizeof(void_f_void_t), NULL);
192           ETag_surfxml_prop_cb_list = xbt_dynar_new(sizeof(void_f_void_t), NULL);
193           STag_surfxml_trace_cb_list = xbt_dynar_new(sizeof(void_f_void_t), NULL);
194           ETag_surfxml_trace_cb_list = xbt_dynar_new(sizeof(void_f_void_t), NULL);
195           STag_surfxml_trace_connect_cb_list =
196               xbt_dynar_new(sizeof(void_f_void_t), NULL);
197           ETag_surfxml_trace_connect_cb_list =
198               xbt_dynar_new(sizeof(void_f_void_t), NULL);
199           STag_surfxml_random_cb_list = xbt_dynar_new(sizeof(void_f_void_t), NULL);
200           ETag_surfxml_random_cb_list = xbt_dynar_new(sizeof(void_f_void_t), NULL);
201           STag_surfxml_AS_cb_list = xbt_dynar_new(sizeof(void_f_void_t), NULL);
202           ETag_surfxml_AS_cb_list = xbt_dynar_new(sizeof(void_f_void_t), NULL);
203           STag_surfxml_ASroute_cb_list =
204               xbt_dynar_new(sizeof(void_f_void_t), NULL);
205           ETag_surfxml_ASroute_cb_list =
206               xbt_dynar_new(sizeof(void_f_void_t), NULL);
207           STag_surfxml_bypassRoute_cb_list =
208               xbt_dynar_new(sizeof(void_f_void_t), NULL);
209           ETag_surfxml_bypassRoute_cb_list =
210               xbt_dynar_new(sizeof(void_f_void_t), NULL);
211           STag_surfxml_cluster_cb_list =
212               xbt_dynar_new(sizeof(void_f_void_t), NULL);
213           ETag_surfxml_cluster_cb_list =
214               xbt_dynar_new(sizeof(void_f_void_t), NULL);
215           STag_surfxml_peer_cb_list =
216               xbt_dynar_new(sizeof(void_f_void_t), NULL);
217           ETag_surfxml_peer_cb_list =
218               xbt_dynar_new(sizeof(void_f_void_t), NULL);
219           STag_surfxml_config_cb_list =
220                           xbt_dynar_new(sizeof(void_f_void_t), NULL);
221           ETag_surfxml_config_cb_list =
222                           xbt_dynar_new(sizeof(void_f_void_t), NULL);
223           STag_surfxml_include_cb_list =
224                           xbt_dynar_new(sizeof(void_f_void_t), NULL);
225           ETag_surfxml_include_cb_list =
226                           xbt_dynar_new(sizeof(void_f_void_t), NULL);
227 }
228
229 void surf_parse_reset_callbacks(void)
230 {
231         surf_parse_free_callbacks();
232         surf_parse_init_callbacks();
233 }
234
235 void surf_parse_free_callbacks(void)
236 {
237   xbt_dynar_free(&STag_surfxml_platform_cb_list);
238   xbt_dynar_free(&ETag_surfxml_platform_cb_list);
239   xbt_dynar_free(&STag_surfxml_host_cb_list);
240   xbt_dynar_free(&ETag_surfxml_host_cb_list);
241   xbt_dynar_free(&STag_surfxml_router_cb_list);
242   xbt_dynar_free(&ETag_surfxml_router_cb_list);
243   xbt_dynar_free(&STag_surfxml_link_cb_list);
244   xbt_dynar_free(&ETag_surfxml_link_cb_list);
245   xbt_dynar_free(&STag_surfxml_route_cb_list);
246   xbt_dynar_free(&ETag_surfxml_route_cb_list);
247   xbt_dynar_free(&STag_surfxml_link_ctn_cb_list);
248   xbt_dynar_free(&ETag_surfxml_link_ctn_cb_list);
249   xbt_dynar_free(&STag_surfxml_process_cb_list);
250   xbt_dynar_free(&ETag_surfxml_process_cb_list);
251   xbt_dynar_free(&STag_surfxml_argument_cb_list);
252   xbt_dynar_free(&ETag_surfxml_argument_cb_list);
253   xbt_dynar_free(&STag_surfxml_prop_cb_list);
254   xbt_dynar_free(&ETag_surfxml_prop_cb_list);
255   xbt_dynar_free(&STag_surfxml_trace_cb_list);
256   xbt_dynar_free(&ETag_surfxml_trace_cb_list);
257   xbt_dynar_free(&STag_surfxml_trace_connect_cb_list);
258   xbt_dynar_free(&ETag_surfxml_trace_connect_cb_list);
259   xbt_dynar_free(&STag_surfxml_random_cb_list);
260   xbt_dynar_free(&ETag_surfxml_random_cb_list);
261   xbt_dynar_free(&STag_surfxml_AS_cb_list);
262   xbt_dynar_free(&ETag_surfxml_AS_cb_list);
263   xbt_dynar_free(&STag_surfxml_ASroute_cb_list);
264   xbt_dynar_free(&ETag_surfxml_ASroute_cb_list);
265   xbt_dynar_free(&STag_surfxml_bypassRoute_cb_list);
266   xbt_dynar_free(&ETag_surfxml_bypassRoute_cb_list);
267   xbt_dynar_free(&STag_surfxml_cluster_cb_list);
268   xbt_dynar_free(&ETag_surfxml_cluster_cb_list);
269   xbt_dynar_free(&STag_surfxml_peer_cb_list);
270   xbt_dynar_free(&ETag_surfxml_peer_cb_list);
271   xbt_dynar_free(&STag_surfxml_config_cb_list);
272   xbt_dynar_free(&ETag_surfxml_config_cb_list);
273   xbt_dynar_free(&STag_surfxml_include_cb_list);
274   xbt_dynar_free(&ETag_surfxml_include_cb_list);
275 }
276
277 /* Stag and Etag parse functions */
278
279 void STag_surfxml_platform(void)
280 {
281   double version = 0.0;
282
283   sscanf(A_surfxml_platform_version, "%lg", &version);
284
285   xbt_assert((version >= 1.0), "******* BIG FAT WARNING *********\n "
286       "You're using an ancient XML file.\n"
287       "Since SimGrid 3.1, units are Bytes, Flops, and seconds "
288       "instead of MBytes, MFlops and seconds.\n"
289
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.\n"
293
294       "Please check also out the SURF section of the ChangeLog for "
295       "the 3.1 version for more information. \n"
296
297       "Last, do not forget to also update your values for "
298       "the calls to MSG_task_create (if any).");
299   xbt_assert((version >= 3.0), "******* BIG FAT WARNING *********\n "
300       "You're using an old XML file.\n"
301       "Use simgrid_update_xml to update your file automatically. "
302       "This program is installed automatically with SimGrid, or "
303       "available in the tools/ directory of the source archive.");
304
305   surfxml_call_cb_functions(STag_surfxml_platform_cb_list);
306
307 }
308
309 #define parse_method(type,name) \
310 void type##Tag_surfxml_##name(void) \
311 { surfxml_call_cb_functions(type##Tag_surfxml_##name##_cb_list); }
312
313 parse_method(E, platform);
314 parse_method(S, host);
315 parse_method(E, host);
316 parse_method(S, router);
317 parse_method(E, router);
318 parse_method(S, link);
319 parse_method(E, link);
320 parse_method(S, route);
321 parse_method(E, route);
322 parse_method(S, link_ctn);
323 parse_method(E, link_ctn);
324 parse_method(S, process);
325 parse_method(E, process);
326 parse_method(S, argument);
327 parse_method(E, argument);
328 parse_method(S, prop);
329 parse_method(E, prop);
330 parse_method(S, trace);
331 parse_method(E, trace);
332 parse_method(S, trace_connect);
333 parse_method(E, trace_connect);
334 parse_method(S, random);
335 parse_method(E, random);
336 parse_method(S, AS);
337 parse_method(E, AS);
338 parse_method(S, ASroute);
339 parse_method(E, ASroute);
340 parse_method(S, bypassRoute);
341 parse_method(E, bypassRoute);
342 parse_method(S, cluster);
343 parse_method(E, cluster);
344 parse_method(S, peer);
345 parse_method(E, peer);
346 parse_method(S, config);
347 parse_method(E, config);
348
349 /* Open and Close parse file */
350
351 void surf_parse_open(const char *file)
352 {
353   static int warned = 0;        /* warn only once */
354   if (!file) {
355     if (!warned) {
356       XBT_WARN
357           ("Bypassing the XML parser since surf_parse_open received a NULL pointer. If it is not what you want, go fix your code.");
358       warned = 1;
359     }
360     return;
361   }
362
363   if (!surf_input_buffer_stack)
364         surf_input_buffer_stack = xbt_dynar_new(sizeof(YY_BUFFER_STATE), NULL);
365   if (!surf_file_to_parse_stack)
366         surf_file_to_parse_stack = xbt_dynar_new(sizeof(FILE *), NULL);
367
368   surf_file_to_parse = surf_fopen(file, "r");
369   xbt_assert((surf_file_to_parse), "Unable to open \"%s\"\n", file);
370   surf_input_buffer = surf_parse__create_buffer(surf_file_to_parse, YY_BUF_SIZE);
371   surf_parse__switch_to_buffer(surf_input_buffer);
372   surf_parse_lineno = 1;
373 }
374
375 void surf_parse_close(void)
376 {
377   if (surf_input_buffer_stack)
378         xbt_dynar_free(&surf_input_buffer_stack);
379   if (surf_file_to_parse_stack)
380         xbt_dynar_free(&surf_file_to_parse_stack);
381
382   if (surf_file_to_parse) {
383     surf_parse__delete_buffer(surf_input_buffer);
384     fclose(surf_file_to_parse);
385     surf_file_to_parse = NULL; //Must be reset for Bypass
386   }
387 }
388
389 /* Parse Function */
390
391 static int _surf_parse(void)
392 {
393   return surf_parse_lex();
394 }
395
396 int_f_void_t surf_parse = _surf_parse;
397
398 void surf_parse_error(char *msg)
399 {
400   fprintf(stderr, "Parse error on line %d: %s\n", surf_parse_lineno, msg);
401   abort();
402 }
403
404 void surf_parse_get_double(double *value, const char *string)
405 {
406   int ret = 0;
407   ret = sscanf(string, "%lg", value);
408   if (ret != 1)
409     surf_parse_error(bprintf("%s is not a double", string));
410 }
411
412 void surf_parse_get_int(int *value, const char *string)
413 {
414   int ret = 0;
415   ret = sscanf(string, "%d", value);
416   if (ret != 1)
417     surf_parse_error(bprintf("%s is not an integer", string));
418 }
419
420 /* Aux parse functions */
421
422 void surfxml_add_callback(xbt_dynar_t cb_list, void_f_void_t function)
423 {
424   xbt_dynar_push(cb_list, &function);
425 }
426
427 void surfxml_del_callback(xbt_dynar_t cb_list, void_f_void_t function)
428 {
429   xbt_ex_t e;
430   unsigned int it=0;
431   void_f_void_t null_f=NULL;
432
433   TRY {
434     it = xbt_dynar_search(cb_list,&function);
435   }
436   CATCH(e) {
437     if (e.category == not_found_error) {
438       xbt_ex_free(e);
439       xbt_die("Trying to remove a callback that is not here! This should not happen");
440     }
441     RETHROW;
442   }
443
444   xbt_dynar_replace(cb_list, it,&null_f);
445 }
446
447 static XBT_INLINE void surfxml_call_cb_functions(xbt_dynar_t cb_list)
448 {
449   unsigned int iterator;
450   void_f_void_t fun;
451   xbt_dynar_foreach(cb_list, iterator, fun) {
452     if (fun) (*fun) ();
453   }
454 }
455
456 /* Init and free parse data */
457
458 static void init_data(void)
459 {
460   if (!surfxml_bufferstack_stack)
461     surfxml_bufferstack_stack = xbt_dynar_new(sizeof(char *), NULL);
462
463   traces_set_list = xbt_dict_new();
464   trace_connect_list_host_avail = xbt_dict_new();
465   trace_connect_list_power = xbt_dict_new();
466   trace_connect_list_link_avail = xbt_dict_new();
467   trace_connect_list_bandwidth = xbt_dict_new();
468   trace_connect_list_latency = xbt_dict_new();
469
470   surfxml_add_callback(STag_surfxml_trace_cb_list, &parse_Stag_trace);
471   surfxml_add_callback(ETag_surfxml_trace_cb_list, &parse_Etag_trace);
472   surfxml_add_callback(STag_surfxml_trace_connect_cb_list,
473                                            &parse_Stag_trace_connect);
474 }
475
476 static void free_data(void)
477 {
478   xbt_dict_free(&trace_connect_list_host_avail);
479   xbt_dict_free(&trace_connect_list_power);
480   xbt_dict_free(&trace_connect_list_link_avail);
481   xbt_dict_free(&trace_connect_list_bandwidth);
482   xbt_dict_free(&trace_connect_list_latency);
483   xbt_dict_free(&traces_set_list);
484   xbt_dict_free(&random_data_list);
485   xbt_dynar_free(&surfxml_bufferstack_stack);
486 }
487
488 /* Here start parse */
489 void parse_platform_file(const char *file)
490 {
491   int parse_status;
492
493   surfxml_buffer_stack_stack_ptr = 1;
494   surfxml_buffer_stack_stack[0] = 0;
495
496   surf_parse_open(file);
497   init_data();
498   parse_status = surf_parse();
499   free_data();
500   surf_parse_close();
501   xbt_assert(!parse_status, "Parse error in %s", file);
502 }
503
504 /* Prop tag functions */
505
506 void parse_properties(const char* prop_id, const char* prop_value)
507 {
508     char *value = NULL;
509         if (!current_property_set)
510             current_property_set = xbt_dict_new();      // Maybe, it should be make a error
511         if(!strcmp(prop_id,"coordinates")){
512                 if(!strcmp(prop_value,"yes") && !COORD_HOST_LEVEL)
513                   {
514                             XBT_INFO("Configuration change: Set '%s' to '%s'", prop_id, prop_value);
515                                 COORD_HOST_LEVEL = xbt_lib_add_level(host_lib,xbt_dynar_free_voidp);
516                                 COORD_ASR_LEVEL  = xbt_lib_add_level(as_router_lib,xbt_dynar_free_voidp);
517                   }
518                 if(strcmp(A_surfxml_prop_value,"yes"))
519                           xbt_die("Setting XML prop coordinates must be \"yes\"");
520           }
521         else{
522                   value = xbt_strdup(prop_value);
523                   xbt_dict_set(current_property_set, prop_id, value, free);
524          }
525 }
526
527 /**
528  * With XML parser
529  */
530 void parse_properties_XML(void)
531 {
532   parse_properties(A_surfxml_prop_id, A_surfxml_prop_value);
533 }
534
535 /*
536  * With lua console
537  */
538 void parse_properties_lua(const char* prop_id, const char* prop_value)
539 {
540          parse_properties(prop_id, prop_value);
541 }
542
543 /* Trace management functions */
544
545 static double trace_periodicity = -1.0;
546 static char *trace_file = NULL;
547 static char *trace_id = NULL;
548
549 static void parse_Stag_trace(void)
550 {
551   trace_id = xbt_strdup(A_surfxml_trace_id);
552   trace_file = xbt_strdup(A_surfxml_trace_file);
553   surf_parse_get_double(&trace_periodicity, A_surfxml_trace_periodicity);
554 }
555
556 static void parse_Etag_trace(void)
557 {
558   tmgr_trace_t trace;
559   if (!trace_file || strcmp(trace_file, "") != 0) {
560     trace = tmgr_trace_new(trace_file);
561   } else {
562     if (strcmp(surfxml_pcdata, "") == 0)
563       trace = NULL;
564     else
565       trace =
566           tmgr_trace_new_from_string(trace_id, surfxml_pcdata,
567                                      trace_periodicity);
568   }
569   xbt_dict_set(traces_set_list, trace_id, (void *) trace, NULL);
570   xbt_free(trace_file);
571   trace_file = NULL;
572   xbt_free(trace_id);
573   trace_id = NULL;
574 }
575
576 static void parse_Stag_trace_connect(void)
577 {
578   xbt_assert(xbt_dict_get_or_null
579               (traces_set_list, A_surfxml_trace_connect_trace),
580               "Cannot connect trace %s to %s: trace unknown",
581               A_surfxml_trace_connect_trace,
582               A_surfxml_trace_connect_element);
583
584   switch (A_surfxml_trace_connect_kind) {
585   case A_surfxml_trace_connect_kind_HOST_AVAIL:
586     xbt_dict_set(trace_connect_list_host_avail,
587                  A_surfxml_trace_connect_trace,
588                  xbt_strdup(A_surfxml_trace_connect_element), free);
589     break;
590   case A_surfxml_trace_connect_kind_POWER:
591     xbt_dict_set(trace_connect_list_power, A_surfxml_trace_connect_trace,
592                  xbt_strdup(A_surfxml_trace_connect_element), free);
593     break;
594   case A_surfxml_trace_connect_kind_LINK_AVAIL:
595     xbt_dict_set(trace_connect_list_link_avail,
596                  A_surfxml_trace_connect_trace,
597                  xbt_strdup(A_surfxml_trace_connect_element), free);
598     break;
599   case A_surfxml_trace_connect_kind_BANDWIDTH:
600     xbt_dict_set(trace_connect_list_bandwidth,
601                  A_surfxml_trace_connect_trace,
602                  xbt_strdup(A_surfxml_trace_connect_element), free);
603     break;
604   case A_surfxml_trace_connect_kind_LATENCY:
605     xbt_dict_set(trace_connect_list_latency, A_surfxml_trace_connect_trace,
606                  xbt_strdup(A_surfxml_trace_connect_element), free);
607     break;
608   default:
609     xbt_die("Cannot connect trace %s to %s: kind of trace unknown",
610             A_surfxml_trace_connect_trace, A_surfxml_trace_connect_element);
611   }
612 }
613
614 /* Random tag functions */
615
616 double get_cpu_power(const char *power)
617 {
618   double power_scale = 0.0;
619   const char *p, *q;
620   char *generator;
621   random_data_t random = NULL;
622   /* randomness is inserted like this: power="$rand(my_random)" */
623   if (((p = strstr(power, "$rand(")) != NULL)
624       && ((q = strstr(power, ")")) != NULL)) {
625     if (p < q) {
626       generator = xbt_malloc(q - (p + 6) + 1);
627       memcpy(generator, p + 6, q - (p + 6));
628       generator[q - (p + 6)] = '\0';
629       random = xbt_dict_get_or_null(random_data_list, generator);
630       xbt_assert(random, "Random generator %s undefined", generator);
631       power_scale = random_generate(random);
632     }
633   } else {
634     surf_parse_get_double(&power_scale, power);
635   }
636   return power_scale;
637 }
638
639 double random_min, random_max, random_mean, random_std_deviation,
640     random_generator;
641 char *random_id;
642
643 static void init_randomness(void)
644 {
645   random_id = A_surfxml_random_id;
646   surf_parse_get_double(&random_min, A_surfxml_random_min);
647   surf_parse_get_double(&random_max, A_surfxml_random_max);
648   surf_parse_get_double(&random_mean, A_surfxml_random_mean);
649   surf_parse_get_double(&random_std_deviation,
650                         A_surfxml_random_std_deviation);
651   random_generator = A_surfxml_random_generator;
652 }
653
654 static void add_randomness(void)
655 {
656   /* If needed aditional properties can be added by using the prop tag */
657   random_data_t random =
658       random_new(random_generator, 0, random_min, random_max, random_mean,
659                  random_std_deviation);
660   xbt_dict_set(random_data_list, random_id, (void *) random,
661                &xbt_free_ref);
662 }
663
664 /**
665  * create CPU resource via CPU Model
666  */
667 void* surf_host_create_resource(char *name, double power_peak,
668                                double power_scale,
669                                tmgr_trace_t power_trace, int core,
670                                e_surf_resource_state_t state_initial,
671                                tmgr_trace_t state_trace,
672                                xbt_dict_t cpu_properties)
673 {
674   return surf_cpu_model->extension.cpu.create_resource(name, power_peak,
675                                                        power_scale,
676                                                        power_trace,
677                                                        core,
678                                                        state_initial,
679                                                        state_trace,
680                                                        cpu_properties);
681 }
682
683 /**
684  * create CPU resource via worsktation_ptask_L07 model
685  */
686
687 void* surf_wsL07_host_create_resource(char *name, double power_peak,
688                                      double power_scale,
689                                      tmgr_trace_t power_trace,
690                                      e_surf_resource_state_t state_initial,
691                                      tmgr_trace_t state_trace,
692                                      xbt_dict_t cpu_properties)
693 {
694   return surf_workstation_model->extension.workstation.cpu_create_resource(name,
695                                                                     power_peak,
696                                                                     power_scale,
697                                                                     power_trace,
698                                                                     state_initial,
699                                                                     state_trace,
700                                                                     cpu_properties);
701 }
702
703 /**
704  * create link resource via network Model
705  */
706 void* surf_link_create_resource(char *name,
707                                double bw_initial,
708                                tmgr_trace_t bw_trace,
709                                double lat_initial,
710                                tmgr_trace_t lat_trace,
711                                e_surf_resource_state_t
712                                state_initial,
713                                tmgr_trace_t state_trace,
714                                e_surf_link_sharing_policy_t policy,
715                                xbt_dict_t properties)
716 {
717   return surf_network_model->extension.network.create_resource(name,
718                                                                bw_initial,
719                                                                bw_trace,
720                                                                lat_initial,
721                                                                lat_trace,
722                                                                state_initial,
723                                                                state_trace,
724                                                                policy,
725                                                                properties);
726 }
727
728 /**
729  * create link resource via workstation_ptask_L07 model
730  */
731
732 void* surf_wsL07_link_create_resource(char *name,
733                                      double bw_initial,
734                                      tmgr_trace_t bw_trace,
735                                      double lat_initial,
736                                      tmgr_trace_t lat_trace,
737                                      e_surf_resource_state_t
738                                      state_initial,
739                                      tmgr_trace_t state_trace,
740                                      e_surf_link_sharing_policy_t
741                                      policy, xbt_dict_t properties)
742 {
743   return surf_workstation_model->extension.workstation.
744       link_create_resource(name, bw_initial, bw_trace, lat_initial,
745                            lat_trace, state_initial, state_trace, policy,
746                            properties);
747 }
748
749 /**
750  *
751  *init new routing model component
752  */
753
754 void surf_AS_new(const char *AS_id, const char *AS_mode)
755 {
756   routing_AS_init(AS_id, AS_mode);
757 }
758
759 void surf_AS_finalize(const char *AS_id)
760 {
761   routing_AS_end(AS_id);
762 }
763
764 /*
765  * add host to the network element list
766  */
767 void surf_route_add_host(const char *host_id)
768 {
769   routing_add_host(host_id);
770 }
771
772 /**
773  * set route
774  */
775 void surf_routing_add_route(const char *src_id, const char *dst_id,
776                             xbt_dynar_t links_id)
777 {
778   unsigned int i;
779   const char *link_id;
780   routing_set_route(src_id, dst_id);
781   xbt_dynar_foreach(links_id, i, link_id) {
782     routing_add_link(link_id);
783   }
784
785   //store the route
786   routing_store_route();
787 }
788
789 /**
790  * Add Traces
791  */
792 void surf_add_host_traces(void)
793 {
794   return surf_cpu_model->extension.cpu.add_traces();
795 }
796
797 void surf_add_link_traces(void)
798 {
799   return surf_network_model->extension.network.add_traces();
800 }
801
802 void surf_wsL07_add_traces(void)
803 {
804   return surf_workstation_model->extension.workstation.add_traces();
805 }