From db9bd966f46b6910fe8fed995efbdbefc8c906d7 Mon Sep 17 00:00:00 2001 From: Gabriel Corona Date: Mon, 30 Nov 2015 14:15:47 +0100 Subject: [PATCH] [surf] Expose (and use) hostCreatedCallbacks in C --- src/bindings/lua/lua_platf.c | 17 ----------------- src/msg/msg_environment.c | 5 ----- src/msg/msg_global.c | 8 +++++++- src/simgrid/host.cpp | 3 ++- src/simix/smx_environment.c | 11 ++--------- src/simix/smx_global.c | 3 +++ src/simix/smx_host.c | 3 +-- src/simix/smx_host_private.h | 2 +- src/simix/smx_vm.c | 6 +++--- src/surf/callbacks.cpp | 21 +++++++++++++++++++++ src/surf/callbacks.h | 24 ++++++++++++++++++++++++ tools/cmake/DefinePackages.cmake | 2 ++ 12 files changed, 66 insertions(+), 39 deletions(-) create mode 100644 src/surf/callbacks.cpp create mode 100644 src/surf/callbacks.h diff --git a/src/bindings/lua/lua_platf.c b/src/bindings/lua/lua_platf.c index 79f81fbd77..f0bb012f98 100644 --- a/src/bindings/lua/lua_platf.c +++ b/src/bindings/lua/lua_platf.c @@ -56,23 +56,6 @@ int console_open(lua_State *L) { int console_close(lua_State *L) { sg_platf_end(); sg_platf_exit(); - - xbt_lib_cursor_t cursor; - void **data; - char *name; - - /* Initialize MSG and WKS hosts */ - XBT_DEBUG("Initialize MSG and WKS hosts"); - xbt_lib_foreach(host_lib, cursor, name, data) { - if(data[SURF_HOST_LEVEL]){ - XBT_DEBUG("\tSee surf host %s",name); - SIMIX_host_create(name); - // THIS IS BRAINDEAD. There is no sg_host_t in that level, but a smx_host_priv. So commenting out for now. - // Lua is broken anyway. Christian will fix it - // __MSG_host_create((sg_host_t)data[SIMIX_HOST_LEVEL]); - } - } - return 0; } diff --git a/src/msg/msg_environment.c b/src/msg/msg_environment.c index fcff8013a0..4fa76e9189 100644 --- a/src/msg/msg_environment.c +++ b/src/msg/msg_environment.c @@ -42,11 +42,6 @@ void MSG_post_create_environment(void) { void **data; char *name; - /* Initialize MSG hosts */ - xbt_lib_foreach(host_lib, cursor, name, data) { - __MSG_host_create(xbt_dict_cursor_get_elm(cursor)); - } - /* Initialize MSG storages */ xbt_lib_foreach(storage_lib, cursor, name, data) { if(data[SIMIX_STORAGE_LEVEL]) diff --git a/src/msg/msg_global.c b/src/msg/msg_global.c index 98e0c95681..99e62cb3b0 100644 --- a/src/msg/msg_global.c +++ b/src/msg/msg_global.c @@ -14,7 +14,7 @@ #include "xbt/ex.h" /* ex_backtrace_display */ #include "xbt/replay.h" #include "simgrid/sg_config.h" /* Configuration mechanism of SimGrid */ - +#include "src/surf/callbacks.h" XBT_LOG_NEW_CATEGORY(msg, "All MSG categories"); XBT_LOG_NEW_DEFAULT_SUBCATEGORY(msg_kernel, msg, @@ -30,6 +30,11 @@ static void _sg_cfg_cb_msg_debug_multiple_use(const char *name, int pos) msg_global->debug_multiple_use = xbt_cfg_get_boolean(_sg_cfg_set, name); } +static void MSG_host_create_(sg_host_t host) +{ + __MSG_host_create(host); +} + /** * \ingroup msg_simulation * \brief Initialize MSG with less verifications @@ -62,6 +67,7 @@ void MSG_init_nocheck(int *argc, char **argv) { SIMIX_function_register_process_cleanup(MSG_process_cleanup_from_SIMIX); sg_platf_postparse_add_cb(MSG_post_create_environment); + surf_host_created_callback(MSG_host_create_); } if(MC_is_active()){ diff --git a/src/simgrid/host.cpp b/src/simgrid/host.cpp index d22a8f3e11..c8f1907b09 100644 --- a/src/simgrid/host.cpp +++ b/src/simgrid/host.cpp @@ -103,7 +103,8 @@ smx_host_priv_t sg_host_simix(sg_host_t host){ return (smx_host_priv_t) xbt_lib_get_level(host, SIMIX_HOST_LEVEL); } void sg_host_simix_set(sg_host_t host, smx_host_priv_t smx_host) { - xbt_lib_set(host_lib,host->key,SIMIX_HOST_LEVEL,smx_host); + xbt_assert(xbt_lib_get_or_null(host_lib,host->key,SIMIX_HOST_LEVEL) == NULL); + xbt_lib_set(host_lib,host->key,SIMIX_HOST_LEVEL,smx_host); } void sg_host_simix_destroy(sg_host_t host) { xbt_lib_unset(host_lib,host->key,SIMIX_HOST_LEVEL,1); diff --git a/src/simix/smx_environment.c b/src/simix/smx_environment.c index 6d1435210f..9217866240 100644 --- a/src/simix/smx_environment.c +++ b/src/simix/smx_environment.c @@ -43,19 +43,12 @@ void SIMIX_create_environment(const char *file) } -void SIMIX_post_create_environment(void) { - - void **host = NULL; +void SIMIX_post_create_environment(void) +{ void **storage = NULL; xbt_lib_cursor_t cursor = NULL; char *name = NULL; - /* Create host at SIMIX level */ - xbt_lib_foreach(host_lib, cursor, name, host) { - if(host[SURF_HOST_LEVEL]) - SIMIX_host_create(name); - } - /* Create storage at SIMIX level */ xbt_lib_foreach(storage_lib, cursor, name, storage) { if(storage[SURF_STORAGE_LEVEL]) diff --git a/src/simix/smx_global.c b/src/simix/smx_global.c index dba302930f..83ef7f340e 100644 --- a/src/simix/smx_global.c +++ b/src/simix/smx_global.c @@ -21,6 +21,8 @@ #include "src/mc/mc_replay.h" #include "simgrid/sg_config.h" +#include "src/surf/callbacks.h" + #ifdef HAVE_MC #include "src/mc/mc_private.h" #include "src/mc/mc_protocol.h" @@ -219,6 +221,7 @@ void SIMIX_global_init(int *argc, char **argv) /* register a function to be called by SURF after the environment creation */ sg_platf_init(); sg_platf_postparse_add_cb(SIMIX_post_create_environment); + surf_host_created_callback(SIMIX_host_create); } if (!simix_timers) { diff --git a/src/simix/smx_host.c b/src/simix/smx_host.c index f4f4779a9b..982545b254 100644 --- a/src/simix/smx_host.c +++ b/src/simix/smx_host.c @@ -20,9 +20,8 @@ static void SIMIX_execution_finish(smx_synchro_t synchro); * \brief Internal function to create a SIMIX host. * \param name name of the host to create */ -void SIMIX_host_create(const char *name) // FIXME: braindead prototype. Take sg_host as parameter +void SIMIX_host_create(sg_host_t host) // FIXME: braindead prototype. Take sg_host as parameter { - sg_host_t host = xbt_lib_get_elm_or_null(host_lib, name); smx_host_priv_t smx_host = xbt_new0(s_smx_host_priv_t, 1); s_smx_process_t proc; diff --git a/src/simix/smx_host_private.h b/src/simix/smx_host_private.h index 1d800234ec..1cac10f5b1 100644 --- a/src/simix/smx_host_private.h +++ b/src/simix/smx_host_private.h @@ -22,7 +22,7 @@ typedef struct s_smx_host_priv { } s_smx_host_priv_t; XBT_PRIVATE void _SIMIX_host_free_process_arg(void *); -XBT_PRIVATE void SIMIX_host_create(const char *name); +XBT_PRIVATE void SIMIX_host_create(sg_host_t host); XBT_PRIVATE void SIMIX_host_destroy(void *host); XBT_PRIVATE void SIMIX_host_add_auto_restart_process(sg_host_t host, diff --git a/src/simix/smx_vm.c b/src/simix/smx_vm.c index 401565cd59..d430a960fb 100644 --- a/src/simix/smx_vm.c +++ b/src/simix/smx_vm.c @@ -24,13 +24,13 @@ sg_host_t SIMIX_vm_create(const char *name, sg_host_t ind_phys_host) { /* Create surf associated resource */ surf_vm_model_create(name, ind_phys_host); - - SIMIX_host_create(name); + sg_host_t host = sg_host_by_name(name); + SIMIX_host_create(host); /* We will be able to register the VM to its physical host, so that we can promptly * retrieve the list VMs on the physical host. */ - return sg_host_by_name(name); + return host; } diff --git a/src/surf/callbacks.cpp b/src/surf/callbacks.cpp new file mode 100644 index 0000000000..d8d48c729a --- /dev/null +++ b/src/surf/callbacks.cpp @@ -0,0 +1,21 @@ +/* Copyright (c) 2015. The SimGrid Team. + * All rights reserved. */ + +/* This program is free software; you can redistribute it and/or modify it + * under the terms of the license (GNU LGPL) which comes with this package. */ + +#include + +#include "src/surf/callbacks.h" +#include "src/surf/surf_interface.hpp" +#include "src/surf/host_interface.hpp" + +void surf_host_created_callback(void (*callback)(sg_host_t)) +{ + hostCreatedCallbacks.connect([callback](Host* host) { + const char* id = host->getName(); + sg_host_t h = sg_host_by_name(id); + xbt_assert(h != NULL, "Host not found for name %s", id); + callback(h); + }); +} diff --git a/src/surf/callbacks.h b/src/surf/callbacks.h new file mode 100644 index 0000000000..efc947b4c3 --- /dev/null +++ b/src/surf/callbacks.h @@ -0,0 +1,24 @@ +/* Copyright (c) 2015. The SimGrid Team. + * All rights reserved. */ + +/* This program is free software; you can redistribute it and/or modify it + * under the terms of the license (GNU LGPL) which comes with this package. */ + +#ifndef SIMGRID_SURF_CALLBACKS_H +#define SIMGRID_SURF_CALLBACKS_H + +/** \file callbacks.h + * + * C interface for the C++ SURF callbacks. + */ + +#include +#include "simgrid/host.h" + +SG_BEGIN_DECL(); + +XBT_PRIVATE void surf_host_created_callback(void (*callback)(sg_host_t)); + +SG_END_DECL(); + +#endif diff --git a/tools/cmake/DefinePackages.cmake b/tools/cmake/DefinePackages.cmake index 4ee2810b7f..68a711f187 100644 --- a/tools/cmake/DefinePackages.cmake +++ b/tools/cmake/DefinePackages.cmake @@ -307,6 +307,8 @@ set(NS3_SRC ) set(SURF_SRC + src/surf/callbacks.h + src/surf/callbacks.cpp src/surf/cpu_cas01.cpp src/surf/cpu_interface.cpp src/surf/cpu_ti.cpp -- 2.20.1