Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of git+ssh://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 "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 xbt_dynar_t surf_input_buffer_stack    = nullptr;
381 static xbt_dynar_t surf_file_to_parse_stack   = nullptr;
382 static xbt_dynar_t surf_parsed_filename_stack = nullptr;
383
384 void STag_surfxml_include()
385 {
386   parse_after_config();
387   XBT_DEBUG("STag_surfxml_include '%s'",A_surfxml_include_file);
388   xbt_dynar_push(surf_parsed_filename_stack,&surf_parsed_filename); // save old file name
389   surf_parsed_filename = xbt_strdup(A_surfxml_include_file);
390
391   xbt_dynar_push(surf_file_to_parse_stack, &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   xbt_dynar_push(surf_input_buffer_stack,&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(xbt_dynar_is_empty(surf_input_buffer_stack)) // 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   xbt_dynar_pop(surf_file_to_parse_stack, &surf_file_to_parse);
430   surf_parse_pop_buffer_state();
431   xbt_dynar_pop(surf_input_buffer_stack,&surf_input_buffer);
432
433   // Restore the filename for error messages
434   free(surf_parsed_filename);
435   xbt_dynar_pop(surf_parsed_filename_stack,&surf_parsed_filename);
436
437   return 1;
438 }
439
440 /* Stag and Etag parse functions */
441
442 void STag_surfxml_platform() {
443   XBT_ATTRIB_UNUSED double version = surf_parse_get_double(A_surfxml_platform_version);
444
445   xbt_assert((version >= 1.0), "******* BIG FAT WARNING *********\n "
446       "You're using an ancient XML file.\n"
447       "Since SimGrid 3.1, units are Bytes, Flops, and seconds "
448       "instead of MBytes, MFlops and seconds.\n"
449
450       "Use simgrid_update_xml to update your file automatically. "
451       "This program is installed automatically with SimGrid, or "
452       "available in the tools/ directory of the source archive.\n"
453
454       "Please check also out the SURF section of the ChangeLog for "
455       "the 3.1 version for more information. \n"
456
457       "Last, do not forget to also update your values for "
458       "the calls to MSG_task_create (if any).");
459   xbt_assert((version >= 3.0), "******* BIG FAT WARNING *********\n "
460       "You're using an old XML file.\n"
461       "Use simgrid_update_xml to update your file automatically. "
462       "This program is installed automatically with SimGrid, or "
463       "available in the tools/ directory of the source archive.");
464   xbt_assert((version >= 4.0), "******* FILE %s IS TOO OLD (v:%.1f) *********\n "
465       "Changes introduced in SimGrid 3.13:\n"
466       "  - 'power' attribute of hosts (and others) got renamed to 'speed'.\n"
467       "  - In <trace_connect>, attribute kind=\"POWER\" is now kind=\"SPEED\".\n"
468       "  - DOCTYPE now point to the rignt URL: http://simgrid.gforge.inria.fr/simgrid/simgrid.dtd\n"
469       "  - speed, bandwidth and latency attributes now MUST have an explicit unit (f, Bps, s by default)"
470       "\n\n"
471       "Use simgrid_update_xml to update your file automatically. "
472       "This program is installed automatically with SimGrid, or "
473       "available in the tools/ directory of the source archive.",surf_parsed_filename, version);
474
475   sg_platf_begin();
476 }
477 void ETag_surfxml_platform(){
478   sg_platf_end();
479 }
480
481 void STag_surfxml_host(){
482   AS_TAG = 0;
483   xbt_assert(current_property_set == nullptr, "Someone forgot to reset the property set to nullptr in its closing tag (or XML malformed)");
484 }
485
486 void STag_surfxml_prop()
487 {
488   if (AS_TAG) { // We need to retrieve the most recently opened AS
489     XBT_DEBUG("Set AS property %s -> %s", A_surfxml_prop_id, A_surfxml_prop_value);
490     simgrid::s4u::NetZone* netzone = simgrid::s4u::Engine::instance()->netzoneByNameOrNull(A_surfxml_AS_id);
491
492     netzone->setProperty(A_surfxml_prop_id, xbt_strdup(A_surfxml_prop_value));
493   }
494   else{
495     if (!current_property_set)
496       current_property_set = xbt_dict_new_homogeneous(&xbt_free_f); // Maybe, it should raise an error
497     xbt_dict_set(current_property_set, A_surfxml_prop_id, xbt_strdup(A_surfxml_prop_value), nullptr);
498     XBT_DEBUG("add prop %s=%s into current property set %p", A_surfxml_prop_id, A_surfxml_prop_value,
499               current_property_set);
500   }
501 }
502
503 void ETag_surfxml_host()    {
504   s_sg_platf_host_cbarg_t host;
505   memset(&host,0,sizeof(host));
506
507   host.properties = current_property_set;
508   current_property_set = nullptr;
509
510   host.id = A_surfxml_host_id;
511
512   host.speed_per_pstate = surf_parse_get_all_speeds(A_surfxml_host_speed, "speed of host", host.id);
513
514   XBT_DEBUG("pstate: %s", A_surfxml_host_pstate);
515   host.core_amount = surf_parse_get_int(A_surfxml_host_core);
516   host.speed_trace = A_surfxml_host_availability___file[0] ? tmgr_trace_new_from_file(A_surfxml_host_availability___file) : nullptr;
517   host.state_trace = A_surfxml_host_state___file[0] ? tmgr_trace_new_from_file(A_surfxml_host_state___file) : nullptr;
518   host.pstate      = surf_parse_get_int(A_surfxml_host_pstate);
519   host.coord       = A_surfxml_host_coordinates;
520
521   sg_platf_new_host(&host);
522 }
523
524 void STag_surfxml_host___link(){
525   XBT_DEBUG("Create a Host_link for %s",A_surfxml_host___link_id);
526   s_sg_platf_host_link_cbarg_t host_link;
527   memset(&host_link,0,sizeof(host_link));
528
529   host_link.id        = A_surfxml_host___link_id;
530   host_link.link_up   = A_surfxml_host___link_up;
531   host_link.link_down = A_surfxml_host___link_down;
532   sg_platf_new_hostlink(&host_link);
533 }
534
535 void STag_surfxml_router(){
536   sg_platf_new_router(A_surfxml_router_id, A_surfxml_router_coordinates);
537 }
538
539 void ETag_surfxml_cluster(){
540   s_sg_platf_cluster_cbarg_t cluster;
541   memset(&cluster,0,sizeof(cluster));
542   cluster.properties = current_property_set;
543   current_property_set = nullptr;
544
545   cluster.id          = A_surfxml_cluster_id;
546   cluster.prefix      = A_surfxml_cluster_prefix;
547   cluster.suffix      = A_surfxml_cluster_suffix;
548   cluster.radicals    = explodesRadical(A_surfxml_cluster_radical);
549   cluster.speeds      = surf_parse_get_all_speeds(A_surfxml_cluster_speed, "speed of cluster", cluster.id);
550   cluster.core_amount = surf_parse_get_int(A_surfxml_cluster_core);
551   cluster.bw          = surf_parse_get_bandwidth(A_surfxml_cluster_bw, "bw of cluster", cluster.id);
552   cluster.lat         = surf_parse_get_time(A_surfxml_cluster_lat, "lat of cluster", cluster.id);
553   if(strcmp(A_surfxml_cluster_bb___bw,""))
554     cluster.bb_bw = surf_parse_get_bandwidth(A_surfxml_cluster_bb___bw, "bb_bw of cluster", cluster.id);
555   if(strcmp(A_surfxml_cluster_bb___lat,""))
556     cluster.bb_lat = surf_parse_get_time(A_surfxml_cluster_bb___lat, "bb_lat of cluster", cluster.id);
557   if(strcmp(A_surfxml_cluster_limiter___link,""))
558     cluster.limiter_link = surf_parse_get_bandwidth(A_surfxml_cluster_limiter___link, "limiter_link of cluster", cluster.id);
559   if(strcmp(A_surfxml_cluster_loopback___bw,""))
560     cluster.loopback_bw = surf_parse_get_bandwidth(A_surfxml_cluster_loopback___bw, "loopback_bw of cluster", cluster.id);
561   if(strcmp(A_surfxml_cluster_loopback___lat,""))
562     cluster.loopback_lat = surf_parse_get_time(A_surfxml_cluster_loopback___lat, "loopback_lat of cluster", cluster.id);
563
564   switch(AX_surfxml_cluster_topology){
565   case A_surfxml_cluster_topology_FLAT:
566     cluster.topology= SURF_CLUSTER_FLAT ;
567     break;
568   case A_surfxml_cluster_topology_TORUS:
569     cluster.topology= SURF_CLUSTER_TORUS ;
570     break;
571   case A_surfxml_cluster_topology_FAT___TREE:
572     cluster.topology = SURF_CLUSTER_FAT_TREE;
573     break;
574   case A_surfxml_cluster_topology_DRAGONFLY:
575     cluster.topology= SURF_CLUSTER_DRAGONFLY ;
576     break;
577   default:
578     surf_parse_error("Invalid cluster topology for cluster %s",
579                      cluster.id);
580     break;
581   }
582   cluster.topo_parameters = A_surfxml_cluster_topo___parameters;
583   cluster.router_id = A_surfxml_cluster_router___id;
584
585   switch (AX_surfxml_cluster_sharing___policy) {
586   case A_surfxml_cluster_sharing___policy_SHARED:
587     cluster.sharing_policy = SURF_LINK_SHARED;
588     break;
589   case A_surfxml_cluster_sharing___policy_FULLDUPLEX:
590     cluster.sharing_policy = SURF_LINK_FULLDUPLEX;
591     break;
592   case A_surfxml_cluster_sharing___policy_FATPIPE:
593     cluster.sharing_policy = SURF_LINK_FATPIPE;
594     break;
595   default:
596     surf_parse_error("Invalid cluster sharing policy for cluster %s", cluster.id);
597     break;
598   }
599   switch (AX_surfxml_cluster_bb___sharing___policy) {
600   case A_surfxml_cluster_bb___sharing___policy_FATPIPE:
601     cluster.bb_sharing_policy = SURF_LINK_FATPIPE;
602     break;
603   case A_surfxml_cluster_bb___sharing___policy_SHARED:
604     cluster.bb_sharing_policy = SURF_LINK_SHARED;
605     break;
606   default:
607     surf_parse_error("Invalid bb sharing policy in cluster %s", cluster.id);
608     break;
609   }
610
611   sg_platf_new_cluster(&cluster);
612 }
613
614 void STag_surfxml_cluster(){
615   AS_TAG = 0;
616   parse_after_config();
617   xbt_assert(current_property_set == nullptr, "Someone forgot to reset the property set to nullptr in its closing tag (or XML malformed)");
618 }
619
620 void STag_surfxml_cabinet(){
621   parse_after_config();
622   s_sg_platf_cabinet_cbarg_t cabinet;
623   memset(&cabinet,0,sizeof(cabinet));
624   cabinet.id      = A_surfxml_cabinet_id;
625   cabinet.prefix  = A_surfxml_cabinet_prefix;
626   cabinet.suffix  = A_surfxml_cabinet_suffix;
627   cabinet.speed   = surf_parse_get_speed(A_surfxml_cabinet_speed, "speed of cabinet", cabinet.id);
628   cabinet.bw      = surf_parse_get_bandwidth(A_surfxml_cabinet_bw, "bw of cabinet", cabinet.id);
629   cabinet.lat     = surf_parse_get_time(A_surfxml_cabinet_lat, "lat of cabinet", cabinet.id);
630   cabinet.radicals = explodesRadical(A_surfxml_cabinet_radical);
631
632   sg_platf_new_cabinet(&cabinet);
633 }
634
635 void STag_surfxml_peer(){
636   parse_after_config();
637   s_sg_platf_peer_cbarg_t peer;
638   memset(&peer,0,sizeof(peer));
639   peer.id          = A_surfxml_peer_id;
640   peer.speed       = surf_parse_get_speed(A_surfxml_peer_speed, "speed of peer", peer.id);
641   peer.bw_in       = surf_parse_get_bandwidth(A_surfxml_peer_bw___in, "bw_in of peer", peer.id);
642   peer.bw_out      = surf_parse_get_bandwidth(A_surfxml_peer_bw___out, "bw_out of peer", peer.id);
643   peer.coord       = A_surfxml_peer_coordinates;
644   peer.speed_trace = A_surfxml_peer_availability___file[0] ? tmgr_trace_new_from_file(A_surfxml_peer_availability___file) : nullptr;
645   peer.state_trace = A_surfxml_peer_state___file[0] ? tmgr_trace_new_from_file(A_surfxml_peer_state___file) : nullptr;
646
647   if (A_surfxml_peer_lat[0] != '\0')
648     XBT_WARN("The latency parameter in <peer> is now deprecated. Use the z coordinate instead of '%s'.",
649              A_surfxml_peer_lat);
650
651   sg_platf_new_peer(&peer);
652 }
653
654 void STag_surfxml_link(){
655   AS_TAG = 0;
656   xbt_assert(current_property_set == nullptr, "Someone forgot to reset the property set to nullptr in its closing tag (or XML malformed)");
657 }
658
659 void ETag_surfxml_link(){
660   LinkCreationArgs link;
661
662   link.properties          = current_property_set;
663   current_property_set     = nullptr;
664
665   link.id                  = std::string(A_surfxml_link_id);
666   link.bandwidth           = surf_parse_get_bandwidth(A_surfxml_link_bandwidth, "bandwidth of link", link.id.c_str());
667   link.bandwidth_trace     = A_surfxml_link_bandwidth___file[0] ? tmgr_trace_new_from_file(A_surfxml_link_bandwidth___file) : nullptr;
668   link.latency             = surf_parse_get_time(A_surfxml_link_latency, "latency of link", link.id.c_str());
669   link.latency_trace       = A_surfxml_link_latency___file[0] ? tmgr_trace_new_from_file(A_surfxml_link_latency___file) : nullptr;
670   link.state_trace         = A_surfxml_link_state___file[0] ? tmgr_trace_new_from_file(A_surfxml_link_state___file):nullptr;
671
672   switch (A_surfxml_link_sharing___policy) {
673   case A_surfxml_link_sharing___policy_SHARED:
674     link.policy = SURF_LINK_SHARED;
675     break;
676   case A_surfxml_link_sharing___policy_FATPIPE:
677      link.policy = SURF_LINK_FATPIPE;
678      break;
679   case A_surfxml_link_sharing___policy_FULLDUPLEX:
680      link.policy = SURF_LINK_FULLDUPLEX;
681      break;
682   default:
683     surf_parse_error("Invalid sharing policy in link %s", link.id.c_str());
684     break;
685   }
686
687   sg_platf_new_link(&link);
688 }
689
690 void STag_surfxml_link___ctn(){
691
692   simgrid::surf::LinkImpl* link;
693   char *link_name=nullptr;
694   switch (A_surfxml_link___ctn_direction) {
695   case AU_surfxml_link___ctn_direction:
696   case A_surfxml_link___ctn_direction_NONE:
697     link = simgrid::surf::LinkImpl::byName(A_surfxml_link___ctn_id);
698     break;
699   case A_surfxml_link___ctn_direction_UP:
700     link_name = bprintf("%s_UP", A_surfxml_link___ctn_id);
701     link      = simgrid::surf::LinkImpl::byName(link_name);
702     break;
703   case A_surfxml_link___ctn_direction_DOWN:
704     link_name = bprintf("%s_DOWN", A_surfxml_link___ctn_id);
705     link      = simgrid::surf::LinkImpl::byName(link_name);
706     break;
707   }
708   xbt_free(link_name); // no-op if it's already nullptr
709
710   surf_parse_assert(link!=nullptr,"No such link: '%s'%s", A_surfxml_link___ctn_id,
711       A_surfxml_link___ctn_direction==A_surfxml_link___ctn_direction_UP?" (upward)":
712           ( A_surfxml_link___ctn_direction==A_surfxml_link___ctn_direction_DOWN?" (downward)":
713               ""));
714   parsed_link_list.push_back(link);
715 }
716
717 void ETag_surfxml_backbone(){
718   LinkCreationArgs link;
719
720   link.properties = nullptr;
721   link.id = std::string(A_surfxml_backbone_id);
722   link.bandwidth = surf_parse_get_bandwidth(A_surfxml_backbone_bandwidth, "bandwidth of backbone", link.id.c_str());
723   link.latency = surf_parse_get_time(A_surfxml_backbone_latency, "latency of backbone", link.id.c_str());
724   link.policy = SURF_LINK_SHARED;
725
726   sg_platf_new_link(&link);
727   routing_cluster_add_backbone(simgrid::surf::LinkImpl::byName(A_surfxml_backbone_id));
728 }
729
730 void STag_surfxml_route(){
731   surf_parse_assert(sg_netpoint_by_name_or_null(A_surfxml_route_src), "Route src='%s' does name a node.",
732                     A_surfxml_route_src);
733   surf_parse_assert(sg_netpoint_by_name_or_null(A_surfxml_route_dst), "Route dst='%s' does name a node.",
734                     A_surfxml_route_dst);
735 }
736
737 void STag_surfxml_ASroute(){
738   surf_parse_assert(sg_netpoint_by_name_or_null(A_surfxml_ASroute_src), "ASroute src='%s' does name a node.",
739                     A_surfxml_route_src);
740   surf_parse_assert(sg_netpoint_by_name_or_null(A_surfxml_ASroute_dst), "ASroute dst='%s' does name a node.",
741                     A_surfxml_route_dst);
742
743   surf_parse_assert(sg_netpoint_by_name_or_null(A_surfxml_ASroute_gw___src), "ASroute gw_src='%s' does name a node.",
744                     A_surfxml_ASroute_gw___src);
745   surf_parse_assert(sg_netpoint_by_name_or_null(A_surfxml_ASroute_gw___dst), "ASroute gw_dst='%s' does name a node.",
746                     A_surfxml_ASroute_gw___dst);
747 }
748
749 void STag_surfxml_bypassRoute(){
750   surf_parse_assert(sg_netpoint_by_name_or_null(A_surfxml_bypassRoute_src), "bypassRoute src='%s' does name a node.",
751                     A_surfxml_bypassRoute_src);
752   surf_parse_assert(sg_netpoint_by_name_or_null(A_surfxml_bypassRoute_dst), "bypassRoute dst='%s' does name a node.",
753                     A_surfxml_bypassRoute_dst);
754 }
755
756 void STag_surfxml_bypassASroute(){
757   surf_parse_assert(sg_netpoint_by_name_or_null(A_surfxml_bypassASroute_src),
758                     "bypassASroute src='%s' does name a node.", A_surfxml_bypassASroute_src);
759   surf_parse_assert(sg_netpoint_by_name_or_null(A_surfxml_bypassASroute_dst),
760                     "bypassASroute dst='%s' does name a node.", A_surfxml_bypassASroute_dst);
761   surf_parse_assert(sg_netpoint_by_name_or_null(A_surfxml_bypassASroute_gw___src),
762                     "bypassASroute gw_src='%s' does name a node.", A_surfxml_bypassASroute_gw___src);
763   surf_parse_assert(sg_netpoint_by_name_or_null(A_surfxml_bypassASroute_gw___dst),
764                     "bypassASroute gw_dst='%s' does name a node.", A_surfxml_bypassASroute_gw___dst);
765 }
766
767 void ETag_surfxml_route(){
768   s_sg_platf_route_cbarg_t route;
769   memset(&route,0,sizeof(route));
770
771   route.src         = sg_netpoint_by_name_or_null(A_surfxml_route_src); // tested to not be nullptr in start tag
772   route.dst         = sg_netpoint_by_name_or_null(A_surfxml_route_dst); // tested to not be nullptr in start tag
773   route.gw_src    = nullptr;
774   route.gw_dst    = nullptr;
775   route.link_list   = new std::vector<simgrid::surf::LinkImpl*>();
776   route.symmetrical = (A_surfxml_route_symmetrical == A_surfxml_route_symmetrical_YES);
777
778   for (auto link: parsed_link_list)
779     route.link_list->push_back(link);
780   parsed_link_list.clear();
781
782   sg_platf_new_route(&route);
783   delete route.link_list;
784 }
785
786 void ETag_surfxml_ASroute(){
787   s_sg_platf_route_cbarg_t ASroute;
788   memset(&ASroute,0,sizeof(ASroute));
789
790   ASroute.src = sg_netpoint_by_name_or_null(A_surfxml_ASroute_src); // tested to not be nullptr in start tag
791   ASroute.dst = sg_netpoint_by_name_or_null(A_surfxml_ASroute_dst); // tested to not be nullptr in start tag
792
793   ASroute.gw_src = sg_netpoint_by_name_or_null(A_surfxml_ASroute_gw___src); // tested to not be nullptr in start tag
794   ASroute.gw_dst = sg_netpoint_by_name_or_null(A_surfxml_ASroute_gw___dst); // tested to not be nullptr in start tag
795
796   ASroute.link_list = new std::vector<simgrid::surf::LinkImpl*>();
797
798   for (auto link: parsed_link_list)
799     ASroute.link_list->push_back(link);
800   parsed_link_list.clear();
801
802   switch (A_surfxml_ASroute_symmetrical) {
803   case AU_surfxml_ASroute_symmetrical:
804   case A_surfxml_ASroute_symmetrical_YES:
805     ASroute.symmetrical = true;
806     break;
807   case A_surfxml_ASroute_symmetrical_NO:
808     ASroute.symmetrical = false;
809     break;
810   }
811
812   sg_platf_new_route(&ASroute);
813   delete ASroute.link_list;
814 }
815
816 void ETag_surfxml_bypassRoute(){
817   s_sg_platf_route_cbarg_t route;
818   memset(&route,0,sizeof(route));
819
820   route.src         = sg_netpoint_by_name_or_null(A_surfxml_bypassRoute_src); // tested to not be nullptr in start tag
821   route.dst         = sg_netpoint_by_name_or_null(A_surfxml_bypassRoute_dst); // tested to not be nullptr in start tag
822   route.gw_src = nullptr;
823   route.gw_dst = nullptr;
824   route.symmetrical = false;
825   route.link_list   = new std::vector<simgrid::surf::LinkImpl*>();
826
827   for (auto link: parsed_link_list)
828     route.link_list->push_back(link);
829   parsed_link_list.clear();
830
831   sg_platf_new_bypassRoute(&route);
832   delete route.link_list;
833 }
834
835 void ETag_surfxml_bypassASroute(){
836   s_sg_platf_route_cbarg_t ASroute;
837   memset(&ASroute,0,sizeof(ASroute));
838
839   ASroute.src         = sg_netpoint_by_name_or_null(A_surfxml_bypassASroute_src);
840   ASroute.dst         = sg_netpoint_by_name_or_null(A_surfxml_bypassASroute_dst);
841   ASroute.link_list   = new std::vector<simgrid::surf::LinkImpl*>();
842   for (auto link: parsed_link_list)
843     ASroute.link_list->push_back(link);
844   parsed_link_list.clear();
845
846   ASroute.symmetrical = false;
847
848   ASroute.gw_src = sg_netpoint_by_name_or_null(A_surfxml_bypassASroute_gw___src);
849   ASroute.gw_dst = sg_netpoint_by_name_or_null(A_surfxml_bypassASroute_gw___dst);
850
851   sg_platf_new_bypassRoute(&ASroute);
852   delete ASroute.link_list;
853 }
854
855 void ETag_surfxml_trace(){
856   s_sg_platf_trace_cbarg_t trace;
857   memset(&trace,0,sizeof(trace));
858
859   trace.id = A_surfxml_trace_id;
860   trace.file = A_surfxml_trace_file;
861   trace.periodicity = surf_parse_get_double(A_surfxml_trace_periodicity);
862   trace.pc_data = surfxml_pcdata;
863
864   sg_platf_new_trace(&trace);
865 }
866
867 void STag_surfxml_trace___connect(){
868   parse_after_config();
869   s_sg_platf_trace_connect_cbarg_t trace_connect;
870   memset(&trace_connect,0,sizeof(trace_connect));
871
872   trace_connect.element = A_surfxml_trace___connect_element;
873   trace_connect.trace = A_surfxml_trace___connect_trace;
874
875   switch (A_surfxml_trace___connect_kind) {
876   case AU_surfxml_trace___connect_kind:
877   case A_surfxml_trace___connect_kind_SPEED:
878     trace_connect.kind =  SURF_TRACE_CONNECT_KIND_SPEED;
879     break;
880   case A_surfxml_trace___connect_kind_BANDWIDTH:
881     trace_connect.kind =  SURF_TRACE_CONNECT_KIND_BANDWIDTH;
882     break;
883   case A_surfxml_trace___connect_kind_HOST___AVAIL:
884     trace_connect.kind =  SURF_TRACE_CONNECT_KIND_HOST_AVAIL;
885     break;
886   case A_surfxml_trace___connect_kind_LATENCY:
887     trace_connect.kind =  SURF_TRACE_CONNECT_KIND_LATENCY;
888     break;
889   case A_surfxml_trace___connect_kind_LINK___AVAIL:
890     trace_connect.kind =  SURF_TRACE_CONNECT_KIND_LINK_AVAIL;
891     break;
892   }
893   sg_platf_trace_connect(&trace_connect);
894 }
895
896 void STag_surfxml_AS(){
897   parse_after_config();
898   AS_TAG                   = 1;
899   s_sg_platf_AS_cbarg_t AS = { A_surfxml_AS_id, (int)A_surfxml_AS_routing};
900
901   sg_platf_new_AS_begin(&AS);
902 }
903 void ETag_surfxml_AS(){
904   sg_platf_new_AS_seal();
905 }
906
907 void STag_surfxml_config(){
908   AS_TAG = 0;
909   xbt_assert(current_property_set == nullptr, "Someone forgot to reset the property set to nullptr in its closing tag (or XML malformed)");
910   XBT_DEBUG("START configuration name = %s",A_surfxml_config_id);
911   if (_sg_cfg_init_status == 2) {
912     surf_parse_error("All <config> tags must be given before any platform elements (such as <AS>, <host>, <cluster>, <link>, etc).");
913   }
914 }
915 void ETag_surfxml_config(){
916   xbt_dict_cursor_t cursor = nullptr;
917   char *key;
918   char *elem;
919   xbt_dict_foreach(current_property_set, cursor, key, elem) {
920     if (xbt_cfg_is_default_value(key)) {
921       char* cfg = bprintf("%s:%s", key, elem);
922       xbt_cfg_set_parse(cfg);
923       free(cfg);
924     } else
925       XBT_INFO("The custom configuration '%s' is already defined by user!",key);
926   }
927   XBT_DEBUG("End configuration name = %s",A_surfxml_config_id);
928
929   xbt_dict_free(&current_property_set);
930   current_property_set = nullptr;
931 }
932
933 static int argc;
934 static char **argv;
935
936 void STag_surfxml_process(){
937   AS_TAG  = 0;
938   argc    = 1;
939   argv    = xbt_new(char *, 1);
940   argv[0] = xbt_strdup(A_surfxml_process_function);
941   xbt_assert(current_property_set == nullptr, "Someone forgot to reset the property set to nullptr in its closing tag (or XML malformed)");
942 }
943
944 void ETag_surfxml_process(){
945   s_sg_platf_process_cbarg_t process;
946   memset(&process,0,sizeof(process));
947
948   process.argc       = argc;
949   process.argv       = (const char **)argv;
950   process.properties = current_property_set;
951   process.host       = A_surfxml_process_host;
952   process.function   = A_surfxml_process_function;
953   process.start_time = surf_parse_get_double(A_surfxml_process_start___time);
954   process.kill_time  = surf_parse_get_double(A_surfxml_process_kill___time);
955
956   switch (A_surfxml_process_on___failure) {
957   case AU_surfxml_process_on___failure:
958   case A_surfxml_process_on___failure_DIE:
959     process.on_failure =  SURF_PROCESS_ON_FAILURE_DIE;
960     break;
961   case A_surfxml_process_on___failure_RESTART:
962     process.on_failure =  SURF_PROCESS_ON_FAILURE_RESTART;
963     break;
964   }
965
966   sg_platf_new_process(&process);
967
968   for (int i = 0; i != argc; ++i)
969     xbt_free(argv[i]);
970   xbt_free(argv);
971   argv = nullptr;
972
973   current_property_set = nullptr;
974 }
975
976 void STag_surfxml_argument(){
977   argc++;
978   argv = (char**)xbt_realloc(argv, (argc) * sizeof(char **));
979   argv[(argc) - 1] = xbt_strdup(A_surfxml_argument_value);
980 }
981
982 void STag_surfxml_model___prop(){
983   if (!current_model_property_set)
984     current_model_property_set = xbt_dict_new_homogeneous(xbt_free_f);
985
986   xbt_dict_set(current_model_property_set, A_surfxml_model___prop_id, xbt_strdup(A_surfxml_model___prop_value), nullptr);
987 }
988
989 void ETag_surfxml_prop(){/* Nothing to do */}
990 void STag_surfxml_random(){/* Nothing to do */}
991 void ETag_surfxml_random(){/* Nothing to do */}
992 void ETag_surfxml_trace___connect(){/* Nothing to do */}
993 void STag_surfxml_trace(){parse_after_config();}
994 void ETag_surfxml_router(){/*Nothing to do*/}
995 void ETag_surfxml_host___link(){/* Nothing to do */}
996 void ETag_surfxml_cabinet(){/* Nothing to do */}
997 void ETag_surfxml_peer(){/* Nothing to do */}
998 void STag_surfxml_backbone(){/* Nothing to do */}
999 void ETag_surfxml_link___ctn(){/* Nothing to do */}
1000 void ETag_surfxml_argument(){/* Nothing to do */}
1001 void ETag_surfxml_model___prop(){/* Nothing to do */}
1002
1003 /* Open and Close parse file */
1004 void surf_parse_open(const char *file)
1005 {
1006   xbt_assert(file, "Cannot parse the nullptr file. Bypassing the parser is strongly deprecated nowadays.");
1007
1008   if (!surf_input_buffer_stack)
1009     surf_input_buffer_stack = xbt_dynar_new(sizeof(YY_BUFFER_STATE), nullptr);
1010   if (!surf_file_to_parse_stack)
1011     surf_file_to_parse_stack = xbt_dynar_new(sizeof(FILE *), nullptr);
1012
1013   if (!surf_parsed_filename_stack)
1014     surf_parsed_filename_stack = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
1015
1016   surf_parsed_filename = xbt_strdup(file);
1017   char *dir = xbt_dirname(file);
1018   xbt_dynar_push(surf_path, &dir);
1019
1020   surf_file_to_parse = surf_fopen(file, "r");
1021   xbt_assert((surf_file_to_parse), "Unable to open \"%s\"\n", file);
1022   surf_input_buffer = surf_parse__create_buffer(surf_file_to_parse, YY_BUF_SIZE);
1023   surf_parse__switch_to_buffer(surf_input_buffer);
1024   surf_parse_lineno = 1;
1025 }
1026
1027 void surf_parse_close()
1028 {
1029   xbt_dynar_free(&surf_input_buffer_stack);
1030   xbt_dynar_free(&surf_file_to_parse_stack);
1031   xbt_dynar_free(&surf_parsed_filename_stack);
1032   if (surf_parsed_filename) {
1033     char *dir = nullptr;
1034     xbt_dynar_pop(surf_path, &dir);
1035     free(dir);
1036   }
1037
1038   free(surf_parsed_filename);
1039   surf_parsed_filename = nullptr;
1040
1041   if (surf_file_to_parse) {
1042     surf_parse__delete_buffer(surf_input_buffer);
1043     fclose(surf_file_to_parse);
1044     surf_file_to_parse = nullptr; //Must be reset for Bypass
1045   }
1046 }
1047
1048 /* Call the lexer to parse the currently opened file. This pointer to function enables bypassing of the parser */
1049 static int _surf_parse() {
1050   return surf_parse_lex();
1051 }
1052
1053 int_f_void_t surf_parse = _surf_parse;
1054
1055 SG_END_DECL()