Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
8156a9765785139de5880997c156045020eb6f7f
[simgrid.git] / src / surf / sg_platf.cpp
1 /* Copyright (c) 2006-2014. 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 "xbt/misc.h"
8 #include "xbt/log.h"
9 #include "xbt/str.h"
10 #include "xbt/dict.h"
11 #include "xbt/RngStream.h"
12 #include <xbt/signal.hpp>
13 #include "simgrid/platf_interface.h"
14 #include "surf/surf.h"
15
16 #include "src/simix/smx_private.h"
17 #include "src/surf/platform.hpp"
18
19 #include "surf/surfxml_parse.h"// FIXME: brain dead public header
20
21 #include "src/surf/platform.hpp"
22 #include "src/surf/cpu_interface.hpp"
23 #include "src/surf/host_interface.hpp"
24 #include "src/surf/network_interface.hpp"
25 #include "surf/surf_routing.h" // FIXME: brain dead public header
26 #include "src/surf/surf_routing_cluster.hpp"
27 #include "src/surf/surf_routing_cluster_torus.hpp"
28 #include "src/surf/surf_routing_cluster_fat_tree.hpp"
29
30 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(surf_parse);
31
32 XBT_PRIVATE xbt_dynar_t mount_list = NULL;
33
34 namespace simgrid {
35 namespace surf {
36
37 simgrid::xbt::signal<void(sg_platf_link_cbarg_t)> on_link;
38 simgrid::xbt::signal<void(sg_platf_cluster_cbarg_t)> on_cluster;
39 simgrid::xbt::signal<void(void)> on_postparse;
40
41 }
42 }
43
44 static int surf_parse_models_setup_already_called = 0;
45
46 /* one RngStream for the platform, to respect some statistic rules */
47 static RngStream sg_platf_rng_stream = NULL;
48
49 /** Module management function: creates all internal data structures */
50 void sg_platf_init(void) {
51 }
52
53 /** Module management function: frees all internal data structures */
54 void sg_platf_exit(void) {
55   simgrid::surf::on_link.disconnect_all_slots();
56   simgrid::surf::on_cluster.disconnect_all_slots();
57   simgrid::surf::on_postparse.disconnect_all_slots();
58
59   /* make sure that we will reinit the models while loading the platf once reinited */
60   surf_parse_models_setup_already_called = 0;
61 }
62
63 /** @brief Add an "host" to the current AS */
64 void sg_platf_new_host(sg_platf_host_cbarg_t host)
65 {
66   xbt_assert(! sg_host_by_name(host->id),
67       "Refusing to create a second host named '%s'.", host->id);
68
69   simgrid::surf::As* current_routing = routing_get_current();
70   if (current_routing->p_hierarchy == SURF_ROUTING_NULL)
71     current_routing->p_hierarchy = SURF_ROUTING_BASE;
72
73   simgrid::surf::NetCard *netcard =
74       new simgrid::surf::NetCardImpl(host->id, SURF_NETWORK_ELEMENT_HOST, current_routing);
75
76   netcard->setId(current_routing->parsePU(netcard));
77   sg_host_t h = simgrid::s4u::Host::by_name_or_create(host->id);
78   h->pimpl_netcard = netcard;
79   simgrid::surf::netcardCreatedCallbacks(netcard);
80
81   if(mount_list){
82     xbt_lib_set(storage_lib, host->id, ROUTING_STORAGE_HOST_LEVEL, (void *) mount_list);
83     mount_list = NULL;
84   }
85
86   if (host->coord && strcmp(host->coord, "")) {
87     unsigned int cursor;
88     char*str;
89
90     if (!COORD_HOST_LEVEL)
91       xbt_die ("To use host coordinates, please add --cfg=network/coordinates:yes to your command line");
92     /* Pre-parse the host coordinates -- FIXME factorize with routers by overloading the routing->parse_PU function*/
93     xbt_dynar_t ctn_str = xbt_str_split_str(host->coord, " ");
94     xbt_dynar_t ctn = xbt_dynar_new(sizeof(double),NULL);
95     xbt_dynar_foreach(ctn_str,cursor, str) {
96       double val = xbt_str_parse_double(str, "Invalid coordinate: %s");
97       xbt_dynar_push(ctn,&val);
98     }
99     xbt_dynar_shrink(ctn, 0);
100     xbt_dynar_free(&ctn_str);
101     h->extension_set(COORD_HOST_LEVEL, (void *) ctn);
102     XBT_DEBUG("Having set host coordinates for '%s'",host->id);
103   }
104
105
106   simgrid::surf::Cpu *cpu = surf_cpu_model_pm->createCpu( h,
107       host->speed_peak,
108       host->pstate,
109       host->speed_scale, host->speed_trace,
110       host->core_amount,
111       host->initiallyOn, host->state_trace);
112   surf_host_model->createHost(host->id, netcard, cpu, host->properties)->attach(h);
113   simgrid::s4u::Host::onCreation(*h);
114
115   if (TRACE_is_enabled() && TRACE_needs_platform())
116     sg_instr_new_host(host);
117 }
118
119 /**
120  * \brief Add a "router" to the network element list
121  */
122 void sg_platf_new_router(sg_platf_router_cbarg_t router)
123 {
124   simgrid::surf::As* current_routing = routing_get_current();
125
126   if (current_routing->p_hierarchy == SURF_ROUTING_NULL)
127     current_routing->p_hierarchy = SURF_ROUTING_BASE;
128   xbt_assert(!xbt_lib_get_or_null(as_router_lib, router->id, ROUTING_ASR_LEVEL),
129              "Reading a router, processing unit \"%s\" already exists",
130              router->id);
131
132   simgrid::surf::NetCard *info = new simgrid::surf::NetCardImpl(router->id, SURF_NETWORK_ELEMENT_ROUTER, current_routing);
133   info->setId(current_routing->parsePU(info));
134   xbt_lib_set(as_router_lib, router->id, ROUTING_ASR_LEVEL, (void *) info);
135   XBT_DEBUG("Having set name '%s' id '%d'", router->id, info->getId());
136   simgrid::surf::netcardCreatedCallbacks(info);
137
138   if (router->coord && strcmp(router->coord, "")) {
139     unsigned int cursor;
140     char*str;
141
142     if (!COORD_ASR_LEVEL)
143       xbt_die ("To use host coordinates, please add --cfg=network/coordinates:yes to your command line");
144     /* Pre-parse the host coordinates */
145     xbt_dynar_t ctn_str = xbt_str_split_str(router->coord, " ");
146     xbt_dynar_t ctn = xbt_dynar_new(sizeof(double),NULL);
147     xbt_dynar_foreach(ctn_str,cursor, str) {
148       double val = xbt_str_parse_double(str, "Invalid coordinate: %s");
149       xbt_dynar_push(ctn,&val);
150     }
151     xbt_dynar_shrink(ctn, 0);
152     xbt_dynar_free(&ctn_str);
153     xbt_lib_set(as_router_lib, router->id, COORD_ASR_LEVEL, (void *) ctn);
154     XBT_DEBUG("Having set router coordinates for '%s'",router->id);
155   }
156
157   if (TRACE_is_enabled() && TRACE_needs_platform())
158     sg_instr_new_router(router);
159 }
160
161 void sg_platf_new_link(sg_platf_link_cbarg_t link){
162   simgrid::surf::on_link(link);
163 }
164
165 void sg_platf_new_cluster(sg_platf_cluster_cbarg_t cluster)
166 {
167   using simgrid::surf::AsCluster;
168   using simgrid::surf::AsClusterTorus;
169   using simgrid::surf::AsClusterFatTree;
170
171   char *host_id, *groups, *link_id = NULL;
172   xbt_dict_t patterns = NULL;
173   int rankId=0;
174
175   s_sg_platf_host_cbarg_t host = SG_PLATF_HOST_INITIALIZER;
176   s_sg_platf_link_cbarg_t link = SG_PLATF_LINK_INITIALIZER;
177
178   unsigned int iter;
179   int start, end, i;
180   xbt_dynar_t radical_elements;
181   xbt_dynar_t radical_ends;
182
183   if ((cluster->availability_trace && strcmp(cluster->availability_trace, ""))
184       || (cluster->state_trace && strcmp(cluster->state_trace, ""))) {
185     patterns = xbt_dict_new_homogeneous(xbt_free_f);
186     xbt_dict_set(patterns, "id", xbt_strdup(cluster->id), NULL);
187     xbt_dict_set(patterns, "prefix", xbt_strdup(cluster->prefix), NULL);
188     xbt_dict_set(patterns, "suffix", xbt_strdup(cluster->suffix), NULL);
189   }
190
191   /* Parse the topology attributes.
192    * Nothing to do in a vanilla cluster, but that's another story for torus and flat_trees */
193   s_sg_platf_AS_cbarg_t AS = SG_PLATF_AS_INITIALIZER;
194   AS.id = cluster->id;
195
196   switch (cluster->topology) {
197   case SURF_CLUSTER_TORUS:
198     AS.routing = A_surfxml_AS_routing_ClusterTorus;
199     break;
200   case SURF_CLUSTER_FAT_TREE:
201     AS.routing = A_surfxml_AS_routing_ClusterFatTree;
202     break;
203   default:
204     AS.routing = A_surfxml_AS_routing_Cluster;
205     break;
206   }
207
208   // What an inventive way of initializing the AS that I have as ancestor :-(
209   sg_platf_new_AS_begin(&AS);
210   simgrid::surf::As *current_routing = routing_get_current();
211   static_cast<AsCluster*>(current_routing)->parse_specific_arguments(cluster);
212
213   if(cluster->loopback_bw!=0 || cluster->loopback_lat!=0){
214       ((AsCluster*)current_routing)->p_nb_links_per_node++;
215       ((AsCluster*)current_routing)->p_has_loopback=1;
216   }
217
218   if(cluster->limiter_link!=0){
219       ((AsCluster*)current_routing)->p_nb_links_per_node++;
220       ((AsCluster*)current_routing)->p_has_limiter=1;
221   }
222
223
224   current_routing->p_linkUpDownList = xbt_dynar_new(sizeof(s_surf_parsing_link_up_down_t),NULL);
225
226   //Make all hosts
227   radical_elements = xbt_str_split(cluster->radical, ",");
228   xbt_dynar_foreach(radical_elements, iter, groups) {
229
230     radical_ends = xbt_str_split(groups, "-");
231     start = surf_parse_get_int(xbt_dynar_get_as(radical_ends, 0, char *));
232
233     switch (xbt_dynar_length(radical_ends)) {
234     case 1:
235       end = start;
236       break;
237     case 2:
238       end = surf_parse_get_int(xbt_dynar_get_as(radical_ends, 1, char *));
239       break;
240     default:
241       surf_parse_error("Malformed radical");
242       break;
243     }
244     for (i = start; i <= end; i++) {
245       host_id = bprintf("%s%d%s", cluster->prefix, i, cluster->suffix);
246       link_id = bprintf("%s_link_%d", cluster->id, i);
247
248       XBT_DEBUG("<host\tid=\"%s\"\tpower=\"%f\">", host_id, cluster->speed);
249
250       memset(&host, 0, sizeof(host));
251       host.id = host_id;
252       if ((cluster->properties != NULL) && (!xbt_dict_is_empty(cluster->properties))) {
253         xbt_dict_cursor_t cursor=NULL;
254         char *key,*data;
255         host.properties = xbt_dict_new();
256
257         xbt_dict_foreach(cluster->properties,cursor,key,data) {
258           xbt_dict_set(host.properties, key, xbt_strdup(data),free);
259         }
260       }
261       if (cluster->availability_trace && strcmp(cluster->availability_trace, "")) {
262         xbt_dict_set(patterns, "radical", bprintf("%d", i), NULL);
263         char *avail_file = xbt_str_varsubst(cluster->availability_trace, patterns);
264         XBT_DEBUG("\tavailability_file=\"%s\"", avail_file);
265         host.speed_trace = tmgr_trace_new_from_file(avail_file);
266         xbt_free(avail_file);
267       } else {
268         XBT_DEBUG("\tavailability_file=\"\"");
269       }
270
271       if (cluster->state_trace && strcmp(cluster->state_trace, "")) {
272         char *avail_file = xbt_str_varsubst(cluster->state_trace, patterns);
273         XBT_DEBUG("\tstate_file=\"%s\"", avail_file);
274         host.state_trace = tmgr_trace_new_from_file(avail_file);
275         xbt_free(avail_file);
276       } else {
277         XBT_DEBUG("\tstate_file=\"\"");
278       }
279
280       host.speed_peak = xbt_dynar_new(sizeof(double), NULL);
281       xbt_dynar_push(host.speed_peak,&cluster->speed);
282       host.pstate = 0;
283
284       //host.power_peak = cluster->power;
285       host.speed_scale = 1.0;
286       host.core_amount = cluster->core_amount;
287       host.initiallyOn = 1;
288       host.coord = "";
289       sg_platf_new_host(&host);
290       xbt_dynar_free(&host.speed_peak);
291       XBT_DEBUG("</host>");
292
293       XBT_DEBUG("<link\tid=\"%s\"\tbw=\"%f\"\tlat=\"%f\"/>", link_id,
294                 cluster->bw, cluster->lat);
295
296
297       s_surf_parsing_link_up_down_t info_lim, info_loop;
298       // All links are saved in a matrix;
299       // every row describes a single node; every node
300       // may have multiple links.
301       // the first column may store a link from x to x if p_has_loopback is set
302       // the second column may store a limiter link if p_has_limiter is set
303       // other columns are to store one or more link for the node
304
305       //add a loopback link
306       if(cluster->loopback_bw!=0 || cluster->loopback_lat!=0){
307         char *tmp_link = bprintf("%s_loopback", link_id);
308         XBT_DEBUG("<loopback\tid=\"%s\"\tbw=\"%f\"/>", tmp_link,
309                 cluster->limiter_link);
310
311
312         memset(&link, 0, sizeof(link));
313         link.id        = tmp_link;
314         link.bandwidth = cluster->loopback_bw;
315         link.latency   = cluster->loopback_lat;
316         link.initiallyOn = 1;
317         link.policy    = SURF_LINK_FATPIPE;
318         sg_platf_new_link(&link);
319         info_loop.link_up   = Link::byName(tmp_link);
320         info_loop.link_down = info_loop.link_up;
321         free(tmp_link);
322         xbt_dynar_set(current_routing->p_linkUpDownList,
323           rankId*(static_cast<AsCluster*>(current_routing))->p_nb_links_per_node, &info_loop);
324       }
325
326       //add a limiter link (shared link to account for maximal bandwidth of the node)
327       if(cluster->limiter_link!=0){
328         char *tmp_link = bprintf("%s_limiter", link_id);
329         XBT_DEBUG("<limiter\tid=\"%s\"\tbw=\"%f\"/>", tmp_link,
330                 cluster->limiter_link);
331
332
333         memset(&link, 0, sizeof(link));
334         link.id = tmp_link;
335         link.bandwidth = cluster->limiter_link;
336         link.latency = 0;
337         link.initiallyOn = 1;
338         link.policy = SURF_LINK_SHARED;
339         sg_platf_new_link(&link);
340         info_lim.link_up = Link::byName(tmp_link);
341         info_lim.link_down = info_lim.link_up;
342         free(tmp_link);
343         auto as_cluster = static_cast<AsCluster*>(current_routing);
344         xbt_dynar_set(current_routing->p_linkUpDownList,
345             rankId*(as_cluster)->p_nb_links_per_node + as_cluster->p_has_loopback ,
346             &info_lim);
347
348       }
349
350
351       //call the cluster function that adds the others links
352       if (cluster->topology == SURF_CLUSTER_FAT_TREE) {
353         ((AsClusterFatTree*) current_routing)->addProcessingNode(i);
354       }
355       else {
356       static_cast<AsCluster*>(current_routing)->create_links_for_node(cluster, i, rankId, rankId*
357           static_cast<AsCluster*>(current_routing)->p_nb_links_per_node
358           + static_cast<AsCluster*>(current_routing)->p_has_loopback
359           + static_cast<AsCluster*>(current_routing)->p_has_limiter );
360       }
361       xbt_free(link_id);
362       xbt_free(host_id);
363       rankId++;
364     }
365
366     xbt_dynar_free(&radical_ends);
367   }
368   xbt_dynar_free(&radical_elements);
369
370   // For fat trees, the links must be created once all nodes have been added
371   if(cluster->topology == SURF_CLUSTER_FAT_TREE) {
372     static_cast<simgrid::surf::AsClusterFatTree*>(current_routing)->create_links();
373   }
374   // Add a router. It is magically used thanks to the way in which surf_routing_cluster is written,
375   // and it's very useful to connect clusters together
376   XBT_DEBUG(" ");
377   XBT_DEBUG("<router id=\"%s\"/>", cluster->router_id);
378   char *newid = NULL;
379   s_sg_platf_router_cbarg_t router = SG_PLATF_ROUTER_INITIALIZER;
380   memset(&router, 0, sizeof(router));
381   router.id = cluster->router_id;
382   router.coord = "";
383   if (!router.id || !strcmp(router.id, ""))
384     router.id = newid =
385         bprintf("%s%s_router%s", cluster->prefix, cluster->id,
386                 cluster->suffix);
387   sg_platf_new_router(&router);
388   ((AsCluster*)current_routing)->p_router = (simgrid::surf::NetCard*) xbt_lib_get_or_null(as_router_lib, router.id, ROUTING_ASR_LEVEL);
389   free(newid);
390
391   //Make the backbone
392   if ((cluster->bb_bw != 0) || (cluster->bb_lat != 0)) {
393     char *link_backbone = bprintf("%s_backbone", cluster->id);
394     XBT_DEBUG("<link\tid=\"%s\" bw=\"%f\" lat=\"%f\"/>", link_backbone,
395               cluster->bb_bw, cluster->bb_lat);
396
397     memset(&link, 0, sizeof(link));
398     link.id        = link_backbone;
399     link.bandwidth = cluster->bb_bw;
400     link.latency   = cluster->bb_lat;
401     link.initiallyOn = 1;
402     link.policy    = cluster->bb_sharing_policy;
403
404     sg_platf_new_link(&link);
405
406     routing_cluster_add_backbone(Link::byName(link_backbone));
407
408     free(link_backbone);
409   }
410
411   XBT_DEBUG("</AS>");
412   sg_platf_new_AS_end();
413   XBT_DEBUG(" ");
414   xbt_dict_free(&patterns); // no op if it were never set
415
416   simgrid::surf::on_cluster(cluster);
417 }
418
419 void sg_platf_new_storage(sg_platf_storage_cbarg_t storage)
420 {
421   xbt_assert(!xbt_lib_get_or_null(storage_lib, storage->id,ROUTING_STORAGE_LEVEL),
422                "Refusing to add a second storage named \"%s\"", storage->id);
423
424   void* stype = xbt_lib_get_or_null(storage_type_lib, storage->type_id,ROUTING_STORAGE_TYPE_LEVEL);
425   xbt_assert(stype,"No storage type '%s'", storage->type_id);
426
427   XBT_DEBUG("ROUTING Create a storage name '%s' with type_id '%s' and content '%s'",
428       storage->id,
429       storage->type_id,
430       storage->content);
431
432   xbt_lib_set(storage_lib,
433       storage->id,
434       ROUTING_STORAGE_LEVEL,
435       (void *) xbt_strdup(storage->type_id));
436
437   // if storage content is not specified use the content of storage_type if any
438   if(!strcmp(storage->content,"") && strcmp(((storage_type_t) stype)->content,"")){
439     storage->content = ((storage_type_t) stype)->content;
440     storage->content_type = ((storage_type_t) stype)->content_type;
441     XBT_DEBUG("For disk '%s' content is empty, inherit the content (of type %s) from storage type '%s' ",
442         storage->id,((storage_type_t) stype)->content_type,
443         ((storage_type_t) stype)->type_id);
444   }
445
446   XBT_DEBUG("SURF storage create resource\n\t\tid '%s'\n\t\ttype '%s' "
447       "\n\t\tmodel '%s' \n\t\tcontent '%s'\n\t\tcontent_type '%s' "
448       "\n\t\tproperties '%p''\n",
449       storage->id,
450       ((storage_type_t) stype)->model,
451       ((storage_type_t) stype)->type_id,
452       storage->content,
453       storage->content_type,
454     storage->properties);
455
456   surf_storage_model->createStorage(storage->id,
457                                      ((storage_type_t) stype)->type_id,
458                                      storage->content,
459                                      storage->content_type,
460                    storage->properties,
461                                      storage->attach);
462 }
463 void sg_platf_new_storage_type(sg_platf_storage_type_cbarg_t storage_type){
464
465   xbt_assert(!xbt_lib_get_or_null(storage_type_lib, storage_type->id,ROUTING_STORAGE_TYPE_LEVEL),
466                "Reading a storage type, processing unit \"%s\" already exists", storage_type->id);
467
468   storage_type_t stype = xbt_new0(s_storage_type_t, 1);
469   stype->model = xbt_strdup(storage_type->model);
470   stype->properties = storage_type->properties;
471   stype->content = xbt_strdup(storage_type->content);
472   stype->content_type = xbt_strdup(storage_type->content_type);
473   stype->type_id = xbt_strdup(storage_type->id);
474   stype->size = storage_type->size;
475   stype->model_properties = storage_type->model_properties;
476
477   XBT_DEBUG("ROUTING Create a storage type id '%s' with model '%s', "
478       "content '%s', and content_type '%s'",
479       stype->type_id,
480       stype->model,
481       storage_type->content,
482       storage_type->content_type);
483
484   xbt_lib_set(storage_type_lib,
485       stype->type_id,
486       ROUTING_STORAGE_TYPE_LEVEL,
487       (void *) stype);
488 }
489 void sg_platf_new_mstorage(sg_platf_mstorage_cbarg_t mstorage)
490 {
491   THROW_UNIMPLEMENTED;
492 }
493
494 static void mount_free(void *p)
495 {
496   mount_t mnt = (mount_t) p;
497   xbt_free(mnt->name);
498 }
499
500 void sg_platf_new_mount(sg_platf_mount_cbarg_t mount){
501   // Verification of an existing storage
502   xbt_assert(xbt_lib_get_or_null(storage_lib, mount->storageId, ROUTING_STORAGE_LEVEL),
503       "Cannot mount non-existent disk \"%s\"", mount->storageId);
504
505   XBT_DEBUG("ROUTING Mount '%s' on '%s'",mount->storageId, mount->name);
506
507   s_mount_t mnt;
508   mnt.storage = surf_storage_resource_priv(surf_storage_resource_by_name(mount->storageId));
509   mnt.name = xbt_strdup(mount->name);
510
511   if(!mount_list){
512     XBT_DEBUG("Create a Mount list for %s",A_surfxml_host_id);
513     mount_list = xbt_dynar_new(sizeof(s_mount_t), mount_free);
514   }
515   xbt_dynar_push(mount_list, &mnt);
516 }
517
518 void sg_platf_new_route(sg_platf_route_cbarg_t route)
519 {
520   routing_get_current()->parseRoute(route);
521 }
522
523 void sg_platf_new_ASroute(sg_platf_route_cbarg_t ASroute)
524 {
525   routing_get_current()->parseASroute(ASroute);
526 }
527
528 void sg_platf_new_bypassRoute(sg_platf_route_cbarg_t bypassRoute)
529 {
530   routing_get_current()->parseBypassroute(bypassRoute);
531 }
532
533 void sg_platf_new_bypassASroute(sg_platf_route_cbarg_t bypassASroute)
534 {
535   routing_get_current()->parseBypassroute(bypassASroute);
536 }
537
538 void sg_platf_new_process(sg_platf_process_cbarg_t process)
539 {
540   if (!simix_global)
541     xbt_die("Cannot create process without SIMIX.");
542
543   sg_host_t host = sg_host_by_name(process->host);
544   if (!host) {
545     // The requested host does not exist. Do a nice message to the user
546     char *tmp = bprintf("Cannot create process '%s': host '%s' does not exist\nExisting hosts: '",process->function, process->host);
547     xbt_strbuff_t msg = xbt_strbuff_new_from(tmp);
548     free(tmp);
549     xbt_dynar_t all_hosts = xbt_dynar_sort_strings(sg_hosts_as_dynar());
550     simgrid::s4u::Host* host;
551     unsigned int cursor;
552     xbt_dynar_foreach(all_hosts,cursor, host) {
553       xbt_strbuff_append(msg,host->name().c_str());
554       xbt_strbuff_append(msg,"', '");
555       if (msg->used > 1024) {
556         msg->data[msg->used-3]='\0';
557         msg->used -= 3;
558
559         xbt_strbuff_append(msg," ...(list truncated)......");// That will be shortened by 3 chars when existing the loop
560       }
561     }
562     msg->data[msg->used-3]='\0';
563     xbt_die("%s", msg->data);
564   }
565   xbt_main_func_t parse_code = SIMIX_get_registered_function(process->function);
566   xbt_assert(parse_code, "Function '%s' unknown", process->function);
567
568   double start_time = process->start_time;
569   double kill_time  = process->kill_time;
570   int auto_restart = process->on_failure == SURF_PROCESS_ON_FAILURE_DIE ? 0 : 1;
571
572   smx_process_arg_t arg = NULL;
573   smx_process_t process_created = NULL;
574
575   arg = xbt_new0(s_smx_process_arg_t, 1);
576   arg->code = parse_code;
577   arg->data = NULL;
578   arg->hostname = sg_host_get_name(host);
579   arg->argc = process->argc;
580   arg->argv = xbt_new(char *,process->argc);
581   int i;
582   for (i=0; i<process->argc; i++)
583     arg->argv[i] = xbt_strdup(process->argv[i]);
584   arg->name = xbt_strdup(arg->argv[0]);
585   arg->kill_time = kill_time;
586   arg->properties = current_property_set;
587   if (!sg_host_simix(host)->boot_processes) {
588     sg_host_simix(host)->boot_processes = xbt_dynar_new(sizeof(smx_process_arg_t), _SIMIX_host_free_process_arg);
589   }
590   xbt_dynar_push_as(sg_host_simix(host)->boot_processes,smx_process_arg_t,arg);
591
592   if (start_time > SIMIX_get_clock()) {
593     arg = xbt_new0(s_smx_process_arg_t, 1);
594     arg->name = (char*)(process->argv)[0];
595     arg->code = parse_code;
596     arg->data = NULL;
597     arg->hostname = sg_host_get_name(host);
598     arg->argc = process->argc;
599     arg->argv = (char**)(process->argv);
600     arg->kill_time = kill_time;
601     arg->properties = current_property_set;
602
603     XBT_DEBUG("Process %s(%s) will be started at time %f", arg->name,
604            arg->hostname, start_time);
605     SIMIX_timer_set(start_time, [](void* arg) {
606       SIMIX_process_create_from_wrapper((smx_process_arg_t) arg);
607     }, arg);
608   } else {                      // start_time <= SIMIX_get_clock()
609     XBT_DEBUG("Starting Process %s(%s) right now", arg->name, sg_host_get_name(host));
610
611     if (simix_global->create_process_function)
612       process_created = simix_global->create_process_function(
613           arg->name,
614                                             parse_code,
615                                             NULL,
616                                             sg_host_get_name(host),
617                                             kill_time,
618                                             process->argc,
619                                             (char**)(process->argv),
620                                             current_property_set,
621                                             auto_restart, NULL);
622     else
623       process_created = simcall_process_create(arg->name, parse_code, NULL, sg_host_get_name(host), kill_time, process->argc,
624           (char**)process->argv, current_property_set,auto_restart);
625
626     /* verify if process has been created (won't be the case if the host is currently dead, but that's fine) */
627     if (!process_created) {
628       return;
629     }
630   }
631   current_property_set = NULL;
632 }
633
634 void sg_platf_route_begin (sg_platf_route_cbarg_t route){
635   route->link_list = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
636 }
637 void sg_platf_ASroute_begin (sg_platf_route_cbarg_t ASroute){
638   ASroute->link_list = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
639 }
640
641 void sg_platf_route_end (sg_platf_route_cbarg_t route){
642   sg_platf_new_route(route);
643 }
644 void sg_platf_ASroute_end (sg_platf_route_cbarg_t ASroute){
645   sg_platf_new_ASroute(ASroute);
646 }
647
648 void sg_platf_route_add_link (const char* link_id, sg_platf_route_cbarg_t route){
649   char *link_name = xbt_strdup(link_id);
650   xbt_dynar_push(route->link_list, &link_name);
651 }
652 void sg_platf_ASroute_add_link (const char* link_id, sg_platf_route_cbarg_t ASroute){
653   char *link_name = xbt_strdup(link_id);
654   xbt_dynar_push(ASroute->link_list, &link_name);
655 }
656
657 void sg_platf_begin() { /* Do nothing: just for symmetry of user code */ }
658
659 void sg_platf_end() {
660   simgrid::surf::on_postparse();
661 }
662
663 void sg_platf_new_AS_begin(sg_platf_AS_cbarg_t AS)
664 {
665   if (!surf_parse_models_setup_already_called) {
666     /* Initialize the surf models. That must be done after we got all config, and before we need the models.
667      * That is, after the last <config> tag, if any, and before the first of cluster|peer|AS|trace|trace_connect
668      *
669      * I'm not sure for <trace> and <trace_connect>, there may be a bug here
670      * (FIXME: check it out by creating a file beginning with one of these tags)
671      * but cluster and peer create ASes internally, so putting the code in there is ok.
672      *
673      * TODO, There used to be a guard protecting here against
674      * xbt_dynar_length(sg_platf_AS_begin_cb_list) because we don't want to
675      * initialize the models if we are parsing the file to get the deployment.
676      * That could happen if the same file would be used for platf and deploy:
677      * it'd contain AS tags even during the deploy parsing. Removing that guard
678      * would result of the models to get re-inited when parsing for deploy.
679      * Currently using the same file for platform and deployment is broken
680      * however. This guard will have to ba adapted in order to make this feature
681      * work again.
682      */
683     surf_parse_models_setup_already_called = 1;
684     surf_config_models_setup();
685   }
686
687   routing_AS_begin(AS);
688   if (TRACE_is_enabled())
689     sg_instr_AS_begin(AS);
690 }
691
692 void sg_platf_new_AS_end()
693 {
694   routing_AS_end();
695   if (TRACE_is_enabled())
696     sg_instr_AS_end();
697 }
698 /* ***************************************** */
699
700 void sg_platf_rng_stream_init(unsigned long seed[6]) {
701   RngStream_SetPackageSeed(seed);
702   sg_platf_rng_stream = RngStream_CreateStream(NULL);
703 }
704
705 RngStream sg_platf_rng_stream_get(const char* id) {
706   RngStream stream = NULL;
707   unsigned int id_hash;
708
709   stream = RngStream_CopyStream(sg_platf_rng_stream);
710   id_hash = xbt_str_hash(id);
711   RngStream_AdvanceState(stream, 0, (long)id_hash);
712
713   return stream;
714 }