Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 1 | /** |
| 2 | * Copyright 2017 Google Inc. |
| 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 | */ |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 16 | #include "dbuspassive.hpp" |
| 17 | |
Patrick Venture | aadb30d | 2020-08-10 09:17:11 -0700 | [diff] [blame] | 18 | #include "dbushelper_interface.hpp" |
James Feist | 98b704e | 2019-06-03 16:24:53 -0700 | [diff] [blame] | 19 | #include "dbuspassiveredundancy.hpp" |
Patrick Venture | aadb30d | 2020-08-10 09:17:11 -0700 | [diff] [blame] | 20 | #include "dbusutil.hpp" |
James Feist | 0c8223b | 2019-05-08 15:33:33 -0700 | [diff] [blame] | 21 | #include "util.hpp" |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 22 | |
Patrick Venture | a83a3ec | 2020-08-04 09:52:05 -0700 | [diff] [blame] | 23 | #include <sdbusplus/bus.hpp> |
| 24 | |
Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 25 | #include <chrono> |
| 26 | #include <cmath> |
Patrick Venture | 0ef1faf | 2018-06-13 12:50:53 -0700 | [diff] [blame] | 27 | #include <memory> |
Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 28 | #include <mutex> |
Patrick Venture | 0ef1faf | 2018-06-13 12:50:53 -0700 | [diff] [blame] | 29 | #include <string> |
James Feist | 1f802f5 | 2019-02-08 13:51:43 -0800 | [diff] [blame] | 30 | #include <variant> |
Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 31 | |
Patrick Venture | a076487 | 2020-08-08 07:48:43 -0700 | [diff] [blame] | 32 | namespace pid_control |
| 33 | { |
| 34 | |
Patrick Venture | 563a356 | 2018-10-30 09:31:26 -0700 | [diff] [blame] | 35 | std::unique_ptr<ReadInterface> DbusPassive::createDbusPassive( |
Patrick Williams | b228bc3 | 2022-07-22 19:26:56 -0500 | [diff] [blame] | 36 | sdbusplus::bus_t& bus, const std::string& type, const std::string& id, |
Patrick Venture | 8729eb9 | 2020-08-10 10:38:44 -0700 | [diff] [blame] | 37 | std::unique_ptr<DbusHelperInterface> helper, const conf::SensorConfig* info, |
James Feist | 98b704e | 2019-06-03 16:24:53 -0700 | [diff] [blame] | 38 | const std::shared_ptr<DbusPassiveRedundancy>& redundancy) |
Patrick Venture | 0ef1faf | 2018-06-13 12:50:53 -0700 | [diff] [blame] | 39 | { |
| 40 | if (helper == nullptr) |
| 41 | { |
| 42 | return nullptr; |
| 43 | } |
Patrick Venture | 7af157b | 2018-10-30 11:24:40 -0700 | [diff] [blame] | 44 | if (!validType(type)) |
Patrick Venture | 0ef1faf | 2018-06-13 12:50:53 -0700 | [diff] [blame] | 45 | { |
| 46 | return nullptr; |
| 47 | } |
| 48 | |
Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 49 | /* Need to get the scale and initial value */ |
Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 50 | /* service == busname */ |
Harvey.Wu | f2efcbb | 2022-02-09 10:24:30 +0800 | [diff] [blame] | 51 | std::string path; |
| 52 | if (info->readPath.empty()) |
| 53 | { |
| 54 | path = getSensorPath(type, id); |
| 55 | } |
| 56 | else |
| 57 | { |
| 58 | path = info->readPath; |
| 59 | } |
Patrick Venture | 34ddc90 | 2018-10-30 11:05:17 -0700 | [diff] [blame] | 60 | |
Patrick Venture | 1df9e87 | 2020-10-08 15:35:01 -0700 | [diff] [blame] | 61 | SensorProperties settings; |
Patrick Venture | f8cb464 | 2018-10-30 12:02:53 -0700 | [diff] [blame] | 62 | bool failed; |
Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 63 | |
Patrick Venture | f8cb464 | 2018-10-30 12:02:53 -0700 | [diff] [blame] | 64 | try |
| 65 | { |
Patrick Venture | 9b93692 | 2020-08-10 11:28:39 -0700 | [diff] [blame] | 66 | std::string service = helper->getService(sensorintf, path); |
Patrick Venture | f8cb464 | 2018-10-30 12:02:53 -0700 | [diff] [blame] | 67 | |
Patrick Venture | 9b93692 | 2020-08-10 11:28:39 -0700 | [diff] [blame] | 68 | helper->getProperties(service, path, &settings); |
| 69 | failed = helper->thresholdsAsserted(service, path); |
Patrick Venture | f8cb464 | 2018-10-30 12:02:53 -0700 | [diff] [blame] | 70 | } |
| 71 | catch (const std::exception& e) |
| 72 | { |
| 73 | return nullptr; |
| 74 | } |
| 75 | |
Patrick Venture | 6b9f599 | 2019-09-10 09:18:28 -0700 | [diff] [blame] | 76 | /* if these values are zero, they're ignored. */ |
| 77 | if (info->ignoreDbusMinMax) |
| 78 | { |
| 79 | settings.min = 0; |
| 80 | settings.max = 0; |
| 81 | } |
| 82 | |
Alex.Song | 8f73ad7 | 2021-10-07 00:18:27 +0800 | [diff] [blame] | 83 | settings.unavailableAsFailed = info->unavailableAsFailed; |
| 84 | |
Patrick Venture | 8729eb9 | 2020-08-10 10:38:44 -0700 | [diff] [blame] | 85 | return std::make_unique<DbusPassive>(bus, type, id, std::move(helper), |
| 86 | settings, failed, path, redundancy); |
Patrick Venture | f8cb464 | 2018-10-30 12:02:53 -0700 | [diff] [blame] | 87 | } |
| 88 | |
James Feist | 98b704e | 2019-06-03 16:24:53 -0700 | [diff] [blame] | 89 | DbusPassive::DbusPassive( |
Patrick Williams | b228bc3 | 2022-07-22 19:26:56 -0500 | [diff] [blame] | 90 | sdbusplus::bus_t& bus, const std::string& type, const std::string& id, |
Patrick Venture | 8729eb9 | 2020-08-10 10:38:44 -0700 | [diff] [blame] | 91 | std::unique_ptr<DbusHelperInterface> helper, |
Patrick Venture | 1df9e87 | 2020-10-08 15:35:01 -0700 | [diff] [blame] | 92 | const SensorProperties& settings, bool failed, const std::string& path, |
James Feist | 98b704e | 2019-06-03 16:24:53 -0700 | [diff] [blame] | 93 | const std::shared_ptr<DbusPassiveRedundancy>& redundancy) : |
Patrick Venture | f8cb464 | 2018-10-30 12:02:53 -0700 | [diff] [blame] | 94 | ReadInterface(), |
Harvey.Wu | f2efcbb | 2022-02-09 10:24:30 +0800 | [diff] [blame] | 95 | _signal(bus, getMatch(path).c_str(), dbusHandleSignal, this), _id(id), |
Patrick Venture | d057130 | 2020-08-10 12:42:23 -0700 | [diff] [blame] | 96 | _helper(std::move(helper)), _failed(failed), path(path), |
James Feist | 98b704e | 2019-06-03 16:24:53 -0700 | [diff] [blame] | 97 | redundancy(redundancy) |
| 98 | |
Patrick Venture | f8cb464 | 2018-10-30 12:02:53 -0700 | [diff] [blame] | 99 | { |
Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 100 | _scale = settings.scale; |
Josh Lehan | 3e2f758 | 2020-09-20 22:06:03 -0700 | [diff] [blame] | 101 | _min = settings.min * std::pow(10.0, _scale); |
| 102 | _max = settings.max * std::pow(10.0, _scale); |
Alex.Song | 8f73ad7 | 2021-10-07 00:18:27 +0800 | [diff] [blame] | 103 | _available = settings.available; |
| 104 | _unavailableAsFailed = settings.unavailableAsFailed; |
Josh Lehan | 3e2f758 | 2020-09-20 22:06:03 -0700 | [diff] [blame] | 105 | |
| 106 | // Cache this type knowledge, to avoid repeated string comparison |
| 107 | _typeMargin = (type == "margin"); |
Alex.Song | 8f73ad7 | 2021-10-07 00:18:27 +0800 | [diff] [blame] | 108 | _typeFan = (type == "fan"); |
Josh Lehan | 3e2f758 | 2020-09-20 22:06:03 -0700 | [diff] [blame] | 109 | |
| 110 | // Force value to be stored, otherwise member would be uninitialized |
| 111 | updateValue(settings.value, true); |
Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | ReadReturn DbusPassive::read(void) |
| 115 | { |
| 116 | std::lock_guard<std::mutex> guard(_lock); |
| 117 | |
Josh Lehan | b300575 | 2022-02-22 20:48:07 -0800 | [diff] [blame] | 118 | ReadReturn r = {_value, _updated, _unscaled}; |
Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 119 | |
| 120 | return r; |
| 121 | } |
| 122 | |
Josh Lehan | b300575 | 2022-02-22 20:48:07 -0800 | [diff] [blame] | 123 | void DbusPassive::setValue(double value, double unscaled) |
Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 124 | { |
| 125 | std::lock_guard<std::mutex> guard(_lock); |
| 126 | |
| 127 | _value = value; |
Josh Lehan | b300575 | 2022-02-22 20:48:07 -0800 | [diff] [blame] | 128 | _unscaled = unscaled; |
Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 129 | _updated = std::chrono::high_resolution_clock::now(); |
| 130 | } |
| 131 | |
Josh Lehan | b300575 | 2022-02-22 20:48:07 -0800 | [diff] [blame] | 132 | void DbusPassive::setValue(double value) |
| 133 | { |
| 134 | // First param is scaled, second param is unscaled, assume same here |
| 135 | setValue(value, value); |
| 136 | } |
| 137 | |
James Feist | 36b7d8e | 2018-10-05 15:39:01 -0700 | [diff] [blame] | 138 | bool DbusPassive::getFailed(void) const |
| 139 | { |
James Feist | 98b704e | 2019-06-03 16:24:53 -0700 | [diff] [blame] | 140 | if (redundancy) |
| 141 | { |
| 142 | const std::set<std::string>& failures = redundancy->getFailed(); |
| 143 | if (failures.find(path) != failures.end()) |
| 144 | { |
| 145 | return true; |
| 146 | } |
| 147 | } |
James Feist | 4b36f26 | 2020-07-07 16:56:41 -0700 | [diff] [blame] | 148 | |
Alex.Song | 8f73ad7 | 2021-10-07 00:18:27 +0800 | [diff] [blame] | 149 | /* |
| 150 | * Unavailable thermal sensors, who are not present or |
| 151 | * power-state-not-matching, should not trigger the failSafe mode. For |
| 152 | * example, when a system stays at a powered-off state, its CPU Temp |
| 153 | * sensors will be unavailable, these unavailable sensors should not be |
| 154 | * treated as failed and trigger failSafe. |
| 155 | * This is important for systems whose Fans are always on. |
| 156 | */ |
| 157 | if (!_typeFan && !_available && !_unavailableAsFailed) |
| 158 | { |
| 159 | return false; |
| 160 | } |
| 161 | |
Josh Lehan | 3e2f758 | 2020-09-20 22:06:03 -0700 | [diff] [blame] | 162 | // If a reading has came in, |
| 163 | // but its value bad in some way (determined by sensor type), |
| 164 | // indicate this sensor has failed, |
| 165 | // until another value comes in that is no longer bad. |
| 166 | // This is different from the overall _failed flag, |
| 167 | // which is set and cleared by other causes. |
| 168 | if (_badReading) |
| 169 | { |
| 170 | return true; |
| 171 | } |
| 172 | |
| 173 | // If a reading has came in, and it is not a bad reading, |
| 174 | // but it indicates there is no more thermal margin left, |
| 175 | // that is bad, something is wrong with the PID loops, |
| 176 | // they are not cooling the system, enable failsafe mode also. |
| 177 | if (_marginHot) |
| 178 | { |
| 179 | return true; |
| 180 | } |
| 181 | |
Alex.Song | 8f73ad7 | 2021-10-07 00:18:27 +0800 | [diff] [blame] | 182 | return _failed || !_available || !_functional; |
James Feist | 36b7d8e | 2018-10-05 15:39:01 -0700 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | void DbusPassive::setFailed(bool value) |
| 186 | { |
| 187 | _failed = value; |
| 188 | } |
| 189 | |
James Feist | 4b36f26 | 2020-07-07 16:56:41 -0700 | [diff] [blame] | 190 | void DbusPassive::setFunctional(bool value) |
| 191 | { |
| 192 | _functional = value; |
| 193 | } |
| 194 | |
Alex.Song | 8f73ad7 | 2021-10-07 00:18:27 +0800 | [diff] [blame] | 195 | void DbusPassive::setAvailable(bool value) |
| 196 | { |
| 197 | _available = value; |
| 198 | } |
| 199 | |
Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 200 | int64_t DbusPassive::getScale(void) |
| 201 | { |
| 202 | return _scale; |
| 203 | } |
| 204 | |
Patrick Venture | 563a356 | 2018-10-30 09:31:26 -0700 | [diff] [blame] | 205 | std::string DbusPassive::getID(void) |
Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 206 | { |
| 207 | return _id; |
| 208 | } |
| 209 | |
James Feist | 75eb769 | 2019-02-25 12:50:02 -0800 | [diff] [blame] | 210 | double DbusPassive::getMax(void) |
| 211 | { |
| 212 | return _max; |
| 213 | } |
| 214 | |
| 215 | double DbusPassive::getMin(void) |
| 216 | { |
| 217 | return _min; |
| 218 | } |
| 219 | |
Josh Lehan | 3e2f758 | 2020-09-20 22:06:03 -0700 | [diff] [blame] | 220 | void DbusPassive::updateValue(double value, bool force) |
| 221 | { |
| 222 | _badReading = false; |
| 223 | |
| 224 | // Do not let a NAN, or other floating-point oddity, be used to update |
| 225 | // the value, as that indicates the sensor has no valid reading. |
| 226 | if (!(std::isfinite(value))) |
| 227 | { |
| 228 | _badReading = true; |
| 229 | |
| 230 | // Do not continue with a bad reading, unless caller forcing |
| 231 | if (!force) |
| 232 | { |
| 233 | return; |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | value *= std::pow(10.0, _scale); |
| 238 | |
| 239 | auto unscaled = value; |
| 240 | scaleSensorReading(_min, _max, value); |
| 241 | |
| 242 | if (_typeMargin) |
| 243 | { |
| 244 | _marginHot = false; |
| 245 | |
| 246 | // Unlike an absolute temperature sensor, |
| 247 | // where 0 degrees C is a good reading, |
| 248 | // a value received of 0 (or negative) margin is worrisome, |
| 249 | // and should be flagged. |
| 250 | // Either it indicates margin not calculated properly, |
| 251 | // or somebody forgot to set the margin-zero setpoint, |
| 252 | // or the system is really overheating that much. |
| 253 | // This is a different condition from _failed |
| 254 | // and _badReading, so it merits its own flag. |
| 255 | // The sensor has not failed, the reading is good, but the zone |
| 256 | // still needs to know that it should go to failsafe mode. |
| 257 | if (unscaled <= 0.0) |
| 258 | { |
| 259 | _marginHot = true; |
| 260 | } |
| 261 | } |
| 262 | |
Josh Lehan | b300575 | 2022-02-22 20:48:07 -0800 | [diff] [blame] | 263 | setValue(value, unscaled); |
Josh Lehan | 3e2f758 | 2020-09-20 22:06:03 -0700 | [diff] [blame] | 264 | } |
| 265 | |
Patrick Williams | b228bc3 | 2022-07-22 19:26:56 -0500 | [diff] [blame] | 266 | int handleSensorValue(sdbusplus::message_t& msg, DbusPassive* owner) |
Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 267 | { |
Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 268 | std::string msgSensor; |
James Feist | 1f802f5 | 2019-02-08 13:51:43 -0800 | [diff] [blame] | 269 | std::map<std::string, std::variant<int64_t, double, bool>> msgData; |
Patrick Venture | d0c7566 | 2018-06-12 19:03:21 -0700 | [diff] [blame] | 270 | |
| 271 | msg.read(msgSensor, msgData); |
Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 272 | |
| 273 | if (msgSensor == "xyz.openbmc_project.Sensor.Value") |
| 274 | { |
| 275 | auto valPropMap = msgData.find("Value"); |
| 276 | if (valPropMap != msgData.end()) |
| 277 | { |
James Feist | 1f802f5 | 2019-02-08 13:51:43 -0800 | [diff] [blame] | 278 | double value = |
| 279 | std::visit(VariantToDoubleVisitor(), valPropMap->second); |
Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 280 | |
Josh Lehan | 3e2f758 | 2020-09-20 22:06:03 -0700 | [diff] [blame] | 281 | owner->updateValue(value, false); |
Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 282 | } |
| 283 | } |
James Feist | 36b7d8e | 2018-10-05 15:39:01 -0700 | [diff] [blame] | 284 | else if (msgSensor == "xyz.openbmc_project.Sensor.Threshold.Critical") |
| 285 | { |
| 286 | auto criticalAlarmLow = msgData.find("CriticalAlarmLow"); |
| 287 | auto criticalAlarmHigh = msgData.find("CriticalAlarmHigh"); |
| 288 | if (criticalAlarmHigh == msgData.end() && |
| 289 | criticalAlarmLow == msgData.end()) |
| 290 | { |
| 291 | return 0; |
| 292 | } |
| 293 | |
| 294 | bool asserted = false; |
| 295 | if (criticalAlarmLow != msgData.end()) |
| 296 | { |
James Feist | 1f802f5 | 2019-02-08 13:51:43 -0800 | [diff] [blame] | 297 | asserted = std::get<bool>(criticalAlarmLow->second); |
James Feist | 36b7d8e | 2018-10-05 15:39:01 -0700 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | // checking both as in theory you could de-assert one threshold and |
| 301 | // assert the other at the same moment |
| 302 | if (!asserted && criticalAlarmHigh != msgData.end()) |
| 303 | { |
James Feist | 1f802f5 | 2019-02-08 13:51:43 -0800 | [diff] [blame] | 304 | asserted = std::get<bool>(criticalAlarmHigh->second); |
James Feist | 36b7d8e | 2018-10-05 15:39:01 -0700 | [diff] [blame] | 305 | } |
| 306 | owner->setFailed(asserted); |
| 307 | } |
Alex.Song | 8f73ad7 | 2021-10-07 00:18:27 +0800 | [diff] [blame] | 308 | else if (msgSensor == "xyz.openbmc_project.State.Decorator.Availability") |
| 309 | { |
| 310 | auto available = msgData.find("Available"); |
| 311 | if (available == msgData.end()) |
| 312 | { |
| 313 | return 0; |
| 314 | } |
| 315 | bool asserted = std::get<bool>(available->second); |
| 316 | owner->setAvailable(asserted); |
| 317 | if (!asserted) |
| 318 | { |
| 319 | // A thermal controller will continue its PID calculation and not |
| 320 | // trigger a 'failsafe' when some inputs are unavailable. |
| 321 | // So, forced to clear the value here to prevent a historical |
| 322 | // value to participate in a latter PID calculation. |
| 323 | owner->updateValue(std::numeric_limits<double>::quiet_NaN(), true); |
| 324 | } |
| 325 | } |
James Feist | 4b36f26 | 2020-07-07 16:56:41 -0700 | [diff] [blame] | 326 | else if (msgSensor == |
| 327 | "xyz.openbmc_project.State.Decorator.OperationalStatus") |
| 328 | { |
| 329 | auto functional = msgData.find("Functional"); |
| 330 | if (functional == msgData.end()) |
| 331 | { |
| 332 | return 0; |
| 333 | } |
| 334 | bool asserted = std::get<bool>(functional->second); |
| 335 | owner->setFunctional(asserted); |
| 336 | } |
Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 337 | |
| 338 | return 0; |
| 339 | } |
Patrick Venture | d0c7566 | 2018-06-12 19:03:21 -0700 | [diff] [blame] | 340 | |
Harvey.Wu | a1ae4fa | 2022-10-28 17:38:35 +0800 | [diff] [blame] | 341 | int dbusHandleSignal(sd_bus_message* msg, void* usrData, |
| 342 | [[maybe_unused]] sd_bus_error* err) |
Patrick Venture | d0c7566 | 2018-06-12 19:03:21 -0700 | [diff] [blame] | 343 | { |
Patrick Williams | b228bc3 | 2022-07-22 19:26:56 -0500 | [diff] [blame] | 344 | auto sdbpMsg = sdbusplus::message_t(msg); |
Patrick Venture | d0c7566 | 2018-06-12 19:03:21 -0700 | [diff] [blame] | 345 | DbusPassive* obj = static_cast<DbusPassive*>(usrData); |
| 346 | |
Patrick Venture | 7af157b | 2018-10-30 11:24:40 -0700 | [diff] [blame] | 347 | return handleSensorValue(sdbpMsg, obj); |
Patrick Venture | d0c7566 | 2018-06-12 19:03:21 -0700 | [diff] [blame] | 348 | } |
Patrick Venture | a076487 | 2020-08-08 07:48:43 -0700 | [diff] [blame] | 349 | |
| 350 | } // namespace pid_control |