Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Detect/fix conflicting visibility and fix visibility wrt lua/java bindings
[simgrid.git] / src / surf / surfxml_parse.c
1 /* Copyright (c) 2006-2015. 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 <errno.h>
8 #include <math.h>
9 #include <stdarg.h> /* va_arg */
10
11 #include "xbt/misc.h"
12 #include "xbt/log.h"
13 #include "xbt/str.h"
14 #include "xbt/file.h"
15 #include "xbt/dict.h"
16 #include "surf/surfxml_parse.h"
17 #include "surf/surf_private.h"
18 #include "surf/random_mgr.h"
19 #include "simgrid/sg_config.h"
20
21 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_parse, surf,
22                                 "Logging specific to the SURF parsing module");
23 #undef CLEANUP
24 int ETag_surfxml_include_state(void);
25
26 #include "simgrid_dtd.c"
27
28 char* surf_parsed_filename = NULL; // to locate parse error messages
29
30 xbt_dynar_t parsed_link_list = NULL;   /* temporary store of current list link of a route */
31 /*
32  * Helping functions
33  */
34 void surf_parse_error(const char *fmt, ...) {
35   va_list va;
36   va_start(va,fmt);
37   int lineno = surf_parse_lineno;
38   char *msg = bvprintf(fmt,va);
39   va_end(va);
40   cleanup();
41   XBT_ERROR("Parse error at %s:%d: %s", surf_parsed_filename, lineno, msg);
42   surf_exit();
43   xbt_die("Exiting now");
44 }
45 void surf_parse_warn(const char *fmt, ...) {
46   va_list va;
47   va_start(va,fmt);
48   char *msg = bvprintf(fmt,va);
49   va_end(va);
50     XBT_WARN("%s:%d: %s", surf_parsed_filename, surf_parse_lineno, msg);
51     free(msg);
52 }
53
54 double surf_parse_get_double(const char *string) {
55   double res;
56   int ret = sscanf(string, "%lg", &res);
57   if (ret != 1)
58     surf_parse_error("%s is not a double", string);
59   //printf("Parsed double [%g] %s\n", res, string);
60   return res;
61 }
62
63 int surf_parse_get_int(const char *string) {
64   int res;
65   int ret = sscanf(string, "%d", &res);
66   if (ret != 1)
67     surf_parse_error("%s is not an integer", string);
68   return res;
69 }
70
71 struct unit_scale {
72   const char *unit;
73   double scale;
74 };
75
76 /* Note: field `unit' for the last element of parametre `units' should be
77  * NULL. */
78 static double surf_parse_get_value_with_unit(const char *string,
79                                              const struct unit_scale *units)
80 {
81   char* ptr;
82   double res;
83   int i;
84   errno = 0;
85   res   = strtod(string, &ptr);
86   if (errno == ERANGE)
87     surf_parse_error("value out of range: %s", string);
88   if (ptr == string)
89     surf_parse_error("cannot parse number: %s", string);
90   for (i = 0; units[i].unit != NULL && strcmp(ptr, units[i].unit) != 0; i++) {
91   }
92   if (units[i].unit != NULL)
93     res *= units[i].scale;
94   else
95     surf_parse_error("unknown unit: %s", ptr);
96   return res;
97 }
98
99 double surf_parse_get_time(const char *string)
100 {
101   const struct unit_scale units[] = {
102     { "w",  7 * 24 * 60 * 60 },
103     { "d",  24 * 60 * 60 },
104     { "h",  60 * 60 },
105     { "m",  60 },
106     { "s",  1.0 },
107     { "",   1.0 },              /* default unit is seconds */
108     { "ms", 1e-3 },
109     { "us", 1e-6 },
110     { "ns", 1e-9 },
111     { "ps", 1e-12 },
112     { NULL, 0 }
113   };
114   return surf_parse_get_value_with_unit(string, units);
115 }
116
117 double surf_parse_get_size(const char *string)
118 {
119   const struct unit_scale units[] = {
120     { "TiB", pow(1024, 4) },
121     { "GiB", pow(1024, 3) },
122     { "MiB", pow(1024, 2) },
123     { "KiB", 1024 },
124     { "TB",  1e12 },
125     { "GB",  1e9 },
126     { "MB",  1e6 },
127     { "kB",  1e3 },
128     { "B",   1.0 },
129     { "",      1.0 },           /* default unit is bytes*/
130     { "Tib", 0.125 * pow(1024, 4) },
131     { "Gib", 0.125 * pow(1024, 3) },
132     { "Mib", 0.125 * pow(1024, 2) },
133     { "Kib", 0.125 * 1024 },
134     { "Tb",  0.125 * 1e12 },
135     { "Gb",  0.125 * 1e9 },
136     { "Mb",  0.125 * 1e6 },
137     { "kb",  0.125 * 1e3 },
138     { "b",   0.125 },
139     { NULL,    0 }
140   };
141   return surf_parse_get_value_with_unit(string, units);
142 }
143
144 double surf_parse_get_bandwidth(const char *string)
145 {
146   const struct unit_scale units[] = {
147     { "TiBps", pow(1024, 4) },
148     { "GiBps", pow(1024, 3) },
149     { "MiBps", pow(1024, 2) },
150     { "KiBps", 1024 },
151     { "TBps",  1e12 },
152     { "GBps",  1e9 },
153     { "MBps",  1e6 },
154     { "kBps",  1e3 },
155     { "Bps",   1.0 },
156     { "",      1.0 },           /* default unit is bytes per second */
157     { "Tibps", 0.125 * pow(1024, 4) },
158     { "Gibps", 0.125 * pow(1024, 3) },
159     { "Mibps", 0.125 * pow(1024, 2) },
160     { "Kibps", 0.125 * 1024 },
161     { "Tbps",  0.125 * 1e12 },
162     { "Gbps",  0.125 * 1e9 },
163     { "Mbps",  0.125 * 1e6 },
164     { "kbps",  0.125 * 1e3 },
165     { "bps",   0.125 },
166     { NULL,    0 }
167   };
168   return surf_parse_get_value_with_unit(string, units);
169 }
170
171 double surf_parse_get_power(const char *string)
172 {
173   const struct unit_scale units[] = {
174     { "yottaflops", 1e24 },
175     { "Yf",         1e24 },
176     { "zettaflops", 1e21 },
177     { "Zf",         1e21 },
178     { "exaflops",   1e18 },
179     { "Ef",         1e18 },
180     { "petaflops",  1e15 },
181     { "Pf",         1e15 },
182     { "teraflops",  1e12 },
183     { "Tf",         1e12 },
184     { "gigaflops",  1e9 },
185     { "Gf",         1e9 },
186     { "megaflops",  1e6 },
187     { "Mf",         1e6 },
188     { "kiloflops",  1e3 },
189     { "kf",         1e3 },
190     { "flops",      1.0 },
191     { "f",          1.0 },
192     { "",           1.0 },      /* default unit is flops */
193     { NULL,         0 }
194   };
195   return surf_parse_get_value_with_unit(string, units);
196 }
197
198 /*
199  * All the callback lists that can be overridden anywhere.
200  * (this list should probably be reduced to the bare minimum to allow the models to work)
201  */
202
203 /* make sure these symbols are defined as strong ones in this file so that the linker can resolve them */
204
205 /* The default current property receiver. Setup in the corresponding opening callbacks. */
206 xbt_dict_t current_property_set = NULL;
207 xbt_dict_t current_model_property_set = NULL;
208 xbt_dict_t as_current_property_set = NULL;
209 int AS_TAG = 0;
210 char* as_name_tab[1024];
211 void* as_dict_tab[1024];
212 int as_prop_nb = 0;
213
214
215 /* dictionary of random generator data */
216 xbt_dict_t random_data_list = NULL;
217
218 YY_BUFFER_STATE surf_input_buffer;
219 FILE *surf_file_to_parse = NULL;
220
221 /*
222  * Stuff relative to storage
223  */
224 void STag_surfxml_storage(void)
225 {
226   AS_TAG = 0;
227   XBT_DEBUG("STag_surfxml_storage");
228   xbt_assert(current_property_set == NULL, "Someone forgot to reset the property set to NULL in its closing tag (or XML malformed)");
229 }
230 void ETag_surfxml_storage(void)
231 {
232   s_sg_platf_storage_cbarg_t storage = SG_PLATF_STORAGE_INITIALIZER;
233   memset(&storage,0,sizeof(storage));
234
235   storage.id           = A_surfxml_storage_id;
236   storage.type_id      = A_surfxml_storage_typeId;
237   storage.content      = A_surfxml_storage_content;
238   storage.content_type = A_surfxml_storage_content___type;
239   storage.properties   = current_property_set;
240   storage.attach       = A_surfxml_storage_attach;
241   sg_platf_new_storage(&storage);
242   current_property_set = NULL;
243 }
244 void STag_surfxml_storage___type(void)
245 {
246   AS_TAG = 0;
247   XBT_DEBUG("STag_surfxml_storage___type");
248   xbt_assert(current_property_set == NULL, "Someone forgot to reset the property set to NULL in its closing tag (or XML malformed)");
249   xbt_assert(current_model_property_set == NULL, "Someone forgot to reset the model property set to NULL in its closing tag (or XML malformed)");
250 }
251 void ETag_surfxml_storage___type(void)
252 {
253   s_sg_platf_storage_type_cbarg_t storage_type = SG_PLATF_STORAGE_TYPE_INITIALIZER;
254   memset(&storage_type,0,sizeof(storage_type));
255
256   storage_type.content          = A_surfxml_storage___type_content;
257   storage_type.content_type     = A_surfxml_storage___type_content___type;
258   storage_type.id               = A_surfxml_storage___type_id;
259   storage_type.model            = A_surfxml_storage___type_model;
260   storage_type.properties       = current_property_set;
261   storage_type.model_properties = current_model_property_set;
262   storage_type.size             = surf_parse_get_size(A_surfxml_storage___type_size);
263   sg_platf_new_storage_type(&storage_type);
264   current_property_set       = NULL;
265   current_model_property_set = NULL;
266 }
267 void STag_surfxml_mstorage(void)
268 {
269   XBT_DEBUG("STag_surfxml_mstorage");
270 }
271 void ETag_surfxml_mstorage(void)
272 {
273   s_sg_platf_mstorage_cbarg_t mstorage = SG_PLATF_MSTORAGE_INITIALIZER;
274   memset(&mstorage,0,sizeof(mstorage));
275
276   mstorage.name    = A_surfxml_mstorage_name;
277   mstorage.type_id = A_surfxml_mstorage_typeId;
278   sg_platf_new_mstorage(&mstorage);
279 }
280 void STag_surfxml_mount(void)
281 {
282   XBT_DEBUG("STag_surfxml_mount");
283 }
284 void ETag_surfxml_mount(void)
285 {
286   s_sg_platf_mount_cbarg_t mount = SG_PLATF_MOUNT_INITIALIZER;
287   memset(&mount,0,sizeof(mount));
288
289   mount.name      = A_surfxml_mount_name;
290   mount.storageId = A_surfxml_mount_storageId;
291   sg_platf_new_mount(&mount);
292 }
293
294 /*
295  * Stuff relative to the <include> tag
296  */
297 static xbt_dynar_t surf_input_buffer_stack    = NULL;
298 static xbt_dynar_t surf_file_to_parse_stack   = NULL;
299 static xbt_dynar_t surf_parsed_filename_stack = NULL;
300
301 void STag_surfxml_include(void)
302 {
303   parse_after_config();
304   XBT_DEBUG("STag_surfxml_include '%s'",A_surfxml_include_file);
305   xbt_dynar_push(surf_parsed_filename_stack,&surf_parsed_filename); // save old file name
306   surf_parsed_filename = xbt_strdup(A_surfxml_include_file);
307
308   xbt_dynar_push(surf_file_to_parse_stack, &surf_file_to_parse); //save old file descriptor
309
310   surf_file_to_parse = surf_fopen(A_surfxml_include_file, "r"); // read new file descriptor
311   xbt_assert((surf_file_to_parse), "Unable to open \"%s\"\n",
312               A_surfxml_include_file);
313   xbt_dynar_push(surf_input_buffer_stack,&surf_input_buffer);
314   surf_input_buffer = surf_parse__create_buffer(surf_file_to_parse, YY_BUF_SIZE);
315   surf_parse_push_buffer_state(surf_input_buffer);
316
317   fflush(NULL);
318 }
319
320 void ETag_surfxml_include(void) {
321 /* Nothing to do when done with reading the include tag.
322  * Instead, the handling should be deferred until the EOF of current buffer -- see below */
323 }
324
325 /** @brief When reaching EOF, check whether we are in an include tag, and behave accordingly if yes
326  *
327  * This function is called automatically by sedding the parser in tools/cmake/MaintainerMode.cmake
328  * Every FAIL on "Premature EOF" is preceded by a call to this function, which role is to restore the
329  * previous buffer if we reached the EOF /of an include file/. Its return code is used to avoid the
330  * error message in that case.
331  *
332  * Yeah, that's terribly hackish, but it works. A better solution should be dealed with in flexml
333  * directly: a command line flag could instruct it to do the correct thing when #include is encountered
334  * on a line. One day maybe, if the maya allow it.
335  */
336 int ETag_surfxml_include_state(void)
337 {
338   fflush(NULL);
339   XBT_DEBUG("ETag_surfxml_include_state '%s'",A_surfxml_include_file);
340
341   if(xbt_dynar_is_empty(surf_input_buffer_stack)) // nope, that's a true premature EOF. Let the parser die verbosely.
342     return 0;
343
344   // Yeah, we were in an <include> Restore state and proceed.
345   fclose(surf_file_to_parse);
346   xbt_dynar_pop(surf_file_to_parse_stack, &surf_file_to_parse);
347   surf_parse_pop_buffer_state();
348   xbt_dynar_pop(surf_input_buffer_stack,&surf_input_buffer);
349
350   // Restore the filename for error messages
351   free(surf_parsed_filename);
352   xbt_dynar_pop(surf_parsed_filename_stack,&surf_parsed_filename);
353
354   return 1;
355 }
356
357
358 void surf_parse_init_callbacks(void)
359 {
360     sg_platf_init();
361 }
362
363 void surf_parse_reset_callbacks(void)
364 {
365   surf_parse_free_callbacks();
366   surf_parse_init_callbacks();
367 }
368
369 void surf_parse_free_callbacks(void)
370 {
371   sg_platf_exit();
372 }
373
374 /* Stag and Etag parse functions */
375
376 void STag_surfxml_platform(void) {
377   XBT_ATTRIB_UNUSED double version = surf_parse_get_double(A_surfxml_platform_version);
378
379   xbt_assert((version >= 1.0), "******* BIG FAT WARNING *********\n "
380       "You're using an ancient XML file.\n"
381       "Since SimGrid 3.1, units are Bytes, Flops, and seconds "
382       "instead of MBytes, MFlops and seconds.\n"
383
384       "Use simgrid_update_xml to update your file automatically. "
385       "This program is installed automatically with SimGrid, or "
386       "available in the tools/ directory of the source archive.\n"
387
388       "Please check also out the SURF section of the ChangeLog for "
389       "the 3.1 version for more information. \n"
390
391       "Last, do not forget to also update your values for "
392       "the calls to MSG_task_create (if any).");
393   xbt_assert((version >= 3.0), "******* BIG FAT WARNING *********\n "
394       "You're using an old XML file.\n"
395       "Use simgrid_update_xml to update your file automatically. "
396       "This program is installed automatically with SimGrid, or "
397       "available in the tools/ directory of the source archive.");
398
399   sg_platf_begin();
400 }
401 void ETag_surfxml_platform(void){
402   sg_platf_end();
403 }
404
405 void STag_surfxml_host(void){
406   AS_TAG = 0;
407   xbt_assert(current_property_set == NULL, "Someone forgot to reset the property set to NULL in its closing tag (or XML malformed)");
408 }
409
410 void STag_surfxml_prop(void)
411 {
412   if(AS_TAG){
413     if (!as_current_property_set){
414       xbt_assert(as_prop_nb < 1024, "Number of AS property reach the limit!!!");
415       as_current_property_set = xbt_dict_new_homogeneous(xbt_free_f); // Maybe, it should raise an error
416       as_name_tab[as_prop_nb] = xbt_strdup(A_surfxml_AS_id);
417       as_dict_tab[as_prop_nb] = as_current_property_set;
418       XBT_DEBUG("PUSH prop set %p for AS '%s'",as_dict_tab[as_prop_nb],as_name_tab[as_prop_nb]);
419       as_prop_nb++;
420     }
421     XBT_DEBUG("add prop %s=%s into current AS property set", A_surfxml_prop_id, A_surfxml_prop_value);
422     xbt_dict_set(as_current_property_set, A_surfxml_prop_id, xbt_strdup(A_surfxml_prop_value), NULL);
423   }
424   else{
425     if (!current_property_set)
426        current_property_set = xbt_dict_new(); // Maybe, it should raise an error
427     xbt_dict_set(current_property_set, A_surfxml_prop_id, xbt_strdup(A_surfxml_prop_value), xbt_free_f);
428     XBT_DEBUG("add prop %s=%s into current property set", A_surfxml_prop_id, A_surfxml_prop_value);
429   }
430 }
431
432 void ETag_surfxml_host(void)    {
433   s_sg_platf_host_cbarg_t host = SG_PLATF_HOST_INITIALIZER;
434   char* buf;
435   memset(&host,0,sizeof(host));
436
437
438   host.properties = current_property_set;
439
440   host.id = A_surfxml_host_id;
441
442   buf = A_surfxml_host_power;
443   XBT_DEBUG("Buffer: %s", buf);
444   host.power_peak = xbt_dynar_new(sizeof(double), NULL);
445   if (strchr(buf, ',') == NULL){
446           double power_value = get_cpu_power(A_surfxml_host_power);
447           xbt_dynar_push_as(host.power_peak,double, power_value);
448   }
449   else {
450           xbt_dynar_t pstate_list = xbt_str_split(buf, ",");
451           int i;
452           for (i = 0; i < xbt_dynar_length(pstate_list); i++) {
453                   double power_value;
454                   char* power_value_str;
455
456                   xbt_dynar_get_cpy(pstate_list, i, &power_value_str);
457                   xbt_str_trim(power_value_str, NULL);
458                   power_value = get_cpu_power(power_value_str);
459                   xbt_dynar_push_as(host.power_peak, double, power_value);
460                   XBT_DEBUG("Power value: %f", power_value);
461           }
462           xbt_dynar_free(&pstate_list);
463   }
464
465   XBT_DEBUG("pstate: %s", A_surfxml_host_pstate);
466   //host.power_peak = get_cpu_power(A_surfxml_host_power);
467   host.power_scale = surf_parse_get_double( A_surfxml_host_availability);
468   host.core_amount = surf_parse_get_int(A_surfxml_host_core);
469   host.power_trace = tmgr_trace_new_from_file(A_surfxml_host_availability___file);
470   host.state_trace = tmgr_trace_new_from_file(A_surfxml_host_state___file);
471   host.pstate      = surf_parse_get_int(A_surfxml_host_pstate);
472
473   xbt_assert((A_surfxml_host_state == A_surfxml_host_state_ON) ||
474         (A_surfxml_host_state == A_surfxml_host_state_OFF), "Invalid state");
475   if (A_surfxml_host_state == A_surfxml_host_state_ON)
476     host.initial_state = SURF_RESOURCE_ON;
477   if (A_surfxml_host_state == A_surfxml_host_state_OFF)
478     host.initial_state = SURF_RESOURCE_OFF;
479   host.coord = A_surfxml_host_coordinates;
480
481   sg_platf_new_host(&host);
482   current_property_set = NULL;
483 }
484
485 void STag_surfxml_host___link(void){
486   XBT_DEBUG("Create a Host_link for %s",A_surfxml_host___link_id);
487   s_sg_platf_host_link_cbarg_t host_link = SG_PLATF_HOST_LINK_INITIALIZER;
488   memset(&host_link,0,sizeof(host_link));
489
490   host_link.id        = A_surfxml_host___link_id;
491   host_link.link_up   = A_surfxml_host___link_up;
492   host_link.link_down = A_surfxml_host___link_down;
493   sg_platf_new_host_link(&host_link);
494 }
495
496 void STag_surfxml_router(void){
497   s_sg_platf_router_cbarg_t router = SG_PLATF_ROUTER_INITIALIZER;
498   memset(&router, 0, sizeof(router));
499
500   router.id    = A_surfxml_router_id;
501   router.coord = A_surfxml_router_coordinates;
502
503   sg_platf_new_router(&router);
504 }
505
506 void ETag_surfxml_cluster(void){
507   s_sg_platf_cluster_cbarg_t cluster = SG_PLATF_CLUSTER_INITIALIZER;
508   memset(&cluster,0,sizeof(cluster));
509   cluster.properties = as_current_property_set;
510
511   cluster.id          = A_surfxml_cluster_id;
512   cluster.prefix      = A_surfxml_cluster_prefix;
513   cluster.suffix      = A_surfxml_cluster_suffix;
514   cluster.radical     = A_surfxml_cluster_radical;
515   cluster.power       = surf_parse_get_power(A_surfxml_cluster_power);
516   cluster.core_amount = surf_parse_get_int(A_surfxml_cluster_core);
517   cluster.bw          = surf_parse_get_bandwidth(A_surfxml_cluster_bw);
518   cluster.lat         = surf_parse_get_time(A_surfxml_cluster_lat);
519   if(strcmp(A_surfxml_cluster_bb___bw,""))
520     cluster.bb_bw = surf_parse_get_bandwidth(A_surfxml_cluster_bb___bw);
521   if(strcmp(A_surfxml_cluster_bb___lat,""))
522     cluster.bb_lat = surf_parse_get_time(A_surfxml_cluster_bb___lat);
523   if(strcmp(A_surfxml_cluster_limiter___link,""))
524     cluster.limiter_link = surf_parse_get_double(A_surfxml_cluster_limiter___link);
525   if(strcmp(A_surfxml_cluster_loopback___bw,""))
526     cluster.loopback_bw = surf_parse_get_bandwidth(A_surfxml_cluster_loopback___bw);
527   if(strcmp(A_surfxml_cluster_loopback___lat,""))
528     cluster.loopback_lat = surf_parse_get_time(A_surfxml_cluster_loopback___lat);
529
530   switch(AX_surfxml_cluster_topology){
531   case A_surfxml_cluster_topology_FLAT:
532     cluster.topology= SURF_CLUSTER_FLAT ;
533     break;
534   case A_surfxml_cluster_topology_TORUS:
535     cluster.topology= SURF_CLUSTER_TORUS ;
536     break;
537   case A_surfxml_cluster_topology_FAT___TREE:
538     cluster.topology = SURF_CLUSTER_FAT_TREE;
539     break;
540   default:
541     surf_parse_error("Invalid cluster topology for cluster %s",
542                      cluster.id);
543     break;
544   }
545   cluster.topo_parameters = A_surfxml_cluster_topo___parameters;
546   cluster.router_id = A_surfxml_cluster_router___id;
547
548   switch (AX_surfxml_cluster_sharing___policy) {
549   case A_surfxml_cluster_sharing___policy_SHARED:
550     cluster.sharing_policy = SURF_LINK_SHARED;
551     break;
552   case A_surfxml_cluster_sharing___policy_FULLDUPLEX:
553     cluster.sharing_policy = SURF_LINK_FULLDUPLEX;
554     break;
555   case A_surfxml_cluster_sharing___policy_FATPIPE:
556     cluster.sharing_policy = SURF_LINK_FATPIPE;
557     break;
558   default:
559     surf_parse_error("Invalid cluster sharing policy for cluster %s",
560                      cluster.id);
561     break;
562   }
563   switch (AX_surfxml_cluster_bb___sharing___policy) {
564   case A_surfxml_cluster_bb___sharing___policy_FATPIPE:
565     cluster.bb_sharing_policy = SURF_LINK_FATPIPE;
566     break;
567   case A_surfxml_cluster_bb___sharing___policy_SHARED:
568     cluster.bb_sharing_policy = SURF_LINK_SHARED;
569     break;
570   default:
571     surf_parse_error("Invalid bb sharing policy in cluster %s",
572                      cluster.id);
573     break;
574   }
575
576   cluster.availability_trace = A_surfxml_cluster_availability___file;
577   cluster.state_trace = A_surfxml_cluster_state___file;
578   sg_platf_new_cluster(&cluster);
579
580   current_property_set = NULL;
581 }
582
583 void STag_surfxml_cluster(void){
584   parse_after_config();
585   xbt_assert(current_property_set == NULL, "Someone forgot to reset the property set to NULL in its closing tag (or XML malformed)");
586 }
587
588 void STag_surfxml_cabinet(void){
589   parse_after_config();
590   s_sg_platf_cabinet_cbarg_t cabinet = SG_PLATF_CABINET_INITIALIZER;
591   memset(&cabinet,0,sizeof(cabinet));
592   cabinet.id      = A_surfxml_cabinet_id;
593   cabinet.prefix  = A_surfxml_cabinet_prefix;
594   cabinet.suffix  = A_surfxml_cabinet_suffix;
595   cabinet.power   = surf_parse_get_power(A_surfxml_cabinet_power);
596   cabinet.bw      = surf_parse_get_bandwidth(A_surfxml_cabinet_bw);
597   cabinet.lat     = surf_parse_get_time(A_surfxml_cabinet_lat);
598   cabinet.radical = A_surfxml_cabinet_radical;
599
600   sg_platf_new_cabinet(&cabinet);
601 }
602
603 void STag_surfxml_peer(void){
604   parse_after_config();
605   s_sg_platf_peer_cbarg_t peer = SG_PLATF_PEER_INITIALIZER;
606   memset(&peer,0,sizeof(peer));
607   peer.id                 = A_surfxml_peer_id;
608   peer.power              = surf_parse_get_power(A_surfxml_peer_power);
609   peer.bw_in              = surf_parse_get_bandwidth(A_surfxml_peer_bw___in);
610   peer.bw_out             = surf_parse_get_bandwidth(A_surfxml_peer_bw___out);
611   peer.lat                = surf_parse_get_time(A_surfxml_peer_lat);
612   peer.coord              = A_surfxml_peer_coordinates;
613   peer.availability_trace = tmgr_trace_new_from_file(A_surfxml_peer_availability___file);
614   peer.state_trace        = tmgr_trace_new_from_file(A_surfxml_peer_state___file);
615
616   sg_platf_new_peer(&peer);
617 }
618
619 void STag_surfxml_link(void){
620   AS_TAG = 0;
621   xbt_assert(current_property_set == NULL, "Someone forgot to reset the property set to NULL in its closing tag (or XML malformed)");
622 }
623
624 void ETag_surfxml_link(void){
625   s_sg_platf_link_cbarg_t link = SG_PLATF_LINK_INITIALIZER;
626   memset(&link,0,sizeof(link));
627
628   link.properties = current_property_set;
629
630   link.id                                            = A_surfxml_link_id;
631   link.bandwidth                                     = surf_parse_get_bandwidth(A_surfxml_link_bandwidth);
632   //printf("Link bandwidth [%g]\n", link.bandwidth);
633   link.bandwidth_trace                               = tmgr_trace_new_from_file(A_surfxml_link_bandwidth___file);
634   link.latency                                       = surf_parse_get_time(A_surfxml_link_latency);
635   //printf("Link latency [%g]\n", link.latency);
636   link.latency_trace                                 = tmgr_trace_new_from_file(A_surfxml_link_latency___file);
637
638   switch (A_surfxml_link_state) {
639   case A_surfxml_link_state_ON:
640     link.state = SURF_RESOURCE_ON;
641     break;
642   case A_surfxml_link_state_OFF:
643     link.state = SURF_RESOURCE_OFF;
644     break;
645   default:
646     surf_parse_error("invalid state for link %s", link.id);
647     break;
648   }
649   link.state_trace = tmgr_trace_new_from_file(A_surfxml_link_state___file);
650
651   switch (A_surfxml_link_sharing___policy) {
652   case A_surfxml_link_sharing___policy_SHARED:
653     link.policy = SURF_LINK_SHARED;
654     break;
655   case A_surfxml_link_sharing___policy_FATPIPE:
656      link.policy = SURF_LINK_FATPIPE;
657      break;
658   case A_surfxml_link_sharing___policy_FULLDUPLEX:
659      link.policy = SURF_LINK_FULLDUPLEX;
660      break;
661   default:
662     surf_parse_error("Invalid sharing policy in link %s", link.id);
663     break;
664   }
665
666   sg_platf_new_link(&link);
667
668   current_property_set = NULL;
669 }
670
671 void STag_surfxml_link___ctn(void){
672
673   char *link_id;
674   switch (A_surfxml_link___ctn_direction) {
675   case AU_surfxml_link___ctn_direction:
676   case A_surfxml_link___ctn_direction_NONE:
677     link_id = xbt_strdup(A_surfxml_link___ctn_id);
678     break;
679   case A_surfxml_link___ctn_direction_UP:
680     link_id = bprintf("%s_UP", A_surfxml_link___ctn_id);
681     break;
682   case A_surfxml_link___ctn_direction_DOWN:
683     link_id = bprintf("%s_DOWN", A_surfxml_link___ctn_id);
684     break;
685   }
686
687   // FIXME we should push the surf link object but it don't
688   // work because of model rulebased
689   xbt_dynar_push(parsed_link_list, &link_id);
690 }
691
692 void ETag_surfxml_backbone(void){
693   s_sg_platf_link_cbarg_t link = SG_PLATF_LINK_INITIALIZER;
694   memset(&link,0,sizeof(link));
695
696   link.properties = NULL;
697
698   link.id = A_surfxml_backbone_id;
699   link.bandwidth = surf_parse_get_bandwidth(A_surfxml_backbone_bandwidth);
700   link.latency = surf_parse_get_time(A_surfxml_backbone_latency);
701   link.state = SURF_RESOURCE_ON;
702   link.policy = SURF_LINK_SHARED;
703
704   sg_platf_new_link(&link);
705   routing_cluster_add_backbone(sg_link_by_name(A_surfxml_backbone_id));
706 }
707
708 void STag_surfxml_route(void){
709   xbt_assert(strlen(A_surfxml_route_src) > 0 || strlen(A_surfxml_route_dst) > 0,
710       "Missing end-points while defining route \"%s\"->\"%s\"",
711       A_surfxml_route_src, A_surfxml_route_dst);
712   parsed_link_list = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
713 }
714
715 void STag_surfxml_ASroute(void){
716   xbt_assert(strlen(A_surfxml_ASroute_src) > 0
717              || strlen(A_surfxml_ASroute_dst) > 0
718              || strlen(A_surfxml_ASroute_gw___src) > 0
719              || strlen(A_surfxml_ASroute_gw___dst) > 0,
720              "Missing end-points while defining route \"%s\"->\"%s\" (with %s and %s as gateways)",
721              A_surfxml_ASroute_src, A_surfxml_ASroute_dst,
722              A_surfxml_ASroute_gw___src, A_surfxml_ASroute_gw___dst);
723   parsed_link_list = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
724 }
725
726 void STag_surfxml_bypassRoute(void){
727   xbt_assert(strlen(A_surfxml_bypassRoute_src) > 0
728              || strlen(A_surfxml_bypassRoute_dst) > 0,
729              "Missing end-points while defining bupass route \"%s\"->\"%s\"",
730              A_surfxml_bypassRoute_src, A_surfxml_bypassRoute_dst);
731   parsed_link_list = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
732 }
733
734 void STag_surfxml_bypassASroute(void){
735   xbt_assert(strlen(A_surfxml_bypassASroute_src) > 0
736              || strlen(A_surfxml_bypassASroute_dst) > 0
737              || strlen(A_surfxml_bypassASroute_gw___src) > 0
738              || strlen(A_surfxml_bypassASroute_gw___dst) > 0,
739              "Missing end-points while defining route \"%s\"->\"%s\" (with %s and %s as gateways)",
740              A_surfxml_bypassASroute_src, A_surfxml_bypassASroute_dst,
741              A_surfxml_bypassASroute_gw___src,A_surfxml_bypassASroute_gw___dst);
742   parsed_link_list = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
743 }
744
745 void ETag_surfxml_route(void){
746   s_sg_platf_route_cbarg_t route = SG_PLATF_ROUTE_INITIALIZER;
747   memset(&route,0,sizeof(route));
748
749   route.src       = A_surfxml_route_src;
750   route.dst       = A_surfxml_route_dst;
751   route.gw_src    = NULL;
752   route.gw_dst    = NULL;
753   route.link_list = parsed_link_list;
754
755   switch (A_surfxml_route_symmetrical) {
756   case AU_surfxml_route_symmetrical:
757   case A_surfxml_route_symmetrical_YES:
758     route.symmetrical = TRUE;
759     break;
760   case A_surfxml_route_symmetrical_NO:
761     route.symmetrical = FALSE;;
762     break;
763   }
764
765   sg_platf_new_route(&route);
766   parsed_link_list = NULL;
767 }
768
769 void ETag_surfxml_ASroute(void){
770   s_sg_platf_route_cbarg_t ASroute = SG_PLATF_ROUTE_INITIALIZER;
771   memset(&ASroute,0,sizeof(ASroute));
772
773   ASroute.src = A_surfxml_ASroute_src;
774   ASroute.dst = A_surfxml_ASroute_dst;
775
776   ASroute.gw_src = sg_routing_edge_by_name_or_null(A_surfxml_ASroute_gw___src);
777   ASroute.gw_dst = sg_routing_edge_by_name_or_null(A_surfxml_ASroute_gw___dst);
778
779   if (A_surfxml_ASroute_gw___src && !ASroute.gw_src)
780     surf_parse_error("gw_src=\"%s\" not found for ASroute from \"%s\" to \"%s\"",
781                      A_surfxml_ASroute_gw___src, ASroute.src, ASroute.dst);
782   if (A_surfxml_ASroute_gw___dst && !ASroute.gw_dst)
783     surf_parse_error("gw_dst=\"%s\" not found for ASroute from \"%s\" to \"%s\"",
784                      A_surfxml_ASroute_gw___dst, ASroute.src, ASroute.dst);
785
786   ASroute.link_list = parsed_link_list;
787
788   switch (A_surfxml_ASroute_symmetrical) {
789   case AU_surfxml_ASroute_symmetrical:
790   case A_surfxml_ASroute_symmetrical_YES:
791     ASroute.symmetrical = TRUE;
792     break;
793   case A_surfxml_ASroute_symmetrical_NO:
794     ASroute.symmetrical = FALSE;
795     break;
796   }
797
798   sg_platf_new_ASroute(&ASroute);
799   parsed_link_list = NULL;
800 }
801
802 void ETag_surfxml_bypassRoute(void){
803   s_sg_platf_route_cbarg_t route = SG_PLATF_ROUTE_INITIALIZER;
804   memset(&route,0,sizeof(route));
805
806   route.src = A_surfxml_bypassRoute_src;
807   route.dst = A_surfxml_bypassRoute_dst;
808   route.gw_src = NULL;
809   route.gw_dst = NULL;
810   route.link_list = parsed_link_list;
811   route.symmetrical = FALSE;
812
813   sg_platf_new_bypassRoute(&route);
814   parsed_link_list = NULL;
815 }
816
817 void ETag_surfxml_bypassASroute(void){
818   s_sg_platf_route_cbarg_t ASroute = SG_PLATF_ROUTE_INITIALIZER;
819   memset(&ASroute,0,sizeof(ASroute));
820
821   ASroute.src         = A_surfxml_bypassASroute_src;
822   ASroute.dst         = A_surfxml_bypassASroute_dst;
823   ASroute.link_list   = parsed_link_list;
824   ASroute.symmetrical = FALSE;
825
826   ASroute.gw_src = sg_routing_edge_by_name_or_null(A_surfxml_bypassASroute_gw___src);
827   ASroute.gw_dst = sg_routing_edge_by_name_or_null(A_surfxml_bypassASroute_gw___dst);
828
829   sg_platf_new_bypassASroute(&ASroute);
830   parsed_link_list = NULL;
831 }
832
833 void ETag_surfxml_trace(void){
834   s_sg_platf_trace_cbarg_t trace = SG_PLATF_TRACE_INITIALIZER;
835   memset(&trace,0,sizeof(trace));
836
837   trace.id = A_surfxml_trace_id;
838   trace.file = A_surfxml_trace_file;
839   trace.periodicity = surf_parse_get_double(A_surfxml_trace_periodicity);
840   trace.pc_data = surfxml_pcdata;
841
842   sg_platf_new_trace(&trace);
843 }
844
845 void STag_surfxml_trace___connect(void){
846   parse_after_config();
847   s_sg_platf_trace_connect_cbarg_t trace_connect = SG_PLATF_TRACE_CONNECT_INITIALIZER;
848   memset(&trace_connect,0,sizeof(trace_connect));
849
850   trace_connect.element = A_surfxml_trace___connect_element;
851   trace_connect.trace = A_surfxml_trace___connect_trace;
852
853   switch (A_surfxml_trace___connect_kind) {
854   case AU_surfxml_trace___connect_kind:
855   case A_surfxml_trace___connect_kind_POWER:
856     trace_connect.kind =  SURF_TRACE_CONNECT_KIND_POWER;
857     break;
858   case A_surfxml_trace___connect_kind_BANDWIDTH:
859     trace_connect.kind =  SURF_TRACE_CONNECT_KIND_BANDWIDTH;
860     break;
861   case A_surfxml_trace___connect_kind_HOST___AVAIL:
862     trace_connect.kind =  SURF_TRACE_CONNECT_KIND_HOST_AVAIL;
863     break;
864   case A_surfxml_trace___connect_kind_LATENCY:
865     trace_connect.kind =  SURF_TRACE_CONNECT_KIND_LATENCY;
866     break;
867   case A_surfxml_trace___connect_kind_LINK___AVAIL:
868     trace_connect.kind =  SURF_TRACE_CONNECT_KIND_LINK_AVAIL;
869     break;
870   }
871   sg_platf_trace_connect(&trace_connect);
872 }
873
874 void STag_surfxml_AS(void){
875   parse_after_config();
876   AS_TAG                   = 1;
877   s_sg_platf_AS_cbarg_t AS = SG_PLATF_AS_INITIALIZER;
878   AS.id                    = A_surfxml_AS_id;
879   AS.routing               = (int)A_surfxml_AS_routing;
880
881   as_current_property_set = NULL;
882
883   sg_platf_new_AS_begin(&AS);
884 }
885 void ETag_surfxml_AS(void){
886   if(as_prop_nb){
887     char *name      = as_name_tab[as_prop_nb-1];
888     xbt_dict_t dict = as_dict_tab[as_prop_nb-1];
889     as_prop_nb--;
890     XBT_DEBUG("POP prop %p for AS '%s'",dict,name);
891     xbt_lib_set(as_router_lib,
892         name,
893       ROUTING_PROP_ASR_LEVEL,
894       dict);
895     xbt_free(name);
896   }
897   sg_platf_new_AS_end();
898 }
899
900 void STag_surfxml_config(void){
901   AS_TAG = 0;
902   xbt_assert(current_property_set == NULL, "Someone forgot to reset the property set to NULL in its closing tag (or XML malformed)");
903   XBT_DEBUG("START configuration name = %s",A_surfxml_config_id);
904   if (_sg_cfg_init_status == 2) {
905     surf_parse_error("All <config> tags must be given before any platform elements (such as <AS>, <host>, <cluster>, <link>, etc).");
906   }
907 }
908 void ETag_surfxml_config(void){
909   xbt_dict_cursor_t cursor = NULL;
910   char *key;
911   char *elem;
912   char *cfg;
913   xbt_dict_foreach(current_property_set, cursor, key, elem) {
914     cfg = bprintf("%s:%s",key,elem);
915     if(xbt_cfg_is_default_value(_sg_cfg_set, key))
916       xbt_cfg_set_parse(_sg_cfg_set, cfg);
917     else
918       XBT_INFO("The custom configuration '%s' is already defined by user!",key);
919     free(cfg);
920   }
921   XBT_DEBUG("End configuration name = %s",A_surfxml_config_id);
922
923   xbt_dict_free(&current_property_set);
924   current_property_set = NULL;
925 }
926
927 static int argc;
928 static char **argv;
929
930 void STag_surfxml_process(void){
931   AS_TAG  = 0;
932   argc    = 1;
933   argv    = xbt_new(char *, 1);
934   argv[0] = xbt_strdup(A_surfxml_process_function);
935   xbt_assert(current_property_set == NULL, "Someone forgot to reset the property set to NULL in its closing tag (or XML malformed)");
936 }
937
938 void ETag_surfxml_process(void){
939   s_sg_platf_process_cbarg_t process = SG_PLATF_PROCESS_INITIALIZER;
940   memset(&process,0,sizeof(process));
941
942   process.argc       = argc;
943   process.argv       = (const char **)argv;
944   process.properties = current_property_set;
945   process.host       = A_surfxml_process_host;
946   process.function   = A_surfxml_process_function;
947   process.start_time = surf_parse_get_double(A_surfxml_process_start___time);
948   process.kill_time  = surf_parse_get_double(A_surfxml_process_kill___time);
949
950   switch (A_surfxml_process_on___failure) {
951   case AU_surfxml_process_on___failure:
952   case A_surfxml_process_on___failure_DIE:
953     process.on_failure =  SURF_PROCESS_ON_FAILURE_DIE;
954     break;
955   case A_surfxml_process_on___failure_RESTART:
956     process.on_failure =  SURF_PROCESS_ON_FAILURE_RESTART;
957     break;
958   }
959
960   sg_platf_new_process(&process);
961   current_property_set = NULL;
962 }
963
964 void STag_surfxml_argument(void){
965   argc++;
966   argv = xbt_realloc(argv, (argc) * sizeof(char *));
967   argv[(argc) - 1] = xbt_strdup(A_surfxml_argument_value);
968 }
969
970 void STag_surfxml_model___prop(void){
971   if (!current_model_property_set)
972     current_model_property_set = xbt_dict_new_homogeneous(xbt_free_f);
973
974   xbt_dict_set(current_model_property_set, A_surfxml_model___prop_id, xbt_strdup(A_surfxml_model___prop_value), NULL);
975 }
976
977 /* ***************************************** */
978 /* TUTORIAL: New TAG                         */
979 void STag_surfxml_gpu(void)
980 {
981   XBT_DEBUG("STag_surfxml_gpu");
982 }
983 void ETag_surfxml_gpu(void)
984 {
985   s_sg_platf_gpu_cbarg_t gpu = SG_PLATF_GPU_INITIALIZER;
986   memset(&gpu,0,sizeof(gpu));
987
988   gpu.name = A_surfxml_gpu_name;
989
990   sg_platf_new_gpu(&gpu);
991 }
992 /* ***************************************** */
993
994 /* nothing to do in those functions */
995 void ETag_surfxml_prop(void){}
996 void STag_surfxml_random(void){}
997 void ETag_surfxml_random(void){}
998 void ETag_surfxml_trace___connect(void){}
999 void STag_surfxml_trace(void){parse_after_config();}
1000 void ETag_surfxml_router(void){}
1001 void ETag_surfxml_host___link(void){}
1002 void ETag_surfxml_cabinet(void){}
1003 void ETag_surfxml_peer(void){}
1004 void STag_surfxml_backbone(void){}
1005 void ETag_surfxml_link___ctn(void){}
1006 void ETag_surfxml_argument(void){}
1007 void ETag_surfxml_model___prop(void){}
1008
1009 /* Open and Close parse file */
1010
1011 void surf_parse_open(const char *file)
1012 {
1013   static int warned = 0;        /* warn only once */
1014   if (!file) {
1015     if (!warned) {
1016       XBT_WARN
1017           ("Bypassing the XML parser since surf_parse_open received a NULL pointer. "
1018               "If it is not what you want, go fix your code.");
1019       warned = 1;
1020     }
1021     return;
1022   }
1023
1024   if (!surf_input_buffer_stack)
1025     surf_input_buffer_stack = xbt_dynar_new(sizeof(YY_BUFFER_STATE), NULL);
1026   if (!surf_file_to_parse_stack)
1027     surf_file_to_parse_stack = xbt_dynar_new(sizeof(FILE *), NULL);
1028
1029   if (!surf_parsed_filename_stack)
1030     surf_parsed_filename_stack = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
1031
1032   surf_parsed_filename = xbt_strdup(file);
1033   char *dir = xbt_dirname(file);
1034   xbt_dynar_push(surf_path, &dir);
1035
1036   surf_file_to_parse = surf_fopen(file, "r");
1037   xbt_assert((surf_file_to_parse), "Unable to open \"%s\"\n", file);
1038   surf_input_buffer = surf_parse__create_buffer(surf_file_to_parse, YY_BUF_SIZE);
1039   surf_parse__switch_to_buffer(surf_input_buffer);
1040   surf_parse_lineno = 1;
1041 }
1042
1043 void surf_parse_close(void)
1044 {
1045   xbt_dynar_free(&surf_input_buffer_stack);
1046   xbt_dynar_free(&surf_file_to_parse_stack);
1047   xbt_dynar_free(&surf_parsed_filename_stack);
1048   if (surf_parsed_filename) {
1049     char *dir = NULL;
1050     xbt_dynar_pop(surf_path, dir);
1051     free(dir);
1052   }
1053
1054   free(surf_parsed_filename);
1055   surf_parsed_filename = NULL;
1056
1057   if (surf_file_to_parse) {
1058     surf_parse__delete_buffer(surf_input_buffer);
1059     fclose(surf_file_to_parse);
1060     surf_file_to_parse = NULL; //Must be reset for Bypass
1061   }
1062 }
1063
1064 /* Call the lexer to parse the currently opened file. This pointer to function enables bypassing of the parser */
1065
1066 static int _surf_parse(void) {
1067   return surf_parse_lex();
1068 }
1069 int_f_void_t surf_parse = _surf_parse;
1070
1071
1072 /* Prop tag functions */
1073
1074 /**
1075  * With XML parser
1076  */
1077
1078 double get_cpu_power(const char *power)
1079 {
1080   double power_scale = 0.0;
1081   const char *p, *q;
1082   char *generator;
1083   random_data_t random = NULL;
1084   /* randomness is inserted like this: power="$rand(my_random)" */
1085   if (((p = strstr(power, "$rand(")) != NULL)
1086       && ((q = strstr(power, ")")) != NULL)) {
1087     if (p < q) {
1088       generator = xbt_malloc(q - (p + 6) + 1);
1089       memcpy(generator, p + 6, q - (p + 6));
1090       generator[q - (p + 6)] = '\0';
1091       random = xbt_dict_get_or_null(random_data_list, generator);
1092       xbt_assert(random, "Random generator %s undefined", generator);
1093       power_scale = random_generate(random);
1094     }
1095   } else {
1096     power_scale = surf_parse_get_power(power);
1097   }
1098   return power_scale;
1099 }
1100
1101 double random_min, random_max, random_mean, random_std_deviation;
1102 e_random_generator_t random_generator;
1103 char *random_id;
1104
1105 xbt_dict_t get_as_router_properties(const char* name)
1106 {
1107   return xbt_lib_get_or_null(as_router_lib, name, ROUTING_PROP_ASR_LEVEL);
1108 }
1109