Logo AND Algorithmique Numérique Distribuée

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