From 5ff6bdbfb4e90fd6bb2262c2a11911eb60ce2c42 Mon Sep 17 00:00:00 2001 From: Martin Quinson Date: Tue, 28 Nov 2017 01:28:09 +0100 Subject: [PATCH] reduce differences between host and link energy plugin --- examples/s4u/energy-link/s4u-energy-link.tesh | 5 ++-- src/surf/plugins/host_energy.cpp | 5 ++-- src/surf/plugins/link_energy.cpp | 26 ++++++++++++++----- 3 files changed, 23 insertions(+), 13 deletions(-) diff --git a/examples/s4u/energy-link/s4u-energy-link.tesh b/examples/s4u/energy-link/s4u-energy-link.tesh index 2622a4b76c..2be5e34dee 100644 --- a/examples/s4u/energy-link/s4u-energy-link.tesh +++ b/examples/s4u/energy-link/s4u-energy-link.tesh @@ -10,8 +10,8 @@ $ ${bindir:=.}/s4u-energy-link$EXEEXT ${srcdir:=.}/../platforms/energy_platform. > [ 0.000000] (2:receiver@MyHost2) Receiving 1 flows ... > [ 10.250000] (2:receiver@MyHost2) receiver done. > [ 10.250000] (1:sender@MyHost1) sender done. -> [ 10.250000] (0:maestro@) Link 'bus' total consumption: 10.750000 > [ 10.250000] (0:maestro@) Total energy over all links: 10.750000 +> [ 10.250000] (0:maestro@) Energy consumption of link 'bus': 10.750000 Joules p And now test with 500000 bytes @@ -23,6 +23,5 @@ $ ${bindir:=.}/s4u-energy-link$EXEEXT ${srcdir:=.}/../platforms/energy_platform. > [ 0.000000] (2:receiver@MyHost2) Receiving 1 flows ... > [510.000000] (2:receiver@MyHost2) receiver done. > [510.000000] (1:sender@MyHost1) sender done. -> [510.000000] (0:maestro@) Link 'bus' total consumption: 1510.000000 > [510.000000] (0:maestro@) Total energy over all links: 1510.000000 - +> [510.000000] (0:maestro@) Energy consumption of link 'bus': 1510.000000 Joules diff --git a/src/surf/plugins/host_energy.cpp b/src/surf/plugins/host_energy.cpp index 21853c6f84..fbd887a48a 100644 --- a/src/surf/plugins/host_energy.cpp +++ b/src/surf/plugins/host_energy.cpp @@ -413,9 +413,8 @@ static void onHostDestruction(simgrid::s4u::Host& host) if (dynamic_cast(&host)) // Ignore virtual machines return; - HostEnergy* host_energy = host.extension(); - host_energy->update(); - XBT_INFO("Energy consumption of host %s: %f Joules", host.getCname(), host_energy->getConsumedEnergy()); + XBT_INFO("Energy consumption of host %s: %f Joules", host.getCname(), + host.extension()->getConsumedEnergy()); } static void onSimulationEnd() diff --git a/src/surf/plugins/link_energy.cpp b/src/surf/plugins/link_energy.cpp index f3b305599a..499fe6ce0f 100644 --- a/src/surf/plugins/link_energy.cpp +++ b/src/surf/plugins/link_energy.cpp @@ -52,7 +52,7 @@ public: ~LinkEnergy(); void initWattsRangeList(); - double getTotalEnergy(); + double getConsumedEnergy(); void update(); private: @@ -135,9 +135,10 @@ double LinkEnergy::getPower() return idle_ + dynamic_power; } -double LinkEnergy::getTotalEnergy() +double LinkEnergy::getConsumedEnergy() { - update(); + if (lastUpdated_ < surf_get_clock()) // We need to simcall this as it modifies the environment + simgrid::simix::kernelImmediate(std::bind(&LinkEnergy::update, this)); return this->totalEnergy_; } } @@ -168,9 +169,7 @@ static void onSimulationEnd() double total_energy = 0.0; // Total dissipated energy (whole platform) for (const auto link : links) { - double link_energy = link->extension()->getTotalEnergy(); - if (strcmp(link->getCname(), "__loopback__")) - XBT_INFO("Link '%s' total consumption: %f", link->getCname(), link_energy); + double link_energy = link->extension()->getConsumedEnergy(); total_energy += link_energy; } @@ -199,7 +198,9 @@ void sg_link_energy_plugin_init() }); simgrid::s4u::Link::onDestruction.connect([](simgrid::s4u::Link& link) { - link.extension()->update(); + if (strcmp(link.getCname(), "__loopback__")) + XBT_INFO("Energy consumption of link '%s': %f Joules", link.getCname(), + link.extension()->getConsumedEnergy()); }); simgrid::s4u::Link::onCommunicationStateChange.connect([](simgrid::surf::NetworkAction* action) { @@ -213,4 +214,15 @@ void sg_link_energy_plugin_init() simgrid::s4u::onSimulationEnd.connect(&onSimulationEnd); } +/** @ingroup plugin_energy + * @brief Returns the total energy consumed by the link so far (in Joules) + * + * Please note that since the consumption is lazily updated, it may require a simcall to update it. + * The result is that the actor requesting this value will be interrupted, + * the value will be updated in kernel mode before returning the control to the requesting actor. + */ +double sg_link_get_consumed_energy(sg_link_t link) +{ + return link->extension()->getConsumedEnergy(); +} SG_END_DECL() -- 2.20.1