Matthew Barth | 45c44ea | 2021-03-03 13:16:14 -0600 | [diff] [blame] | 1 | /** |
| 2 | * Copyright © 2021 IBM Corporation |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | #include "net_target_decrease.hpp" |
| 17 | |
| 18 | #include "../manager.hpp" |
| 19 | #include "../zone.hpp" |
| 20 | #include "action.hpp" |
| 21 | #include "group.hpp" |
| 22 | |
| 23 | #include <fmt/format.h> |
| 24 | |
| 25 | #include <nlohmann/json.hpp> |
| 26 | #include <phosphor-logging/log.hpp> |
| 27 | |
| 28 | #include <algorithm> |
| 29 | #include <variant> |
| 30 | |
| 31 | namespace phosphor::fan::control::json |
| 32 | { |
| 33 | |
| 34 | using json = nlohmann::json; |
| 35 | using namespace phosphor::logging; |
| 36 | |
Matthew Barth | 19c7749 | 2021-04-08 10:06:06 -0500 | [diff] [blame] | 37 | NetTargetDecrease::NetTargetDecrease(const json& jsonObj, |
| 38 | const std::vector<Group>& groups) : |
| 39 | ActionBase(jsonObj, groups) |
Matthew Barth | 45c44ea | 2021-03-03 13:16:14 -0600 | [diff] [blame] | 40 | { |
| 41 | setState(jsonObj); |
| 42 | setDelta(jsonObj); |
| 43 | } |
| 44 | |
Matthew Barth | 6d2476c | 2021-04-08 10:48:57 -0500 | [diff] [blame] | 45 | void NetTargetDecrease::run(Zone& zone) |
Matthew Barth | 45c44ea | 2021-03-03 13:16:14 -0600 | [diff] [blame] | 46 | { |
Matthew Barth | 20afdda | 2021-12-01 14:30:50 -0600 | [diff] [blame] | 47 | if (!_stateParameter.empty()) |
| 48 | { |
| 49 | auto s = Manager::getParameter(_stateParameter); |
| 50 | if (!s) |
| 51 | { |
Matthew Barth | 20afdda | 2021-12-01 14:30:50 -0600 | [diff] [blame] | 52 | return; |
| 53 | } |
| 54 | _state = *s; |
| 55 | } |
| 56 | |
Matthew Barth | 45c44ea | 2021-03-03 13:16:14 -0600 | [diff] [blame] | 57 | auto netDelta = zone.getDecDelta(); |
Matthew Barth | 6d2476c | 2021-04-08 10:48:57 -0500 | [diff] [blame] | 58 | for (const auto& group : _groups) |
Matthew Barth | 45c44ea | 2021-03-03 13:16:14 -0600 | [diff] [blame] | 59 | { |
Matthew Barth | 6d2476c | 2021-04-08 10:48:57 -0500 | [diff] [blame] | 60 | for (const auto& member : group.getMembers()) |
Matthew Barth | 45c44ea | 2021-03-03 13:16:14 -0600 | [diff] [blame] | 61 | { |
Matthew Barth | 6d2476c | 2021-04-08 10:48:57 -0500 | [diff] [blame] | 62 | try |
Matthew Barth | 45c44ea | 2021-03-03 13:16:14 -0600 | [diff] [blame] | 63 | { |
Matthew Barth | 6d2476c | 2021-04-08 10:48:57 -0500 | [diff] [blame] | 64 | auto value = Manager::getObjValueVariant( |
| 65 | member, group.getInterface(), group.getProperty()); |
| 66 | if (std::holds_alternative<int64_t>(value) || |
| 67 | std::holds_alternative<double>(value)) |
Matthew Barth | 45c44ea | 2021-03-03 13:16:14 -0600 | [diff] [blame] | 68 | { |
Matthew Barth | 6d2476c | 2021-04-08 10:48:57 -0500 | [diff] [blame] | 69 | if (value >= _state) |
| 70 | { |
| 71 | // No decrease allowed for this group |
| 72 | netDelta = 0; |
| 73 | break; |
| 74 | } |
| 75 | else |
| 76 | { |
| 77 | // Decrease factor is the difference in configured state |
| 78 | // to the current value's state |
| 79 | uint64_t deltaFactor = 0; |
| 80 | if (auto dblPtr = std::get_if<double>(&value)) |
| 81 | { |
| 82 | deltaFactor = static_cast<uint64_t>( |
| 83 | std::get<double>(_state) - *dblPtr); |
| 84 | } |
| 85 | else |
| 86 | { |
| 87 | deltaFactor = static_cast<uint64_t>( |
| 88 | std::get<int64_t>(_state) - |
| 89 | std::get<int64_t>(value)); |
| 90 | } |
| 91 | |
| 92 | // Multiply the decrease factor by the configured delta |
| 93 | // to get the net decrease delta for the given group |
| 94 | // member. The lowest net decrease delta of the entire |
| 95 | // group is the decrease requested. |
| 96 | if (netDelta == 0) |
| 97 | { |
| 98 | netDelta = deltaFactor * _delta; |
| 99 | } |
| 100 | else |
| 101 | { |
| 102 | netDelta = std::min(netDelta, deltaFactor * _delta); |
| 103 | } |
| 104 | } |
| 105 | } |
| 106 | else if (std::holds_alternative<bool>(value) || |
| 107 | std::holds_alternative<std::string>(value)) |
| 108 | { |
| 109 | // Where a group of booleans or strings equal the state |
| 110 | // provided, request a decrease of the configured delta |
| 111 | if (_state == value) |
| 112 | { |
| 113 | if (netDelta == 0) |
| 114 | { |
| 115 | netDelta = _delta; |
| 116 | } |
| 117 | else |
| 118 | { |
| 119 | netDelta = std::min(netDelta, _delta); |
| 120 | } |
| 121 | } |
Matthew Barth | 45c44ea | 2021-03-03 13:16:14 -0600 | [diff] [blame] | 122 | } |
| 123 | else |
| 124 | { |
Matthew Barth | 6d2476c | 2021-04-08 10:48:57 -0500 | [diff] [blame] | 125 | // Unsupported group member type for this action |
| 126 | log<level::ERR>( |
| 127 | fmt::format("Action {}: Unsupported group member type " |
| 128 | "given. [object = {} : {} : {}]", |
| 129 | ActionBase::getName(), member, |
| 130 | group.getInterface(), group.getProperty()) |
| 131 | .c_str()); |
Matthew Barth | 45c44ea | 2021-03-03 13:16:14 -0600 | [diff] [blame] | 132 | } |
| 133 | } |
Matthew Barth | 6d2476c | 2021-04-08 10:48:57 -0500 | [diff] [blame] | 134 | catch (const std::out_of_range& oore) |
Matthew Barth | 45c44ea | 2021-03-03 13:16:14 -0600 | [diff] [blame] | 135 | { |
Matthew Barth | 6d2476c | 2021-04-08 10:48:57 -0500 | [diff] [blame] | 136 | // Property value not found, netDelta unchanged |
Matthew Barth | 45c44ea | 2021-03-03 13:16:14 -0600 | [diff] [blame] | 137 | } |
| 138 | } |
Matthew Barth | 6d2476c | 2021-04-08 10:48:57 -0500 | [diff] [blame] | 139 | // Update group's decrease allowed state |
| 140 | zone.setDecreaseAllow(group.getName(), !(netDelta == 0)); |
Matthew Barth | 45c44ea | 2021-03-03 13:16:14 -0600 | [diff] [blame] | 141 | } |
Matthew Barth | 45c44ea | 2021-03-03 13:16:14 -0600 | [diff] [blame] | 142 | // Request target decrease to occur on decrease interval |
| 143 | zone.requestDecrease(netDelta); |
| 144 | } |
| 145 | |
| 146 | void NetTargetDecrease::setState(const json& jsonObj) |
| 147 | { |
Matthew Barth | 20afdda | 2021-12-01 14:30:50 -0600 | [diff] [blame] | 148 | if (jsonObj.contains("state")) |
Matthew Barth | 45c44ea | 2021-03-03 13:16:14 -0600 | [diff] [blame] | 149 | { |
Matthew Barth | 20afdda | 2021-12-01 14:30:50 -0600 | [diff] [blame] | 150 | _state = getJsonValue(jsonObj["state"]); |
Matthew Barth | 45c44ea | 2021-03-03 13:16:14 -0600 | [diff] [blame] | 151 | } |
Matthew Barth | 20afdda | 2021-12-01 14:30:50 -0600 | [diff] [blame] | 152 | else if (jsonObj.contains("state_parameter_name")) |
| 153 | { |
| 154 | _stateParameter = jsonObj["state_parameter_name"].get<std::string>(); |
| 155 | } |
| 156 | else |
| 157 | { |
| 158 | throw ActionParseError{ |
| 159 | ActionBase::getName(), |
| 160 | "Missing required state or state_parameter_name value"}; |
| 161 | } |
Matthew Barth | 45c44ea | 2021-03-03 13:16:14 -0600 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | void NetTargetDecrease::setDelta(const json& jsonObj) |
| 165 | { |
| 166 | if (!jsonObj.contains("delta")) |
| 167 | { |
| 168 | throw ActionParseError{ActionBase::getName(), |
| 169 | "Missing required delta value"}; |
| 170 | } |
| 171 | _delta = jsonObj["delta"].get<uint64_t>(); |
| 172 | } |
| 173 | |
| 174 | } // namespace phosphor::fan::control::json |