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 xbt_dynar_t sg_platf_ASroute_cb_list = NULL; // of sg_platf_ASroute_cb_t
27 xbt_dynar_t sg_platf_bypassRoute_cb_list = NULL; // of sg_platf_bypassRoute_cb_t
28 xbt_dynar_t sg_platf_bypassASroute_cb_list = NULL; // of sg_platf_bypassASroute_cb_t
30 /* ***************************************** */
31 /* TUTORIAL: New TAG */
33 xbt_dynar_t sg_platf_gpu_cb_list = NULL;
34 /* ***************************************** */
37 static int surf_parse_models_setup_already_called = 0;
39 /* one RngStream for the platform, to respect some statistic rules */
40 static RngStream sg_platf_rng_stream = NULL;
42 /** Module management function: creates all internal data structures */
43 void sg_platf_init(void) {
45 //FIXME : Ugly, but useful...
46 if (sg_platf_gpu_cb_list)
47 return; //Already initialized, so do nothing...
49 sg_platf_link_cb_list = xbt_dynar_new(sizeof(sg_platf_link_cb_t), NULL);
50 sg_platf_cluster_cb_list = xbt_dynar_new(sizeof(sg_platf_cluster_cb_t), NULL);
51 sg_platf_postparse_cb_list = xbt_dynar_new(sizeof(sg_platf_link_cb_t),NULL);
53 sg_platf_ASroute_cb_list = xbt_dynar_new(sizeof(sg_platf_route_cb_t), NULL);
54 sg_platf_bypassRoute_cb_list = xbt_dynar_new(sizeof(sg_platf_route_cb_t), NULL);
55 sg_platf_bypassASroute_cb_list = xbt_dynar_new(sizeof(sg_platf_route_cb_t), NULL);
57 /* ***************************************** */
58 /* TUTORIAL: New TAG */
60 sg_platf_gpu_cb_list = xbt_dynar_new(sizeof(sg_platf_gpu_cb_t), NULL);
61 /* ***************************************** */
63 /** Module management function: frees all internal data structures */
64 void sg_platf_exit(void) {
65 xbt_dynar_free(&sg_platf_link_cb_list);
66 xbt_dynar_free(&sg_platf_postparse_cb_list);
67 xbt_dynar_free(&sg_platf_cluster_cb_list);
69 xbt_dynar_free(&sg_platf_ASroute_cb_list);
70 xbt_dynar_free(&sg_platf_bypassRoute_cb_list);
71 xbt_dynar_free(&sg_platf_bypassASroute_cb_list);
73 /* ***************************************** */
74 /* TUTORIAL: New TAG */
76 xbt_dynar_free(&sg_platf_gpu_cb_list);
78 /* ***************************************** */
80 /* make sure that we will reinit the models while loading the platf once reinited */
81 surf_parse_models_setup_already_called = 0;
84 void sg_platf_new_host(sg_platf_host_cbarg_t host)
86 xbt_assert(! sg_host_by_name(host->id),
87 "Refusing to create a second host named '%s'.", host->id);
89 RoutingEdge *net = NULL;
90 As* current_routing = routing_get_current();
92 net = routing_add_host(current_routing, host);
94 Cpu *cpu = surf_cpu_model_pm->createCpu(
104 surf_host_model->createHost(host->id, net, cpu);
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 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 RoutingEdge *info = new RoutingEdgeImpl(xbt_strdup(router->id),
125 SURF_NETWORK_ELEMENT_ROUTER,
127 info->setId(current_routing->parsePU(info));
128 xbt_lib_set(as_router_lib, router->id, ROUTING_ASR_LEVEL, (void *) info);
129 XBT_DEBUG("Having set name '%s' id '%d'", router->id, info->getId());
130 routingEdgeCreatedCallbacks(info);
132 if (router->coord && strcmp(router->coord, "")) {
136 if (!COORD_ASR_LEVEL)
137 xbt_die ("To use host coordinates, please add --cfg=network/coordinates:yes to your command line");
138 /* Pre-parse the host coordinates */
139 xbt_dynar_t ctn_str = xbt_str_split_str(router->coord, " ");
140 xbt_dynar_t ctn = xbt_dynar_new(sizeof(double),NULL);
141 xbt_dynar_foreach(ctn_str,cursor, str) {
142 double val = atof(str);
143 xbt_dynar_push(ctn,&val);
145 xbt_dynar_shrink(ctn, 0);
146 xbt_dynar_free(&ctn_str);
147 xbt_lib_set(as_router_lib, router->id, COORD_ASR_LEVEL, (void *) ctn);
148 XBT_DEBUG("Having set router coordinates for '%s'",router->id);
151 if (TRACE_is_enabled())
152 sg_instr_new_router(router);
155 void sg_platf_new_link(sg_platf_link_cbarg_t link){
156 unsigned int iterator;
157 sg_platf_link_cb_t fun;
158 xbt_dynar_foreach(sg_platf_link_cb_list, iterator, fun) {
163 void sg_platf_new_cluster(sg_platf_cluster_cbarg_t cluster){
164 unsigned int iterator;
165 sg_platf_cluster_cb_t fun;
166 xbt_dynar_foreach(sg_platf_cluster_cb_list, iterator, fun) {
171 void sg_platf_new_storage(sg_platf_storage_cbarg_t storage)
173 xbt_assert(!xbt_lib_get_or_null(storage_lib, storage->id,ROUTING_STORAGE_LEVEL),
174 "Reading a storage, processing unit \"%s\" already exists", storage->id);
176 // Verification of an existing type_id
178 void* storage_type = xbt_lib_get_or_null(storage_type_lib, storage->type_id,ROUTING_STORAGE_TYPE_LEVEL);
180 xbt_assert(storage_type,"Reading a storage, type id \"%s\" does not exists", storage->type_id);
182 XBT_DEBUG("ROUTING Create a storage name '%s' with type_id '%s' and content '%s'",
187 xbt_lib_set(storage_lib,
189 ROUTING_STORAGE_LEVEL,
190 (void *) xbt_strdup(storage->type_id));
192 void* stype = xbt_lib_get_or_null(storage_type_lib,
194 ROUTING_STORAGE_TYPE_LEVEL);
195 if(!stype) xbt_die("No storage type '%s'",storage->type_id);
197 // if storage content is not specified use the content of storage_type if exist
198 if(!strcmp(storage->content,"") && strcmp(((storage_type_t) stype)->content,"")){
199 storage->content = ((storage_type_t) stype)->content;
200 storage->content_type = ((storage_type_t) stype)->content_type;
201 XBT_DEBUG("For disk '%s' content is empty, inherit the content (of type %s) from storage type '%s' ",
202 storage->id,((storage_type_t) stype)->content_type,
203 ((storage_type_t) stype)->type_id);
206 XBT_DEBUG("SURF storage create resource\n\t\tid '%s'\n\t\ttype '%s' "
207 "\n\t\tmodel '%s' \n\t\tcontent '%s'\n\t\tcontent_type '%s' "
208 "\n\t\tproperties '%p''\n",
210 ((storage_type_t) stype)->model,
211 ((storage_type_t) stype)->type_id,
213 storage->content_type,
214 storage->properties);
216 surf_storage_model->createStorage(storage->id,
217 ((storage_type_t) stype)->type_id,
219 storage->content_type,
223 void sg_platf_new_storage_type(sg_platf_storage_type_cbarg_t storage_type){
225 xbt_assert(!xbt_lib_get_or_null(storage_type_lib, storage_type->id,ROUTING_STORAGE_TYPE_LEVEL),
226 "Reading a storage type, processing unit \"%s\" already exists", storage_type->id);
228 storage_type_t stype = xbt_new0(s_storage_type_t, 1);
229 stype->model = xbt_strdup(storage_type->model);
230 stype->properties = storage_type->properties;
231 stype->content = xbt_strdup(storage_type->content);
232 stype->content_type = xbt_strdup(storage_type->content_type);
233 stype->type_id = xbt_strdup(storage_type->id);
234 stype->size = storage_type->size;
235 stype->model_properties = storage_type->model_properties;
237 XBT_DEBUG("ROUTING Create a storage type id '%s' with model '%s', "
238 "content '%s', and content_type '%s'",
241 storage_type->content,
242 storage_type->content_type);
244 xbt_lib_set(storage_type_lib,
246 ROUTING_STORAGE_TYPE_LEVEL,
249 void sg_platf_new_mstorage(sg_platf_mstorage_cbarg_t mstorage)
252 // mount_t mnt = xbt_new0(s_mount_t, 1);
253 // mnt->id = xbt_strdup(mstorage->type_id);
254 // mnt->name = xbt_strdup(mstorage->name);
257 // XBT_DEBUG("Creata a Mount list for %s",A_surfxml_host_id);
258 // mount_list = xbt_dynar_new(sizeof(char *), NULL);
260 // xbt_dynar_push(mount_list,(void *) mnt);
264 // XBT_DEBUG("ROUTING Mount a storage name '%s' with type_id '%s'",mstorage->name, mstorage->id);
267 static void mount_free(void *p)
269 mount_t mnt = (mount_t) p;
273 void sg_platf_new_mount(sg_platf_mount_cbarg_t mount){
274 // Verification of an existing storage
276 void* storage = xbt_lib_get_or_null(storage_lib, mount->storageId, ROUTING_STORAGE_LEVEL);
278 xbt_assert(storage,"Disk id \"%s\" does not exists", mount->storageId);
280 XBT_DEBUG("ROUTING Mount '%s' on '%s'",mount->storageId, mount->name);
283 mnt.storage = surf_storage_resource_priv(surf_storage_resource_by_name(mount->storageId));
284 mnt.name = xbt_strdup(mount->name);
287 XBT_DEBUG("Create a Mount list for %s",A_surfxml_host_id);
288 mount_list = xbt_dynar_new(sizeof(s_mount_t), mount_free);
290 xbt_dynar_push(mount_list, &mnt);
293 void sg_platf_new_route(sg_platf_route_cbarg_t route)
295 routing_get_current()->parseRoute(route);
298 void sg_platf_new_ASroute(sg_platf_route_cbarg_t ASroute) {
299 unsigned int iterator;
300 sg_platf_route_cb_t fun;
301 xbt_dynar_foreach(sg_platf_ASroute_cb_list, iterator, fun) {
305 void sg_platf_new_bypassRoute(sg_platf_route_cbarg_t bypassRoute) {
306 unsigned int iterator;
307 sg_platf_route_cb_t fun;
308 xbt_dynar_foreach(sg_platf_bypassRoute_cb_list, iterator, fun) {
312 void sg_platf_new_bypassASroute(sg_platf_route_cbarg_t bypassASroute) {
313 unsigned int iterator;
314 sg_platf_route_cb_t fun;
315 xbt_dynar_foreach(sg_platf_bypassASroute_cb_list, iterator, fun) {
320 void sg_platf_new_process(sg_platf_process_cbarg_t process)
323 xbt_die("Cannot create process without SIMIX.");
325 sg_host_t host = sg_host_by_name(process->host);
327 THROWF(arg_error, 0, "Host '%s' unknown", process->host);
328 xbt_main_func_t parse_code = SIMIX_get_registered_function(process->function);
329 xbt_assert(parse_code, "Function '%s' unknown", process->function);
331 double start_time = process->start_time;
332 double kill_time = process->kill_time;
333 int auto_restart = process->on_failure == SURF_PROCESS_ON_FAILURE_DIE ? 0 : 1;
335 smx_process_arg_t arg = NULL;
336 smx_process_t process_created = NULL;
338 arg = xbt_new0(s_smx_process_arg_t, 1);
339 arg->code = parse_code;
341 arg->hostname = sg_host_get_name(host);
342 arg->argc = process->argc;
343 arg->argv = xbt_new(char *,process->argc);
345 for (i=0; i<process->argc; i++)
346 arg->argv[i] = xbt_strdup(process->argv[i]);
347 arg->name = xbt_strdup(arg->argv[0]);
348 arg->kill_time = kill_time;
349 arg->properties = current_property_set;
350 if (!sg_host_simix(host)->boot_processes) {
351 sg_host_simix(host)->boot_processes = xbt_dynar_new(sizeof(smx_process_arg_t), _SIMIX_host_free_process_arg);
353 xbt_dynar_push_as(sg_host_simix(host)->boot_processes,smx_process_arg_t,arg);
355 if (start_time > SIMIX_get_clock()) {
356 arg = xbt_new0(s_smx_process_arg_t, 1);
357 arg->name = (char*)(process->argv)[0];
358 arg->code = parse_code;
360 arg->hostname = sg_host_get_name(host);
361 arg->argc = process->argc;
362 arg->argv = (char**)(process->argv);
363 arg->kill_time = kill_time;
364 arg->properties = current_property_set;
366 XBT_DEBUG("Process %s(%s) will be started at time %f", arg->name,
367 arg->hostname, start_time);
368 SIMIX_timer_set(start_time, [](void* arg) {
369 SIMIX_process_create_from_wrapper((smx_process_arg_t) arg);
371 } else { // start_time <= SIMIX_get_clock()
372 XBT_DEBUG("Starting Process %s(%s) right now", process->argv[0], sg_host_get_name(host));
374 if (simix_global->create_process_function)
375 process_created = simix_global->create_process_function(
376 (char*)(process->argv)[0],
379 sg_host_get_name(host),
382 (char**)(process->argv),
383 current_property_set,
386 process_created = simcall_process_create((char*)(process->argv)[0], parse_code, NULL, sg_host_get_name(host), kill_time, process->argc,
387 (char**)process->argv, current_property_set,auto_restart);
389 /* verify if process has been created (won't be the case if the host is currently dead, but that's fine) */
390 if (!process_created) {
394 current_property_set = NULL;
397 void sg_platf_route_begin (sg_platf_route_cbarg_t route){
398 route->link_list = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
400 void sg_platf_ASroute_begin (sg_platf_route_cbarg_t ASroute){
401 ASroute->link_list = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
404 void sg_platf_route_end (sg_platf_route_cbarg_t route){
405 sg_platf_new_route(route);
407 void sg_platf_ASroute_end (sg_platf_route_cbarg_t ASroute){
408 sg_platf_new_ASroute(ASroute);
411 void sg_platf_route_add_link (const char* link_id, sg_platf_route_cbarg_t route){
412 char *link_name = xbt_strdup(link_id);
413 xbt_dynar_push(route->link_list, &link_name);
415 void sg_platf_ASroute_add_link (const char* link_id, sg_platf_route_cbarg_t ASroute){
416 char *link_name = xbt_strdup(link_id);
417 xbt_dynar_push(ASroute->link_list, &link_name);
420 void sg_platf_begin() { /* Do nothing: just for symmetry of user code */ }
422 void sg_platf_end() {
423 unsigned int iterator;
425 xbt_dynar_foreach(sg_platf_postparse_cb_list, iterator, fun) {
430 void sg_platf_new_AS_begin(sg_platf_AS_cbarg_t AS)
432 if (!surf_parse_models_setup_already_called) {
433 /* Initialize the surf models. That must be done after we got all config, and before we need the models.
434 * That is, after the last <config> tag, if any, and before the first of cluster|peer|AS|trace|trace_connect
436 * I'm not sure for <trace> and <trace_connect>, there may be a bug here
437 * (FIXME: check it out by creating a file beginning with one of these tags)
438 * but cluster and peer create ASes internally, so putting the code in there is ok.
440 * TODO, There used to be a guard protecting here against
441 * xbt_dynar_length(sg_platf_AS_begin_cb_list) because we don't want to
442 * initialize the models if we are parsing the file to get the deployment.
443 * That could happen if the same file would be used for platf and deploy:
444 * it'd contain AS tags even during the deploy parsing. Removing that guard
445 * would result of the models to get re-inited when parsing for deploy.
446 * Currently using the same file for platform and deployment is broken
447 * however. This guard will have to ba adapted in order to make this feature
450 surf_parse_models_setup_already_called = 1;
451 surf_config_models_setup();
454 routing_AS_begin(AS);
455 if (TRACE_is_enabled())
456 sg_instr_AS_begin(AS);
459 void sg_platf_new_AS_end()
462 if (TRACE_is_enabled())
466 /* ***************************************** */
467 /* TUTORIAL: New TAG */
469 void sg_platf_new_gpu(sg_platf_gpu_cbarg_t gpu) {
470 unsigned int iterator;
472 xbt_dynar_foreach(sg_platf_gpu_cb_list, iterator, fun) {
477 void sg_platf_gpu_add_cb(sg_platf_gpu_cb_t fct) {
478 xbt_dynar_push(sg_platf_gpu_cb_list, &fct);
481 /* ***************************************** */
483 void sg_platf_link_add_cb(sg_platf_link_cb_t fct) {
484 xbt_dynar_push(sg_platf_link_cb_list, &fct);
486 void sg_platf_cluster_add_cb(sg_platf_cluster_cb_t fct) {
487 xbt_dynar_push(sg_platf_cluster_cb_list, &fct);
489 void sg_platf_postparse_add_cb(void_f_void_t fct) {
490 xbt_dynar_push(sg_platf_postparse_cb_list, &fct);
492 void sg_platf_ASroute_add_cb(sg_platf_route_cb_t fct) {
493 xbt_dynar_push(sg_platf_ASroute_cb_list, &fct);
495 void sg_platf_bypassRoute_add_cb(sg_platf_route_cb_t fct) {
496 xbt_dynar_push(sg_platf_bypassRoute_cb_list, &fct);
498 void sg_platf_bypassASroute_add_cb(sg_platf_route_cb_t fct) {
499 xbt_dynar_push(sg_platf_bypassASroute_cb_list, &fct);
501 void sg_platf_rng_stream_init(unsigned long seed[6]) {
502 RngStream_SetPackageSeed(seed);
503 sg_platf_rng_stream = RngStream_CreateStream(NULL);
506 RngStream sg_platf_rng_stream_get(const char* id) {
507 RngStream stream = NULL;
508 unsigned int id_hash;
510 stream = RngStream_CopyStream(sg_platf_rng_stream);
511 id_hash = xbt_str_hash(id);
512 RngStream_AdvanceState(stream, 0, (long)id_hash);