Logo AND Algorithmique Numérique Distribuée

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