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( |
William A. Kennington III | 122b843 | 2018-10-30 18:39:21 -0700 | [diff] [blame] | 29 | TimerConf&& tConf, |
Matthew Barth | 2a646c5 | 2017-10-05 17:04:11 -0500 | [diff] [blame] | 30 | std::vector<Action>&& actions); |
| 31 | |
| 32 | /** |
Matthew Barth | 98726c4 | 2017-10-17 10:35:20 -0500 | [diff] [blame] | 33 | * @brief An action that sets the floor to the default fan floor speed |
| 34 | * @details Sets the fan floor to the defined default fan floor speed when a |
| 35 | * service associated to the given group has terminated. Once all services |
| 36 | * are functional and providing the sensors again, the fan floor is allowed |
| 37 | * to be set normally. |
| 38 | * |
| 39 | * @param[in] zone - Zone containing fans |
| 40 | * @param[in] group - Group of sensors to determine services' states |
| 41 | */ |
| 42 | void default_floor_on_missing_owner(Zone& zone, const Group& group); |
| 43 | |
| 44 | /** |
Matthew Barth | 0decd1b | 2017-10-24 15:58:17 -0500 | [diff] [blame] | 45 | * @brief An action to set a speed when a service owner is missing |
| 46 | * @details Sets the fans to the given speed when any service owner associated |
| 47 | * to the group is missing. Once all services are functional and providing |
| 48 | * the event data again, active fan speed changes are allowed. |
| 49 | * |
| 50 | * @param[in] speed - Speed to set the zone to |
| 51 | * |
| 52 | * @return Action lambda function |
| 53 | * An Action function that sets the zone to the given speed if any service |
| 54 | * owners are missing. |
| 55 | */ |
| 56 | Action set_speed_on_missing_owner(uint64_t speed); |
| 57 | |
| 58 | /** |
Matthew Barth | b280bfa | 2017-09-15 09:56:50 -0500 | [diff] [blame] | 59 | * @brief An action to set the request speed base |
| 60 | * @details A new target speed is determined using a speed delta being added |
| 61 | * or subtracted, for increases or decrease respectively, from a base speed. |
| 62 | * This base speed defaults to be the current target speed or is set to a |
| 63 | * different base speed(i.e. the fans' tach feedback speed) to request a new |
| 64 | * target from. |
| 65 | * |
| 66 | * @param[in] zone - Zone containing fans |
| 67 | * @param[in] group - Group of sensors to determine base from |
| 68 | */ |
| 69 | void set_request_speed_base_with_max(Zone& zone, const Group& group); |
| 70 | |
| 71 | /** |
Matthew Barth | 17d1fe2 | 2017-05-11 15:00:36 -0500 | [diff] [blame] | 72 | * @brief An action to set the speed on a zone |
Matthew Barth | 861d77c | 2017-05-22 14:18:25 -0500 | [diff] [blame] | 73 | * @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] | 74 | * properties in the group are set to the given state |
| 75 | * |
| 76 | * @param[in] count - Number of properties |
| 77 | * @param[in] state - Value the property(s) needed to be set at |
| 78 | * @param[in] speed - Speed to set the zone to |
| 79 | * |
| 80 | * @return Lambda function |
| 81 | * A lambda function to set the zone speed when the number of properties |
| 82 | * within the group are at a certain value |
| 83 | */ |
Matthew Barth | 9e741ed | 2017-06-02 16:29:09 -0500 | [diff] [blame] | 84 | template <typename T> |
| 85 | 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] | 86 | { |
Matthew Barth | 9e741ed | 2017-06-02 16:29:09 -0500 | [diff] [blame] | 87 | return [count, |
| 88 | speed, |
| 89 | state = std::forward<T>(state)](auto& zone, auto& group) |
Matthew Barth | 17d1fe2 | 2017-05-11 15:00:36 -0500 | [diff] [blame] | 90 | { |
Matthew Barth | e0f67c8 | 2018-05-08 15:47:12 -0500 | [diff] [blame] | 91 | size_t numAtState = 0; |
| 92 | for (auto& entry : group) |
Matthew Barth | 17d1fe2 | 2017-05-11 15:00:36 -0500 | [diff] [blame] | 93 | { |
Matthew Barth | e0f67c8 | 2018-05-08 15:47:12 -0500 | [diff] [blame] | 94 | try |
| 95 | { |
| 96 | if (zone.template getPropertyValue<T>( |
| 97 | entry.first, |
| 98 | std::get<intfPos>(entry.second), |
| 99 | std::get<propPos>(entry.second)) == state) |
| 100 | { |
| 101 | numAtState++; |
| 102 | } |
| 103 | } |
| 104 | catch (const std::out_of_range& oore) |
| 105 | { |
| 106 | // Default to property not equal when not found |
| 107 | } |
| 108 | if (numAtState >= count) |
| 109 | { |
| 110 | zone.setSpeed(speed); |
| 111 | break; |
| 112 | } |
Matthew Barth | 17d1fe2 | 2017-05-11 15:00:36 -0500 | [diff] [blame] | 113 | } |
Matthew Barth | 60b0076 | 2017-08-15 13:39:06 -0500 | [diff] [blame] | 114 | // Update group's fan control active allowed based on action results |
| 115 | zone.setActiveAllow(&group, !(numAtState >= count)); |
Matthew Barth | 17d1fe2 | 2017-05-11 15:00:36 -0500 | [diff] [blame] | 116 | }; |
| 117 | } |
| 118 | |
Matthew Barth | 4af419c | 2017-06-12 13:39:31 -0500 | [diff] [blame] | 119 | /** |
| 120 | * @brief An action to set the floor speed on a zone |
| 121 | * @details Based on the average of the defined sensor group values, the floor |
| 122 | * speed is selected from the first map key entry that the average sensor value |
| 123 | * is less than. |
| 124 | * |
| 125 | * @param[in] val_to_speed - Ordered map of sensor value-to-speed |
| 126 | * |
Matthew Barth | b280bfa | 2017-09-15 09:56:50 -0500 | [diff] [blame] | 127 | * @return Action lambda function |
| 128 | * 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] | 129 | * property values within the group is below the lowest sensor value given |
| 130 | */ |
Matthew Barth | b280bfa | 2017-09-15 09:56:50 -0500 | [diff] [blame] | 131 | Action set_floor_from_average_sensor_value( |
| 132 | std::map<int64_t, uint64_t>&& val_to_speed); |
Matthew Barth | 4af419c | 2017-06-12 13:39:31 -0500 | [diff] [blame] | 133 | |
Matthew Barth | e0ca13e | 2017-06-13 16:29:09 -0500 | [diff] [blame] | 134 | /** |
| 135 | * @brief An action to set the ceiling speed on a zone |
Matthew Barth | b280bfa | 2017-09-15 09:56:50 -0500 | [diff] [blame] | 136 | * @details Based on the average of the defined sensor group values, the |
| 137 | * ceiling speed is selected from the map key transition point that the average |
| 138 | * sensor value falls within depending on the key values direction from what |
| 139 | * was previously read. |
Matthew Barth | e0ca13e | 2017-06-13 16:29:09 -0500 | [diff] [blame] | 140 | * |
| 141 | * @param[in] val_to_speed - Ordered map of sensor value-to-speed transitions |
| 142 | * |
Matthew Barth | b280bfa | 2017-09-15 09:56:50 -0500 | [diff] [blame] | 143 | * @return Action lambda function |
| 144 | * 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] | 145 | * property values within the group is above(increasing) or |
| 146 | * below(decreasing) the key transition point |
| 147 | */ |
Matthew Barth | b280bfa | 2017-09-15 09:56:50 -0500 | [diff] [blame] | 148 | Action set_ceiling_from_average_sensor_value( |
| 149 | std::map<int64_t, uint64_t>&& val_to_speed); |
Matthew Barth | e0ca13e | 2017-06-13 16:29:09 -0500 | [diff] [blame] | 150 | |
Matthew Barth | 2462352 | 2017-06-21 14:09:57 -0500 | [diff] [blame] | 151 | /** |
| 152 | * @brief An action to set the speed increase delta and request speed change |
| 153 | * @details Provides the ability to determine what the net increase delta the |
| 154 | * zone's fan speeds should be updated by from their current target speed and |
| 155 | * request that new target speed. |
| 156 | * |
| 157 | * @param[in] state - State to compare the group's property value to |
Matthew Barth | 172f393 | 2017-08-14 11:07:46 -0500 | [diff] [blame] | 158 | * @param[in] factor - Factor to apply to the calculated net delta |
Matthew Barth | 2462352 | 2017-06-21 14:09:57 -0500 | [diff] [blame] | 159 | * @param[in] speedDelta - Speed delta of the group |
| 160 | * |
| 161 | * @return Lambda function |
| 162 | * A lambda function that determines the net increase delta and requests |
| 163 | * a new target speed with that increase for the zone. |
| 164 | */ |
| 165 | template <typename T> |
Matthew Barth | 172f393 | 2017-08-14 11:07:46 -0500 | [diff] [blame] | 166 | auto set_net_increase_speed(T&& state, T&& factor, uint64_t speedDelta) |
Matthew Barth | 2462352 | 2017-06-21 14:09:57 -0500 | [diff] [blame] | 167 | { |
| 168 | return [speedDelta, |
Matthew Barth | 172f393 | 2017-08-14 11:07:46 -0500 | [diff] [blame] | 169 | factor = std::forward<T>(factor), |
Matthew Barth | 2462352 | 2017-06-21 14:09:57 -0500 | [diff] [blame] | 170 | state = std::forward<T>(state)](auto& zone, auto& group) |
| 171 | { |
| 172 | auto netDelta = zone.getIncSpeedDelta(); |
| 173 | std::for_each( |
| 174 | group.begin(), |
| 175 | group.end(), |
Matthew Barth | 172f393 | 2017-08-14 11:07:46 -0500 | [diff] [blame] | 176 | [&zone, &state, &factor, &speedDelta, &netDelta]( |
Matthew Barth | bc65160 | 2017-08-10 16:59:43 -0500 | [diff] [blame] | 177 | auto const& entry) |
Matthew Barth | 2462352 | 2017-06-21 14:09:57 -0500 | [diff] [blame] | 178 | { |
Matthew Barth | bc65160 | 2017-08-10 16:59:43 -0500 | [diff] [blame] | 179 | try |
Matthew Barth | 2462352 | 2017-06-21 14:09:57 -0500 | [diff] [blame] | 180 | { |
Matthew Barth | bc65160 | 2017-08-10 16:59:43 -0500 | [diff] [blame] | 181 | T value = zone.template getPropertyValue<T>( |
| 182 | entry.first, |
| 183 | std::get<intfPos>(entry.second), |
| 184 | std::get<propPos>(entry.second)); |
| 185 | // TODO openbmc/phosphor-fan-presence#7 - Support possible |
| 186 | // state types for comparison |
| 187 | if (value >= state) |
| 188 | { |
Matthew Barth | 172f393 | 2017-08-14 11:07:46 -0500 | [diff] [blame] | 189 | // Increase by at least a single delta(factor) |
Matthew Barth | bc65160 | 2017-08-10 16:59:43 -0500 | [diff] [blame] | 190 | // to attempt bringing under 'state' |
Matthew Barth | 172f393 | 2017-08-14 11:07:46 -0500 | [diff] [blame] | 191 | auto delta = std::max( |
| 192 | (value - state), |
| 193 | factor); |
| 194 | // Increase is the factor applied to the |
| 195 | // difference times the given speed delta |
| 196 | netDelta = std::max( |
| 197 | netDelta, |
| 198 | (delta/factor) * speedDelta); |
Matthew Barth | bc65160 | 2017-08-10 16:59:43 -0500 | [diff] [blame] | 199 | } |
| 200 | } |
| 201 | catch (const std::out_of_range& oore) |
| 202 | { |
| 203 | // Property value not found, netDelta unchanged |
Matthew Barth | 2462352 | 2017-06-21 14:09:57 -0500 | [diff] [blame] | 204 | } |
| 205 | } |
| 206 | ); |
Matthew Barth | 240397b | 2017-06-22 11:23:30 -0500 | [diff] [blame] | 207 | // Request speed change for target speed update |
| 208 | zone.requestSpeedIncrease(netDelta); |
Matthew Barth | 2462352 | 2017-06-21 14:09:57 -0500 | [diff] [blame] | 209 | }; |
| 210 | } |
| 211 | |
Matthew Barth | 0ce99d8 | 2017-06-22 15:07:29 -0500 | [diff] [blame] | 212 | /** |
| 213 | * @brief An action to set the speed decrease delta and request speed change |
| 214 | * @details Provides the ability to determine what the net decrease delta each |
| 215 | * zone's fan speeds should be updated by from their current target speed, and |
| 216 | * request that speed change occur on the next decrease interval. |
| 217 | * |
| 218 | * @param[in] state - State to compare the group's property value to |
Matthew Barth | 172f393 | 2017-08-14 11:07:46 -0500 | [diff] [blame] | 219 | * @param[in] factor - Factor to apply to the calculated net delta |
Matthew Barth | 0ce99d8 | 2017-06-22 15:07:29 -0500 | [diff] [blame] | 220 | * @param[in] speedDelta - Speed delta of the group |
| 221 | * |
| 222 | * @return Lambda function |
| 223 | * A lambda function that determines the net decrease delta and requests |
| 224 | * a new target speed with that decrease for the zone. |
| 225 | */ |
| 226 | template <typename T> |
Matthew Barth | 172f393 | 2017-08-14 11:07:46 -0500 | [diff] [blame] | 227 | auto set_net_decrease_speed(T&& state, T&& factor, uint64_t speedDelta) |
Matthew Barth | 0ce99d8 | 2017-06-22 15:07:29 -0500 | [diff] [blame] | 228 | { |
| 229 | return [speedDelta, |
Matthew Barth | 172f393 | 2017-08-14 11:07:46 -0500 | [diff] [blame] | 230 | factor = std::forward<T>(factor), |
Matthew Barth | 0ce99d8 | 2017-06-22 15:07:29 -0500 | [diff] [blame] | 231 | state = std::forward<T>(state)](auto& zone, auto& group) |
| 232 | { |
| 233 | auto netDelta = zone.getDecSpeedDelta(); |
Matthew Barth | e4338cd | 2017-12-14 11:14:30 -0600 | [diff] [blame] | 234 | for (auto& entry : group) |
| 235 | { |
| 236 | try |
Matthew Barth | 0ce99d8 | 2017-06-22 15:07:29 -0500 | [diff] [blame] | 237 | { |
Matthew Barth | e4338cd | 2017-12-14 11:14:30 -0600 | [diff] [blame] | 238 | T value = zone.template getPropertyValue<T>( |
| 239 | entry.first, |
| 240 | std::get<intfPos>(entry.second), |
| 241 | std::get<propPos>(entry.second)); |
| 242 | // TODO openbmc/phosphor-fan-presence#7 - Support possible |
| 243 | // state types for comparison |
| 244 | if (value < state) |
Matthew Barth | 0ce99d8 | 2017-06-22 15:07:29 -0500 | [diff] [blame] | 245 | { |
Matthew Barth | e4338cd | 2017-12-14 11:14:30 -0600 | [diff] [blame] | 246 | if (netDelta == 0) |
Matthew Barth | 0ce99d8 | 2017-06-22 15:07:29 -0500 | [diff] [blame] | 247 | { |
Matthew Barth | e4338cd | 2017-12-14 11:14:30 -0600 | [diff] [blame] | 248 | netDelta = ((state - value)/factor) * speedDelta; |
| 249 | } |
| 250 | else |
| 251 | { |
| 252 | // Decrease is the factor applied to the |
| 253 | // difference times the given speed delta |
| 254 | netDelta = std::min( |
| 255 | netDelta, |
| 256 | ((state - value)/factor) * speedDelta); |
Matthew Barth | 0ce99d8 | 2017-06-22 15:07:29 -0500 | [diff] [blame] | 257 | } |
Matthew Barth | bc65160 | 2017-08-10 16:59:43 -0500 | [diff] [blame] | 258 | } |
Matthew Barth | e4338cd | 2017-12-14 11:14:30 -0600 | [diff] [blame] | 259 | else |
Matthew Barth | bc65160 | 2017-08-10 16:59:43 -0500 | [diff] [blame] | 260 | { |
Matthew Barth | e4338cd | 2017-12-14 11:14:30 -0600 | [diff] [blame] | 261 | // No decrease allowed for this group |
| 262 | netDelta = 0; |
| 263 | break; |
Matthew Barth | 0ce99d8 | 2017-06-22 15:07:29 -0500 | [diff] [blame] | 264 | } |
| 265 | } |
Matthew Barth | e4338cd | 2017-12-14 11:14:30 -0600 | [diff] [blame] | 266 | catch (const std::out_of_range& oore) |
| 267 | { |
| 268 | // Property value not found, netDelta unchanged |
| 269 | } |
| 270 | } |
| 271 | // Update group's decrease allowed state |
| 272 | zone.setDecreaseAllow(&group, !(netDelta == 0)); |
Matthew Barth | 0ce99d8 | 2017-06-22 15:07:29 -0500 | [diff] [blame] | 273 | // Request speed decrease to occur on decrease interval |
| 274 | zone.requestSpeedDecrease(netDelta); |
| 275 | }; |
| 276 | } |
| 277 | |
Matthew Barth | c410dda | 2019-01-11 16:31:13 -0600 | [diff] [blame^] | 278 | /** |
| 279 | * @brief An action to use an alternate set of events |
| 280 | * @details Provides the ability to replace a default set of events with an |
| 281 | * alternate set of events based on all members of a group being at a specified |
| 282 | * state. When any member of the group no longer matches the provided state, |
| 283 | * the alternate set of events are replaced with the defaults. |
| 284 | * |
| 285 | * @param[in] state - State to compare the group's property value to |
| 286 | * @param[in] defEvents - The default set of events |
| 287 | * @param[in] altEvents - The alternate set of events |
| 288 | * |
| 289 | * @return Lambda function |
| 290 | * A lambda function that checks all group members are at a specified state |
| 291 | * and replacing the default set of events with an alternate set of events. |
| 292 | */ |
| 293 | template <typename T> |
| 294 | auto use_alternate_events_on_state(T&& state, |
| 295 | std::vector<SetSpeedEvent>&& defEvents, |
| 296 | std::vector<SetSpeedEvent>&& altEvents) |
| 297 | { |
| 298 | return [state = std::forward<T>(state), |
| 299 | defEvents = std::move(defEvents), |
| 300 | altEvents = std::move(altEvents)](auto& zone, auto& group) |
| 301 | { |
| 302 | // Compare all group entries to the state |
| 303 | auto useAlt = std::all_of( |
| 304 | group.begin(), |
| 305 | group.end(), |
| 306 | [&zone, &state](auto const& entry) |
| 307 | { |
| 308 | try |
| 309 | { |
| 310 | return zone.template getPropertyValue<T>( |
| 311 | entry.first, |
| 312 | std::get<intfPos>(entry.second), |
| 313 | std::get<propPos>(entry.second)) == state; |
| 314 | } |
| 315 | catch (const std::out_of_range& oore) |
| 316 | { |
| 317 | // Default to property not equal when not found |
| 318 | return false; |
| 319 | } |
| 320 | }); |
| 321 | |
| 322 | const std::vector<SetSpeedEvent> *rmEvents = &altEvents; |
| 323 | const std::vector<SetSpeedEvent> *initEvents = &defEvents; |
| 324 | |
| 325 | if (useAlt) |
| 326 | { |
| 327 | rmEvents = &defEvents; |
| 328 | initEvents = &altEvents; |
| 329 | } |
| 330 | |
| 331 | // Remove events |
| 332 | std::for_each( |
| 333 | rmEvents->begin(), |
| 334 | rmEvents->end(), |
| 335 | [&zone](auto const& entry) |
| 336 | { |
| 337 | zone.removeEvent(entry); |
| 338 | }); |
| 339 | // Init events |
| 340 | std::for_each( |
| 341 | initEvents->begin(), |
| 342 | initEvents->end(), |
| 343 | [&zone](auto const& entry) |
| 344 | { |
| 345 | zone.initEvent(entry); |
| 346 | }); |
| 347 | }; |
| 348 | } |
| 349 | |
Matthew Barth | 17d1fe2 | 2017-05-11 15:00:36 -0500 | [diff] [blame] | 350 | } // namespace action |
| 351 | } // namespace control |
| 352 | } // namespace fan |
| 353 | } // namespace phosphor |