Logo AND Algorithmique Numérique Distribuée

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