Logo AND Algorithmique Numérique Distribuée

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