1 /* Copyright (c) 2006-2014. The SimGrid Team.
2 * All rights reserved. */
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. */
11 #include "xbt/RngStream.h"
12 #include <xbt/signal.hpp>
13 #include "simgrid/platf_interface.h"
14 #include "surf/surf_routing.h"
15 #include "surf/surf.h"
17 #include "src/simix/smx_private.h"
18 #include "src/surf/platform.hpp"
20 #include "cpu_interface.hpp"
21 #include "host_interface.hpp"
23 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(surf_parse);
28 simgrid::xbt::signal<void(sg_platf_link_cbarg_t)> on_link;
29 simgrid::xbt::signal<void(sg_platf_cluster_cbarg_t)> on_cluster;
30 simgrid::xbt::signal<void(void)> on_postparse;
35 static int surf_parse_models_setup_already_called = 0;
37 /* one RngStream for the platform, to respect some statistic rules */
38 static RngStream sg_platf_rng_stream = NULL;
40 /** Module management function: creates all internal data structures */
41 void sg_platf_init(void) {
44 /** Module management function: frees all internal data structures */
45 void sg_platf_exit(void) {
46 simgrid::surf::on_link.disconnect_all_slots();
47 simgrid::surf::on_cluster.disconnect_all_slots();
48 simgrid::surf::on_postparse.disconnect_all_slots();
50 /* make sure that we will reinit the models while loading the platf once reinited */
51 surf_parse_models_setup_already_called = 0;
54 /** @brief Add an "host" to the current AS */
55 void sg_platf_new_host(sg_platf_host_cbarg_t host)
57 xbt_assert(! sg_host_by_name(host->id),
58 "Refusing to create a second host named '%s'.", host->id);
60 simgrid::surf::As* current_routing = routing_get_current();
61 if (current_routing->p_hierarchy == SURF_ROUTING_NULL)
62 current_routing->p_hierarchy = SURF_ROUTING_BASE;
64 simgrid::surf::NetCard *netcard =
65 new simgrid::surf::NetCardImpl(xbt_strdup(host->id), -1, SURF_NETWORK_ELEMENT_HOST, current_routing);
67 netcard->setId(current_routing->parsePU(netcard));
68 sg_host_t h = simgrid::s4u::Host::by_name_or_create(host->id);
69 h->pimpl_netcard = netcard;
70 simgrid::surf::netcardCreatedCallbacks(netcard);
73 xbt_lib_set(storage_lib, host->id, ROUTING_STORAGE_HOST_LEVEL, (void *) mount_list);
77 if (host->coord && strcmp(host->coord, "")) {
81 if (!COORD_HOST_LEVEL)
82 xbt_die ("To use host coordinates, please add --cfg=network/coordinates:yes to your command line");
83 /* Pre-parse the host coordinates -- FIXME factorize with routers by overloading the routing->parse_PU function*/
84 xbt_dynar_t ctn_str = xbt_str_split_str(host->coord, " ");
85 xbt_dynar_t ctn = xbt_dynar_new(sizeof(double),NULL);
86 xbt_dynar_foreach(ctn_str,cursor, str) {
87 double val = xbt_str_parse_double(str, "Invalid coordinate: %s");
88 xbt_dynar_push(ctn,&val);
90 xbt_dynar_shrink(ctn, 0);
91 xbt_dynar_free(&ctn_str);
92 h->extension_set(COORD_HOST_LEVEL, (void *) ctn);
93 XBT_DEBUG("Having set host coordinates for '%s'",host->id);
97 simgrid::surf::Cpu *cpu = surf_cpu_model_pm->createCpu( h,
100 host->speed_scale, host->speed_trace,
102 host->initiallyOn, host->state_trace);
103 surf_host_model->createHost(host->id, netcard, cpu, host->properties)->attach(h);
104 simgrid::s4u::Host::onCreation(*h);
106 if (TRACE_is_enabled() && TRACE_needs_platform())
107 sg_instr_new_host(host);
111 * \brief Add a "router" to the network element list
113 void sg_platf_new_router(sg_platf_router_cbarg_t router)
115 simgrid::surf::As* current_routing = routing_get_current();
117 if (current_routing->p_hierarchy == SURF_ROUTING_NULL)
118 current_routing->p_hierarchy = SURF_ROUTING_BASE;
119 xbt_assert(!xbt_lib_get_or_null(as_router_lib, router->id, ROUTING_ASR_LEVEL),
120 "Reading a router, processing unit \"%s\" already exists",
123 simgrid::surf::NetCard *info = new simgrid::surf::NetCardImpl(
124 xbt_strdup(router->id), -1, SURF_NETWORK_ELEMENT_ROUTER, current_routing);
125 info->setId(current_routing->parsePU(info));
126 xbt_lib_set(as_router_lib, router->id, ROUTING_ASR_LEVEL, (void *) info);
127 XBT_DEBUG("Having set name '%s' id '%d'", router->id, info->getId());
128 simgrid::surf::netcardCreatedCallbacks(info);
130 if (router->coord && strcmp(router->coord, "")) {
134 if (!COORD_ASR_LEVEL)
135 xbt_die ("To use host coordinates, please add --cfg=network/coordinates:yes to your command line");
136 /* Pre-parse the host coordinates */
137 xbt_dynar_t ctn_str = xbt_str_split_str(router->coord, " ");
138 xbt_dynar_t ctn = xbt_dynar_new(sizeof(double),NULL);
139 xbt_dynar_foreach(ctn_str,cursor, str) {
140 double val = xbt_str_parse_double(str, "Invalid coordinate: %s");
141 xbt_dynar_push(ctn,&val);
143 xbt_dynar_shrink(ctn, 0);
144 xbt_dynar_free(&ctn_str);
145 xbt_lib_set(as_router_lib, router->id, COORD_ASR_LEVEL, (void *) ctn);
146 XBT_DEBUG("Having set router coordinates for '%s'",router->id);
149 if (TRACE_is_enabled() && TRACE_needs_platform())
150 sg_instr_new_router(router);
153 void sg_platf_new_link(sg_platf_link_cbarg_t link){
154 simgrid::surf::on_link(link);
157 void sg_platf_new_cluster(sg_platf_cluster_cbarg_t cluster)
159 routing_new_cluster(cluster);
160 simgrid::surf::on_cluster(cluster);
163 void sg_platf_new_storage(sg_platf_storage_cbarg_t storage)
165 xbt_assert(!xbt_lib_get_or_null(storage_lib, storage->id,ROUTING_STORAGE_LEVEL),
166 "Refusing to add a second storage named \"%s\"", storage->id);
168 void* stype = xbt_lib_get_or_null(storage_type_lib, storage->type_id,ROUTING_STORAGE_TYPE_LEVEL);
169 xbt_assert(stype,"No storage type '%s'", storage->type_id);
171 XBT_DEBUG("ROUTING Create a storage name '%s' with type_id '%s' and content '%s'",
176 xbt_lib_set(storage_lib,
178 ROUTING_STORAGE_LEVEL,
179 (void *) xbt_strdup(storage->type_id));
181 // if storage content is not specified use the content of storage_type if any
182 if(!strcmp(storage->content,"") && strcmp(((storage_type_t) stype)->content,"")){
183 storage->content = ((storage_type_t) stype)->content;
184 storage->content_type = ((storage_type_t) stype)->content_type;
185 XBT_DEBUG("For disk '%s' content is empty, inherit the content (of type %s) from storage type '%s' ",
186 storage->id,((storage_type_t) stype)->content_type,
187 ((storage_type_t) stype)->type_id);
190 XBT_DEBUG("SURF storage create resource\n\t\tid '%s'\n\t\ttype '%s' "
191 "\n\t\tmodel '%s' \n\t\tcontent '%s'\n\t\tcontent_type '%s' "
192 "\n\t\tproperties '%p''\n",
194 ((storage_type_t) stype)->model,
195 ((storage_type_t) stype)->type_id,
197 storage->content_type,
198 storage->properties);
200 surf_storage_model->createStorage(storage->id,
201 ((storage_type_t) stype)->type_id,
203 storage->content_type,
207 void sg_platf_new_storage_type(sg_platf_storage_type_cbarg_t storage_type){
209 xbt_assert(!xbt_lib_get_or_null(storage_type_lib, storage_type->id,ROUTING_STORAGE_TYPE_LEVEL),
210 "Reading a storage type, processing unit \"%s\" already exists", storage_type->id);
212 storage_type_t stype = xbt_new0(s_storage_type_t, 1);
213 stype->model = xbt_strdup(storage_type->model);
214 stype->properties = storage_type->properties;
215 stype->content = xbt_strdup(storage_type->content);
216 stype->content_type = xbt_strdup(storage_type->content_type);
217 stype->type_id = xbt_strdup(storage_type->id);
218 stype->size = storage_type->size;
219 stype->model_properties = storage_type->model_properties;
221 XBT_DEBUG("ROUTING Create a storage type id '%s' with model '%s', "
222 "content '%s', and content_type '%s'",
225 storage_type->content,
226 storage_type->content_type);
228 xbt_lib_set(storage_type_lib,
230 ROUTING_STORAGE_TYPE_LEVEL,
233 void sg_platf_new_mstorage(sg_platf_mstorage_cbarg_t mstorage)
236 // mount_t mnt = xbt_new0(s_mount_t, 1);
237 // mnt->id = xbt_strdup(mstorage->type_id);
238 // mnt->name = xbt_strdup(mstorage->name);
241 // XBT_DEBUG("Creata a Mount list for %s",A_surfxml_host_id);
242 // mount_list = xbt_dynar_new(sizeof(char *), NULL);
244 // xbt_dynar_push(mount_list,(void *) mnt);
248 // XBT_DEBUG("ROUTING Mount a storage name '%s' with type_id '%s'",mstorage->name, mstorage->id);
251 static void mount_free(void *p)
253 mount_t mnt = (mount_t) p;
257 void sg_platf_new_mount(sg_platf_mount_cbarg_t mount){
258 // Verification of an existing storage
260 void* storage = xbt_lib_get_or_null(storage_lib, mount->storageId, ROUTING_STORAGE_LEVEL);
262 xbt_assert(storage,"Disk id \"%s\" does not exists", mount->storageId);
264 XBT_DEBUG("ROUTING Mount '%s' on '%s'",mount->storageId, mount->name);
267 mnt.storage = surf_storage_resource_priv(surf_storage_resource_by_name(mount->storageId));
268 mnt.name = xbt_strdup(mount->name);
271 XBT_DEBUG("Create a Mount list for %s",A_surfxml_host_id);
272 mount_list = xbt_dynar_new(sizeof(s_mount_t), mount_free);
274 xbt_dynar_push(mount_list, &mnt);
277 void sg_platf_new_route(sg_platf_route_cbarg_t route)
279 routing_get_current()->parseRoute(route);
282 void sg_platf_new_ASroute(sg_platf_route_cbarg_t ASroute)
284 routing_get_current()->parseASroute(ASroute);
287 void sg_platf_new_bypassRoute(sg_platf_route_cbarg_t bypassRoute)
289 routing_get_current()->parseBypassroute(bypassRoute);
292 void sg_platf_new_bypassASroute(sg_platf_route_cbarg_t bypassASroute)
294 routing_get_current()->parseBypassroute(bypassASroute);
297 void sg_platf_new_process(sg_platf_process_cbarg_t process)
300 xbt_die("Cannot create process without SIMIX.");
302 sg_host_t host = sg_host_by_name(process->host);
304 // The requested host does not exist. Do a nice message to the user
305 char *tmp = bprintf("Cannot create process '%s': host '%s' does not exist\nExisting hosts: '",process->function, process->host);
306 xbt_strbuff_t msg = xbt_strbuff_new_from(tmp);
308 xbt_dynar_t all_hosts = xbt_dynar_sort_strings(sg_hosts_as_dynar());
309 simgrid::s4u::Host* host;
311 xbt_dynar_foreach(all_hosts,cursor, host) {
312 xbt_strbuff_append(msg,host->name().c_str());
313 xbt_strbuff_append(msg,"', '");
314 if (msg->used > 1024) {
315 msg->data[msg->used-3]='\0';
318 xbt_strbuff_append(msg," ...(list truncated)......");// That will be shortened by 3 chars when existing the loop
321 msg->data[msg->used-3]='\0';
322 xbt_die("%s", msg->data);
324 xbt_main_func_t parse_code = SIMIX_get_registered_function(process->function);
325 xbt_assert(parse_code, "Function '%s' unknown", process->function);
327 double start_time = process->start_time;
328 double kill_time = process->kill_time;
329 int auto_restart = process->on_failure == SURF_PROCESS_ON_FAILURE_DIE ? 0 : 1;
331 smx_process_arg_t arg = NULL;
332 smx_process_t process_created = NULL;
334 arg = xbt_new0(s_smx_process_arg_t, 1);
335 arg->code = parse_code;
337 arg->hostname = sg_host_get_name(host);
338 arg->argc = process->argc;
339 arg->argv = xbt_new(char *,process->argc);
341 for (i=0; i<process->argc; i++)
342 arg->argv[i] = xbt_strdup(process->argv[i]);
343 arg->name = xbt_strdup(arg->argv[0]);
344 arg->kill_time = kill_time;
345 arg->properties = current_property_set;
346 if (!sg_host_simix(host)->boot_processes) {
347 sg_host_simix(host)->boot_processes = xbt_dynar_new(sizeof(smx_process_arg_t), _SIMIX_host_free_process_arg);
349 xbt_dynar_push_as(sg_host_simix(host)->boot_processes,smx_process_arg_t,arg);
351 if (start_time > SIMIX_get_clock()) {
352 arg = xbt_new0(s_smx_process_arg_t, 1);
353 arg->name = (char*)(process->argv)[0];
354 arg->code = parse_code;
356 arg->hostname = sg_host_get_name(host);
357 arg->argc = process->argc;
358 arg->argv = (char**)(process->argv);
359 arg->kill_time = kill_time;
360 arg->properties = current_property_set;
362 XBT_DEBUG("Process %s(%s) will be started at time %f", arg->name,
363 arg->hostname, start_time);
364 SIMIX_timer_set(start_time, [](void* arg) {
365 SIMIX_process_create_from_wrapper((smx_process_arg_t) arg);
367 } else { // start_time <= SIMIX_get_clock()
368 XBT_DEBUG("Starting Process %s(%s) right now", arg->name, sg_host_get_name(host));
370 if (simix_global->create_process_function)
371 process_created = simix_global->create_process_function(
375 sg_host_get_name(host),
378 (char**)(process->argv),
379 current_property_set,
382 process_created = simcall_process_create(arg->name, parse_code, NULL, sg_host_get_name(host), kill_time, process->argc,
383 (char**)process->argv, current_property_set,auto_restart);
385 /* verify if process has been created (won't be the case if the host is currently dead, but that's fine) */
386 if (!process_created) {
390 current_property_set = NULL;
393 void sg_platf_route_begin (sg_platf_route_cbarg_t route){
394 route->link_list = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
396 void sg_platf_ASroute_begin (sg_platf_route_cbarg_t ASroute){
397 ASroute->link_list = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
400 void sg_platf_route_end (sg_platf_route_cbarg_t route){
401 sg_platf_new_route(route);
403 void sg_platf_ASroute_end (sg_platf_route_cbarg_t ASroute){
404 sg_platf_new_ASroute(ASroute);
407 void sg_platf_route_add_link (const char* link_id, sg_platf_route_cbarg_t route){
408 char *link_name = xbt_strdup(link_id);
409 xbt_dynar_push(route->link_list, &link_name);
411 void sg_platf_ASroute_add_link (const char* link_id, sg_platf_route_cbarg_t ASroute){
412 char *link_name = xbt_strdup(link_id);
413 xbt_dynar_push(ASroute->link_list, &link_name);
416 void sg_platf_begin() { /* Do nothing: just for symmetry of user code */ }
418 void sg_platf_end() {
419 simgrid::surf::on_postparse();
422 void sg_platf_new_AS_begin(sg_platf_AS_cbarg_t AS)
424 if (!surf_parse_models_setup_already_called) {
425 /* Initialize the surf models. That must be done after we got all config, and before we need the models.
426 * That is, after the last <config> tag, if any, and before the first of cluster|peer|AS|trace|trace_connect
428 * I'm not sure for <trace> and <trace_connect>, there may be a bug here
429 * (FIXME: check it out by creating a file beginning with one of these tags)
430 * but cluster and peer create ASes internally, so putting the code in there is ok.
432 * TODO, There used to be a guard protecting here against
433 * xbt_dynar_length(sg_platf_AS_begin_cb_list) because we don't want to
434 * initialize the models if we are parsing the file to get the deployment.
435 * That could happen if the same file would be used for platf and deploy:
436 * it'd contain AS tags even during the deploy parsing. Removing that guard
437 * would result of the models to get re-inited when parsing for deploy.
438 * Currently using the same file for platform and deployment is broken
439 * however. This guard will have to ba adapted in order to make this feature
442 surf_parse_models_setup_already_called = 1;
443 surf_config_models_setup();
446 routing_AS_begin(AS);
447 if (TRACE_is_enabled())
448 sg_instr_AS_begin(AS);
451 void sg_platf_new_AS_end()
454 if (TRACE_is_enabled())
457 /* ***************************************** */
459 void sg_platf_rng_stream_init(unsigned long seed[6]) {
460 RngStream_SetPackageSeed(seed);
461 sg_platf_rng_stream = RngStream_CreateStream(NULL);
464 RngStream sg_platf_rng_stream_get(const char* id) {
465 RngStream stream = NULL;
466 unsigned int id_hash;
468 stream = RngStream_CopyStream(sg_platf_rng_stream);
469 id_hash = xbt_str_hash(id);
470 RngStream_AdvanceState(stream, 0, (long)id_hash);