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 "simgrid/platf_interface.h"
13 #include "surf/surf_routing.h"
14 #include "surf/surf.h"
16 #include "src/simix/smx_private.h"
18 #include "cpu_interface.hpp"
19 #include "host_interface.hpp"
21 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(surf_parse);
22 xbt_dynar_t sg_platf_link_cb_list = NULL; // of sg_platf_link_cb_t
23 xbt_dynar_t sg_platf_cluster_cb_list = NULL; // of sg_platf_cluster_cb_t
24 xbt_dynar_t sg_platf_postparse_cb_list = NULL; // of void_f_void_t
26 static int surf_parse_models_setup_already_called = 0;
28 /* one RngStream for the platform, to respect some statistic rules */
29 static RngStream sg_platf_rng_stream = NULL;
31 /** Module management function: creates all internal data structures */
32 void sg_platf_init(void) {
34 //FIXME : Ugly, but useful...
35 if (sg_platf_postparse_cb_list)
36 return; //Already initialized, so do nothing...
38 sg_platf_link_cb_list = xbt_dynar_new(sizeof(sg_platf_link_cb_t), NULL);
39 sg_platf_cluster_cb_list = xbt_dynar_new(sizeof(sg_platf_cluster_cb_t), NULL);
40 sg_platf_postparse_cb_list = xbt_dynar_new(sizeof(sg_platf_link_cb_t),NULL);
43 /** Module management function: frees all internal data structures */
44 void sg_platf_exit(void) {
45 xbt_dynar_free(&sg_platf_link_cb_list);
46 xbt_dynar_free(&sg_platf_postparse_cb_list);
47 xbt_dynar_free(&sg_platf_cluster_cb_list);
49 /* make sure that we will reinit the models while loading the platf once reinited */
50 surf_parse_models_setup_already_called = 0;
53 void sg_platf_new_host(sg_platf_host_cbarg_t host)
55 xbt_assert(! sg_host_by_name(host->id),
56 "Refusing to create a second host named '%s'.", host->id);
58 simgrid::surf::NetCard *net = NULL;
59 simgrid::surf::As* current_routing = routing_get_current();
61 net = routing_add_host(current_routing, host);
63 sg_host_t h = simgrid::Host::by_name_or_create(host->id);
64 simgrid::surf::Cpu *cpu = surf_cpu_model_pm->createCpu(
73 surf_host_model->createHost(host->id, net, cpu, host->properties)->attach(h);
74 simgrid::Host::onCreation(*h);
76 if (TRACE_is_enabled() && TRACE_needs_platform())
77 sg_instr_new_host(host);
81 * \brief Add a "router" to the network element list
83 void sg_platf_new_router(sg_platf_router_cbarg_t router)
85 simgrid::surf::As* current_routing = routing_get_current();
87 if (current_routing->p_hierarchy == SURF_ROUTING_NULL)
88 current_routing->p_hierarchy = SURF_ROUTING_BASE;
89 xbt_assert(!xbt_lib_get_or_null(as_router_lib, router->id, ROUTING_ASR_LEVEL),
90 "Reading a router, processing unit \"%s\" already exists",
93 simgrid::surf::NetCard *info = new simgrid::surf::NetCardImpl(
94 xbt_strdup(router->id), -1, SURF_NETWORK_ELEMENT_ROUTER, current_routing);
95 info->setId(current_routing->parsePU(info));
96 xbt_lib_set(as_router_lib, router->id, ROUTING_ASR_LEVEL, (void *) info);
97 XBT_DEBUG("Having set name '%s' id '%d'", router->id, info->getId());
98 simgrid::surf::routingEdgeCreatedCallbacks(info);
100 if (router->coord && strcmp(router->coord, "")) {
104 if (!COORD_ASR_LEVEL)
105 xbt_die ("To use host coordinates, please add --cfg=network/coordinates:yes to your command line");
106 /* Pre-parse the host coordinates */
107 xbt_dynar_t ctn_str = xbt_str_split_str(router->coord, " ");
108 xbt_dynar_t ctn = xbt_dynar_new(sizeof(double),NULL);
109 xbt_dynar_foreach(ctn_str,cursor, str) {
110 double val = atof(str);
111 xbt_dynar_push(ctn,&val);
113 xbt_dynar_shrink(ctn, 0);
114 xbt_dynar_free(&ctn_str);
115 xbt_lib_set(as_router_lib, router->id, COORD_ASR_LEVEL, (void *) ctn);
116 XBT_DEBUG("Having set router coordinates for '%s'",router->id);
119 if (TRACE_is_enabled())
120 sg_instr_new_router(router);
123 void sg_platf_new_link(sg_platf_link_cbarg_t link){
124 unsigned int iterator;
125 sg_platf_link_cb_t fun;
126 xbt_dynar_foreach(sg_platf_link_cb_list, iterator, fun) {
131 void sg_platf_new_cluster(sg_platf_cluster_cbarg_t cluster)
133 routing_new_cluster(cluster);
135 unsigned int iterator;
136 sg_platf_cluster_cb_t fun;
137 xbt_dynar_foreach(sg_platf_cluster_cb_list, iterator, fun) {
142 void sg_platf_new_storage(sg_platf_storage_cbarg_t storage)
144 xbt_assert(!xbt_lib_get_or_null(storage_lib, storage->id,ROUTING_STORAGE_LEVEL),
145 "Refusing to add a second storage named \"%s\"", storage->id);
147 void* stype = xbt_lib_get_or_null(storage_type_lib, storage->type_id,ROUTING_STORAGE_TYPE_LEVEL);
148 xbt_assert(stype,"No storage type '%s'", storage->type_id);
150 XBT_DEBUG("ROUTING Create a storage name '%s' with type_id '%s' and content '%s'",
155 xbt_lib_set(storage_lib,
157 ROUTING_STORAGE_LEVEL,
158 (void *) xbt_strdup(storage->type_id));
160 // if storage content is not specified use the content of storage_type if any
161 if(!strcmp(storage->content,"") && strcmp(((storage_type_t) stype)->content,"")){
162 storage->content = ((storage_type_t) stype)->content;
163 storage->content_type = ((storage_type_t) stype)->content_type;
164 XBT_DEBUG("For disk '%s' content is empty, inherit the content (of type %s) from storage type '%s' ",
165 storage->id,((storage_type_t) stype)->content_type,
166 ((storage_type_t) stype)->type_id);
169 XBT_DEBUG("SURF storage create resource\n\t\tid '%s'\n\t\ttype '%s' "
170 "\n\t\tmodel '%s' \n\t\tcontent '%s'\n\t\tcontent_type '%s' "
171 "\n\t\tproperties '%p''\n",
173 ((storage_type_t) stype)->model,
174 ((storage_type_t) stype)->type_id,
176 storage->content_type,
177 storage->properties);
179 surf_storage_model->createStorage(storage->id,
180 ((storage_type_t) stype)->type_id,
182 storage->content_type,
186 void sg_platf_new_storage_type(sg_platf_storage_type_cbarg_t storage_type){
188 xbt_assert(!xbt_lib_get_or_null(storage_type_lib, storage_type->id,ROUTING_STORAGE_TYPE_LEVEL),
189 "Reading a storage type, processing unit \"%s\" already exists", storage_type->id);
191 storage_type_t stype = xbt_new0(s_storage_type_t, 1);
192 stype->model = xbt_strdup(storage_type->model);
193 stype->properties = storage_type->properties;
194 stype->content = xbt_strdup(storage_type->content);
195 stype->content_type = xbt_strdup(storage_type->content_type);
196 stype->type_id = xbt_strdup(storage_type->id);
197 stype->size = storage_type->size;
198 stype->model_properties = storage_type->model_properties;
200 XBT_DEBUG("ROUTING Create a storage type id '%s' with model '%s', "
201 "content '%s', and content_type '%s'",
204 storage_type->content,
205 storage_type->content_type);
207 xbt_lib_set(storage_type_lib,
209 ROUTING_STORAGE_TYPE_LEVEL,
212 void sg_platf_new_mstorage(sg_platf_mstorage_cbarg_t mstorage)
215 // mount_t mnt = xbt_new0(s_mount_t, 1);
216 // mnt->id = xbt_strdup(mstorage->type_id);
217 // mnt->name = xbt_strdup(mstorage->name);
220 // XBT_DEBUG("Creata a Mount list for %s",A_surfxml_host_id);
221 // mount_list = xbt_dynar_new(sizeof(char *), NULL);
223 // xbt_dynar_push(mount_list,(void *) mnt);
227 // XBT_DEBUG("ROUTING Mount a storage name '%s' with type_id '%s'",mstorage->name, mstorage->id);
230 static void mount_free(void *p)
232 mount_t mnt = (mount_t) p;
236 void sg_platf_new_mount(sg_platf_mount_cbarg_t mount){
237 // Verification of an existing storage
239 void* storage = xbt_lib_get_or_null(storage_lib, mount->storageId, ROUTING_STORAGE_LEVEL);
241 xbt_assert(storage,"Disk id \"%s\" does not exists", mount->storageId);
243 XBT_DEBUG("ROUTING Mount '%s' on '%s'",mount->storageId, mount->name);
246 mnt.storage = surf_storage_resource_priv(surf_storage_resource_by_name(mount->storageId));
247 mnt.name = xbt_strdup(mount->name);
250 XBT_DEBUG("Create a Mount list for %s",A_surfxml_host_id);
251 mount_list = xbt_dynar_new(sizeof(s_mount_t), mount_free);
253 xbt_dynar_push(mount_list, &mnt);
256 void sg_platf_new_route(sg_platf_route_cbarg_t route)
258 routing_get_current()->parseRoute(route);
261 void sg_platf_new_ASroute(sg_platf_route_cbarg_t ASroute)
263 routing_get_current()->parseASroute(ASroute);
266 void sg_platf_new_bypassRoute(sg_platf_route_cbarg_t bypassRoute)
268 routing_get_current()->parseBypassroute(bypassRoute);
271 void sg_platf_new_bypassASroute(sg_platf_route_cbarg_t bypassASroute)
273 routing_get_current()->parseBypassroute(bypassASroute);
276 void sg_platf_new_process(sg_platf_process_cbarg_t process)
279 xbt_die("Cannot create process without SIMIX.");
281 sg_host_t host = sg_host_by_name(process->host);
283 THROWF(arg_error, 0, "Host '%s' unknown", process->host);
284 xbt_main_func_t parse_code = SIMIX_get_registered_function(process->function);
285 xbt_assert(parse_code, "Function '%s' unknown", process->function);
287 double start_time = process->start_time;
288 double kill_time = process->kill_time;
289 int auto_restart = process->on_failure == SURF_PROCESS_ON_FAILURE_DIE ? 0 : 1;
291 smx_process_arg_t arg = NULL;
292 smx_process_t process_created = NULL;
294 arg = xbt_new0(s_smx_process_arg_t, 1);
295 arg->code = parse_code;
297 arg->hostname = sg_host_get_name(host);
298 arg->argc = process->argc;
299 arg->argv = xbt_new(char *,process->argc);
301 for (i=0; i<process->argc; i++)
302 arg->argv[i] = xbt_strdup(process->argv[i]);
303 arg->name = xbt_strdup(arg->argv[0]);
304 arg->kill_time = kill_time;
305 arg->properties = current_property_set;
306 if (!sg_host_simix(host)->boot_processes) {
307 sg_host_simix(host)->boot_processes = xbt_dynar_new(sizeof(smx_process_arg_t), _SIMIX_host_free_process_arg);
309 xbt_dynar_push_as(sg_host_simix(host)->boot_processes,smx_process_arg_t,arg);
311 if (start_time > SIMIX_get_clock()) {
312 arg = xbt_new0(s_smx_process_arg_t, 1);
313 arg->name = (char*)(process->argv)[0];
314 arg->code = parse_code;
316 arg->hostname = sg_host_get_name(host);
317 arg->argc = process->argc;
318 arg->argv = (char**)(process->argv);
319 arg->kill_time = kill_time;
320 arg->properties = current_property_set;
322 XBT_DEBUG("Process %s(%s) will be started at time %f", arg->name,
323 arg->hostname, start_time);
324 SIMIX_timer_set(start_time, [](void* arg) {
325 SIMIX_process_create_from_wrapper((smx_process_arg_t) arg);
327 } else { // start_time <= SIMIX_get_clock()
328 XBT_DEBUG("Starting Process %s(%s) right now", process->argv[0], sg_host_get_name(host));
330 if (simix_global->create_process_function)
331 process_created = simix_global->create_process_function(
332 (char*)(process->argv)[0],
335 sg_host_get_name(host),
338 (char**)(process->argv),
339 current_property_set,
342 process_created = simcall_process_create((char*)(process->argv)[0], parse_code, NULL, sg_host_get_name(host), kill_time, process->argc,
343 (char**)process->argv, current_property_set,auto_restart);
345 /* verify if process has been created (won't be the case if the host is currently dead, but that's fine) */
346 if (!process_created) {
350 current_property_set = NULL;
353 void sg_platf_route_begin (sg_platf_route_cbarg_t route){
354 route->link_list = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
356 void sg_platf_ASroute_begin (sg_platf_route_cbarg_t ASroute){
357 ASroute->link_list = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
360 void sg_platf_route_end (sg_platf_route_cbarg_t route){
361 sg_platf_new_route(route);
363 void sg_platf_ASroute_end (sg_platf_route_cbarg_t ASroute){
364 sg_platf_new_ASroute(ASroute);
367 void sg_platf_route_add_link (const char* link_id, sg_platf_route_cbarg_t route){
368 char *link_name = xbt_strdup(link_id);
369 xbt_dynar_push(route->link_list, &link_name);
371 void sg_platf_ASroute_add_link (const char* link_id, sg_platf_route_cbarg_t ASroute){
372 char *link_name = xbt_strdup(link_id);
373 xbt_dynar_push(ASroute->link_list, &link_name);
376 void sg_platf_begin() { /* Do nothing: just for symmetry of user code */ }
378 void sg_platf_end() {
379 unsigned int iterator;
381 xbt_dynar_foreach(sg_platf_postparse_cb_list, iterator, fun) {
386 void sg_platf_new_AS_begin(sg_platf_AS_cbarg_t AS)
388 if (!surf_parse_models_setup_already_called) {
389 /* Initialize the surf models. That must be done after we got all config, and before we need the models.
390 * That is, after the last <config> tag, if any, and before the first of cluster|peer|AS|trace|trace_connect
392 * I'm not sure for <trace> and <trace_connect>, there may be a bug here
393 * (FIXME: check it out by creating a file beginning with one of these tags)
394 * but cluster and peer create ASes internally, so putting the code in there is ok.
396 * TODO, There used to be a guard protecting here against
397 * xbt_dynar_length(sg_platf_AS_begin_cb_list) because we don't want to
398 * initialize the models if we are parsing the file to get the deployment.
399 * That could happen if the same file would be used for platf and deploy:
400 * it'd contain AS tags even during the deploy parsing. Removing that guard
401 * would result of the models to get re-inited when parsing for deploy.
402 * Currently using the same file for platform and deployment is broken
403 * however. This guard will have to ba adapted in order to make this feature
406 surf_parse_models_setup_already_called = 1;
407 surf_config_models_setup();
410 routing_AS_begin(AS);
411 if (TRACE_is_enabled())
412 sg_instr_AS_begin(AS);
415 void sg_platf_new_AS_end()
418 if (TRACE_is_enabled())
421 /* ***************************************** */
423 void sg_platf_link_add_cb(sg_platf_link_cb_t fct) {
424 xbt_dynar_push(sg_platf_link_cb_list, &fct);
426 void sg_platf_cluster_add_cb(sg_platf_cluster_cb_t fct) {
427 xbt_dynar_push(sg_platf_cluster_cb_list, &fct);
429 void sg_platf_postparse_add_cb(void_f_void_t fct) {
430 xbt_dynar_push(sg_platf_postparse_cb_list, &fct);
433 void sg_platf_rng_stream_init(unsigned long seed[6]) {
434 RngStream_SetPackageSeed(seed);
435 sg_platf_rng_stream = RngStream_CreateStream(NULL);
438 RngStream sg_platf_rng_stream_get(const char* id) {
439 RngStream stream = NULL;
440 unsigned int id_hash;
442 stream = RngStream_CopyStream(sg_platf_rng_stream);
443 id_hash = xbt_str_hash(id);
444 RngStream_AdvanceState(stream, 0, (long)id_hash);