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 | |
| 45 | void NetTargetDecrease::run(Zone& zone, const Group& group) |
| 46 | { |
| 47 | auto netDelta = zone.getDecDelta(); |
| 48 | for (const auto& member : group.getMembers()) |
| 49 | { |
| 50 | try |
| 51 | { |
| 52 | auto value = Manager::getObjValueVariant( |
| 53 | member, group.getInterface(), group.getProperty()); |
| 54 | if (std::holds_alternative<int64_t>(value) || |
| 55 | std::holds_alternative<double>(value)) |
| 56 | { |
| 57 | if (value >= _state) |
| 58 | { |
| 59 | // No decrease allowed for this group |
| 60 | netDelta = 0; |
| 61 | break; |
| 62 | } |
| 63 | else |
| 64 | { |
| 65 | // Decrease factor is the difference in configured state to |
| 66 | // the current value's state |
| 67 | uint64_t deltaFactor = 0; |
| 68 | if (auto dblPtr = std::get_if<double>(&value)) |
| 69 | { |
| 70 | deltaFactor = static_cast<uint64_t>( |
| 71 | std::get<double>(_state) - *dblPtr); |
| 72 | } |
| 73 | else |
| 74 | { |
| 75 | deltaFactor = |
| 76 | static_cast<uint64_t>(std::get<int64_t>(_state) - |
| 77 | std::get<int64_t>(value)); |
| 78 | } |
| 79 | |
| 80 | // Multiply the decrease factor by the configured delta to |
| 81 | // get the net decrease delta for the given group member. |
| 82 | // The lowest net decrease delta of the entire group is the |
| 83 | // decrease requested. |
| 84 | if (netDelta == 0) |
| 85 | { |
| 86 | netDelta = deltaFactor * _delta; |
| 87 | } |
| 88 | else |
| 89 | { |
| 90 | netDelta = std::min(netDelta, deltaFactor * _delta); |
| 91 | } |
| 92 | } |
| 93 | } |
| 94 | else if (std::holds_alternative<bool>(value) || |
| 95 | std::holds_alternative<std::string>(value)) |
| 96 | { |
| 97 | // Where a group of booleans or strings equal the state |
| 98 | // provided, request a decrease of the configured delta |
| 99 | if (_state == value) |
| 100 | { |
| 101 | if (netDelta == 0) |
| 102 | { |
| 103 | netDelta = _delta; |
| 104 | } |
| 105 | else |
| 106 | { |
| 107 | netDelta = std::min(netDelta, _delta); |
| 108 | } |
| 109 | } |
| 110 | } |
| 111 | else |
| 112 | { |
| 113 | // Unsupported group member type for this action |
| 114 | log<level::ERR>( |
| 115 | fmt::format("Action {}: Unsupported group member type " |
| 116 | "given. [object = {} : {} : {}]", |
| 117 | ActionBase::getName(), member, |
| 118 | group.getInterface(), group.getProperty()) |
| 119 | .c_str()); |
| 120 | } |
| 121 | } |
| 122 | catch (const std::out_of_range& oore) |
| 123 | { |
| 124 | // Property value not found, netDelta unchanged |
| 125 | } |
| 126 | } |
| 127 | // Update group's decrease allowed state |
| 128 | zone.setDecreaseAllow(group.getName(), !(netDelta == 0)); |
| 129 | // Request target decrease to occur on decrease interval |
| 130 | zone.requestDecrease(netDelta); |
| 131 | } |
| 132 | |
| 133 | void NetTargetDecrease::setState(const json& jsonObj) |
| 134 | { |
| 135 | if (!jsonObj.contains("state")) |
| 136 | { |
| 137 | throw ActionParseError{ActionBase::getName(), |
| 138 | "Missing required state value"}; |
| 139 | } |
| 140 | _state = getJsonValue(jsonObj["state"]); |
| 141 | } |
| 142 | |
| 143 | void NetTargetDecrease::setDelta(const json& jsonObj) |
| 144 | { |
| 145 | if (!jsonObj.contains("delta")) |
| 146 | { |
| 147 | throw ActionParseError{ActionBase::getName(), |
| 148 | "Missing required delta value"}; |
| 149 | } |
| 150 | _delta = jsonObj["delta"].get<uint64_t>(); |
| 151 | } |
| 152 | |
| 153 | } // namespace phosphor::fan::control::json |