From d9fb9c3b94a08a8a49509e49e3dc847f6c215b99 Mon Sep 17 00:00:00 2001 From: Martin Quinson Date: Mon, 18 Nov 2019 06:55:28 +0100 Subject: [PATCH] Energy plugins now throws std::logic_error when not properly inited before use --- src/plugins/host_energy.cpp | 30 +++++++++++++++--------------- src/plugins/link_energy.cpp | 3 +++ 2 files changed, 18 insertions(+), 15 deletions(-) diff --git a/src/plugins/host_energy.cpp b/src/plugins/host_energy.cpp index 45d75ed3db..bd348e2236 100644 --- a/src/plugins/host_energy.cpp +++ b/src/plugins/host_energy.cpp @@ -460,7 +460,7 @@ static void on_creation(simgrid::s4u::Host& host) if (dynamic_cast(&host)) // Ignore virtual machines return; - // TODO Trace: set to zero the energy variable associated to host->getName() + // TODO Trace: set to zero the energy variable associated to host->get_name() host.extension_set(new HostEnergy(&host)); } @@ -581,6 +581,13 @@ void sg_host_energy_update_all() }); } +static void ensure_plugin_inited() +{ + if (not HostEnergy::EXTENSION_ID.valid()) + throw std::logic_error("The Energy plugin is not active. Please call sg_host_energy_plugin_init() before calling " + "any function related to that plugin."); +} + /** @ingroup plugin_host_energy * @brief Returns the total energy consumed by the host so far (in Joules) * @@ -590,8 +597,7 @@ void sg_host_energy_update_all() */ double sg_host_get_consumed_energy(sg_host_t host) { - xbt_assert(HostEnergy::EXTENSION_ID.valid(), - "The Energy plugin is not active. Please call sg_host_energy_plugin_init() during initialization."); + ensure_plugin_inited(); return host->extension()->get_consumed_energy(); } @@ -600,8 +606,7 @@ double sg_host_get_consumed_energy(sg_host_t host) */ double sg_host_get_idle_consumption(sg_host_t host) { - xbt_assert(HostEnergy::EXTENSION_ID.valid(), - "The Energy plugin is not active. Please call sg_host_energy_plugin_init() during initialization."); + ensure_plugin_inited(); return host->extension()->get_watt_idle_at(0); } @@ -610,8 +615,7 @@ double sg_host_get_idle_consumption(sg_host_t host) */ double sg_host_get_idle_consumption_at(sg_host_t host, int pstate) { - xbt_assert(HostEnergy::EXTENSION_ID.valid(), - "The Energy plugin is not active. Please call sg_host_energy_plugin_init() during initialization."); + ensure_plugin_inited(); return host->extension()->get_watt_idle_at(pstate); } @@ -620,8 +624,7 @@ double sg_host_get_idle_consumption_at(sg_host_t host, int pstate) */ double sg_host_get_wattmin_at(sg_host_t host, int pstate) { - xbt_assert(HostEnergy::EXTENSION_ID.valid(), - "The Energy plugin is not active. Please call sg_host_energy_plugin_init() during initialization."); + ensure_plugin_inited(); return host->extension()->get_watt_min_at(pstate); } /** @ingroup plugin_host_energy @@ -629,8 +632,7 @@ double sg_host_get_wattmin_at(sg_host_t host, int pstate) */ double sg_host_get_wattmax_at(sg_host_t host, int pstate) { - xbt_assert(HostEnergy::EXTENSION_ID.valid(), - "The Energy plugin is not active. Please call sg_host_energy_plugin_init() during initialization."); + ensure_plugin_inited(); return host->extension()->get_watt_max_at(pstate); } /** @ingroup plugin_host_energy @@ -638,8 +640,7 @@ double sg_host_get_wattmax_at(sg_host_t host, int pstate) */ double sg_host_get_power_range_slope_at(sg_host_t host, int pstate) { - xbt_assert(HostEnergy::EXTENSION_ID.valid(), - "The Energy plugin is not active. Please call sg_host_energy_plugin_init() during initialization."); + ensure_plugin_inited(); return host->extension()->get_power_range_slope_at(pstate); } /** @ingroup plugin_host_energy @@ -647,7 +648,6 @@ double sg_host_get_power_range_slope_at(sg_host_t host, int pstate) */ double sg_host_get_current_consumption(sg_host_t host) { - xbt_assert(HostEnergy::EXTENSION_ID.valid(), - "The Energy plugin is not active. Please call sg_host_energy_plugin_init() during initialization."); + ensure_plugin_inited(); return host->extension()->get_current_watts_value(); } diff --git a/src/plugins/link_energy.cpp b/src/plugins/link_energy.cpp index 18c8ae1463..b2b5e10089 100644 --- a/src/plugins/link_energy.cpp +++ b/src/plugins/link_energy.cpp @@ -228,5 +228,8 @@ void sg_link_energy_plugin_init() */ double sg_link_get_consumed_energy(sg_link_t link) { + if (not LinkEnergy::EXTENSION_ID.valid()) + throw std::logic_error("The Energy plugin is not active. Please call sg_link_energy_plugin_init() before calling " + "sg_link_get_consumed_energy()."); return link->extension()->get_consumed_energy(); } -- 2.20.1