Logo AND Algorithmique Numérique Distribuée

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