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_peer_cb_list = NULL; // of sg_platf_peer_cb_t
24 xbt_dynar_t sg_platf_cluster_cb_list = NULL; // of sg_platf_cluster_cb_t
25 xbt_dynar_t sg_platf_cabinet_cb_list = NULL; // of sg_platf_cluster_cb_t
26 xbt_dynar_t sg_platf_postparse_cb_list = NULL; // of void_f_void_t
27 xbt_dynar_t sg_platf_prop_cb_list = NULL; // of sg_platf_prop_cb_t
29 xbt_dynar_t sg_platf_route_cb_list = NULL; // of sg_platf_route_cb_t
30 xbt_dynar_t sg_platf_ASroute_cb_list = NULL; // of sg_platf_ASroute_cb_t
31 xbt_dynar_t sg_platf_bypassRoute_cb_list = NULL; // of sg_platf_bypassRoute_cb_t
32 xbt_dynar_t sg_platf_bypassASroute_cb_list = NULL; // of sg_platf_bypassASroute_cb_t
34 xbt_dynar_t sg_platf_trace_cb_list = NULL;
35 xbt_dynar_t sg_platf_trace_connect_cb_list = NULL;
37 /* ***************************************** */
38 /* TUTORIAL: New TAG */
40 xbt_dynar_t sg_platf_gpu_cb_list = NULL;
41 /* ***************************************** */
44 static int surf_parse_models_setup_already_called = 0;
46 /* one RngStream for the platform, to respect some statistic rules */
47 static RngStream sg_platf_rng_stream = NULL;
49 /** Module management function: creates all internal data structures */
50 void sg_platf_init(void) {
52 //FIXME : Ugly, but useful...
53 if (sg_platf_gpu_cb_list)
54 return; //Already initialized, so do nothing...
56 sg_platf_link_cb_list = xbt_dynar_new(sizeof(sg_platf_link_cb_t), NULL);
57 sg_platf_peer_cb_list = xbt_dynar_new(sizeof(sg_platf_peer_cb_t), NULL);
58 sg_platf_cluster_cb_list = xbt_dynar_new(sizeof(sg_platf_cluster_cb_t), NULL);
59 sg_platf_cabinet_cb_list = xbt_dynar_new(sizeof(sg_platf_cabinet_cb_t), NULL);
60 sg_platf_postparse_cb_list = xbt_dynar_new(sizeof(sg_platf_link_cb_t),NULL);
61 sg_platf_prop_cb_list = xbt_dynar_new(sizeof(sg_platf_prop_cb_t),NULL);
63 sg_platf_route_cb_list = xbt_dynar_new(sizeof(sg_platf_route_cb_t), NULL);
64 sg_platf_ASroute_cb_list = xbt_dynar_new(sizeof(sg_platf_route_cb_t), NULL);
65 sg_platf_bypassRoute_cb_list = xbt_dynar_new(sizeof(sg_platf_route_cb_t), NULL);
66 sg_platf_bypassASroute_cb_list = xbt_dynar_new(sizeof(sg_platf_route_cb_t), NULL);
68 sg_platf_trace_cb_list = xbt_dynar_new(sizeof(sg_platf_trace_cb_t), NULL);
69 sg_platf_trace_connect_cb_list = xbt_dynar_new(sizeof(sg_platf_trace_connect_cb_t), NULL);
71 /* ***************************************** */
72 /* TUTORIAL: New TAG */
74 sg_platf_gpu_cb_list = xbt_dynar_new(sizeof(sg_platf_gpu_cb_t), NULL);
75 /* ***************************************** */
77 /** Module management function: frees all internal data structures */
78 void sg_platf_exit(void) {
79 xbt_dynar_free(&sg_platf_link_cb_list);
80 xbt_dynar_free(&sg_platf_postparse_cb_list);
81 xbt_dynar_free(&sg_platf_peer_cb_list);
82 xbt_dynar_free(&sg_platf_cluster_cb_list);
83 xbt_dynar_free(&sg_platf_cabinet_cb_list);
84 xbt_dynar_free(&sg_platf_prop_cb_list);
86 xbt_dynar_free(&sg_platf_trace_cb_list);
87 xbt_dynar_free(&sg_platf_trace_connect_cb_list);
89 xbt_dynar_free(&sg_platf_route_cb_list);
90 xbt_dynar_free(&sg_platf_ASroute_cb_list);
91 xbt_dynar_free(&sg_platf_bypassRoute_cb_list);
92 xbt_dynar_free(&sg_platf_bypassASroute_cb_list);
94 /* ***************************************** */
95 /* TUTORIAL: New TAG */
97 xbt_dynar_free(&sg_platf_gpu_cb_list);
99 /* ***************************************** */
101 /* make sure that we will reinit the models while loading the platf once reinited */
102 surf_parse_models_setup_already_called = 0;
105 void sg_platf_new_host(sg_platf_host_cbarg_t host)
107 xbt_assert(! sg_host_by_name(host->id),
108 "Refusing to create a second host named '%s'.", host->id);
110 RoutingEdge *net = NULL;
111 As* current_routing = routing_get_current();
113 net = routing_add_host(current_routing, host);
115 Cpu *cpu = surf_cpu_model_pm->createCpu(
125 surf_host_model->createHost(host->id, net, cpu);
127 if (TRACE_is_enabled() && TRACE_needs_platform())
128 sg_instr_new_host(host);
132 * \brief Add a "router" to the network element list
134 void sg_platf_new_router(sg_platf_router_cbarg_t router)
136 As* current_routing = routing_get_current();
138 if (current_routing->p_hierarchy == SURF_ROUTING_NULL)
139 current_routing->p_hierarchy = SURF_ROUTING_BASE;
140 xbt_assert(!xbt_lib_get_or_null(as_router_lib, router->id, ROUTING_ASR_LEVEL),
141 "Reading a router, processing unit \"%s\" already exists",
144 RoutingEdge *info = new RoutingEdgeImpl(xbt_strdup(router->id),
146 SURF_NETWORK_ELEMENT_ROUTER,
148 info->setId(current_routing->parsePU(info));
149 xbt_lib_set(as_router_lib, router->id, ROUTING_ASR_LEVEL, (void *) info);
150 XBT_DEBUG("Having set name '%s' id '%d'", router->id, info->getId());
151 routingEdgeCreatedCallbacks(info);
153 if (router->coord && strcmp(router->coord, "")) {
157 if (!COORD_ASR_LEVEL)
158 xbt_die ("To use host coordinates, please add --cfg=network/coordinates:yes to your command line");
159 /* Pre-parse the host coordinates */
160 xbt_dynar_t ctn_str = xbt_str_split_str(router->coord, " ");
161 xbt_dynar_t ctn = xbt_dynar_new(sizeof(double),NULL);
162 xbt_dynar_foreach(ctn_str,cursor, str) {
163 double val = atof(str);
164 xbt_dynar_push(ctn,&val);
166 xbt_dynar_shrink(ctn, 0);
167 xbt_dynar_free(&ctn_str);
168 xbt_lib_set(as_router_lib, router->id, COORD_ASR_LEVEL, (void *) ctn);
169 XBT_DEBUG("Having set router coordinates for '%s'",router->id);
172 if (TRACE_is_enabled())
173 sg_instr_new_router(router);
176 void sg_platf_new_link(sg_platf_link_cbarg_t link){
177 unsigned int iterator;
178 sg_platf_link_cb_t fun;
179 xbt_dynar_foreach(sg_platf_link_cb_list, iterator, fun) {
184 void sg_platf_new_peer(sg_platf_peer_cbarg_t peer){
185 unsigned int iterator;
186 sg_platf_peer_cb_t fun;
187 xbt_dynar_foreach(sg_platf_peer_cb_list, iterator, fun) {
191 void sg_platf_new_cluster(sg_platf_cluster_cbarg_t cluster){
192 unsigned int iterator;
193 sg_platf_cluster_cb_t fun;
194 xbt_dynar_foreach(sg_platf_cluster_cb_list, iterator, fun) {
198 void sg_platf_new_cabinet(sg_platf_cabinet_cbarg_t cabinet){
199 unsigned int iterator;
200 sg_platf_cabinet_cb_t fun;
201 xbt_dynar_foreach(sg_platf_cabinet_cb_list, iterator, fun) {
205 void sg_platf_new_storage(sg_platf_storage_cbarg_t storage)
207 xbt_assert(!xbt_lib_get_or_null(storage_lib, storage->id,ROUTING_STORAGE_LEVEL),
208 "Reading a storage, processing unit \"%s\" already exists", storage->id);
210 // Verification of an existing type_id
212 void* storage_type = xbt_lib_get_or_null(storage_type_lib, storage->type_id,ROUTING_STORAGE_TYPE_LEVEL);
214 xbt_assert(storage_type,"Reading a storage, type id \"%s\" does not exists", storage->type_id);
216 XBT_DEBUG("ROUTING Create a storage name '%s' with type_id '%s' and content '%s'",
221 xbt_lib_set(storage_lib,
223 ROUTING_STORAGE_LEVEL,
224 (void *) xbt_strdup(storage->type_id));
226 void* stype = xbt_lib_get_or_null(storage_type_lib,
228 ROUTING_STORAGE_TYPE_LEVEL);
229 if(!stype) xbt_die("No storage type '%s'",storage->type_id);
231 // if storage content is not specified use the content of storage_type if exist
232 if(!strcmp(storage->content,"") && strcmp(((storage_type_t) stype)->content,"")){
233 storage->content = ((storage_type_t) stype)->content;
234 storage->content_type = ((storage_type_t) stype)->content_type;
235 XBT_DEBUG("For disk '%s' content is empty, inherit the content (of type %s) from storage type '%s' ",
236 storage->id,((storage_type_t) stype)->content_type,
237 ((storage_type_t) stype)->type_id);
240 XBT_DEBUG("SURF storage create resource\n\t\tid '%s'\n\t\ttype '%s' "
241 "\n\t\tmodel '%s' \n\t\tcontent '%s'\n\t\tcontent_type '%s' "
242 "\n\t\tproperties '%p''\n",
244 ((storage_type_t) stype)->model,
245 ((storage_type_t) stype)->type_id,
247 storage->content_type,
248 storage->properties);
250 surf_storage_model->createStorage(storage->id,
251 ((storage_type_t) stype)->type_id,
253 storage->content_type,
257 void sg_platf_new_storage_type(sg_platf_storage_type_cbarg_t storage_type){
259 xbt_assert(!xbt_lib_get_or_null(storage_type_lib, storage_type->id,ROUTING_STORAGE_TYPE_LEVEL),
260 "Reading a storage type, processing unit \"%s\" already exists", storage_type->id);
262 storage_type_t stype = xbt_new0(s_storage_type_t, 1);
263 stype->model = xbt_strdup(storage_type->model);
264 stype->properties = storage_type->properties;
265 stype->content = xbt_strdup(storage_type->content);
266 stype->content_type = xbt_strdup(storage_type->content_type);
267 stype->type_id = xbt_strdup(storage_type->id);
268 stype->size = storage_type->size;
269 stype->model_properties = storage_type->model_properties;
271 XBT_DEBUG("ROUTING Create a storage type id '%s' with model '%s', "
272 "content '%s', and content_type '%s'",
275 storage_type->content,
276 storage_type->content_type);
278 xbt_lib_set(storage_type_lib,
280 ROUTING_STORAGE_TYPE_LEVEL,
283 void sg_platf_new_mstorage(sg_platf_mstorage_cbarg_t mstorage)
286 // mount_t mnt = xbt_new0(s_mount_t, 1);
287 // mnt->id = xbt_strdup(mstorage->type_id);
288 // mnt->name = xbt_strdup(mstorage->name);
291 // XBT_DEBUG("Creata a Mount list for %s",A_surfxml_host_id);
292 // mount_list = xbt_dynar_new(sizeof(char *), NULL);
294 // xbt_dynar_push(mount_list,(void *) mnt);
298 // XBT_DEBUG("ROUTING Mount a storage name '%s' with type_id '%s'",mstorage->name, mstorage->id);
301 static void mount_free(void *p)
303 mount_t mnt = (mount_t) p;
307 void sg_platf_new_mount(sg_platf_mount_cbarg_t mount){
308 // Verification of an existing storage
310 void* storage = xbt_lib_get_or_null(storage_lib, mount->storageId, ROUTING_STORAGE_LEVEL);
312 xbt_assert(storage,"Disk id \"%s\" does not exists", mount->storageId);
314 XBT_DEBUG("ROUTING Mount '%s' on '%s'",mount->storageId, mount->name);
317 mnt.storage = surf_storage_resource_priv(surf_storage_resource_by_name(mount->storageId));
318 mnt.name = xbt_strdup(mount->name);
321 XBT_DEBUG("Create a Mount list for %s",A_surfxml_host_id);
322 mount_list = xbt_dynar_new(sizeof(s_mount_t), mount_free);
324 xbt_dynar_push(mount_list, &mnt);
327 void sg_platf_new_route(sg_platf_route_cbarg_t route) {
328 unsigned int iterator;
329 sg_platf_route_cb_t fun;
330 xbt_dynar_foreach(sg_platf_route_cb_list, iterator, fun) {
334 void sg_platf_new_ASroute(sg_platf_route_cbarg_t ASroute) {
335 unsigned int iterator;
336 sg_platf_route_cb_t fun;
337 xbt_dynar_foreach(sg_platf_ASroute_cb_list, iterator, fun) {
341 void sg_platf_new_bypassRoute(sg_platf_route_cbarg_t bypassRoute) {
342 unsigned int iterator;
343 sg_platf_route_cb_t fun;
344 xbt_dynar_foreach(sg_platf_bypassRoute_cb_list, iterator, fun) {
348 void sg_platf_new_bypassASroute(sg_platf_route_cbarg_t bypassASroute) {
349 unsigned int iterator;
350 sg_platf_route_cb_t fun;
351 xbt_dynar_foreach(sg_platf_bypassASroute_cb_list, iterator, fun) {
355 void sg_platf_new_prop(sg_platf_prop_cbarg_t prop) {
356 unsigned int iterator;
357 sg_platf_prop_cb_t fun;
358 xbt_dynar_foreach(sg_platf_prop_cb_list, iterator, fun) {
362 void sg_platf_new_trace(sg_platf_trace_cbarg_t trace) {
363 unsigned int iterator;
364 sg_platf_trace_cb_t fun;
365 xbt_dynar_foreach(sg_platf_trace_cb_list, iterator, fun) {
369 void sg_platf_trace_connect(sg_platf_trace_connect_cbarg_t trace_connect) {
370 unsigned int iterator;
371 sg_platf_trace_connect_cb_t fun;
372 xbt_dynar_foreach(sg_platf_trace_connect_cb_list, iterator, fun) {
377 void sg_platf_new_process(sg_platf_process_cbarg_t process)
380 xbt_die("Cannot create process without SIMIX.");
382 sg_host_t host = sg_host_by_name(process->host);
384 THROWF(arg_error, 0, "Host '%s' unknown", process->host);
385 xbt_main_func_t parse_code = SIMIX_get_registered_function(process->function);
386 xbt_assert(parse_code, "Function '%s' unknown", process->function);
388 double start_time = process->start_time;
389 double kill_time = process->kill_time;
390 int auto_restart = process->on_failure == SURF_PROCESS_ON_FAILURE_DIE ? 0 : 1;
392 smx_process_arg_t arg = NULL;
393 smx_process_t process_created = NULL;
395 arg = xbt_new0(s_smx_process_arg_t, 1);
396 arg->code = parse_code;
398 arg->hostname = sg_host_get_name(host);
399 arg->argc = process->argc;
400 arg->argv = xbt_new(char *,process->argc);
402 for (i=0; i<process->argc; i++)
403 arg->argv[i] = xbt_strdup(process->argv[i]);
404 arg->name = xbt_strdup(arg->argv[0]);
405 arg->kill_time = kill_time;
406 arg->properties = current_property_set;
407 if (!sg_host_simix(host)->boot_processes) {
408 sg_host_simix(host)->boot_processes = xbt_dynar_new(sizeof(smx_process_arg_t), _SIMIX_host_free_process_arg);
410 xbt_dynar_push_as(sg_host_simix(host)->boot_processes,smx_process_arg_t,arg);
412 if (start_time > SIMIX_get_clock()) {
413 arg = xbt_new0(s_smx_process_arg_t, 1);
414 arg->name = (char*)(process->argv)[0];
415 arg->code = parse_code;
417 arg->hostname = sg_host_get_name(host);
418 arg->argc = process->argc;
419 arg->argv = (char**)(process->argv);
420 arg->kill_time = kill_time;
421 arg->properties = current_property_set;
423 XBT_DEBUG("Process %s(%s) will be started at time %f", arg->name,
424 arg->hostname, start_time);
425 SIMIX_timer_set(start_time, [](void* arg) {
426 SIMIX_process_create_from_wrapper((smx_process_arg_t) arg);
428 } else { // start_time <= SIMIX_get_clock()
429 XBT_DEBUG("Starting Process %s(%s) right now", process->argv[0], sg_host_get_name(host));
431 if (simix_global->create_process_function)
432 process_created = simix_global->create_process_function(
433 (char*)(process->argv)[0],
436 sg_host_get_name(host),
439 (char**)(process->argv),
440 current_property_set,
443 process_created = simcall_process_create((char*)(process->argv)[0], parse_code, NULL, sg_host_get_name(host), kill_time, process->argc,
444 (char**)process->argv, current_property_set,auto_restart);
446 /* verify if process has been created (won't be the case if the host is currently dead, but that's fine) */
447 if (!process_created) {
451 current_property_set = NULL;
454 void sg_platf_route_begin (sg_platf_route_cbarg_t route){
455 route->link_list = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
457 void sg_platf_ASroute_begin (sg_platf_route_cbarg_t ASroute){
458 ASroute->link_list = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
461 void sg_platf_route_end (sg_platf_route_cbarg_t route){
462 sg_platf_new_route(route);
464 void sg_platf_ASroute_end (sg_platf_route_cbarg_t ASroute){
465 sg_platf_new_ASroute(ASroute);
468 void sg_platf_route_add_link (const char* link_id, sg_platf_route_cbarg_t route){
469 char *link_name = xbt_strdup(link_id);
470 xbt_dynar_push(route->link_list, &link_name);
472 void sg_platf_ASroute_add_link (const char* link_id, sg_platf_route_cbarg_t ASroute){
473 char *link_name = xbt_strdup(link_id);
474 xbt_dynar_push(ASroute->link_list, &link_name);
477 void sg_platf_begin() { /* Do nothing: just for symmetry of user code */ }
479 void sg_platf_end() {
480 unsigned int iterator;
482 xbt_dynar_foreach(sg_platf_postparse_cb_list, iterator, fun) {
487 void sg_platf_new_AS_begin(sg_platf_AS_cbarg_t AS)
489 if (!surf_parse_models_setup_already_called) {
490 /* Initialize the surf models. That must be done after we got all config, and before we need the models.
491 * That is, after the last <config> tag, if any, and before the first of cluster|peer|AS|trace|trace_connect
493 * I'm not sure for <trace> and <trace_connect>, there may be a bug here
494 * (FIXME: check it out by creating a file beginning with one of these tags)
495 * but cluster and peer create ASes internally, so putting the code in there is ok.
497 * TODO, There used to be a guard protecting here against
498 * xbt_dynar_length(sg_platf_AS_begin_cb_list) because we don't want to
499 * initialize the models if we are parsing the file to get the deployment.
500 * That could happen if the same file would be used for platf and deploy:
501 * it'd contain AS tags even during the deploy parsing. Removing that guard
502 * would result of the models to get re-inited when parsing for deploy.
503 * Currently using the same file for platform and deployment is broken
504 * however. This guard will have to ba adapted in order to make this feature
507 surf_parse_models_setup_already_called = 1;
508 surf_config_models_setup();
511 routing_AS_begin(AS);
512 if (TRACE_is_enabled())
513 sg_instr_AS_begin(AS);
516 void sg_platf_new_AS_end()
519 if (TRACE_is_enabled())
523 /* ***************************************** */
524 /* TUTORIAL: New TAG */
526 void sg_platf_new_gpu(sg_platf_gpu_cbarg_t gpu) {
527 unsigned int iterator;
529 xbt_dynar_foreach(sg_platf_gpu_cb_list, iterator, fun) {
534 void sg_platf_gpu_add_cb(sg_platf_gpu_cb_t fct) {
535 xbt_dynar_push(sg_platf_gpu_cb_list, &fct);
538 /* ***************************************** */
540 void sg_platf_link_add_cb(sg_platf_link_cb_t fct) {
541 xbt_dynar_push(sg_platf_link_cb_list, &fct);
543 void sg_platf_peer_add_cb(sg_platf_peer_cb_t fct) {
544 xbt_dynar_push(sg_platf_peer_cb_list, &fct);
546 void sg_platf_cluster_add_cb(sg_platf_cluster_cb_t fct) {
547 xbt_dynar_push(sg_platf_cluster_cb_list, &fct);
549 void sg_platf_cabinet_add_cb(sg_platf_cabinet_cb_t fct) {
550 xbt_dynar_push(sg_platf_cabinet_cb_list, &fct);
552 void sg_platf_postparse_add_cb(void_f_void_t fct) {
553 xbt_dynar_push(sg_platf_postparse_cb_list, &fct);
555 void sg_platf_route_add_cb(sg_platf_route_cb_t fct) {
556 xbt_dynar_push(sg_platf_route_cb_list, &fct);
558 void sg_platf_ASroute_add_cb(sg_platf_route_cb_t fct) {
559 xbt_dynar_push(sg_platf_ASroute_cb_list, &fct);
561 void sg_platf_bypassRoute_add_cb(sg_platf_route_cb_t fct) {
562 xbt_dynar_push(sg_platf_bypassRoute_cb_list, &fct);
564 void sg_platf_bypassASroute_add_cb(sg_platf_route_cb_t fct) {
565 xbt_dynar_push(sg_platf_bypassASroute_cb_list, &fct);
567 void sg_platf_prop_add_cb(sg_platf_prop_cb_t fct) {
568 xbt_dynar_push(sg_platf_prop_cb_list, &fct);
570 void sg_platf_trace_add_cb(sg_platf_trace_cb_t fct) {
571 xbt_dynar_push(sg_platf_trace_cb_list, &fct);
573 void sg_platf_trace_connect_add_cb(sg_platf_trace_connect_cb_t fct) {
574 xbt_dynar_push(sg_platf_trace_connect_cb_list, &fct);
576 void sg_platf_rng_stream_init(unsigned long seed[6]) {
577 RngStream_SetPackageSeed(seed);
578 sg_platf_rng_stream = RngStream_CreateStream(NULL);
581 RngStream sg_platf_rng_stream_get(const char* id) {
582 RngStream stream = NULL;
583 unsigned int id_hash;
585 stream = RngStream_CopyStream(sg_platf_rng_stream);
586 id_hash = xbt_str_hash(id);
587 RngStream_AdvanceState(stream, 0, (long)id_hash);