From e3bb3fa0da0fe033ac8ff471ea382010177855c8 Mon Sep 17 00:00:00 2001 From: Martin Quinson Date: Thu, 15 Sep 2016 18:24:54 +0200 Subject: [PATCH 1/1] hide a global data container --- include/simgrid/host.h | 2 ++ include/surf/surf_routing.h | 1 - src/msg/msg_host.cpp | 2 +- src/msg/msg_vm.cpp | 2 +- src/s4u/s4u_host.cpp | 8 ++++++++ src/simgrid/host.cpp | 7 +++++++ src/surf/sg_platf.cpp | 1 + src/surf/surf_interface.cpp | 9 ++------- src/surf/surf_routing.cpp | 1 - teshsuite/simdag/flatifier/flatifier.cpp | 21 ++++++++++++--------- teshsuite/simdag/is-router/is-router.cpp | 19 +++++++++++-------- 11 files changed, 45 insertions(+), 28 deletions(-) diff --git a/include/simgrid/host.h b/include/simgrid/host.h index 6713ccfaa8..e1d6c26daf 100644 --- a/include/simgrid/host.h +++ b/include/simgrid/host.h @@ -16,6 +16,8 @@ SG_BEGIN_DECL() +XBT_PUBLIC(void) sg_host_exit(); + XBT_PUBLIC(size_t) sg_host_count(); XBT_PUBLIC(sg_host_t *) sg_host_list(); diff --git a/include/surf/surf_routing.h b/include/surf/surf_routing.h index c846b7778d..075f7c7b67 100644 --- a/include/surf/surf_routing.h +++ b/include/surf/surf_routing.h @@ -11,7 +11,6 @@ SG_BEGIN_DECL() -XBT_PUBLIC_DATA(xbt_dict_t) host_list; XBT_PUBLIC_DATA(int) SIMIX_STORAGE_LEVEL; //Simix storage level XBT_PUBLIC_DATA(int) COORD_HOST_LEVEL; //Coordinates level diff --git a/src/msg/msg_host.cpp b/src/msg/msg_host.cpp index 4d09dffdd0..1d369c0313 100644 --- a/src/msg/msg_host.cpp +++ b/src/msg/msg_host.cpp @@ -128,7 +128,7 @@ void __MSG_host_priv_free(msg_host_priv_t priv) */ int MSG_get_host_number() { - return xbt_dict_length(host_list); + return sg_host_count(); } /** \ingroup m_host_management diff --git a/src/msg/msg_vm.cpp b/src/msg/msg_vm.cpp index 2198f6d593..46dae391f7 100644 --- a/src/msg/msg_vm.cpp +++ b/src/msg/msg_vm.cpp @@ -196,7 +196,7 @@ msg_vm_t MSG_vm_create_core(msg_host_t ind_pm, const char *name) { /* make sure the VM of the same name does not exit */ { - simgrid::s4u::Host* ind_host_tmp = (simgrid::s4u::Host*) xbt_dict_get_or_null(host_list, name); + simgrid::s4u::Host* ind_host_tmp = sg_host_by_name(name); if (ind_host_tmp != nullptr && sg_host_simix(ind_host_tmp) != nullptr) { XBT_ERROR("host %s already exits", name); return nullptr; diff --git a/src/s4u/s4u_host.cpp b/src/s4u/s4u_host.cpp index 7d13e3385c..5db68c5809 100644 --- a/src/s4u/s4u_host.cpp +++ b/src/s4u/s4u_host.cpp @@ -20,6 +20,8 @@ #include "simgrid/s4u/host.hpp" #include "simgrid/s4u/storage.hpp" +xbt_dict_t host_list = nullptr; // FIXME: make it a static field of Host + int MSG_HOST_LEVEL = -1; int USER_HOST_LEVEL = -1; @@ -55,6 +57,12 @@ Host *Host::by_name(std::string name) { } Host* Host::by_name_or_null(const char* name) { + if (host_list == nullptr) + host_list = xbt_dict_new_homogeneous([](void*p) { + simgrid::s4u::Host* host = static_cast(p); + simgrid::s4u::Host::onDestruction(*host); + delete host; + }); return (Host*) xbt_dict_get_or_null(host_list, name); } Host* Host::by_name_or_create(const char* name) diff --git a/src/simgrid/host.cpp b/src/simgrid/host.cpp index 77aa8ef1aa..f3701a734c 100644 --- a/src/simgrid/host.cpp +++ b/src/simgrid/host.cpp @@ -14,6 +14,13 @@ XBT_LOG_NEW_DEFAULT_SUBCATEGORY(sg_host, sd, "Logging specific to sg_hosts"); +extern xbt_dict_t host_list; // FIXME:killme don't dupplicate the content of s4u::Host this way + +void sg_host_exit() +{ + xbt_dict_free(&host_list); +} + size_t sg_host_count() { return xbt_dict_length(host_list); diff --git a/src/surf/sg_platf.cpp b/src/surf/sg_platf.cpp index e1971f0ad1..a1ff3b100c 100644 --- a/src/surf/sg_platf.cpp +++ b/src/surf/sg_platf.cpp @@ -570,6 +570,7 @@ void sg_platf_new_process(sg_platf_process_cbarg_t process) msg->used -= 3; xbt_strbuff_append(msg," ...(list truncated)......");// That will be shortened by 3 chars when existing the loop + break; } } msg->data[msg->used-3]='\0'; diff --git a/src/surf/surf_interface.cpp b/src/surf/surf_interface.cpp index 1829975b7e..194b467879 100644 --- a/src/surf/surf_interface.cpp +++ b/src/surf/surf_interface.cpp @@ -289,15 +289,10 @@ void sg_version(int *ver_major,int *ver_minor,int *ver_patch) { void surf_init(int *argc, char **argv) { - if (host_list != nullptr) // Already initialized + if (USER_HOST_LEVEL != -1) // Already initialized return; XBT_DEBUG("Create all Libs"); - host_list = xbt_dict_new_homogeneous([](void*p) { - simgrid::s4u::Host* host = static_cast(p); - simgrid::s4u::Host::onDestruction(*host); - delete host; - }); USER_HOST_LEVEL = simgrid::s4u::Host::extension_create(nullptr); as_router_lib = xbt_lib_new(); @@ -338,7 +333,7 @@ void surf_exit() xbt_dynar_free(&surf_path); - xbt_dict_free(&host_list); + sg_host_exit(); xbt_lib_free(&as_router_lib); xbt_lib_free(&storage_lib); sg_link_exit(); diff --git a/src/surf/surf_routing.cpp b/src/surf/surf_routing.cpp index e12af64f42..b6c6e65500 100644 --- a/src/surf/surf_routing.cpp +++ b/src/surf/surf_routing.cpp @@ -34,7 +34,6 @@ namespace routing { * @ingroup SURF_build_api * @brief A library containing all known hosts */ -xbt_dict_t host_list = nullptr; int COORD_HOST_LEVEL = -1; //Coordinates level diff --git a/teshsuite/simdag/flatifier/flatifier.cpp b/teshsuite/simdag/flatifier/flatifier.cpp index 6e9499a953..db4dede420 100644 --- a/teshsuite/simdag/flatifier/flatifier.cpp +++ b/teshsuite/simdag/flatifier/flatifier.cpp @@ -147,14 +147,16 @@ int main(int argc, char **argv) } sg_host_t host1, host2; - xbt_dict_foreach(host_list, cursor_src, src, host1){ // Routes from host - value1 = sg_host_by_name(src)->pimpl_netcard; - xbt_dict_foreach(host_list, cursor_dst, dst, host2){ //to host + for (unsigned int it_src = 0; it_src < totalHosts; it_src++) { // Routes from host + host1 = hosts[it_src]; + value1 = sg_host_by_name(host1->name().c_str())->pimpl_netcard; + for (unsigned int it_dst = 0; it_dst < totalHosts; it_dst++) { // Routes to host + host2 = hosts[it_dst]; std::vector *route = new std::vector(); - value2 = sg_host_by_name(dst)->pimpl_netcard; + value2 = host2->pimpl_netcard; routing_platf->getRouteAndLatency(value1, value2, route,nullptr); if (! route->empty()){ - std::printf(" \n ", src, dst); + std::printf(" \n ", host1->name().c_str(), host2->name().c_str()); for (auto link: *route) std::printf("",link->getName()); std::printf("\n \n"); @@ -164,7 +166,7 @@ int main(int argc, char **argv) xbt_lib_foreach(as_router_lib, cursor_dst, dst, value2){ //to router value2 = (sg_netcard_t)xbt_lib_get_or_null(as_router_lib,dst,ROUTING_ASR_LEVEL); if(value2->isRouter()){ - std::printf(" \n ", src, dst); + std::printf(" \n ", host1->name().c_str(), dst); std::vector *route = new std::vector(); routing_platf->getRouteAndLatency((sg_netcard_t)value1,(sg_netcard_t)value2,route,nullptr); for (auto link : *route) @@ -190,10 +192,11 @@ int main(int argc, char **argv) std::printf("\n \n"); } } - xbt_dict_foreach(host_list, cursor_dst, dst, value2){ //to host - std::printf(" \n ",src, dst); + for (unsigned int it_dst = 0; it_dst < totalHosts; it_dst++) { // Routes to host + host2 = hosts[it_dst]; + std::printf(" \n ",src, host2->name().c_str()); std::vector *route = new std::vector(); - value2 = sg_host_by_name(dst)->pimpl_netcard; + value2 = host2->pimpl_netcard; routing_platf->getRouteAndLatency((sg_netcard_t)value1,(sg_netcard_t)value2,route, nullptr); for(auto link : *route) std::printf("",link->getName()); diff --git a/teshsuite/simdag/is-router/is-router.cpp b/teshsuite/simdag/is-router/is-router.cpp index 680fce3fb6..9039cdc96a 100644 --- a/teshsuite/simdag/is-router/is-router.cpp +++ b/teshsuite/simdag/is-router/is-router.cpp @@ -6,28 +6,31 @@ #include #include "simgrid/simdag.h" #include "src/surf/surf_routing.hpp" +#include "simgrid/s4u/host.hpp" + int main(int argc, char **argv) { /* SD initialization */ - int size; - xbt_lib_cursor_t cursor = nullptr; char *key, *data; SD_init(&argc, argv); - - /* creation of the environment */ SD_create_environment(argv[1]); - size = xbt_dict_length(host_list) + xbt_lib_length(as_router_lib); + xbt_dynar_t hosts = sg_hosts_as_dynar(); + int size = sg_host_count() + xbt_lib_length(as_router_lib); printf("Host number: %zu, link number: %d, elmts number: %d\n", sg_host_count(), sg_link_count(), size); - xbt_dict_foreach(host_list, cursor, key, data) { - simgrid::kernel::routing::NetCard * nc = sg_netcard_by_name_or_null(key); - printf(" - Seen: \"%s\". Type: %s\n", key, nc->isRouter() ? "router" : (nc->isAS()?"AS":"host")); + int it; + xbt_dynar_foreach(hosts, it, data) { + sg_host_t host = (sg_host_t)data; + simgrid::kernel::routing::NetCard * nc = host->pimpl_netcard; + printf(" - Seen: \"%s\". Type: %s\n", host->name().c_str(), nc->isRouter() ? "router" : (nc->isAS()?"AS":"host")); } + xbt_dynar_free(&hosts); + xbt_lib_cursor_t cursor = nullptr; xbt_lib_foreach(as_router_lib, cursor, key, data) { simgrid::kernel::routing::NetCard * nc = sg_netcard_by_name_or_null(key); printf(" - Seen: \"%s\". Type: %s\n", key, nc->isRouter() ? "router" : (nc->isAS()?"AS":"host")); -- 2.20.1