From 6430525cf576da6bee8d0cfd41332f8260cb9ffd Mon Sep 17 00:00:00 2001 From: Martin Quinson Date: Sun, 8 Jul 2018 02:27:39 +0200 Subject: [PATCH] Allow to register plugins from their file, w/o touching the core implem --- src/plugins/host_dvfs.cpp | 2 ++ src/plugins/host_energy.cpp | 2 ++ src/plugins/host_load.cpp | 2 ++ src/plugins/link_energy.cpp | 3 +++ src/surf/surf_interface.cpp | 26 +++++++++++-------- src/surf/surf_interface.hpp | 10 ++++++- .../energy-consumption/energy-consumption.c | 2 +- 7 files changed, 34 insertions(+), 13 deletions(-) diff --git a/src/plugins/host_dvfs.cpp b/src/plugins/host_dvfs.cpp index b99df53b9b..202442686e 100644 --- a/src/plugins/host_dvfs.cpp +++ b/src/plugins/host_dvfs.cpp @@ -10,6 +10,8 @@ #include +SIMGRID_REGISTER_PLUGIN(host_dvfs, "Dvfs support", &sg_host_dvfs_plugin_init) + /** @addtogroup SURF_plugin_load This plugin makes it very simple for users to obtain the current load for each host. diff --git a/src/plugins/host_energy.cpp b/src/plugins/host_energy.cpp index 1e7bdb7b46..6334dddffe 100644 --- a/src/plugins/host_energy.cpp +++ b/src/plugins/host_energy.cpp @@ -13,6 +13,8 @@ #include #include +SIMGRID_REGISTER_PLUGIN(host_energy, "Cpu energy consumption.", &sg_host_energy_plugin_init) + /** @addtogroup plugin_energy This is the energy plugin, enabling to account not only for computation time, but also for the dissipated energy in the diff --git a/src/plugins/host_load.cpp b/src/plugins/host_load.cpp index 9c297c7d1e..264ea2d095 100644 --- a/src/plugins/host_load.cpp +++ b/src/plugins/host_load.cpp @@ -8,6 +8,8 @@ #include "src/plugins/vm/VirtualMachineImpl.hpp" #include +SIMGRID_REGISTER_PLUGIN(host_load, "Cpu load", &sg_host_load_plugin_init) + /** @addtogroup plugin_load This plugin makes it very simple for users to obtain the current load for each host. diff --git a/src/plugins/link_energy.cpp b/src/plugins/link_energy.cpp index 2f42d3eee1..2057c0ed59 100644 --- a/src/plugins/link_energy.cpp +++ b/src/plugins/link_energy.cpp @@ -6,11 +6,14 @@ #include "simgrid/plugins/energy.h" #include "simgrid/s4u/Engine.hpp" #include "src/surf/network_interface.hpp" +#include "src/surf/surf_interface.hpp" #include "surf/surf.hpp" #include #include +SIMGRID_REGISTER_PLUGIN(link_energy, "Link energy consumption.", &sg_link_energy_plugin_init) + /** @addtogroup SURF_plugin_energy diff --git a/src/surf/surf_interface.cpp b/src/surf/surf_interface.cpp index 93a3b170e4..ce3e759d70 100644 --- a/src/surf/surf_interface.cpp +++ b/src/surf/surf_interface.cpp @@ -36,17 +36,21 @@ std::vector host_that_restart; std::set watched_hosts; extern std::map storage_types; -#include // FIXME: this plug-in should not be linked to the core -#include // FIXME: this plug-in should not be linked to the core -#include // FIXME: this plug-in should not be linked to the core - -s_surf_model_description_t surf_plugin_description[] = { - {"host_energy", "Cpu energy consumption.", &sg_host_energy_plugin_init}, - {"link_energy", "Link energy consumption.", &sg_link_energy_plugin_init}, - {"host_dvfs", "Dvfs support", &sg_host_dvfs_plugin_init}, - {"host_load", "Cpu load.", &sg_host_load_plugin_init}, - {nullptr, nullptr, nullptr} /* this array must be nullptr terminated */ -}; +s_surf_model_description_t* surf_plugin_description = nullptr; +XBT_PUBLIC void simgrid_add_plugin_description(const char* name, const char* description, void_f_void_t init_fun) +{ + static int plugin_amount = 0; + + /* no need to check for plugin name conflict: the compiler already ensures that the generated + * simgrid_##id##_plugin_register() is unique */ + + plugin_amount++; + surf_plugin_description = static_cast( + xbt_realloc(surf_plugin_description, sizeof(s_surf_model_description_t) * (plugin_amount + 2))); + + surf_plugin_description[plugin_amount - 1] = {name, description, init_fun}; + surf_plugin_description[plugin_amount] = {nullptr, nullptr, nullptr}; // this array must be null terminated +} /* Don't forget to update the option description in smx_config when you change this */ s_surf_model_description_t surf_network_model_description[] = { diff --git a/src/surf/surf_interface.hpp b/src/surf/surf_interface.hpp index 75c69dd682..e1af3a481e 100644 --- a/src/surf/surf_interface.hpp +++ b/src/surf/surf_interface.hpp @@ -221,8 +221,16 @@ typedef struct surf_model_description s_surf_model_description_t; XBT_PUBLIC int find_model_description(s_surf_model_description_t* table, std::string name); XBT_PUBLIC void model_help(const char* category, s_surf_model_description_t* table); +#define SIMGRID_REGISTER_PLUGIN(id, desc, init) \ + void simgrid_##id##_plugin_register(); \ + void XBT_ATTRIB_CONSTRUCTOR(800) simgrid_##id##_plugin_register() { \ + simgrid_add_plugin_description(#id, desc, init); \ + } + +XBT_PUBLIC void simgrid_add_plugin_description(const char* name, const char* description, void_f_void_t init_fun); + /** @brief The list of all available plugins */ -XBT_PUBLIC_DATA s_surf_model_description_t surf_plugin_description[]; +XBT_PUBLIC_DATA s_surf_model_description_t* surf_plugin_description; /** @brief The list of all available optimization modes (both for cpu and networks). * These optimization modes can be set using --cfg=cpu/optim:... and --cfg=network/optim:... */ XBT_PUBLIC_DATA s_surf_model_description_t surf_optimization_mode_description[]; diff --git a/teshsuite/msg/energy-consumption/energy-consumption.c b/teshsuite/msg/energy-consumption/energy-consumption.c index 64e0744471..0698a4276a 100644 --- a/teshsuite/msg/energy-consumption/energy-consumption.c +++ b/teshsuite/msg/energy-consumption/energy-consumption.c @@ -68,8 +68,8 @@ static int dvfs(int argc, char* argv[]) int main(int argc, char* argv[]) { - sg_host_energy_plugin_init(); MSG_init(&argc, argv); + MSG_config("plugin", "host_energy"); xbt_assert(argc == 2, "Usage: %s platform_file\n\tExample: %s msg_platform.xml\n", argv[0], argv[0]); -- 2.20.1