Matthew Barth | 17d1fe2 | 2017-05-11 15:00:36 -0500 | [diff] [blame] | 1 | #pragma once |
| 2 | |
| 3 | #include <algorithm> |
Matthew Barth | 4af419c | 2017-06-12 13:39:31 -0500 | [diff] [blame] | 4 | #include <numeric> |
Matthew Barth | b280bfa | 2017-09-15 09:56:50 -0500 | [diff] [blame] | 5 | #include "types.hpp" |
| 6 | #include "zone.hpp" |
Matthew Barth | 17d1fe2 | 2017-05-11 15:00:36 -0500 | [diff] [blame] | 7 | |
| 8 | namespace phosphor |
| 9 | { |
| 10 | namespace fan |
| 11 | { |
| 12 | namespace control |
| 13 | { |
| 14 | namespace action |
| 15 | { |
| 16 | |
| 17 | /** |
Matthew Barth | 2a646c5 | 2017-10-05 17:04:11 -0500 | [diff] [blame^] | 18 | * @brief An action that wraps a list of actions with a timer |
| 19 | * @details Sets up a list of actions to be invoked when the defined timer |
| 20 | * expires (or for each expiration of a repeating timer). |
| 21 | * |
| 22 | * @param[in] tConf - Timer configuration parameters |
| 23 | * @param[in] action - List of actions to be called when the timer expires |
| 24 | * |
| 25 | * @return Action lambda function |
| 26 | * An Action function that creates a timer |
| 27 | */ |
| 28 | Action call_actions_based_on_timer( |
| 29 | Timer&& tConf, |
| 30 | std::vector<Action>&& actions); |
| 31 | |
| 32 | /** |
Matthew Barth | b280bfa | 2017-09-15 09:56:50 -0500 | [diff] [blame] | 33 | * @brief An action to set the request speed base |
| 34 | * @details A new target speed is determined using a speed delta being added |
| 35 | * or subtracted, for increases or decrease respectively, from a base speed. |
| 36 | * This base speed defaults to be the current target speed or is set to a |
| 37 | * different base speed(i.e. the fans' tach feedback speed) to request a new |
| 38 | * target from. |
| 39 | * |
| 40 | * @param[in] zone - Zone containing fans |
| 41 | * @param[in] group - Group of sensors to determine base from |
| 42 | */ |
| 43 | void set_request_speed_base_with_max(Zone& zone, const Group& group); |
| 44 | |
| 45 | /** |
Matthew Barth | 17d1fe2 | 2017-05-11 15:00:36 -0500 | [diff] [blame] | 46 | * @brief An action to set the speed on a zone |
Matthew Barth | 861d77c | 2017-05-22 14:18:25 -0500 | [diff] [blame] | 47 | * @details The zone is held at the given speed when a defined number of |
Matthew Barth | 17d1fe2 | 2017-05-11 15:00:36 -0500 | [diff] [blame] | 48 | * properties in the group are set to the given state |
| 49 | * |
| 50 | * @param[in] count - Number of properties |
| 51 | * @param[in] state - Value the property(s) needed to be set at |
| 52 | * @param[in] speed - Speed to set the zone to |
| 53 | * |
| 54 | * @return Lambda function |
| 55 | * A lambda function to set the zone speed when the number of properties |
| 56 | * within the group are at a certain value |
| 57 | */ |
Matthew Barth | 9e741ed | 2017-06-02 16:29:09 -0500 | [diff] [blame] | 58 | template <typename T> |
| 59 | auto count_state_before_speed(size_t count, T&& state, uint64_t speed) |
Matthew Barth | 17d1fe2 | 2017-05-11 15:00:36 -0500 | [diff] [blame] | 60 | { |
Matthew Barth | 9e741ed | 2017-06-02 16:29:09 -0500 | [diff] [blame] | 61 | return [count, |
| 62 | speed, |
| 63 | state = std::forward<T>(state)](auto& zone, auto& group) |
Matthew Barth | 17d1fe2 | 2017-05-11 15:00:36 -0500 | [diff] [blame] | 64 | { |
| 65 | size_t numAtState = std::count_if( |
| 66 | group.begin(), |
| 67 | group.end(), |
| 68 | [&zone, &state](auto const& entry) |
| 69 | { |
Matthew Barth | bc65160 | 2017-08-10 16:59:43 -0500 | [diff] [blame] | 70 | try |
| 71 | { |
| 72 | return zone.template getPropertyValue<T>( |
| 73 | entry.first, |
| 74 | std::get<intfPos>(entry.second), |
| 75 | std::get<propPos>(entry.second)) == state; |
| 76 | } |
| 77 | catch (const std::out_of_range& oore) |
| 78 | { |
| 79 | // Default to property not equal when not found |
| 80 | return false; |
| 81 | } |
Matthew Barth | 17d1fe2 | 2017-05-11 15:00:36 -0500 | [diff] [blame] | 82 | }); |
Matthew Barth | 17d1fe2 | 2017-05-11 15:00:36 -0500 | [diff] [blame] | 83 | if (numAtState >= count) |
| 84 | { |
| 85 | zone.setSpeed(speed); |
| 86 | } |
Matthew Barth | 60b0076 | 2017-08-15 13:39:06 -0500 | [diff] [blame] | 87 | // Update group's fan control active allowed based on action results |
| 88 | zone.setActiveAllow(&group, !(numAtState >= count)); |
Matthew Barth | 17d1fe2 | 2017-05-11 15:00:36 -0500 | [diff] [blame] | 89 | }; |
| 90 | } |
| 91 | |
Matthew Barth | 4af419c | 2017-06-12 13:39:31 -0500 | [diff] [blame] | 92 | /** |
| 93 | * @brief An action to set the floor speed on a zone |
| 94 | * @details Based on the average of the defined sensor group values, the floor |
| 95 | * speed is selected from the first map key entry that the average sensor value |
| 96 | * is less than. |
| 97 | * |
| 98 | * @param[in] val_to_speed - Ordered map of sensor value-to-speed |
| 99 | * |
Matthew Barth | b280bfa | 2017-09-15 09:56:50 -0500 | [diff] [blame] | 100 | * @return Action lambda function |
| 101 | * An Action function to set the zone's floor speed when the average of |
Matthew Barth | 4af419c | 2017-06-12 13:39:31 -0500 | [diff] [blame] | 102 | * property values within the group is below the lowest sensor value given |
| 103 | */ |
Matthew Barth | b280bfa | 2017-09-15 09:56:50 -0500 | [diff] [blame] | 104 | Action set_floor_from_average_sensor_value( |
| 105 | std::map<int64_t, uint64_t>&& val_to_speed); |
Matthew Barth | 4af419c | 2017-06-12 13:39:31 -0500 | [diff] [blame] | 106 | |
Matthew Barth | e0ca13e | 2017-06-13 16:29:09 -0500 | [diff] [blame] | 107 | /** |
| 108 | * @brief An action to set the ceiling speed on a zone |
Matthew Barth | b280bfa | 2017-09-15 09:56:50 -0500 | [diff] [blame] | 109 | * @details Based on the average of the defined sensor group values, the |
| 110 | * ceiling speed is selected from the map key transition point that the average |
| 111 | * sensor value falls within depending on the key values direction from what |
| 112 | * was previously read. |
Matthew Barth | e0ca13e | 2017-06-13 16:29:09 -0500 | [diff] [blame] | 113 | * |
| 114 | * @param[in] val_to_speed - Ordered map of sensor value-to-speed transitions |
| 115 | * |
Matthew Barth | b280bfa | 2017-09-15 09:56:50 -0500 | [diff] [blame] | 116 | * @return Action lambda function |
| 117 | * An Action function to set the zone's ceiling speed when the average of |
Matthew Barth | e0ca13e | 2017-06-13 16:29:09 -0500 | [diff] [blame] | 118 | * property values within the group is above(increasing) or |
| 119 | * below(decreasing) the key transition point |
| 120 | */ |
Matthew Barth | b280bfa | 2017-09-15 09:56:50 -0500 | [diff] [blame] | 121 | Action set_ceiling_from_average_sensor_value( |
| 122 | std::map<int64_t, uint64_t>&& val_to_speed); |
Matthew Barth | e0ca13e | 2017-06-13 16:29:09 -0500 | [diff] [blame] | 123 | |
Matthew Barth | 2462352 | 2017-06-21 14:09:57 -0500 | [diff] [blame] | 124 | /** |
| 125 | * @brief An action to set the speed increase delta and request speed change |
| 126 | * @details Provides the ability to determine what the net increase delta the |
| 127 | * zone's fan speeds should be updated by from their current target speed and |
| 128 | * request that new target speed. |
| 129 | * |
| 130 | * @param[in] state - State to compare the group's property value to |
Matthew Barth | 172f393 | 2017-08-14 11:07:46 -0500 | [diff] [blame] | 131 | * @param[in] factor - Factor to apply to the calculated net delta |
Matthew Barth | 2462352 | 2017-06-21 14:09:57 -0500 | [diff] [blame] | 132 | * @param[in] speedDelta - Speed delta of the group |
| 133 | * |
| 134 | * @return Lambda function |
| 135 | * A lambda function that determines the net increase delta and requests |
| 136 | * a new target speed with that increase for the zone. |
| 137 | */ |
| 138 | template <typename T> |
Matthew Barth | 172f393 | 2017-08-14 11:07:46 -0500 | [diff] [blame] | 139 | auto set_net_increase_speed(T&& state, T&& factor, uint64_t speedDelta) |
Matthew Barth | 2462352 | 2017-06-21 14:09:57 -0500 | [diff] [blame] | 140 | { |
| 141 | return [speedDelta, |
Matthew Barth | 172f393 | 2017-08-14 11:07:46 -0500 | [diff] [blame] | 142 | factor = std::forward<T>(factor), |
Matthew Barth | 2462352 | 2017-06-21 14:09:57 -0500 | [diff] [blame] | 143 | state = std::forward<T>(state)](auto& zone, auto& group) |
| 144 | { |
| 145 | auto netDelta = zone.getIncSpeedDelta(); |
| 146 | std::for_each( |
| 147 | group.begin(), |
| 148 | group.end(), |
Matthew Barth | 172f393 | 2017-08-14 11:07:46 -0500 | [diff] [blame] | 149 | [&zone, &state, &factor, &speedDelta, &netDelta]( |
Matthew Barth | bc65160 | 2017-08-10 16:59:43 -0500 | [diff] [blame] | 150 | auto const& entry) |
Matthew Barth | 2462352 | 2017-06-21 14:09:57 -0500 | [diff] [blame] | 151 | { |
Matthew Barth | bc65160 | 2017-08-10 16:59:43 -0500 | [diff] [blame] | 152 | try |
Matthew Barth | 2462352 | 2017-06-21 14:09:57 -0500 | [diff] [blame] | 153 | { |
Matthew Barth | bc65160 | 2017-08-10 16:59:43 -0500 | [diff] [blame] | 154 | T value = zone.template getPropertyValue<T>( |
| 155 | entry.first, |
| 156 | std::get<intfPos>(entry.second), |
| 157 | std::get<propPos>(entry.second)); |
| 158 | // TODO openbmc/phosphor-fan-presence#7 - Support possible |
| 159 | // state types for comparison |
| 160 | if (value >= state) |
| 161 | { |
Matthew Barth | 172f393 | 2017-08-14 11:07:46 -0500 | [diff] [blame] | 162 | // Increase by at least a single delta(factor) |
Matthew Barth | bc65160 | 2017-08-10 16:59:43 -0500 | [diff] [blame] | 163 | // to attempt bringing under 'state' |
Matthew Barth | 172f393 | 2017-08-14 11:07:46 -0500 | [diff] [blame] | 164 | auto delta = std::max( |
| 165 | (value - state), |
| 166 | factor); |
| 167 | // Increase is the factor applied to the |
| 168 | // difference times the given speed delta |
| 169 | netDelta = std::max( |
| 170 | netDelta, |
| 171 | (delta/factor) * speedDelta); |
Matthew Barth | bc65160 | 2017-08-10 16:59:43 -0500 | [diff] [blame] | 172 | } |
| 173 | } |
| 174 | catch (const std::out_of_range& oore) |
| 175 | { |
| 176 | // Property value not found, netDelta unchanged |
Matthew Barth | 2462352 | 2017-06-21 14:09:57 -0500 | [diff] [blame] | 177 | } |
| 178 | } |
| 179 | ); |
Matthew Barth | 240397b | 2017-06-22 11:23:30 -0500 | [diff] [blame] | 180 | // Request speed change for target speed update |
| 181 | zone.requestSpeedIncrease(netDelta); |
Matthew Barth | 2462352 | 2017-06-21 14:09:57 -0500 | [diff] [blame] | 182 | }; |
| 183 | } |
| 184 | |
Matthew Barth | 0ce99d8 | 2017-06-22 15:07:29 -0500 | [diff] [blame] | 185 | /** |
| 186 | * @brief An action to set the speed decrease delta and request speed change |
| 187 | * @details Provides the ability to determine what the net decrease delta each |
| 188 | * zone's fan speeds should be updated by from their current target speed, and |
| 189 | * request that speed change occur on the next decrease interval. |
| 190 | * |
| 191 | * @param[in] state - State to compare the group's property value to |
Matthew Barth | 172f393 | 2017-08-14 11:07:46 -0500 | [diff] [blame] | 192 | * @param[in] factor - Factor to apply to the calculated net delta |
Matthew Barth | 0ce99d8 | 2017-06-22 15:07:29 -0500 | [diff] [blame] | 193 | * @param[in] speedDelta - Speed delta of the group |
| 194 | * |
| 195 | * @return Lambda function |
| 196 | * A lambda function that determines the net decrease delta and requests |
| 197 | * a new target speed with that decrease for the zone. |
| 198 | */ |
| 199 | template <typename T> |
Matthew Barth | 172f393 | 2017-08-14 11:07:46 -0500 | [diff] [blame] | 200 | auto set_net_decrease_speed(T&& state, T&& factor, uint64_t speedDelta) |
Matthew Barth | 0ce99d8 | 2017-06-22 15:07:29 -0500 | [diff] [blame] | 201 | { |
| 202 | return [speedDelta, |
Matthew Barth | 172f393 | 2017-08-14 11:07:46 -0500 | [diff] [blame] | 203 | factor = std::forward<T>(factor), |
Matthew Barth | 0ce99d8 | 2017-06-22 15:07:29 -0500 | [diff] [blame] | 204 | state = std::forward<T>(state)](auto& zone, auto& group) |
| 205 | { |
| 206 | auto netDelta = zone.getDecSpeedDelta(); |
| 207 | std::for_each( |
| 208 | group.begin(), |
| 209 | group.end(), |
Matthew Barth | 172f393 | 2017-08-14 11:07:46 -0500 | [diff] [blame] | 210 | [&zone, &state, &factor, &speedDelta, &netDelta](auto const& entry) |
Matthew Barth | 0ce99d8 | 2017-06-22 15:07:29 -0500 | [diff] [blame] | 211 | { |
Matthew Barth | bc65160 | 2017-08-10 16:59:43 -0500 | [diff] [blame] | 212 | try |
Matthew Barth | 0ce99d8 | 2017-06-22 15:07:29 -0500 | [diff] [blame] | 213 | { |
Matthew Barth | bc65160 | 2017-08-10 16:59:43 -0500 | [diff] [blame] | 214 | T value = zone.template getPropertyValue<T>( |
| 215 | entry.first, |
| 216 | std::get<intfPos>(entry.second), |
| 217 | std::get<propPos>(entry.second)); |
| 218 | // TODO openbmc/phosphor-fan-presence#7 - Support possible |
| 219 | // state types for comparison |
| 220 | if (value < state) |
Matthew Barth | 0ce99d8 | 2017-06-22 15:07:29 -0500 | [diff] [blame] | 221 | { |
Matthew Barth | bc65160 | 2017-08-10 16:59:43 -0500 | [diff] [blame] | 222 | if (netDelta == 0) |
| 223 | { |
Matthew Barth | 172f393 | 2017-08-14 11:07:46 -0500 | [diff] [blame] | 224 | netDelta = ((state - value)/factor) * speedDelta; |
Matthew Barth | bc65160 | 2017-08-10 16:59:43 -0500 | [diff] [blame] | 225 | } |
| 226 | else |
| 227 | { |
Matthew Barth | 172f393 | 2017-08-14 11:07:46 -0500 | [diff] [blame] | 228 | // Decrease is the factor applied to the |
| 229 | // difference times the given speed delta |
| 230 | netDelta = std::min( |
| 231 | netDelta, |
| 232 | ((state - value)/factor) * speedDelta); |
Matthew Barth | bc65160 | 2017-08-10 16:59:43 -0500 | [diff] [blame] | 233 | } |
Matthew Barth | 0ce99d8 | 2017-06-22 15:07:29 -0500 | [diff] [blame] | 234 | } |
Matthew Barth | bc65160 | 2017-08-10 16:59:43 -0500 | [diff] [blame] | 235 | } |
| 236 | catch (const std::out_of_range& oore) |
| 237 | { |
| 238 | // Property value not found, netDelta unchanged |
Matthew Barth | 0ce99d8 | 2017-06-22 15:07:29 -0500 | [diff] [blame] | 239 | } |
| 240 | } |
| 241 | ); |
| 242 | // Request speed decrease to occur on decrease interval |
| 243 | zone.requestSpeedDecrease(netDelta); |
| 244 | }; |
| 245 | } |
| 246 | |
Matthew Barth | 17d1fe2 | 2017-05-11 15:00:36 -0500 | [diff] [blame] | 247 | } // namespace action |
| 248 | } // namespace control |
| 249 | } // namespace fan |
| 250 | } // namespace phosphor |