Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
f74756677803d8b27dbfaffe2cf85dfcfaa67683
[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 static std::vector<double> surf_parse_get_all_speeds(char* speeds, const char* entity_kind, const char* id){
274
275   std::vector<double> speed_per_pstate;
276
277   if (strchr(speeds, ',') == nullptr){
278     double speed = surf_parse_get_speed(speeds, entity_kind, id);
279     speed_per_pstate.push_back(speed);
280   } else {
281     xbt_dynar_t pstate_list = xbt_str_split(speeds, ",");
282     unsigned int i;
283     char* speed_str;
284     xbt_dynar_foreach(pstate_list, i, speed_str) {
285       xbt_str_trim(speed_str, nullptr);
286       double speed = surf_parse_get_speed(speed_str,entity_kind, id);
287       speed_per_pstate.push_back(speed);
288       XBT_DEBUG("Speed value: %f", speed);
289     }
290     xbt_dynar_free(&pstate_list);
291   }
292   return speed_per_pstate;
293 }
294
295 /*
296  * All the callback lists that can be overridden anywhere.
297  * (this list should probably be reduced to the bare minimum to allow the models to work)
298  */
299
300 /* make sure these symbols are defined as strong ones in this file so that the linker can resolve them */
301
302 /* The default current property receiver. Setup in the corresponding opening callbacks. */
303 xbt_dict_t current_property_set = nullptr;
304 xbt_dict_t current_model_property_set = nullptr;
305 int AS_TAG                            = 0; // Whether we just opened an AS tag (to see what to do with the properties)
306
307 /* dictionary of random generator data */
308 xbt_dict_t random_data_list = nullptr;
309
310 YY_BUFFER_STATE surf_input_buffer;
311 FILE *surf_file_to_parse = nullptr;
312
313 /*
314  * Stuff relative to storage
315  */
316 void STag_surfxml_storage()
317 {
318   AS_TAG = 0;
319   XBT_DEBUG("STag_surfxml_storage");
320   xbt_assert(current_property_set == nullptr, "Someone forgot to reset the property set to nullptr in its closing tag (or XML malformed)");
321 }
322 void ETag_surfxml_storage()
323 {
324   s_sg_platf_storage_cbarg_t storage;
325   memset(&storage,0,sizeof(storage));
326
327   storage.properties   = current_property_set;
328   current_property_set = nullptr;
329
330   storage.id           = A_surfxml_storage_id;
331   storage.type_id      = A_surfxml_storage_typeId;
332   storage.content      = A_surfxml_storage_content;
333   storage.content_type = A_surfxml_storage_content___type;
334   storage.attach       = A_surfxml_storage_attach;
335   sg_platf_new_storage(&storage);
336 }
337 void STag_surfxml_storage___type()
338 {
339   AS_TAG = 0;
340   XBT_DEBUG("STag_surfxml_storage___type");
341   xbt_assert(current_property_set == nullptr, "Someone forgot to reset the property set to nullptr in its closing tag (or XML malformed)");
342   xbt_assert(current_model_property_set == nullptr, "Someone forgot to reset the model property set to nullptr in its closing tag (or XML malformed)");
343 }
344 void ETag_surfxml_storage___type()
345 {
346   s_sg_platf_storage_type_cbarg_t storage_type;
347   memset(&storage_type,0,sizeof(storage_type));
348
349   storage_type.properties = current_property_set;
350   current_property_set    = nullptr;
351
352   storage_type.model_properties = current_model_property_set;
353   current_model_property_set    = nullptr;
354
355   storage_type.content          = A_surfxml_storage___type_content;
356   storage_type.content_type     = A_surfxml_storage___type_content___type;
357   storage_type.id               = A_surfxml_storage___type_id;
358   storage_type.model            = A_surfxml_storage___type_model;
359   storage_type.size             = surf_parse_get_size(A_surfxml_storage___type_size,
360         "size of storage type", storage_type.id);
361   sg_platf_new_storage_type(&storage_type);
362 }
363 void STag_surfxml_mount()
364 {
365   XBT_DEBUG("STag_surfxml_mount");
366 }
367 void ETag_surfxml_mount()
368 {
369   s_sg_platf_mount_cbarg_t mount;
370   memset(&mount,0,sizeof(mount));
371
372   mount.name      = A_surfxml_mount_name;
373   mount.storageId = A_surfxml_mount_storageId;
374   sg_platf_new_mount(&mount);
375 }
376
377 /*
378  * Stuff relative to the <include> tag
379  */
380 static std::vector<YY_BUFFER_STATE> surf_input_buffer_stack;
381 static std::vector<FILE*> surf_file_to_parse_stack;
382 static std::vector<char*> surf_parsed_filename_stack;
383
384 void STag_surfxml_include()
385 {
386   parse_after_config();
387   XBT_DEBUG("STag_surfxml_include '%s'",A_surfxml_include_file);
388   surf_parsed_filename_stack.push_back(surf_parsed_filename); // save old file name
389   surf_parsed_filename = xbt_strdup(A_surfxml_include_file);
390
391   surf_file_to_parse_stack.push_back(surf_file_to_parse); // save old file descriptor
392
393   surf_file_to_parse = surf_fopen(A_surfxml_include_file, "r"); // read new file descriptor
394   xbt_assert((surf_file_to_parse), "Unable to open \"%s\"\n", A_surfxml_include_file);
395
396   surf_input_buffer_stack.push_back(surf_input_buffer);
397   surf_input_buffer = surf_parse__create_buffer(surf_file_to_parse, YY_BUF_SIZE);
398   surf_parse_push_buffer_state(surf_input_buffer);
399
400   fflush(nullptr);
401 }
402
403 void ETag_surfxml_include() {
404 /* Nothing to do when done with reading the include tag.
405  * Instead, the handling should be deferred until the EOF of current buffer -- see below */
406 }
407
408 /** @brief When reaching EOF, check whether we are in an include tag, and behave accordingly if yes
409  *
410  * This function is called automatically by sedding the parser in tools/cmake/MaintainerMode.cmake
411  * Every FAIL on "Premature EOF" is preceded by a call to this function, which role is to restore the
412  * previous buffer if we reached the EOF /of an include file/. Its return code is used to avoid the
413  * error message in that case.
414  *
415  * Yeah, that's terribly hackish, but it works. A better solution should be dealed with in flexml
416  * directly: a command line flag could instruct it to do the correct thing when the include directive is encountered
417  * on a line. One day maybe, if the maya allow it.
418  */
419 int ETag_surfxml_include_state()
420 {
421   fflush(nullptr);
422   XBT_DEBUG("ETag_surfxml_include_state '%s'",A_surfxml_include_file);
423
424   if (surf_input_buffer_stack.empty()) // nope, that's a true premature EOF. Let the parser die verbosely.
425     return 0;
426
427   // Yeah, we were in an <include> Restore state and proceed.
428   fclose(surf_file_to_parse);
429   surf_file_to_parse = surf_file_to_parse_stack.back();
430   surf_file_to_parse_stack.pop_back();
431   surf_parse_pop_buffer_state();
432   surf_input_buffer = surf_input_buffer_stack.back();
433   surf_input_buffer_stack.pop_back();
434
435   // Restore the filename for error messages
436   free(surf_parsed_filename);
437   surf_parsed_filename = surf_parsed_filename_stack.back();
438   surf_parsed_filename_stack.pop_back();
439
440   return 1;
441 }
442
443 /* Stag and Etag parse functions */
444
445 void STag_surfxml_platform() {
446   XBT_ATTRIB_UNUSED double version = surf_parse_get_double(A_surfxml_platform_version);
447
448   xbt_assert((version >= 1.0), "******* BIG FAT WARNING *********\n "
449       "You're using an ancient XML file.\n"
450       "Since SimGrid 3.1, units are Bytes, Flops, and seconds "
451       "instead of MBytes, MFlops and seconds.\n"
452
453       "Use simgrid_update_xml to update your file automatically. "
454       "This program is installed automatically with SimGrid, or "
455       "available in the tools/ directory of the source archive.\n"
456
457       "Please check also out the SURF section of the ChangeLog for "
458       "the 3.1 version for more information. \n"
459
460       "Last, do not forget to also update your values for "
461       "the calls to MSG_task_create (if any).");
462   xbt_assert((version >= 3.0), "******* BIG FAT WARNING *********\n "
463       "You're using an old XML file.\n"
464       "Use simgrid_update_xml to update your file automatically. "
465       "This program is installed automatically with SimGrid, or "
466       "available in the tools/ directory of the source archive.");
467   xbt_assert((version >= 4.0), "******* FILE %s IS TOO OLD (v:%.1f) *********\n "
468       "Changes introduced in SimGrid 3.13:\n"
469       "  - 'power' attribute of hosts (and others) got renamed to 'speed'.\n"
470       "  - In <trace_connect>, attribute kind=\"POWER\" is now kind=\"SPEED\".\n"
471       "  - DOCTYPE now point to the rignt URL: http://simgrid.gforge.inria.fr/simgrid/simgrid.dtd\n"
472       "  - speed, bandwidth and latency attributes now MUST have an explicit unit (f, Bps, s by default)"
473       "\n\n"
474       "Use simgrid_update_xml to update your file automatically. "
475       "This program is installed automatically with SimGrid, or "
476       "available in the tools/ directory of the source archive.",surf_parsed_filename, version);
477
478   sg_platf_begin();
479 }
480 void ETag_surfxml_platform(){
481   sg_platf_end();
482 }
483
484 void STag_surfxml_host(){
485   AS_TAG = 0;
486   xbt_assert(current_property_set == nullptr, "Someone forgot to reset the property set to nullptr in its closing tag (or XML malformed)");
487 }
488
489 void STag_surfxml_prop()
490 {
491   if (AS_TAG) { // We need to retrieve the most recently opened AS
492     XBT_DEBUG("Set AS property %s -> %s", A_surfxml_prop_id, A_surfxml_prop_value);
493     simgrid::s4u::NetZone* netzone = simgrid::s4u::Engine::instance()->netzoneByNameOrNull(A_surfxml_AS_id);
494
495     netzone->setProperty(A_surfxml_prop_id, xbt_strdup(A_surfxml_prop_value));
496   }
497   else{
498     if (!current_property_set)
499       current_property_set = xbt_dict_new_homogeneous(&xbt_free_f); // Maybe, it should raise an error
500     xbt_dict_set(current_property_set, A_surfxml_prop_id, xbt_strdup(A_surfxml_prop_value), nullptr);
501     XBT_DEBUG("add prop %s=%s into current property set %p", A_surfxml_prop_id, A_surfxml_prop_value,
502               current_property_set);
503   }
504 }
505
506 void ETag_surfxml_host()    {
507   s_sg_platf_host_cbarg_t host;
508   memset(&host,0,sizeof(host));
509
510   host.properties = current_property_set;
511   current_property_set = nullptr;
512
513   host.id = A_surfxml_host_id;
514
515   host.speed_per_pstate = surf_parse_get_all_speeds(A_surfxml_host_speed, "speed of host", host.id);
516
517   XBT_DEBUG("pstate: %s", A_surfxml_host_pstate);
518   host.core_amount = surf_parse_get_int(A_surfxml_host_core);
519   host.speed_trace = A_surfxml_host_availability___file[0] ? tmgr_trace_new_from_file(A_surfxml_host_availability___file) : nullptr;
520   host.state_trace = A_surfxml_host_state___file[0] ? tmgr_trace_new_from_file(A_surfxml_host_state___file) : nullptr;
521   host.pstate      = surf_parse_get_int(A_surfxml_host_pstate);
522   host.coord       = A_surfxml_host_coordinates;
523
524   sg_platf_new_host(&host);
525 }
526
527 void STag_surfxml_host___link(){
528   XBT_DEBUG("Create a Host_link for %s",A_surfxml_host___link_id);
529   s_sg_platf_host_link_cbarg_t host_link;
530   memset(&host_link,0,sizeof(host_link));
531
532   host_link.id        = A_surfxml_host___link_id;
533   host_link.link_up   = A_surfxml_host___link_up;
534   host_link.link_down = A_surfxml_host___link_down;
535   sg_platf_new_hostlink(&host_link);
536 }
537
538 void STag_surfxml_router(){
539   sg_platf_new_router(A_surfxml_router_id, A_surfxml_router_coordinates);
540 }
541
542 void ETag_surfxml_cluster(){
543   s_sg_platf_cluster_cbarg_t cluster;
544   memset(&cluster,0,sizeof(cluster));
545   cluster.properties = current_property_set;
546   current_property_set = nullptr;
547
548   cluster.id          = A_surfxml_cluster_id;
549   cluster.prefix      = A_surfxml_cluster_prefix;
550   cluster.suffix      = A_surfxml_cluster_suffix;
551   cluster.radicals    = explodesRadical(A_surfxml_cluster_radical);
552   cluster.speeds      = surf_parse_get_all_speeds(A_surfxml_cluster_speed, "speed of cluster", cluster.id);
553   cluster.core_amount = surf_parse_get_int(A_surfxml_cluster_core);
554   cluster.bw          = surf_parse_get_bandwidth(A_surfxml_cluster_bw, "bw of cluster", cluster.id);
555   cluster.lat         = surf_parse_get_time(A_surfxml_cluster_lat, "lat of cluster", cluster.id);
556   if(strcmp(A_surfxml_cluster_bb___bw,""))
557     cluster.bb_bw = surf_parse_get_bandwidth(A_surfxml_cluster_bb___bw, "bb_bw of cluster", cluster.id);
558   if(strcmp(A_surfxml_cluster_bb___lat,""))
559     cluster.bb_lat = surf_parse_get_time(A_surfxml_cluster_bb___lat, "bb_lat of cluster", cluster.id);
560   if(strcmp(A_surfxml_cluster_limiter___link,""))
561     cluster.limiter_link = surf_parse_get_bandwidth(A_surfxml_cluster_limiter___link, "limiter_link of cluster", cluster.id);
562   if(strcmp(A_surfxml_cluster_loopback___bw,""))
563     cluster.loopback_bw = surf_parse_get_bandwidth(A_surfxml_cluster_loopback___bw, "loopback_bw of cluster", cluster.id);
564   if(strcmp(A_surfxml_cluster_loopback___lat,""))
565     cluster.loopback_lat = surf_parse_get_time(A_surfxml_cluster_loopback___lat, "loopback_lat of cluster", cluster.id);
566
567   switch(AX_surfxml_cluster_topology){
568   case A_surfxml_cluster_topology_FLAT:
569     cluster.topology= SURF_CLUSTER_FLAT ;
570     break;
571   case A_surfxml_cluster_topology_TORUS:
572     cluster.topology= SURF_CLUSTER_TORUS ;
573     break;
574   case A_surfxml_cluster_topology_FAT___TREE:
575     cluster.topology = SURF_CLUSTER_FAT_TREE;
576     break;
577   case A_surfxml_cluster_topology_DRAGONFLY:
578     cluster.topology= SURF_CLUSTER_DRAGONFLY ;
579     break;
580   default:
581     surf_parse_error("Invalid cluster topology for cluster %s",
582                      cluster.id);
583     break;
584   }
585   cluster.topo_parameters = A_surfxml_cluster_topo___parameters;
586   cluster.router_id = A_surfxml_cluster_router___id;
587
588   switch (AX_surfxml_cluster_sharing___policy) {
589   case A_surfxml_cluster_sharing___policy_SHARED:
590     cluster.sharing_policy = SURF_LINK_SHARED;
591     break;
592   case A_surfxml_cluster_sharing___policy_FULLDUPLEX:
593     cluster.sharing_policy = SURF_LINK_FULLDUPLEX;
594     break;
595   case A_surfxml_cluster_sharing___policy_FATPIPE:
596     cluster.sharing_policy = SURF_LINK_FATPIPE;
597     break;
598   default:
599     surf_parse_error("Invalid cluster sharing policy for cluster %s", cluster.id);
600     break;
601   }
602   switch (AX_surfxml_cluster_bb___sharing___policy) {
603   case A_surfxml_cluster_bb___sharing___policy_FATPIPE:
604     cluster.bb_sharing_policy = SURF_LINK_FATPIPE;
605     break;
606   case A_surfxml_cluster_bb___sharing___policy_SHARED:
607     cluster.bb_sharing_policy = SURF_LINK_SHARED;
608     break;
609   default:
610     surf_parse_error("Invalid bb sharing policy in cluster %s", cluster.id);
611     break;
612   }
613
614   sg_platf_new_cluster(&cluster);
615 }
616
617 void STag_surfxml_cluster(){
618   AS_TAG = 0;
619   parse_after_config();
620   xbt_assert(current_property_set == nullptr, "Someone forgot to reset the property set to nullptr in its closing tag (or XML malformed)");
621 }
622
623 void STag_surfxml_cabinet(){
624   parse_after_config();
625   s_sg_platf_cabinet_cbarg_t cabinet;
626   memset(&cabinet,0,sizeof(cabinet));
627   cabinet.id      = A_surfxml_cabinet_id;
628   cabinet.prefix  = A_surfxml_cabinet_prefix;
629   cabinet.suffix  = A_surfxml_cabinet_suffix;
630   cabinet.speed   = surf_parse_get_speed(A_surfxml_cabinet_speed, "speed of cabinet", cabinet.id);
631   cabinet.bw      = surf_parse_get_bandwidth(A_surfxml_cabinet_bw, "bw of cabinet", cabinet.id);
632   cabinet.lat     = surf_parse_get_time(A_surfxml_cabinet_lat, "lat of cabinet", cabinet.id);
633   cabinet.radicals = explodesRadical(A_surfxml_cabinet_radical);
634
635   sg_platf_new_cabinet(&cabinet);
636 }
637
638 void STag_surfxml_peer(){
639   parse_after_config();
640   s_sg_platf_peer_cbarg_t peer;
641   memset(&peer,0,sizeof(peer));
642   peer.id          = A_surfxml_peer_id;
643   peer.speed       = surf_parse_get_speed(A_surfxml_peer_speed, "speed of peer", peer.id);
644   peer.bw_in       = surf_parse_get_bandwidth(A_surfxml_peer_bw___in, "bw_in of peer", peer.id);
645   peer.bw_out      = surf_parse_get_bandwidth(A_surfxml_peer_bw___out, "bw_out of peer", peer.id);
646   peer.coord       = A_surfxml_peer_coordinates;
647   peer.speed_trace = A_surfxml_peer_availability___file[0] ? tmgr_trace_new_from_file(A_surfxml_peer_availability___file) : nullptr;
648   peer.state_trace = A_surfxml_peer_state___file[0] ? tmgr_trace_new_from_file(A_surfxml_peer_state___file) : nullptr;
649
650   if (A_surfxml_peer_lat[0] != '\0')
651     XBT_WARN("The latency parameter in <peer> is now deprecated. Use the z coordinate instead of '%s'.",
652              A_surfxml_peer_lat);
653
654   sg_platf_new_peer(&peer);
655 }
656
657 void STag_surfxml_link(){
658   AS_TAG = 0;
659   xbt_assert(current_property_set == nullptr, "Someone forgot to reset the property set to nullptr in its closing tag (or XML malformed)");
660 }
661
662 void ETag_surfxml_link(){
663   LinkCreationArgs link;
664
665   link.properties          = current_property_set;
666   current_property_set     = nullptr;
667
668   link.id                  = std::string(A_surfxml_link_id);
669   link.bandwidth           = surf_parse_get_bandwidth(A_surfxml_link_bandwidth, "bandwidth of link", link.id.c_str());
670   link.bandwidth_trace     = A_surfxml_link_bandwidth___file[0] ? tmgr_trace_new_from_file(A_surfxml_link_bandwidth___file) : nullptr;
671   link.latency             = surf_parse_get_time(A_surfxml_link_latency, "latency of link", link.id.c_str());
672   link.latency_trace       = A_surfxml_link_latency___file[0] ? tmgr_trace_new_from_file(A_surfxml_link_latency___file) : nullptr;
673   link.state_trace         = A_surfxml_link_state___file[0] ? tmgr_trace_new_from_file(A_surfxml_link_state___file):nullptr;
674
675   switch (A_surfxml_link_sharing___policy) {
676   case A_surfxml_link_sharing___policy_SHARED:
677     link.policy = SURF_LINK_SHARED;
678     break;
679   case A_surfxml_link_sharing___policy_FATPIPE:
680      link.policy = SURF_LINK_FATPIPE;
681      break;
682   case A_surfxml_link_sharing___policy_FULLDUPLEX:
683      link.policy = SURF_LINK_FULLDUPLEX;
684      break;
685   default:
686     surf_parse_error("Invalid sharing policy in link %s", link.id.c_str());
687     break;
688   }
689
690   sg_platf_new_link(&link);
691 }
692
693 void STag_surfxml_link___ctn(){
694
695   simgrid::surf::LinkImpl* link;
696   char *link_name=nullptr;
697   switch (A_surfxml_link___ctn_direction) {
698   case AU_surfxml_link___ctn_direction:
699   case A_surfxml_link___ctn_direction_NONE:
700     link = simgrid::surf::LinkImpl::byName(A_surfxml_link___ctn_id);
701     break;
702   case A_surfxml_link___ctn_direction_UP:
703     link_name = bprintf("%s_UP", A_surfxml_link___ctn_id);
704     link      = simgrid::surf::LinkImpl::byName(link_name);
705     break;
706   case A_surfxml_link___ctn_direction_DOWN:
707     link_name = bprintf("%s_DOWN", A_surfxml_link___ctn_id);
708     link      = simgrid::surf::LinkImpl::byName(link_name);
709     break;
710   }
711   xbt_free(link_name); // no-op if it's already nullptr
712
713   surf_parse_assert(link!=nullptr,"No such link: '%s'%s", A_surfxml_link___ctn_id,
714       A_surfxml_link___ctn_direction==A_surfxml_link___ctn_direction_UP?" (upward)":
715           ( A_surfxml_link___ctn_direction==A_surfxml_link___ctn_direction_DOWN?" (downward)":
716               ""));
717   parsed_link_list.push_back(link);
718 }
719
720 void ETag_surfxml_backbone(){
721   LinkCreationArgs link;
722
723   link.properties = nullptr;
724   link.id = std::string(A_surfxml_backbone_id);
725   link.bandwidth = surf_parse_get_bandwidth(A_surfxml_backbone_bandwidth, "bandwidth of backbone", link.id.c_str());
726   link.latency = surf_parse_get_time(A_surfxml_backbone_latency, "latency of backbone", link.id.c_str());
727   link.policy = SURF_LINK_SHARED;
728
729   sg_platf_new_link(&link);
730   routing_cluster_add_backbone(simgrid::surf::LinkImpl::byName(A_surfxml_backbone_id));
731 }
732
733 void STag_surfxml_route(){
734   surf_parse_assert(sg_netpoint_by_name_or_null(A_surfxml_route_src), "Route src='%s' does name a node.",
735                     A_surfxml_route_src);
736   surf_parse_assert(sg_netpoint_by_name_or_null(A_surfxml_route_dst), "Route dst='%s' does name a node.",
737                     A_surfxml_route_dst);
738 }
739
740 void STag_surfxml_ASroute(){
741   surf_parse_assert(sg_netpoint_by_name_or_null(A_surfxml_ASroute_src), "ASroute src='%s' does name a node.",
742                     A_surfxml_route_src);
743   surf_parse_assert(sg_netpoint_by_name_or_null(A_surfxml_ASroute_dst), "ASroute dst='%s' does name a node.",
744                     A_surfxml_route_dst);
745
746   surf_parse_assert(sg_netpoint_by_name_or_null(A_surfxml_ASroute_gw___src), "ASroute gw_src='%s' does name a node.",
747                     A_surfxml_ASroute_gw___src);
748   surf_parse_assert(sg_netpoint_by_name_or_null(A_surfxml_ASroute_gw___dst), "ASroute gw_dst='%s' does name a node.",
749                     A_surfxml_ASroute_gw___dst);
750 }
751
752 void STag_surfxml_bypassRoute(){
753   surf_parse_assert(sg_netpoint_by_name_or_null(A_surfxml_bypassRoute_src), "bypassRoute src='%s' does name a node.",
754                     A_surfxml_bypassRoute_src);
755   surf_parse_assert(sg_netpoint_by_name_or_null(A_surfxml_bypassRoute_dst), "bypassRoute dst='%s' does name a node.",
756                     A_surfxml_bypassRoute_dst);
757 }
758
759 void STag_surfxml_bypassASroute(){
760   surf_parse_assert(sg_netpoint_by_name_or_null(A_surfxml_bypassASroute_src),
761                     "bypassASroute src='%s' does name a node.", A_surfxml_bypassASroute_src);
762   surf_parse_assert(sg_netpoint_by_name_or_null(A_surfxml_bypassASroute_dst),
763                     "bypassASroute dst='%s' does name a node.", A_surfxml_bypassASroute_dst);
764   surf_parse_assert(sg_netpoint_by_name_or_null(A_surfxml_bypassASroute_gw___src),
765                     "bypassASroute gw_src='%s' does name a node.", A_surfxml_bypassASroute_gw___src);
766   surf_parse_assert(sg_netpoint_by_name_or_null(A_surfxml_bypassASroute_gw___dst),
767                     "bypassASroute gw_dst='%s' does name a node.", A_surfxml_bypassASroute_gw___dst);
768 }
769
770 void ETag_surfxml_route(){
771   s_sg_platf_route_cbarg_t route;
772   memset(&route,0,sizeof(route));
773
774   route.src         = sg_netpoint_by_name_or_null(A_surfxml_route_src); // tested to not be nullptr in start tag
775   route.dst         = sg_netpoint_by_name_or_null(A_surfxml_route_dst); // tested to not be nullptr in start tag
776   route.gw_src    = nullptr;
777   route.gw_dst    = nullptr;
778   route.link_list   = new std::vector<simgrid::surf::LinkImpl*>();
779   route.symmetrical = (A_surfxml_route_symmetrical == A_surfxml_route_symmetrical_YES);
780
781   for (auto link: parsed_link_list)
782     route.link_list->push_back(link);
783   parsed_link_list.clear();
784
785   sg_platf_new_route(&route);
786   delete route.link_list;
787 }
788
789 void ETag_surfxml_ASroute(){
790   s_sg_platf_route_cbarg_t ASroute;
791   memset(&ASroute,0,sizeof(ASroute));
792
793   ASroute.src = sg_netpoint_by_name_or_null(A_surfxml_ASroute_src); // tested to not be nullptr in start tag
794   ASroute.dst = sg_netpoint_by_name_or_null(A_surfxml_ASroute_dst); // tested to not be nullptr in start tag
795
796   ASroute.gw_src = sg_netpoint_by_name_or_null(A_surfxml_ASroute_gw___src); // tested to not be nullptr in start tag
797   ASroute.gw_dst = sg_netpoint_by_name_or_null(A_surfxml_ASroute_gw___dst); // tested to not be nullptr in start tag
798
799   ASroute.link_list = new std::vector<simgrid::surf::LinkImpl*>();
800
801   for (auto link: parsed_link_list)
802     ASroute.link_list->push_back(link);
803   parsed_link_list.clear();
804
805   switch (A_surfxml_ASroute_symmetrical) {
806   case AU_surfxml_ASroute_symmetrical:
807   case A_surfxml_ASroute_symmetrical_YES:
808     ASroute.symmetrical = true;
809     break;
810   case A_surfxml_ASroute_symmetrical_NO:
811     ASroute.symmetrical = false;
812     break;
813   }
814
815   sg_platf_new_route(&ASroute);
816   delete ASroute.link_list;
817 }
818
819 void ETag_surfxml_bypassRoute(){
820   s_sg_platf_route_cbarg_t route;
821   memset(&route,0,sizeof(route));
822
823   route.src         = sg_netpoint_by_name_or_null(A_surfxml_bypassRoute_src); // tested to not be nullptr in start tag
824   route.dst         = sg_netpoint_by_name_or_null(A_surfxml_bypassRoute_dst); // tested to not be nullptr in start tag
825   route.gw_src = nullptr;
826   route.gw_dst = nullptr;
827   route.symmetrical = false;
828   route.link_list   = new std::vector<simgrid::surf::LinkImpl*>();
829
830   for (auto link: parsed_link_list)
831     route.link_list->push_back(link);
832   parsed_link_list.clear();
833
834   sg_platf_new_bypassRoute(&route);
835   delete route.link_list;
836 }
837
838 void ETag_surfxml_bypassASroute(){
839   s_sg_platf_route_cbarg_t ASroute;
840   memset(&ASroute,0,sizeof(ASroute));
841
842   ASroute.src         = sg_netpoint_by_name_or_null(A_surfxml_bypassASroute_src);
843   ASroute.dst         = sg_netpoint_by_name_or_null(A_surfxml_bypassASroute_dst);
844   ASroute.link_list   = new std::vector<simgrid::surf::LinkImpl*>();
845   for (auto link: parsed_link_list)
846     ASroute.link_list->push_back(link);
847   parsed_link_list.clear();
848
849   ASroute.symmetrical = false;
850
851   ASroute.gw_src = sg_netpoint_by_name_or_null(A_surfxml_bypassASroute_gw___src);
852   ASroute.gw_dst = sg_netpoint_by_name_or_null(A_surfxml_bypassASroute_gw___dst);
853
854   sg_platf_new_bypassRoute(&ASroute);
855   delete ASroute.link_list;
856 }
857
858 void ETag_surfxml_trace(){
859   s_sg_platf_trace_cbarg_t trace;
860   memset(&trace,0,sizeof(trace));
861
862   trace.id = A_surfxml_trace_id;
863   trace.file = A_surfxml_trace_file;
864   trace.periodicity = surf_parse_get_double(A_surfxml_trace_periodicity);
865   trace.pc_data = surfxml_pcdata;
866
867   sg_platf_new_trace(&trace);
868 }
869
870 void STag_surfxml_trace___connect(){
871   parse_after_config();
872   s_sg_platf_trace_connect_cbarg_t trace_connect;
873   memset(&trace_connect,0,sizeof(trace_connect));
874
875   trace_connect.element = A_surfxml_trace___connect_element;
876   trace_connect.trace = A_surfxml_trace___connect_trace;
877
878   switch (A_surfxml_trace___connect_kind) {
879   case AU_surfxml_trace___connect_kind:
880   case A_surfxml_trace___connect_kind_SPEED:
881     trace_connect.kind =  SURF_TRACE_CONNECT_KIND_SPEED;
882     break;
883   case A_surfxml_trace___connect_kind_BANDWIDTH:
884     trace_connect.kind =  SURF_TRACE_CONNECT_KIND_BANDWIDTH;
885     break;
886   case A_surfxml_trace___connect_kind_HOST___AVAIL:
887     trace_connect.kind =  SURF_TRACE_CONNECT_KIND_HOST_AVAIL;
888     break;
889   case A_surfxml_trace___connect_kind_LATENCY:
890     trace_connect.kind =  SURF_TRACE_CONNECT_KIND_LATENCY;
891     break;
892   case A_surfxml_trace___connect_kind_LINK___AVAIL:
893     trace_connect.kind =  SURF_TRACE_CONNECT_KIND_LINK_AVAIL;
894     break;
895   }
896   sg_platf_trace_connect(&trace_connect);
897 }
898
899 void STag_surfxml_AS(){
900   parse_after_config();
901   AS_TAG                   = 1;
902   s_sg_platf_AS_cbarg_t AS = { A_surfxml_AS_id, (int)A_surfxml_AS_routing};
903
904   sg_platf_new_AS_begin(&AS);
905 }
906 void ETag_surfxml_AS(){
907   sg_platf_new_AS_seal();
908 }
909
910 void STag_surfxml_config(){
911   AS_TAG = 0;
912   xbt_assert(current_property_set == nullptr, "Someone forgot to reset the property set to nullptr in its closing tag (or XML malformed)");
913   XBT_DEBUG("START configuration name = %s",A_surfxml_config_id);
914   if (_sg_cfg_init_status == 2) {
915     surf_parse_error("All <config> tags must be given before any platform elements (such as <AS>, <host>, <cluster>, <link>, etc).");
916   }
917 }
918 void ETag_surfxml_config(){
919   xbt_dict_cursor_t cursor = nullptr;
920   char *key;
921   char *elem;
922   xbt_dict_foreach(current_property_set, cursor, key, elem) {
923     if (xbt_cfg_is_default_value(key)) {
924       char* cfg = bprintf("%s:%s", key, elem);
925       xbt_cfg_set_parse(cfg);
926       free(cfg);
927     } else
928       XBT_INFO("The custom configuration '%s' is already defined by user!",key);
929   }
930   XBT_DEBUG("End configuration name = %s",A_surfxml_config_id);
931
932   xbt_dict_free(&current_property_set);
933   current_property_set = nullptr;
934 }
935
936 static int argc;
937 static char **argv;
938
939 void STag_surfxml_process(){
940   AS_TAG  = 0;
941   argc    = 1;
942   argv    = xbt_new(char *, 1);
943   argv[0] = xbt_strdup(A_surfxml_process_function);
944   xbt_assert(current_property_set == nullptr, "Someone forgot to reset the property set to nullptr in its closing tag (or XML malformed)");
945 }
946
947 void ETag_surfxml_process(){
948   s_sg_platf_process_cbarg_t process;
949   memset(&process,0,sizeof(process));
950
951   process.argc       = argc;
952   process.argv       = (const char **)argv;
953   process.properties = current_property_set;
954   process.host       = A_surfxml_process_host;
955   process.function   = A_surfxml_process_function;
956   process.start_time = surf_parse_get_double(A_surfxml_process_start___time);
957   process.kill_time  = surf_parse_get_double(A_surfxml_process_kill___time);
958
959   switch (A_surfxml_process_on___failure) {
960   case AU_surfxml_process_on___failure:
961   case A_surfxml_process_on___failure_DIE:
962     process.on_failure =  SURF_PROCESS_ON_FAILURE_DIE;
963     break;
964   case A_surfxml_process_on___failure_RESTART:
965     process.on_failure =  SURF_PROCESS_ON_FAILURE_RESTART;
966     break;
967   }
968
969   sg_platf_new_process(&process);
970
971   for (int i = 0; i != argc; ++i)
972     xbt_free(argv[i]);
973   xbt_free(argv);
974   argv = nullptr;
975
976   current_property_set = nullptr;
977 }
978
979 void STag_surfxml_argument(){
980   argc++;
981   argv = (char**)xbt_realloc(argv, (argc) * sizeof(char **));
982   argv[(argc) - 1] = xbt_strdup(A_surfxml_argument_value);
983 }
984
985 void STag_surfxml_model___prop(){
986   if (!current_model_property_set)
987     current_model_property_set = xbt_dict_new_homogeneous(xbt_free_f);
988
989   xbt_dict_set(current_model_property_set, A_surfxml_model___prop_id, xbt_strdup(A_surfxml_model___prop_value), nullptr);
990 }
991
992 void ETag_surfxml_prop(){/* Nothing to do */}
993 void STag_surfxml_random(){/* Nothing to do */}
994 void ETag_surfxml_random(){/* Nothing to do */}
995 void ETag_surfxml_trace___connect(){/* Nothing to do */}
996 void STag_surfxml_trace(){parse_after_config();}
997 void ETag_surfxml_router(){/*Nothing to do*/}
998 void ETag_surfxml_host___link(){/* Nothing to do */}
999 void ETag_surfxml_cabinet(){/* Nothing to do */}
1000 void ETag_surfxml_peer(){/* Nothing to do */}
1001 void STag_surfxml_backbone(){/* Nothing to do */}
1002 void ETag_surfxml_link___ctn(){/* Nothing to do */}
1003 void ETag_surfxml_argument(){/* Nothing to do */}
1004 void ETag_surfxml_model___prop(){/* Nothing to do */}
1005
1006 /* Open and Close parse file */
1007 void surf_parse_open(const char *file)
1008 {
1009   xbt_assert(file, "Cannot parse the nullptr file. Bypassing the parser is strongly deprecated nowadays.");
1010
1011   surf_parsed_filename = xbt_strdup(file);
1012   char *dir = xbt_dirname(file);
1013   xbt_dynar_push(surf_path, &dir);
1014
1015   surf_file_to_parse = surf_fopen(file, "r");
1016   xbt_assert((surf_file_to_parse), "Unable to open \"%s\"\n", file);
1017   surf_input_buffer = surf_parse__create_buffer(surf_file_to_parse, YY_BUF_SIZE);
1018   surf_parse__switch_to_buffer(surf_input_buffer);
1019   surf_parse_lineno = 1;
1020 }
1021
1022 void surf_parse_close()
1023 {
1024   if (surf_parsed_filename) {
1025     char *dir = nullptr;
1026     xbt_dynar_pop(surf_path, &dir);
1027     free(dir);
1028   }
1029
1030   free(surf_parsed_filename);
1031   surf_parsed_filename = nullptr;
1032
1033   if (surf_file_to_parse) {
1034     surf_parse__delete_buffer(surf_input_buffer);
1035     fclose(surf_file_to_parse);
1036     surf_file_to_parse = nullptr; //Must be reset for Bypass
1037   }
1038 }
1039
1040 /* Call the lexer to parse the currently opened file. This pointer to function enables bypassing of the parser */
1041 static int _surf_parse() {
1042   return surf_parse_lex();
1043 }
1044
1045 int_f_void_t surf_parse = _surf_parse;
1046
1047 SG_END_DECL()