From bc65160987751be9cc0070284395ec1d752de0f1 Mon Sep 17 00:00:00 2001 From: Matthew Barth Date: Thu, 10 Aug 2017 16:59:43 -0500 Subject: Handle any missing properties for actions Update getting a property value from the zone's cache to throw an exception when not found. Handle these exceptions appropriately for each action where it gets each property value of a group. Change-Id: Icbc0b04f492d3680de77dbe3f27cabf7504ce6b4 Signed-off-by: Matthew Barth --- control/actions.hpp | 279 +++++++++++++++++++++++++++++++--------------------- control/zone.hpp | 2 +- 2 files changed, 167 insertions(+), 114 deletions(-) (limited to 'control') diff --git a/control/actions.hpp b/control/actions.hpp index 325cbbd..ebdff60 100644 --- a/control/actions.hpp +++ b/control/actions.hpp @@ -37,10 +37,18 @@ auto count_state_before_speed(size_t count, T&& state, uint64_t speed) group.end(), [&zone, &state](auto const& entry) { - return zone.template getPropertyValue( - entry.first, - std::get(entry.second), - std::get(entry.second)) == state; + try + { + return zone.template getPropertyValue( + entry.first, + std::get(entry.second), + std::get(entry.second)) == state; + } + catch (const std::out_of_range& oore) + { + // Default to property not equal when not found + return false; + } }); // Update group's fan control active allowed based on action results zone.setActiveAllow(&group, !(numAtState >= count)); @@ -71,29 +79,42 @@ auto set_floor_from_average_sensor_value( auto speed = zone.getDefFloor(); if (group.size() != 0) { + auto count = 0; auto sumValue = std::accumulate( group.begin(), group.end(), 0, - [&zone](int64_t sum, auto const& entry) + [&zone, &count](int64_t sum, auto const& entry) { - return sum + zone.template getPropertyValue( - entry.first, - std::get(entry.second), - std::get(entry.second)); + try + { + return sum + + zone.template getPropertyValue( + entry.first, + std::get(entry.second), + std::get(entry.second)); + } + catch (const std::out_of_range& oore) + { + count++; + return sum; + } }); - auto avgValue= sumValue / group.size(); - auto it = std::find_if( - val_to_speed.begin(), - val_to_speed.end(), - [&avgValue](auto const& entry) + if ((group.size() - count) > 0) + { + auto avgValue = sumValue / (group.size() - count); + auto it = std::find_if( + val_to_speed.begin(), + val_to_speed.end(), + [&avgValue](auto const& entry) + { + return avgValue < entry.first; + } + ); + if (it != std::end(val_to_speed)) { - return avgValue < entry.first; + speed = (*it).second; } - ); - if (it != std::end(val_to_speed)) - { - speed = (*it).second; } } zone.setFloor(speed); @@ -122,84 +143,99 @@ auto set_ceiling_from_average_sensor_value( auto speed = zone.getCeiling(); if (group.size() != 0) { + auto count = 0; auto sumValue = std::accumulate( group.begin(), group.end(), 0, - [&zone](int64_t sum, auto const& entry) + [&zone, &count](int64_t sum, auto const& entry) { - return sum + zone.template getPropertyValue( - entry.first, - std::get(entry.second), - std::get(entry.second)); - }); - auto avgValue = sumValue / group.size(); - auto prevValue = zone.swapCeilingKeyValue(avgValue); - if (avgValue != prevValue) - {// Only check if previous and new values differ - if (avgValue < prevValue) - {// Value is decreasing from previous - for (auto it = val_to_speed.rbegin(); - it != val_to_speed.rend(); - ++it) - { - if (it == val_to_speed.rbegin() && - avgValue >= it->first) + try { - // Value is at/above last map key, - // set ceiling speed to the last map key's value - speed = it->second; - break; + return sum + + zone.template getPropertyValue( + entry.first, + std::get(entry.second), + std::get(entry.second)); } - else if (std::next(it, 1) == val_to_speed.rend() && - avgValue <= it->first) + catch (const std::out_of_range& oore) { - // Value is at/below first map key, - // set ceiling speed to the first map key's value - speed = it->second; - break; + count++; + return sum; } - if (avgValue < it->first && - it->first <= prevValue) + }); + if ((group.size() - count) > 0) + { + auto avgValue = sumValue / (group.size() - count); + auto prevValue = zone.swapCeilingKeyValue(avgValue); + if (avgValue != prevValue) + {// Only check if previous and new values differ + if (avgValue < prevValue) + {// Value is decreasing from previous + for (auto it = val_to_speed.rbegin(); + it != val_to_speed.rend(); + ++it) { - // Value decreased & transitioned across a map key, - // update ceiling speed to this map key's value - // when new value is below map's key and the key - // is at/below the previous value - speed = it->second; + if (it == val_to_speed.rbegin() && + avgValue >= it->first) + { + // Value is at/above last map key, set + // ceiling speed to the last map key's value + speed = it->second; + break; + } + else if (std::next(it, 1) == val_to_speed.rend() && + avgValue <= it->first) + { + // Value is at/below first map key, set + // ceiling speed to the first map key's value + speed = it->second; + break; + } + if (avgValue < it->first && + it->first <= prevValue) + { + // Value decreased & transitioned across + // a map key, update ceiling speed to this + // map key's value when new value is below + // map's key and the key is at/below the + // previous value + speed = it->second; + } } } - } - else - {// Value is increasing from previous - for (auto it = val_to_speed.begin(); - it != val_to_speed.end(); - ++it) - { - if (it == val_to_speed.begin() && - avgValue <= it->first) - { - // Value is at/below first map key, - // set ceiling speed to the first map key's value - speed = it->second; - break; - } - else if (std::next(it, 1) == val_to_speed.end() && - avgValue >= it->first) - { - // Value is at/above last map key, - // set ceiling speed to the last map key's value - speed = it->second; - break; - } - if (avgValue > it->first && - it->first >= prevValue) + else + {// Value is increasing from previous + for (auto it = val_to_speed.begin(); + it != val_to_speed.end(); + ++it) { - // Value increased & transitioned across a map key, - // update ceiling speed to this map key's value - // when new value is above map's key and the key - // is at/above the previous value - speed = it->second; + if (it == val_to_speed.begin() && + avgValue <= it->first) + { + // Value is at/below first map key, set + // ceiling speed to the first map key's value + speed = it->second; + break; + } + else if (std::next(it, 1) == val_to_speed.end() && + avgValue >= it->first) + { + // Value is at/above last map key, set + // ceiling speed to the last map key's value + speed = it->second; + break; + } + if (avgValue > it->first && + it->first >= prevValue) + { + // Value increased & transitioned across + // a map key, update ceiling speed to this + // map key's value when new value is above + // map's key and the key is at/above the + // previous value + speed = it->second; + } } } } @@ -228,25 +264,35 @@ auto set_net_increase_speed(T&& state, uint64_t speedDelta) return [speedDelta, state = std::forward(state)](auto& zone, auto& group) { + T singleDelta = 1; auto netDelta = zone.getIncSpeedDelta(); std::for_each( group.begin(), group.end(), - [&zone, &state, &speedDelta, &netDelta](auto const& entry) + [&zone, &state, &speedDelta, &singleDelta, &netDelta]( + auto const& entry) { - T value = zone.template getPropertyValue( - entry.first, - std::get(entry.second), - std::get(entry.second)); - // TODO openbmc/phosphor-fan-presence#7 - Support possible - // state types for comparison - if (value >= state) + try + { + T value = zone.template getPropertyValue( + entry.first, + std::get(entry.second), + std::get(entry.second)); + // TODO openbmc/phosphor-fan-presence#7 - Support possible + // state types for comparison + if (value >= state) + { + // Increase by at least a single delta + // to attempt bringing under 'state' + auto delta = std::max((value - state), singleDelta); + // Increase is the difference times + // the given speed delta + netDelta = std::max(netDelta, delta * speedDelta); + } + } + catch (const std::out_of_range& oore) { - // Increase by at least a single delta - // to attempt bringing under 'state' - auto delta = std::max((value - state), 1); - // Increase is the difference times the given speed delta - netDelta = std::max(netDelta, delta * speedDelta); + // Property value not found, netDelta unchanged } } ); @@ -280,26 +326,33 @@ auto set_net_decrease_speed(T&& state, uint64_t speedDelta) group.end(), [&zone, &state, &speedDelta, &netDelta](auto const& entry) { - T value = zone.template getPropertyValue( - entry.first, - std::get(entry.second), - std::get(entry.second)); - // TODO openbmc/phosphor-fan-presence#7 - Support possible - // state types for comparison - if (value < state) + try { - if (netDelta == 0) + T value = zone.template getPropertyValue( + entry.first, + std::get(entry.second), + std::get(entry.second)); + // TODO openbmc/phosphor-fan-presence#7 - Support possible + // state types for comparison + if (value < state) { - netDelta = (state - value) * speedDelta; - } - else - { - // Decrease is the difference times - // the given speed delta - netDelta = std::min(netDelta, - (state - value) * speedDelta); + if (netDelta == 0) + { + netDelta = (state - value) * speedDelta; + } + else + { + // Decrease is the difference times + // the given speed delta + netDelta = std::min(netDelta, + (state - value) * speedDelta); + } } } + catch (const std::out_of_range& oore) + { + // Property value not found, netDelta unchanged + } } ); // Request speed decrease to occur on decrease interval diff --git a/control/zone.hpp b/control/zone.hpp index 32707cb..a56b1c7 100644 --- a/control/zone.hpp +++ b/control/zone.hpp @@ -114,7 +114,7 @@ class Zone const std::string& property) { return sdbusplus::message::variant_ns::get( - _properties[object][interface][property]); + _properties.at(object).at(interface).at(property)); }; /** -- cgit v1.2.1