X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/blobdiff_plain/50ddfe2f5b5035e0ed9556b334d6977ee81ff83a..598f97dc55ad76b3253cd2e591d96eb8fe785674:/src/plugins/host_energy.cpp diff --git a/src/plugins/host_energy.cpp b/src/plugins/host_energy.cpp index be43b29592..2d9f84b5c7 100644 --- a/src/plugins/host_energy.cpp +++ b/src/plugins/host_energy.cpp @@ -1,4 +1,4 @@ -/* Copyright (c) 2010-2018. The SimGrid Team. All rights reserved. */ +/* Copyright (c) 2010-2019. The SimGrid Team. All rights reserved. */ /* This program is free software; you can redistribute it and/or modify it * under the terms of the license (GNU LGPL) which comes with this package. */ @@ -134,6 +134,7 @@ public: double get_current_watts_value(); double get_current_watts_value(double cpu_load); double get_consumed_energy(); + double get_idle_consumption(); double get_watt_min_at(int pstate); double get_watt_max_at(int pstate); void update(); @@ -202,7 +203,7 @@ HostEnergy::HostEnergy(simgrid::s4u::Host* ptr) : host_(ptr), last_updated_(surf if (off_power_str != nullptr) { try { this->watts_off_ = std::stod(std::string(off_power_str)); - } catch (std::invalid_argument& ia) { + } catch (const std::invalid_argument&) { throw std::invalid_argument(std::string("Invalid value for property watt_off of host ") + host_->get_cname() + ": " + off_power_str); } @@ -212,6 +213,14 @@ HostEnergy::HostEnergy(simgrid::s4u::Host* ptr) : host_(ptr), last_updated_(surf HostEnergy::~HostEnergy() = default; +double HostEnergy::get_idle_consumption() +{ + xbt_assert(not power_range_watts_list_.empty(), "No power range properties specified for host %s", + host_->get_cname()); + + return power_range_watts_list_[0].idle_; +} + double HostEnergy::get_watt_min_at(int pstate) { xbt_assert(not power_range_watts_list_.empty(), "No power range properties specified for host %s", @@ -235,7 +244,7 @@ double HostEnergy::get_current_watts_value() if (this->pstate_ == pstate_off_) // The host is off (or was off at the beginning of this time interval) return this->watts_off_; - double current_speed = host_->get_speed(); + double current_speed = host_->get_pstate_speed(this->pstate_); double cpu_load; @@ -243,16 +252,17 @@ double HostEnergy::get_current_watts_value() // Some users declare a pstate of speed 0 flops (e.g., to model boot time). // We consider that the machine is then fully loaded. That's arbitrary but it avoids a NaN cpu_load = 1; - else + else { cpu_load = host_->pimpl_cpu->get_constraint()->get_usage() / current_speed; - /** Divide by the number of cores here **/ - cpu_load /= host_->pimpl_cpu->get_core_count(); + /** Divide by the number of cores here **/ + cpu_load /= host_->pimpl_cpu->get_core_count(); - if (cpu_load > 1) // A machine with a load > 1 consumes as much as a fully loaded machine, not more - cpu_load = 1; - if (cpu_load > 0) - host_was_used_ = true; + if (cpu_load > 1) // A machine with a load > 1 consumes as much as a fully loaded machine, not more + cpu_load = 1; + if (cpu_load > 0) + host_was_used_ = true; + } /* The problem with this model is that the load is always 0 or 1, never something less. * Another possibility could be to model the total energy as @@ -281,10 +291,10 @@ double HostEnergy::get_current_watts_value(double cpu_load) /* min_power corresponds to the power consumed when only one core is active */ /* max_power is the power consumed at 100% cpu load */ auto range = power_range_watts_list_.at(this->pstate_); - double current_power = 0; - double min_power = 0; - double max_power = 0; - double power_slope = 0; + double current_power; + double min_power; + double max_power; + double power_slope; if (cpu_load > 0) { /* Something is going on, the machine is not idle */ min_power = range.min_; @@ -300,9 +310,8 @@ double HostEnergy::get_current_watts_value(double cpu_load) * i.e., we need min_power + (maxCpuLoad-1/coreCount)*power_slope == max_power * (maxCpuLoad is by definition 1) */ - double power_slope; int coreCount = host_->get_core_count(); - double coreReciprocal = static_cast(1) / static_cast(coreCount); + double coreReciprocal = 1.0 / coreCount; if (coreCount > 1) power_slope = (max_power - min_power) / (1 - coreReciprocal); else @@ -310,6 +319,9 @@ double HostEnergy::get_current_watts_value(double cpu_load) current_power = min_power + (cpu_load - coreReciprocal) * power_slope; } else { /* Our machine is idle, take the dedicated value! */ + min_power = 0; + max_power = 0; + power_slope = 0; current_power = range.idle_; } @@ -322,7 +334,7 @@ double HostEnergy::get_current_watts_value(double cpu_load) double HostEnergy::get_consumed_energy() { if (last_updated_ < surf_get_clock()) // We need to simcall this as it modifies the environment - simgrid::simix::simcall(std::bind(&HostEnergy::update, this)); + simgrid::kernel::actor::simcall(std::bind(&HostEnergy::update, this)); return total_energy_; } @@ -351,13 +363,13 @@ void HostEnergy::init_watts_range_list() // In this case, 1core == AllCores current_power_values.push_back(current_power_values.at(1)); } else { // size == 3 - current_power_values[2] = current_power_values.at(1); + current_power_values[1] = current_power_values.at(2); + current_power_values[2] = current_power_values.at(2); static bool displayed_warning = false; if (not displayed_warning) { // Otherwise we get in the worst case no_pstate*no_hosts warnings XBT_WARN("Host %s is a single-core machine and part of the power profile is '%s'" ", which is in the 'Idle:OneCore:AllCores' format." - " Since this is a single-core machine, AllCores and OneCore are identical." - " Here, only the value for 'OneCore' is used.", host_->get_cname(), current_power_values_str.c_str()); + " Here, only the value for 'AllCores' is used.", host_->get_cname(), current_power_values_str.c_str()); displayed_warning = true; } } @@ -399,10 +411,10 @@ static void on_creation(simgrid::s4u::Host& host) host.extension_set(new HostEnergy(&host)); } -static void on_action_state_change(simgrid::surf::CpuAction* action, +static void on_action_state_change(simgrid::kernel::resource::CpuAction const& action, simgrid::kernel::resource::Action::State /*previous*/) { - for (simgrid::surf::Cpu* const& cpu : action->cpus()) { + for (simgrid::kernel::resource::Cpu* const& cpu : action.cpus()) { simgrid::s4u::Host* host = cpu->get_host(); if (host != nullptr) { @@ -422,9 +434,9 @@ static void on_action_state_change(simgrid::surf::CpuAction* action, /* This callback is fired either when the host changes its state (on/off) ("onStateChange") or its speed * (because the user changed the pstate, or because of external trace events) ("onSpeedChange") */ -static void on_host_change(simgrid::s4u::Host& host) +static void on_host_change(simgrid::s4u::Host const& host) { - if (dynamic_cast(&host)) // Ignore virtual machines + if (dynamic_cast(&host)) // Ignore virtual machines return; HostEnergy* host_energy = host.extension(); @@ -432,9 +444,9 @@ static void on_host_change(simgrid::s4u::Host& host) host_energy->update(); } -static void on_host_destruction(simgrid::s4u::Host& host) +static void on_host_destruction(simgrid::s4u::Host const& host) { - if (dynamic_cast(&host)) // Ignore virtual machines + if (dynamic_cast(&host)) // Ignore virtual machines return; XBT_INFO("Energy consumption of host %s: %f Joules", host.get_cname(), @@ -477,20 +489,21 @@ void sg_host_energy_plugin_init() simgrid::s4u::Host::on_state_change.connect(&on_host_change); simgrid::s4u::Host::on_speed_change.connect(&on_host_change); simgrid::s4u::Host::on_destruction.connect(&on_host_destruction); - simgrid::s4u::on_simulation_end.connect(&on_simulation_end); - simgrid::surf::CpuAction::on_state_change.connect(&on_action_state_change); + simgrid::s4u::Engine::on_simulation_end.connect(&on_simulation_end); + simgrid::kernel::resource::CpuAction::on_state_change.connect(&on_action_state_change); // We may only have one actor on a node. If that actor executes something like // compute -> recv -> compute // the recv operation will not trigger a "CpuAction::on_state_change". This means // that the next trigger would be the 2nd compute, hence ignoring the idle time // during the recv call. By updating at the beginning of a compute, we can // fix that. (If the cpu is not idle, this is not required.) - simgrid::kernel::activity::ExecImpl::on_creation.connect([](simgrid::kernel::activity::ExecImplPtr activity){ - if (activity->host_ != nullptr) { // We only run on one host - simgrid::s4u::Host* host = activity->host_; - if (dynamic_cast(activity->host_)) - host = dynamic_cast(activity->host_)->get_pm(); - + simgrid::kernel::activity::ExecImpl::on_creation.connect([](simgrid::kernel::activity::ExecImpl const& activity) { + if (activity.get_host_number() == 1) { // We only run on one host + simgrid::s4u::Host* host = activity.get_host(); + simgrid::s4u::VirtualMachine* vm = dynamic_cast(host); + if (vm != nullptr) + host = vm->get_pm(); + xbt_assert(host != nullptr); host->extension()->update(); } }); @@ -504,11 +517,13 @@ void sg_host_energy_plugin_init() */ void sg_host_energy_update_all() { - simgrid::simix::simcall([]() { + simgrid::kernel::actor::simcall([]() { std::vector list = simgrid::s4u::Engine::get_instance()->get_all_hosts(); for (auto const& host : list) - if (dynamic_cast(host) == nullptr) // Ignore virtual machines + if (dynamic_cast(host) == nullptr) { // Ignore virtual machines + xbt_assert(host != nullptr); host->extension()->update(); + } }); } @@ -526,6 +541,16 @@ double sg_host_get_consumed_energy(sg_host_t host) return host->extension()->get_consumed_energy(); } +/** @ingroup plugin_energy + * @brief Get the amount of watt dissipated when the host is idling + */ +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."); + return host->extension()->get_idle_consumption(); +} + /** @ingroup plugin_energy * @brief Get the amount of watt dissipated at the given pstate when the host is idling */