Logo AND Algorithmique Numérique Distribuée

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