~LinkEnergy();
void initWattsRangeList();
- double getTotalEnergy();
+ double getConsumedEnergy();
void update();
private:
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_;
}
}
static void onSimulationEnd()
{
- std::vector<simgrid::s4u::Link*> links;
- simgrid::s4u::Engine::getInstance()->getLinkList(&links);
+ std::vector<simgrid::s4u::Link*> links = simgrid::s4u::Engine::getInstance()->getAllLinks();
double total_energy = 0.0; // Total dissipated energy (whole platform)
for (const auto link : links) {
- double link_energy = link->extension<LinkEnergy>()->getTotalEnergy();
- if (strcmp(link->getCname(), "__loopback__"))
- XBT_INFO("Link '%s' total consumption: %f", link->getCname(), link_energy);
+ double link_energy = link->extension<LinkEnergy>()->getConsumedEnergy();
total_energy += link_energy;
}
}
/* **************************** Public interface *************************** */
SG_BEGIN_DECL()
+int sg_link_energy_is_inited()
+{
+ return LinkEnergy::EXTENSION_ID.valid();
+}
/** \ingroup SURF_plugin_energy
* \brief Enable energy plugin
* \details Enable energy plugin to get joules consumption of each cpu. You should call this function before
return;
LinkEnergy::EXTENSION_ID = simgrid::s4u::Link::extension_create<LinkEnergy>();
+ xbt_assert(sg_host_count() == 0, "Please call sg_link_energy_plugin_init() before initializing the platform.");
+
simgrid::s4u::Link::onCreation.connect([](simgrid::s4u::Link& link) {
link.extension_set(new LinkEnergy(&link));
});
});
simgrid::s4u::Link::onDestruction.connect([](simgrid::s4u::Link& link) {
- link.extension<LinkEnergy>()->update();
+ if (strcmp(link.getCname(), "__loopback__"))
+ XBT_INFO("Energy consumption of link '%s': %f Joules", link.getCname(),
+ link.extension<LinkEnergy>()->getConsumedEnergy());
});
simgrid::s4u::Link::onCommunicationStateChange.connect([](simgrid::surf::NetworkAction* action) {
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<LinkEnergy>()->getConsumedEnergy();
+}
SG_END_DECL()