Logo AND Algorithmique Numérique Distribuée

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