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"
15 #include "src/simix/smx_private.h"
17 #include "cpu_interface.hpp"
18 #include "host_interface.hpp"
20 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(surf_parse);
21 xbt_dynar_t sg_platf_host_cb_list = NULL; // of sg_platf_host_cb_t
22 xbt_dynar_t sg_platf_host_link_cb_list = NULL; // of sg_platf_host_link_cb_t
23 xbt_dynar_t sg_platf_link_cb_list = NULL; // of sg_platf_link_cb_t
24 xbt_dynar_t sg_platf_router_cb_list = NULL; // of sg_platf_router_cb_t
25 xbt_dynar_t sg_platf_peer_cb_list = NULL; // of sg_platf_peer_cb_t
26 xbt_dynar_t sg_platf_cluster_cb_list = NULL; // of sg_platf_cluster_cb_t
27 xbt_dynar_t sg_platf_cabinet_cb_list = NULL; // of sg_platf_cluster_cb_t
28 xbt_dynar_t sg_platf_AS_begin_cb_list = NULL; //of sg_platf_AS_begin_cb_t
29 xbt_dynar_t sg_platf_AS_end_cb_list = NULL; //of void_f_void_t
30 xbt_dynar_t sg_platf_postparse_cb_list = NULL; // of void_f_void_t
31 xbt_dynar_t sg_platf_prop_cb_list = NULL; // of sg_platf_prop_cb_t
33 xbt_dynar_t sg_platf_route_cb_list = NULL; // of sg_platf_route_cb_t
34 xbt_dynar_t sg_platf_ASroute_cb_list = NULL; // of sg_platf_ASroute_cb_t
35 xbt_dynar_t sg_platf_bypassRoute_cb_list = NULL; // of sg_platf_bypassRoute_cb_t
36 xbt_dynar_t sg_platf_bypassASroute_cb_list = NULL; // of sg_platf_bypassASroute_cb_t
38 xbt_dynar_t sg_platf_trace_cb_list = NULL;
39 xbt_dynar_t sg_platf_trace_connect_cb_list = NULL;
41 xbt_dynar_t sg_platf_mount_cb_list = NULL; // of sg_platf_storage_cb_t
43 /* ***************************************** */
44 /* TUTORIAL: New TAG */
46 xbt_dynar_t sg_platf_gpu_cb_list = NULL;
47 /* ***************************************** */
50 static int surf_parse_models_setup_already_called = 0;
52 /* one RngStream for the platform, to respect some statistic rules */
53 static RngStream sg_platf_rng_stream = NULL;
55 /** Module management function: creates all internal data structures */
56 void sg_platf_init(void) {
58 //FIXME : Ugly, but useful...
59 if(sg_platf_host_cb_list)
60 return; //Already initialized, so do nothing...
62 sg_platf_host_cb_list = xbt_dynar_new(sizeof(sg_platf_host_cb_t), NULL);
63 sg_platf_host_link_cb_list = xbt_dynar_new(sizeof(sg_platf_host_link_cb_t), NULL);
64 sg_platf_router_cb_list = xbt_dynar_new(sizeof(sg_platf_router_cb_t), NULL);
65 sg_platf_link_cb_list = xbt_dynar_new(sizeof(sg_platf_link_cb_t), NULL);
66 sg_platf_peer_cb_list = xbt_dynar_new(sizeof(sg_platf_peer_cb_t), NULL);
67 sg_platf_cluster_cb_list = xbt_dynar_new(sizeof(sg_platf_cluster_cb_t), NULL);
68 sg_platf_cabinet_cb_list = xbt_dynar_new(sizeof(sg_platf_cabinet_cb_t), NULL);
69 sg_platf_postparse_cb_list = xbt_dynar_new(sizeof(sg_platf_link_cb_t),NULL);
70 sg_platf_AS_begin_cb_list = xbt_dynar_new(sizeof(sg_platf_AS_cb_t),NULL);
71 sg_platf_AS_end_cb_list = xbt_dynar_new(sizeof(sg_platf_AS_cb_t),NULL);
72 sg_platf_prop_cb_list = xbt_dynar_new(sizeof(sg_platf_prop_cb_t),NULL);
74 sg_platf_route_cb_list = xbt_dynar_new(sizeof(sg_platf_route_cb_t), NULL);
75 sg_platf_ASroute_cb_list = xbt_dynar_new(sizeof(sg_platf_route_cb_t), NULL);
76 sg_platf_bypassRoute_cb_list = xbt_dynar_new(sizeof(sg_platf_route_cb_t), NULL);
77 sg_platf_bypassASroute_cb_list = xbt_dynar_new(sizeof(sg_platf_route_cb_t), NULL);
79 sg_platf_trace_cb_list = xbt_dynar_new(sizeof(sg_platf_trace_cb_t), NULL);
80 sg_platf_trace_connect_cb_list = xbt_dynar_new(sizeof(sg_platf_trace_connect_cb_t), NULL);
82 sg_platf_mount_cb_list = xbt_dynar_new(sizeof(sg_platf_storage_cb_t), NULL);
84 /* ***************************************** */
85 /* TUTORIAL: New TAG */
87 sg_platf_gpu_cb_list = xbt_dynar_new(sizeof(sg_platf_gpu_cb_t), NULL);
88 /* ***************************************** */
90 /** Module management function: frees all internal data structures */
91 void sg_platf_exit(void) {
92 xbt_dynar_free(&sg_platf_host_cb_list);
93 xbt_dynar_free(&sg_platf_host_link_cb_list);
94 xbt_dynar_free(&sg_platf_router_cb_list);
95 xbt_dynar_free(&sg_platf_link_cb_list);
96 xbt_dynar_free(&sg_platf_postparse_cb_list);
97 xbt_dynar_free(&sg_platf_peer_cb_list);
98 xbt_dynar_free(&sg_platf_cluster_cb_list);
99 xbt_dynar_free(&sg_platf_cabinet_cb_list);
100 xbt_dynar_free(&sg_platf_AS_begin_cb_list);
101 xbt_dynar_free(&sg_platf_AS_end_cb_list);
102 xbt_dynar_free(&sg_platf_prop_cb_list);
104 xbt_dynar_free(&sg_platf_trace_cb_list);
105 xbt_dynar_free(&sg_platf_trace_connect_cb_list);
107 xbt_dynar_free(&sg_platf_route_cb_list);
108 xbt_dynar_free(&sg_platf_ASroute_cb_list);
109 xbt_dynar_free(&sg_platf_bypassRoute_cb_list);
110 xbt_dynar_free(&sg_platf_bypassASroute_cb_list);
112 xbt_dynar_free(&sg_platf_mount_cb_list);
114 /* ***************************************** */
115 /* TUTORIAL: New TAG */
117 xbt_dynar_free(&sg_platf_gpu_cb_list);
119 /* ***************************************** */
121 /* make sure that we will reinit the models while loading the platf once reinited */
122 surf_parse_models_setup_already_called = 0;
125 void sg_platf_new_host(sg_platf_host_cbarg_t host)
128 xbt_assert(! sg_host_by_name(host->id),
129 "Refusing to create a second host named '%s'.", host->id);
131 RoutingEdge *net = NULL;
132 As* current_routing = routing_get_current();
134 net = routing_add_host(current_routing, host);
136 Cpu *cpu = surf_cpu_model_pm->createCpu(
146 surf_host_model->createHost(host->id, net, cpu);
148 unsigned int iterator;
149 sg_platf_host_cb_t fun;
150 xbt_dynar_foreach(sg_platf_host_cb_list, iterator, fun) {
154 void sg_platf_new_host_link(sg_platf_host_link_cbarg_t h){
155 unsigned int iterator;
156 sg_platf_host_link_cb_t fun;
157 xbt_dynar_foreach(sg_platf_host_link_cb_list, iterator, fun) {
161 void sg_platf_new_router(sg_platf_router_cbarg_t router) {
162 unsigned int iterator;
163 sg_platf_router_cb_t fun;
164 xbt_dynar_foreach(sg_platf_router_cb_list, iterator, fun) {
168 void sg_platf_new_link(sg_platf_link_cbarg_t link){
169 unsigned int iterator;
170 sg_platf_link_cb_t fun;
171 xbt_dynar_foreach(sg_platf_link_cb_list, iterator, fun) {
176 void sg_platf_new_peer(sg_platf_peer_cbarg_t peer){
177 unsigned int iterator;
178 sg_platf_peer_cb_t fun;
179 xbt_dynar_foreach(sg_platf_peer_cb_list, iterator, fun) {
183 void sg_platf_new_cluster(sg_platf_cluster_cbarg_t cluster){
184 unsigned int iterator;
185 sg_platf_cluster_cb_t fun;
186 xbt_dynar_foreach(sg_platf_cluster_cb_list, iterator, fun) {
190 void sg_platf_new_cabinet(sg_platf_cabinet_cbarg_t cabinet){
191 unsigned int iterator;
192 sg_platf_cabinet_cb_t fun;
193 xbt_dynar_foreach(sg_platf_cabinet_cb_list, iterator, fun) {
197 void sg_platf_new_storage(sg_platf_storage_cbarg_t storage)
199 xbt_assert(!xbt_lib_get_or_null(storage_lib, storage->id,ROUTING_STORAGE_LEVEL),
200 "Reading a storage, processing unit \"%s\" already exists", storage->id);
202 // Verification of an existing type_id
204 void* storage_type = xbt_lib_get_or_null(storage_type_lib, storage->type_id,ROUTING_STORAGE_TYPE_LEVEL);
206 xbt_assert(storage_type,"Reading a storage, type id \"%s\" does not exists", storage->type_id);
208 XBT_DEBUG("ROUTING Create a storage name '%s' with type_id '%s' and content '%s'",
213 xbt_lib_set(storage_lib,
215 ROUTING_STORAGE_LEVEL,
216 (void *) xbt_strdup(storage->type_id));
218 void* stype = xbt_lib_get_or_null(storage_type_lib,
220 ROUTING_STORAGE_TYPE_LEVEL);
221 if(!stype) xbt_die("No storage type '%s'",storage->type_id);
223 // if storage content is not specified use the content of storage_type if exist
224 if(!strcmp(storage->content,"") && strcmp(((storage_type_t) stype)->content,"")){
225 storage->content = ((storage_type_t) stype)->content;
226 storage->content_type = ((storage_type_t) stype)->content_type;
227 XBT_DEBUG("For disk '%s' content is empty, inherit the content (of type %s) from storage type '%s' ",
228 storage->id,((storage_type_t) stype)->content_type,
229 ((storage_type_t) stype)->type_id);
232 XBT_DEBUG("SURF storage create resource\n\t\tid '%s'\n\t\ttype '%s' "
233 "\n\t\tmodel '%s' \n\t\tcontent '%s'\n\t\tcontent_type '%s' "
234 "\n\t\tproperties '%p''\n",
236 ((storage_type_t) stype)->model,
237 ((storage_type_t) stype)->type_id,
239 storage->content_type,
240 storage->properties);
242 surf_storage_model->createStorage(storage->id,
243 ((storage_type_t) stype)->type_id,
245 storage->content_type,
249 void sg_platf_new_storage_type(sg_platf_storage_type_cbarg_t storage_type){
251 xbt_assert(!xbt_lib_get_or_null(storage_type_lib, storage_type->id,ROUTING_STORAGE_TYPE_LEVEL),
252 "Reading a storage type, processing unit \"%s\" already exists", storage_type->id);
254 storage_type_t stype = xbt_new0(s_storage_type_t, 1);
255 stype->model = xbt_strdup(storage_type->model);
256 stype->properties = storage_type->properties;
257 stype->content = xbt_strdup(storage_type->content);
258 stype->content_type = xbt_strdup(storage_type->content_type);
259 stype->type_id = xbt_strdup(storage_type->id);
260 stype->size = storage_type->size;
261 stype->model_properties = storage_type->model_properties;
263 XBT_DEBUG("ROUTING Create a storage type id '%s' with model '%s', "
264 "content '%s', and content_type '%s'",
267 storage_type->content,
268 storage_type->content_type);
270 xbt_lib_set(storage_type_lib,
272 ROUTING_STORAGE_TYPE_LEVEL,
275 void sg_platf_new_mstorage(sg_platf_mstorage_cbarg_t mstorage)
278 // mount_t mnt = xbt_new0(s_mount_t, 1);
279 // mnt->id = xbt_strdup(mstorage->type_id);
280 // mnt->name = xbt_strdup(mstorage->name);
283 // XBT_DEBUG("Creata a Mount list for %s",A_surfxml_host_id);
284 // mount_list = xbt_dynar_new(sizeof(char *), NULL);
286 // xbt_dynar_push(mount_list,(void *) mnt);
290 // XBT_DEBUG("ROUTING Mount a storage name '%s' with type_id '%s'",mstorage->name, mstorage->id);
292 void sg_platf_new_mount(sg_platf_mount_cbarg_t mount){
293 unsigned int iterator;
294 sg_platf_mount_cb_t fun;
295 xbt_dynar_foreach(sg_platf_mount_cb_list, iterator, fun) {
299 void sg_platf_new_route(sg_platf_route_cbarg_t route) {
300 unsigned int iterator;
301 sg_platf_route_cb_t fun;
302 xbt_dynar_foreach(sg_platf_route_cb_list, iterator, fun) {
306 void sg_platf_new_ASroute(sg_platf_route_cbarg_t ASroute) {
307 unsigned int iterator;
308 sg_platf_route_cb_t fun;
309 xbt_dynar_foreach(sg_platf_ASroute_cb_list, iterator, fun) {
313 void sg_platf_new_bypassRoute(sg_platf_route_cbarg_t bypassRoute) {
314 unsigned int iterator;
315 sg_platf_route_cb_t fun;
316 xbt_dynar_foreach(sg_platf_bypassRoute_cb_list, iterator, fun) {
320 void sg_platf_new_bypassASroute(sg_platf_route_cbarg_t bypassASroute) {
321 unsigned int iterator;
322 sg_platf_route_cb_t fun;
323 xbt_dynar_foreach(sg_platf_bypassASroute_cb_list, iterator, fun) {
327 void sg_platf_new_prop(sg_platf_prop_cbarg_t prop) {
328 unsigned int iterator;
329 sg_platf_prop_cb_t fun;
330 xbt_dynar_foreach(sg_platf_prop_cb_list, iterator, fun) {
334 void sg_platf_new_trace(sg_platf_trace_cbarg_t trace) {
335 unsigned int iterator;
336 sg_platf_trace_cb_t fun;
337 xbt_dynar_foreach(sg_platf_trace_cb_list, iterator, fun) {
341 void sg_platf_trace_connect(sg_platf_trace_connect_cbarg_t trace_connect) {
342 unsigned int iterator;
343 sg_platf_trace_connect_cb_t fun;
344 xbt_dynar_foreach(sg_platf_trace_connect_cb_list, iterator, fun) {
349 void sg_platf_new_process(sg_platf_process_cbarg_t process)
352 xbt_die("Cannot create process without SIMIX.");
354 sg_host_t host = sg_host_by_name(process->host);
356 THROWF(arg_error, 0, "Host '%s' unknown", process->host);
357 xbt_main_func_t parse_code = SIMIX_get_registered_function(process->function);
358 xbt_assert(parse_code, "Function '%s' unknown", process->function);
360 double start_time = process->start_time;
361 double kill_time = process->kill_time;
362 int auto_restart = process->on_failure == SURF_PROCESS_ON_FAILURE_DIE ? 0 : 1;
364 smx_process_arg_t arg = NULL;
365 smx_process_t process_created = NULL;
367 arg = xbt_new0(s_smx_process_arg_t, 1);
368 arg->code = parse_code;
370 arg->hostname = sg_host_get_name(host);
371 arg->argc = process->argc;
372 arg->argv = xbt_new(char *,process->argc);
374 for (i=0; i<process->argc; i++)
375 arg->argv[i] = xbt_strdup(process->argv[i]);
376 arg->name = xbt_strdup(arg->argv[0]);
377 arg->kill_time = kill_time;
378 arg->properties = current_property_set;
379 if (!sg_host_simix(host)->boot_processes) {
380 sg_host_simix(host)->boot_processes = xbt_dynar_new(sizeof(smx_process_arg_t), _SIMIX_host_free_process_arg);
382 xbt_dynar_push_as(sg_host_simix(host)->boot_processes,smx_process_arg_t,arg);
384 if (start_time > SIMIX_get_clock()) {
385 arg = xbt_new0(s_smx_process_arg_t, 1);
386 arg->name = (char*)(process->argv)[0];
387 arg->code = parse_code;
389 arg->hostname = sg_host_get_name(host);
390 arg->argc = process->argc;
391 arg->argv = (char**)(process->argv);
392 arg->kill_time = kill_time;
393 arg->properties = current_property_set;
395 XBT_DEBUG("Process %s(%s) will be started at time %f", arg->name,
396 arg->hostname, start_time);
397 SIMIX_timer_set(start_time, [](void* arg) {
398 SIMIX_process_create_from_wrapper((smx_process_arg_t) arg);
400 } else { // start_time <= SIMIX_get_clock()
401 XBT_DEBUG("Starting Process %s(%s) right now", process->argv[0], sg_host_get_name(host));
403 if (simix_global->create_process_function)
404 process_created = simix_global->create_process_function(
405 (char*)(process->argv)[0],
408 sg_host_get_name(host),
411 (char**)(process->argv),
412 current_property_set,
415 process_created = simcall_process_create((char*)(process->argv)[0], parse_code, NULL, sg_host_get_name(host), kill_time, process->argc,
416 (char**)process->argv, current_property_set,auto_restart);
418 /* verify if process has been created (won't be the case if the host is currently dead, but that's fine) */
419 if (!process_created) {
423 current_property_set = NULL;
426 void sg_platf_route_begin (sg_platf_route_cbarg_t route){
427 route->link_list = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
429 void sg_platf_ASroute_begin (sg_platf_route_cbarg_t ASroute){
430 ASroute->link_list = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
433 void sg_platf_route_end (sg_platf_route_cbarg_t route){
434 sg_platf_new_route(route);
436 void sg_platf_ASroute_end (sg_platf_route_cbarg_t ASroute){
437 sg_platf_new_ASroute(ASroute);
440 void sg_platf_route_add_link (const char* link_id, sg_platf_route_cbarg_t route){
441 char *link_name = xbt_strdup(link_id);
442 xbt_dynar_push(route->link_list, &link_name);
444 void sg_platf_ASroute_add_link (const char* link_id, sg_platf_route_cbarg_t ASroute){
445 char *link_name = xbt_strdup(link_id);
446 xbt_dynar_push(ASroute->link_list, &link_name);
449 void sg_platf_begin() { /* Do nothing: just for symmetry of user code */ }
451 void sg_platf_end() {
452 unsigned int iterator;
454 xbt_dynar_foreach(sg_platf_postparse_cb_list, iterator, fun) {
459 void sg_platf_new_AS_begin(sg_platf_AS_cbarg_t AS) {
460 unsigned int iterator;
461 sg_platf_AS_cb_t fun;
463 if (!surf_parse_models_setup_already_called && !xbt_dynar_is_empty(sg_platf_AS_begin_cb_list)) {
464 /* Initialize the surf models. That must be done after we got all config, and before we need the models.
465 * That is, after the last <config> tag, if any, and before the first of cluster|peer|AS|trace|trace_connect
467 * I'm not sure for <trace> and <trace_connect>, there may be a bug here
468 * (FIXME: check it out by creating a file beginning with one of these tags)
469 * but cluster and peer create ASes internally, so putting the code in there is ok.
471 * We are also guarding against xbt_dynar_length(sg_platf_AS_begin_cb_list) because we don't
472 * want to initialize the models if we are parsing the file to get the deployment. That could happen if
473 * the same file would be used for platf and deploy: it'd contain AS tags even during the deploy parsing.
474 * Removing that guard would result of the models to get re-inited when parsing for deploy.
476 surf_parse_models_setup_already_called = 1;
477 surf_config_models_setup();
480 xbt_dynar_foreach(sg_platf_AS_begin_cb_list, iterator, fun) {
485 void sg_platf_new_AS_end() {
486 unsigned int iterator;
488 xbt_dynar_foreach(sg_platf_AS_end_cb_list, iterator, fun) {
493 /* ***************************************** */
494 /* TUTORIAL: New TAG */
496 void sg_platf_new_gpu(sg_platf_gpu_cbarg_t gpu) {
497 unsigned int iterator;
499 xbt_dynar_foreach(sg_platf_gpu_cb_list, iterator, fun) {
504 void sg_platf_gpu_add_cb(sg_platf_gpu_cb_t fct) {
505 xbt_dynar_push(sg_platf_gpu_cb_list, &fct);
508 /* ***************************************** */
511 void sg_platf_host_add_cb(sg_platf_host_cb_t fct) {
512 xbt_dynar_push(sg_platf_host_cb_list, &fct);
514 void sg_platf_host_link_add_cb(sg_platf_host_link_cb_t fct) {
515 xbt_dynar_push(sg_platf_host_link_cb_list, &fct);
517 void sg_platf_link_add_cb(sg_platf_link_cb_t fct) {
518 xbt_dynar_push(sg_platf_link_cb_list, &fct);
520 void sg_platf_router_add_cb(sg_platf_router_cb_t fct) {
521 xbt_dynar_push(sg_platf_router_cb_list, &fct);
523 void sg_platf_peer_add_cb(sg_platf_peer_cb_t fct) {
524 xbt_dynar_push(sg_platf_peer_cb_list, &fct);
526 void sg_platf_cluster_add_cb(sg_platf_cluster_cb_t fct) {
527 xbt_dynar_push(sg_platf_cluster_cb_list, &fct);
529 void sg_platf_cabinet_add_cb(sg_platf_cabinet_cb_t fct) {
530 xbt_dynar_push(sg_platf_cabinet_cb_list, &fct);
532 void sg_platf_postparse_add_cb(void_f_void_t fct) {
533 xbt_dynar_push(sg_platf_postparse_cb_list, &fct);
535 void sg_platf_AS_begin_add_cb(sg_platf_AS_cb_t fct) {
536 xbt_dynar_push(sg_platf_AS_begin_cb_list, &fct);
538 void sg_platf_AS_end_add_cb(sg_platf_AS_cb_t fct) {
539 xbt_dynar_push(sg_platf_AS_end_cb_list, &fct);
541 void sg_platf_mount_add_cb(sg_platf_mount_cb_t fct) {
542 xbt_dynar_push(sg_platf_mount_cb_list, &fct);
544 void sg_platf_route_add_cb(sg_platf_route_cb_t fct) {
545 xbt_dynar_push(sg_platf_route_cb_list, &fct);
547 void sg_platf_ASroute_add_cb(sg_platf_route_cb_t fct) {
548 xbt_dynar_push(sg_platf_ASroute_cb_list, &fct);
550 void sg_platf_bypassRoute_add_cb(sg_platf_route_cb_t fct) {
551 xbt_dynar_push(sg_platf_bypassRoute_cb_list, &fct);
553 void sg_platf_bypassASroute_add_cb(sg_platf_route_cb_t fct) {
554 xbt_dynar_push(sg_platf_bypassASroute_cb_list, &fct);
556 void sg_platf_prop_add_cb(sg_platf_prop_cb_t fct) {
557 xbt_dynar_push(sg_platf_prop_cb_list, &fct);
559 void sg_platf_trace_add_cb(sg_platf_trace_cb_t fct) {
560 xbt_dynar_push(sg_platf_trace_cb_list, &fct);
562 void sg_platf_trace_connect_add_cb(sg_platf_trace_connect_cb_t fct) {
563 xbt_dynar_push(sg_platf_trace_connect_cb_list, &fct);
565 void sg_platf_rng_stream_init(unsigned long seed[6]) {
566 RngStream_SetPackageSeed(seed);
567 sg_platf_rng_stream = RngStream_CreateStream(NULL);
570 RngStream sg_platf_rng_stream_get(const char* id) {
571 RngStream stream = NULL;
572 unsigned int id_hash;
574 stream = RngStream_CopyStream(sg_platf_rng_stream);
575 id_hash = xbt_str_hash(id);
576 RngStream_AdvanceState(stream, 0, (long)id_hash);