~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_;
}
}
using simgrid::plugin::LinkEnergy;
/* **************************** events callback *************************** */
-static void onCreation(simgrid::s4u::Link& link)
-{
- XBT_DEBUG("onCreation is called for link: %s", link.getCname());
- link.extension_set(new LinkEnergy(&link));
-}
-
static void onCommunicate(simgrid::surf::NetworkAction* action, simgrid::s4u::Host* src, simgrid::s4u::Host* dst)
{
XBT_DEBUG("onCommunicate is called");
continue;
XBT_DEBUG("Update link %s", link->getCname());
- // Get the link_energy extension for the relevant link
LinkEnergy* link_energy = link->piface_.extension<LinkEnergy>();
link_energy->initWattsRangeList();
link_energy->update();
}
}
-static void onActionStateChange(simgrid::surf::NetworkAction* action)
-{
- XBT_DEBUG("onActionStateChange is called");
- for (simgrid::surf::LinkImpl* link : action->links()) {
-
- if (link == nullptr)
- continue;
-
- // Get the link_energy extension for the relevant link
- LinkEnergy* link_energy = link->piface_.extension<LinkEnergy>();
- link_energy->update();
- }
-}
-
-static void onLinkStateChange(simgrid::s4u::Link& link)
-{
- XBT_DEBUG("onLinkStateChange is called for link: %s", link.getCname());
-
- LinkEnergy* link_energy = link.extension<LinkEnergy>();
- link_energy->update();
-}
-
-static void onLinkDestruction(simgrid::s4u::Link& link)
+static void onSimulationEnd()
{
- XBT_DEBUG("onLinkDestruction is called for link: %s", link.getCname());
-
- LinkEnergy* link_energy = link.extension<LinkEnergy>();
- link_energy->update();
-}
+ std::vector<simgrid::s4u::Link*> links = simgrid::s4u::Engine::getInstance()->getAllLinks();
-static void computeAndDisplayTotalEnergy()
-{
- std::vector<simgrid::s4u::Link*> link_list;
- simgrid::s4u::Engine::getInstance()->getLinkList(&link_list);
double total_energy = 0.0; // Total dissipated energy (whole platform)
- for (const auto link : link_list) {
- LinkEnergy* link_energy = link->extension<LinkEnergy>();
-
- double a_link_total_energy = link_energy->getTotalEnergy();
- total_energy += a_link_total_energy;
- const char* name = link->getCname();
- if (strcmp(name, "__loopback__"))
- XBT_INFO("Link '%s' total consumption: %f", name, a_link_total_energy);
+ for (const auto link : links) {
+ double link_energy = link->extension<LinkEnergy>()->getConsumedEnergy();
+ total_energy += link_energy;
}
XBT_INFO("Total energy over all links: %f", total_energy);
}
-
-static void onSimulationEnd()
-{
- computeAndDisplayTotalEnergy();
-}
/* **************************** 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>();
- simgrid::s4u::Link::onCreation.connect(&onCreation);
- simgrid::s4u::Link::onStateChange.connect(&onLinkStateChange);
- simgrid::s4u::Link::onDestruction.connect(&onLinkDestruction);
- simgrid::s4u::Link::onCommunicationStateChange.connect(&onActionStateChange);
+ 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::onStateChange.connect([](simgrid::s4u::Link& link) {
+ link.extension<LinkEnergy>()->update();
+ });
+
+ simgrid::s4u::Link::onDestruction.connect([](simgrid::s4u::Link& link) {
+ 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) {
+ for (simgrid::surf::LinkImpl* link : action->links()) {
+ if (link != nullptr)
+ link->piface_.extension<LinkEnergy>()->update();
+ }
+ });
+
simgrid::s4u::Link::onCommunicate.connect(&onCommunicate);
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()