| Andrew Jeffery | e73bd0a | 2023-01-25 10:39:57 +1030 | [diff] [blame] | 1 | #include "Thresholds.hpp" |
| 2 | |
| Ed Tanous | eacbfdd | 2024-04-04 12:00:24 -0700 | [diff] [blame] | 3 | #include "Utils.hpp" |
| Andrew Jeffery | e73bd0a | 2023-01-25 10:39:57 +1030 | [diff] [blame] | 4 | #include "VariantVisitors.hpp" |
| 5 | #include "sensor.hpp" |
| 6 | |
| James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 7 | #include <boost/algorithm/string/replace.hpp> |
| Ed Tanous | eacbfdd | 2024-04-04 12:00:24 -0700 | [diff] [blame] | 8 | #include <boost/asio/error.hpp> |
| 9 | #include <boost/asio/steady_timer.hpp> |
| Patrick Venture | 96e97db | 2019-10-31 13:44:38 -0700 | [diff] [blame] | 10 | #include <boost/container/flat_map.hpp> |
| George Liu | 6198435 | 2025-02-24 14:47:34 +0800 | [diff] [blame] | 11 | #include <phosphor-logging/lg2.hpp> |
| Ed Tanous | eacbfdd | 2024-04-04 12:00:24 -0700 | [diff] [blame] | 12 | #include <sdbusplus/asio/connection.hpp> |
| 13 | #include <sdbusplus/asio/object_server.hpp> |
| 14 | #include <sdbusplus/exception.hpp> |
| 15 | #include <sdbusplus/message.hpp> |
| James Feist | 38fb598 | 2020-05-28 10:09:54 -0700 | [diff] [blame] | 16 | |
| 17 | #include <array> |
| Ed Tanous | eacbfdd | 2024-04-04 12:00:24 -0700 | [diff] [blame] | 18 | #include <chrono> |
| 19 | #include <cstddef> |
| 20 | #include <cstdint> |
| Ed Tanous | eacbfdd | 2024-04-04 12:00:24 -0700 | [diff] [blame] | 21 | #include <limits> |
| Patrick Venture | 96e97db | 2019-10-31 13:44:38 -0700 | [diff] [blame] | 22 | #include <memory> |
| Patrick Venture | 96e97db | 2019-10-31 13:44:38 -0700 | [diff] [blame] | 23 | #include <string> |
| Zbigniew Kurzynski | 0a4c480 | 2020-04-01 11:22:27 +0200 | [diff] [blame] | 24 | #include <tuple> |
| Patrick Venture | 96e97db | 2019-10-31 13:44:38 -0700 | [diff] [blame] | 25 | #include <utility> |
| 26 | #include <variant> |
| 27 | #include <vector> |
| James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 28 | |
| James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 29 | namespace thresholds |
| 30 | { |
| Ed Tanous | c8fed20 | 2022-01-12 14:24:45 -0800 | [diff] [blame] | 31 | Level findThresholdLevel(uint8_t sev) |
| James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 32 | { |
| Ed Tanous | c8fed20 | 2022-01-12 14:24:45 -0800 | [diff] [blame] | 33 | for (const ThresholdDefinition& prop : thresProp) |
| James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 34 | { |
| Ed Tanous | c8fed20 | 2022-01-12 14:24:45 -0800 | [diff] [blame] | 35 | if (prop.sevOrder == sev) |
| James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 36 | { |
| Jayashree Dhanapal | 45f2702 | 2021-12-07 12:56:35 +0530 | [diff] [blame] | 37 | return prop.level; |
| James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 38 | } |
| 39 | } |
| Jayashree Dhanapal | 45f2702 | 2021-12-07 12:56:35 +0530 | [diff] [blame] | 40 | return Level::ERROR; |
| James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 41 | } |
| 42 | |
| Ed Tanous | c8fed20 | 2022-01-12 14:24:45 -0800 | [diff] [blame] | 43 | Direction findThresholdDirection(const std::string& direct) |
| James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 44 | { |
| Ed Tanous | c8fed20 | 2022-01-12 14:24:45 -0800 | [diff] [blame] | 45 | if (direct == "greater than") |
| James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 46 | { |
| Ed Tanous | c8fed20 | 2022-01-12 14:24:45 -0800 | [diff] [blame] | 47 | return Direction::HIGH; |
| 48 | } |
| 49 | if (direct == "less than") |
| 50 | { |
| 51 | return Direction::LOW; |
| James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 52 | } |
| Jayashree Dhanapal | 45f2702 | 2021-12-07 12:56:35 +0530 | [diff] [blame] | 53 | return Direction::ERROR; |
| James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 54 | } |
| 55 | |
| Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 56 | bool parseThresholdsFromConfig( |
| James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 57 | const SensorData& sensorData, |
| 58 | std::vector<thresholds::Threshold>& thresholdVector, |
| Matt Spinler | 5636d52 | 2021-03-17 14:52:18 -0500 | [diff] [blame] | 59 | const std::string* matchLabel, const int* sensorIndex) |
| James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 60 | { |
| Zev Weiss | f783c69 | 2022-08-12 18:21:02 -0700 | [diff] [blame] | 61 | for (const auto& [intf, cfg] : sensorData) |
| James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 62 | { |
| Zev Weiss | f783c69 | 2022-08-12 18:21:02 -0700 | [diff] [blame] | 63 | if (intf.find("Thresholds") == std::string::npos) |
| James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 64 | { |
| 65 | continue; |
| 66 | } |
| 67 | if (matchLabel != nullptr) |
| 68 | { |
| Zev Weiss | f783c69 | 2022-08-12 18:21:02 -0700 | [diff] [blame] | 69 | auto labelFind = cfg.find("Label"); |
| 70 | if (labelFind == cfg.end()) |
| Ed Tanous | 8a57ec0 | 2020-10-09 12:46:52 -0700 | [diff] [blame] | 71 | { |
| James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 72 | continue; |
| Ed Tanous | 8a57ec0 | 2020-10-09 12:46:52 -0700 | [diff] [blame] | 73 | } |
| James Feist | 3eb8262 | 2019-02-08 13:10:22 -0800 | [diff] [blame] | 74 | if (std::visit(VariantToStringVisitor(), labelFind->second) != |
| 75 | *matchLabel) |
| Ed Tanous | 8a57ec0 | 2020-10-09 12:46:52 -0700 | [diff] [blame] | 76 | { |
| James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 77 | continue; |
| Ed Tanous | 8a57ec0 | 2020-10-09 12:46:52 -0700 | [diff] [blame] | 78 | } |
| James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 79 | } |
| Matt Spinler | 5636d52 | 2021-03-17 14:52:18 -0500 | [diff] [blame] | 80 | |
| 81 | if (sensorIndex != nullptr) |
| 82 | { |
| Zev Weiss | f783c69 | 2022-08-12 18:21:02 -0700 | [diff] [blame] | 83 | auto indexFind = cfg.find("Index"); |
| Matt Spinler | 5636d52 | 2021-03-17 14:52:18 -0500 | [diff] [blame] | 84 | |
| 85 | // If we're checking for index 1, a missing Index is OK. |
| Zev Weiss | f783c69 | 2022-08-12 18:21:02 -0700 | [diff] [blame] | 86 | if ((indexFind == cfg.end()) && (*sensorIndex != 1)) |
| Matt Spinler | 5636d52 | 2021-03-17 14:52:18 -0500 | [diff] [blame] | 87 | { |
| 88 | continue; |
| 89 | } |
| 90 | |
| Zev Weiss | f783c69 | 2022-08-12 18:21:02 -0700 | [diff] [blame] | 91 | if ((indexFind != cfg.end()) && |
| Matt Spinler | 5636d52 | 2021-03-17 14:52:18 -0500 | [diff] [blame] | 92 | (std::visit(VariantToIntVisitor(), indexFind->second) != |
| 93 | *sensorIndex)) |
| 94 | { |
| 95 | continue; |
| 96 | } |
| 97 | } |
| 98 | |
| Rashmica Gupta | 1e34cec | 2021-08-31 16:47:39 +1000 | [diff] [blame] | 99 | double hysteresis = std::numeric_limits<double>::quiet_NaN(); |
| Zev Weiss | f783c69 | 2022-08-12 18:21:02 -0700 | [diff] [blame] | 100 | auto hysteresisFind = cfg.find("Hysteresis"); |
| 101 | if (hysteresisFind != cfg.end()) |
| Rashmica Gupta | 1e34cec | 2021-08-31 16:47:39 +1000 | [diff] [blame] | 102 | { |
| Patrick Williams | 2aaf717 | 2024-08-16 15:20:40 -0400 | [diff] [blame] | 103 | hysteresis = |
| 104 | std::visit(VariantToDoubleVisitor(), hysteresisFind->second); |
| Rashmica Gupta | 1e34cec | 2021-08-31 16:47:39 +1000 | [diff] [blame] | 105 | } |
| 106 | |
| Zev Weiss | f783c69 | 2022-08-12 18:21:02 -0700 | [diff] [blame] | 107 | auto directionFind = cfg.find("Direction"); |
| 108 | auto severityFind = cfg.find("Severity"); |
| 109 | auto valueFind = cfg.find("Value"); |
| 110 | if (valueFind == cfg.end() || severityFind == cfg.end() || |
| 111 | directionFind == cfg.end()) |
| James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 112 | { |
| George Liu | 6198435 | 2025-02-24 14:47:34 +0800 | [diff] [blame] | 113 | lg2::error( |
| 114 | "Malformed threshold on configuration interface: '{INTERFACE}'", |
| 115 | "INTERFACE", intf); |
| James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 116 | return false; |
| 117 | } |
| Patrick Williams | 2aaf717 | 2024-08-16 15:20:40 -0400 | [diff] [blame] | 118 | unsigned int severity = |
| 119 | std::visit(VariantToUnsignedIntVisitor(), severityFind->second); |
| Jayashree Dhanapal | 45f2702 | 2021-12-07 12:56:35 +0530 | [diff] [blame] | 120 | |
| Patrick Williams | 2aaf717 | 2024-08-16 15:20:40 -0400 | [diff] [blame] | 121 | std::string directions = |
| 122 | std::visit(VariantToStringVisitor(), directionFind->second); |
| Jayashree Dhanapal | 45f2702 | 2021-12-07 12:56:35 +0530 | [diff] [blame] | 123 | |
| Ed Tanous | c8fed20 | 2022-01-12 14:24:45 -0800 | [diff] [blame] | 124 | Level level = findThresholdLevel(severity); |
| 125 | Direction direction = findThresholdDirection(directions); |
| Jayashree Dhanapal | 45f2702 | 2021-12-07 12:56:35 +0530 | [diff] [blame] | 126 | |
| Jayashree Dhanapal | 091c92c | 2022-01-11 14:55:20 +0530 | [diff] [blame] | 127 | if ((level == Level::ERROR) || (direction == Direction::ERROR)) |
| James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 128 | { |
| Jayashree Dhanapal | 45f2702 | 2021-12-07 12:56:35 +0530 | [diff] [blame] | 129 | continue; |
| James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 130 | } |
| James Feist | 13f340b | 2019-03-07 16:36:11 -0800 | [diff] [blame] | 131 | double val = std::visit(VariantToDoubleVisitor(), valueFind->second); |
| James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 132 | |
| Rashmica Gupta | 1e34cec | 2021-08-31 16:47:39 +1000 | [diff] [blame] | 133 | thresholdVector.emplace_back(level, direction, val, hysteresis); |
| James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 134 | } |
| 135 | return true; |
| 136 | } |
| 137 | |
| James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 138 | void persistThreshold(const std::string& path, const std::string& baseInterface, |
| 139 | const thresholds::Threshold& threshold, |
| James Feist | a222ba7 | 2019-03-01 15:57:51 -0800 | [diff] [blame] | 140 | std::shared_ptr<sdbusplus::asio::connection>& conn, |
| Cheng C Yang | 6b1247a | 2020-03-09 23:48:39 +0800 | [diff] [blame] | 141 | size_t thresholdCount, const std::string& labelMatch) |
| James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 142 | { |
| Brad Bishop | fbb44ad | 2019-11-08 09:42:37 -0500 | [diff] [blame] | 143 | for (size_t ii = 0; ii < thresholdCount; ii++) |
| James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 144 | { |
| Patrick Williams | 2aaf717 | 2024-08-16 15:20:40 -0400 | [diff] [blame] | 145 | std::string thresholdInterface = |
| 146 | baseInterface + ".Thresholds" + std::to_string(ii); |
| James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 147 | conn->async_method_call( |
| Zev Weiss | afd1504 | 2022-07-18 12:28:40 -0700 | [diff] [blame] | 148 | [&, path, threshold, thresholdInterface, |
| 149 | labelMatch](const boost::system::error_code& ec, |
| 150 | const SensorBaseConfigMap& result) { |
| Ed Tanous | bb67932 | 2022-05-16 16:10:00 -0700 | [diff] [blame] | 151 | if (ec) |
| 152 | { |
| Patrick Williams | 2aaf717 | 2024-08-16 15:20:40 -0400 | [diff] [blame] | 153 | return; // threshold not supported |
| Ed Tanous | bb67932 | 2022-05-16 16:10:00 -0700 | [diff] [blame] | 154 | } |
| Patrick Williams | 2aaf717 | 2024-08-16 15:20:40 -0400 | [diff] [blame] | 155 | |
| 156 | if (!labelMatch.empty()) |
| 157 | { |
| 158 | auto labelFind = result.find("Label"); |
| 159 | if (labelFind == result.end()) |
| 160 | { |
| George Liu | 6198435 | 2025-02-24 14:47:34 +0800 | [diff] [blame] | 161 | lg2::error("No label in threshold configuration"); |
| Patrick Williams | 2aaf717 | 2024-08-16 15:20:40 -0400 | [diff] [blame] | 162 | return; |
| 163 | } |
| 164 | std::string label = |
| 165 | std::visit(VariantToStringVisitor(), labelFind->second); |
| 166 | if (label != labelMatch) |
| 167 | { |
| 168 | return; |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | auto directionFind = result.find("Direction"); |
| 173 | auto severityFind = result.find("Severity"); |
| 174 | auto valueFind = result.find("Value"); |
| 175 | if (valueFind == result.end() || severityFind == result.end() || |
| 176 | directionFind == result.end()) |
| 177 | { |
| George Liu | 6198435 | 2025-02-24 14:47:34 +0800 | [diff] [blame] | 178 | lg2::error("Malformed threshold in configuration"); |
| Patrick Williams | 2aaf717 | 2024-08-16 15:20:40 -0400 | [diff] [blame] | 179 | return; |
| 180 | } |
| 181 | unsigned int severity = std::visit( |
| 182 | VariantToUnsignedIntVisitor(), severityFind->second); |
| 183 | |
| 184 | std::string dir = |
| 185 | std::visit(VariantToStringVisitor(), directionFind->second); |
| 186 | if ((findThresholdLevel(severity) != threshold.level) || |
| 187 | (findThresholdDirection(dir) != threshold.direction)) |
| 188 | { |
| 189 | return; // not the droid we're looking for |
| 190 | } |
| 191 | |
| 192 | std::variant<double> value(threshold.value); |
| 193 | conn->async_method_call( |
| 194 | [](const boost::system::error_code& ec) { |
| 195 | if (ec) |
| 196 | { |
| George Liu | 6198435 | 2025-02-24 14:47:34 +0800 | [diff] [blame] | 197 | lg2::error( |
| 198 | "Error setting threshold: '{ERROR_MESSAGE}'", |
| 199 | "ERROR_MESSAGE", ec.message()); |
| Patrick Williams | 2aaf717 | 2024-08-16 15:20:40 -0400 | [diff] [blame] | 200 | } |
| 201 | }, |
| 202 | entityManagerName, path, "org.freedesktop.DBus.Properties", |
| 203 | "Set", thresholdInterface, "Value", value); |
| Patrick Williams | 597e842 | 2023-10-20 11:19:01 -0500 | [diff] [blame] | 204 | }, |
| Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 205 | entityManagerName, path, "org.freedesktop.DBus.Properties", |
| James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 206 | "GetAll", thresholdInterface); |
| 207 | } |
| 208 | } |
| 209 | |
| Jae Hyun Yoo | 95b8a2d | 2019-02-25 20:15:09 -0800 | [diff] [blame] | 210 | void updateThresholds(Sensor* sensor) |
| 211 | { |
| Jae Hyun Yoo | 95b8a2d | 2019-02-25 20:15:09 -0800 | [diff] [blame] | 212 | for (const auto& threshold : sensor->thresholds) |
| 213 | { |
| Jayashree Dhanapal | 5667808 | 2022-01-04 17:27:20 +0530 | [diff] [blame] | 214 | std::shared_ptr<sdbusplus::asio::dbus_interface> interface = |
| 215 | sensor->getThresholdInterface(threshold.level); |
| 216 | |
| Jae Hyun Yoo | 95b8a2d | 2019-02-25 20:15:09 -0800 | [diff] [blame] | 217 | if (!interface) |
| 218 | { |
| 219 | continue; |
| 220 | } |
| Jayashree Dhanapal | 45f2702 | 2021-12-07 12:56:35 +0530 | [diff] [blame] | 221 | |
| Patrick Williams | 2aaf717 | 2024-08-16 15:20:40 -0400 | [diff] [blame] | 222 | std::string property = |
| 223 | Sensor::propertyLevel(threshold.level, threshold.direction); |
| Jayashree Dhanapal | 45f2702 | 2021-12-07 12:56:35 +0530 | [diff] [blame] | 224 | if (property.empty()) |
| 225 | { |
| 226 | continue; |
| 227 | } |
| Jae Hyun Yoo | 95b8a2d | 2019-02-25 20:15:09 -0800 | [diff] [blame] | 228 | interface->set_property(property, threshold.value); |
| 229 | } |
| 230 | } |
| 231 | |
| Josh Lehan | 883fb3a | 2020-02-27 14:41:39 -0800 | [diff] [blame] | 232 | // Debugging counters |
| 233 | static int cHiTrue = 0; |
| 234 | static int cHiFalse = 0; |
| 235 | static int cHiMidstate = 0; |
| 236 | static int cLoTrue = 0; |
| 237 | static int cLoFalse = 0; |
| 238 | static int cLoMidstate = 0; |
| 239 | static int cDebugThrottle = 0; |
| Zhikui Ren | d3da128 | 2020-09-11 17:02:01 -0700 | [diff] [blame] | 240 | static constexpr int assertLogCount = 10; |
| Josh Lehan | 883fb3a | 2020-02-27 14:41:39 -0800 | [diff] [blame] | 241 | |
| Zhikui Ren | 59b8b9e | 2020-06-26 18:34:22 -0700 | [diff] [blame] | 242 | struct ChangeParam |
| James Feist | 251c782 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 243 | { |
| Zhikui Ren | 59b8b9e | 2020-06-26 18:34:22 -0700 | [diff] [blame] | 244 | ChangeParam(Threshold whichThreshold, bool status, double value) : |
| 245 | threshold(whichThreshold), asserted(status), assertValue(value) |
| 246 | {} |
| 247 | |
| 248 | Threshold threshold; |
| 249 | bool asserted; |
| 250 | double assertValue; |
| 251 | }; |
| 252 | |
| 253 | static std::vector<ChangeParam> checkThresholds(Sensor* sensor, double value) |
| 254 | { |
| 255 | std::vector<ChangeParam> thresholdChanges; |
| James Feist | 251c782 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 256 | if (sensor->thresholds.empty()) |
| 257 | { |
| James Feist | 46342ec | 2019-03-06 14:03:41 -0800 | [diff] [blame] | 258 | return thresholdChanges; |
| James Feist | 251c782 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 259 | } |
| James Feist | 46342ec | 2019-03-06 14:03:41 -0800 | [diff] [blame] | 260 | |
| James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 261 | for (auto& threshold : sensor->thresholds) |
| James Feist | 251c782 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 262 | { |
| Josh Lehan | 883fb3a | 2020-02-27 14:41:39 -0800 | [diff] [blame] | 263 | // Use "Schmitt trigger" logic to avoid threshold trigger spam, |
| 264 | // if value is noisy while hovering very close to a threshold. |
| 265 | // When a threshold is crossed, indicate true immediately, |
| 266 | // but require more distance to be crossed the other direction, |
| 267 | // before resetting the indicator back to false. |
| James Feist | 46342ec | 2019-03-06 14:03:41 -0800 | [diff] [blame] | 268 | if (threshold.direction == thresholds::Direction::HIGH) |
| James Feist | dc6c55f | 2018-10-31 12:53:20 -0700 | [diff] [blame] | 269 | { |
| James Feist | 551087a | 2019-12-09 11:17:12 -0800 | [diff] [blame] | 270 | if (value >= threshold.value) |
| James Feist | 251c782 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 271 | { |
| Zhikui Ren | 59b8b9e | 2020-06-26 18:34:22 -0700 | [diff] [blame] | 272 | thresholdChanges.emplace_back(threshold, true, value); |
| Zhikui Ren | d3da128 | 2020-09-11 17:02:01 -0700 | [diff] [blame] | 273 | if (++cHiTrue < assertLogCount) |
| 274 | { |
| George Liu | 6198435 | 2025-02-24 14:47:34 +0800 | [diff] [blame] | 275 | lg2::info( |
| 276 | "Sensor name: {NAME}, high threshold: {THRESHOLD}, " |
| 277 | "assert value: {VALUE}, raw data: {RAW_DATA}", |
| 278 | "NAME", sensor->name, "THRESHOLD", threshold.value, |
| 279 | "VALUE", value, "RAW_DATA", sensor->rawValue); |
| Zhikui Ren | d3da128 | 2020-09-11 17:02:01 -0700 | [diff] [blame] | 280 | } |
| Josh Lehan | 883fb3a | 2020-02-27 14:41:39 -0800 | [diff] [blame] | 281 | } |
| Rashmica Gupta | 1e34cec | 2021-08-31 16:47:39 +1000 | [diff] [blame] | 282 | else if (value < (threshold.value - threshold.hysteresis)) |
| Josh Lehan | 883fb3a | 2020-02-27 14:41:39 -0800 | [diff] [blame] | 283 | { |
| Zhikui Ren | 59b8b9e | 2020-06-26 18:34:22 -0700 | [diff] [blame] | 284 | thresholdChanges.emplace_back(threshold, false, value); |
| Josh Lehan | 883fb3a | 2020-02-27 14:41:39 -0800 | [diff] [blame] | 285 | ++cHiFalse; |
| James Feist | 251c782 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 286 | } |
| Patrick Venture | 66235d4 | 2019-10-11 08:31:27 -0700 | [diff] [blame] | 287 | else |
| James Feist | 251c782 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 288 | { |
| Josh Lehan | 883fb3a | 2020-02-27 14:41:39 -0800 | [diff] [blame] | 289 | ++cHiMidstate; |
| James Feist | 251c782 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 290 | } |
| 291 | } |
| Josh Lehan | 883fb3a | 2020-02-27 14:41:39 -0800 | [diff] [blame] | 292 | else if (threshold.direction == thresholds::Direction::LOW) |
| James Feist | 251c782 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 293 | { |
| James Feist | 551087a | 2019-12-09 11:17:12 -0800 | [diff] [blame] | 294 | if (value <= threshold.value) |
| James Feist | 251c782 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 295 | { |
| Zhikui Ren | 59b8b9e | 2020-06-26 18:34:22 -0700 | [diff] [blame] | 296 | thresholdChanges.emplace_back(threshold, true, value); |
| Zhikui Ren | d3da128 | 2020-09-11 17:02:01 -0700 | [diff] [blame] | 297 | if (++cLoTrue < assertLogCount) |
| 298 | { |
| George Liu | 6198435 | 2025-02-24 14:47:34 +0800 | [diff] [blame] | 299 | lg2::info( |
| 300 | "Sensor name: {NAME}, low threshold: {THRESHOLD}, " |
| 301 | "assert value: {VALUE}, raw data: {RAW_DATA}", |
| 302 | "NAME", sensor->name, "THRESHOLD", threshold.value, |
| 303 | "VALUE", value, "RAW_DATA", sensor->rawValue); |
| Zhikui Ren | d3da128 | 2020-09-11 17:02:01 -0700 | [diff] [blame] | 304 | } |
| Josh Lehan | 883fb3a | 2020-02-27 14:41:39 -0800 | [diff] [blame] | 305 | } |
| Rashmica Gupta | 1e34cec | 2021-08-31 16:47:39 +1000 | [diff] [blame] | 306 | else if (value > (threshold.value + threshold.hysteresis)) |
| Josh Lehan | 883fb3a | 2020-02-27 14:41:39 -0800 | [diff] [blame] | 307 | { |
| Zhikui Ren | 59b8b9e | 2020-06-26 18:34:22 -0700 | [diff] [blame] | 308 | thresholdChanges.emplace_back(threshold, false, value); |
| Josh Lehan | 883fb3a | 2020-02-27 14:41:39 -0800 | [diff] [blame] | 309 | ++cLoFalse; |
| James Feist | 251c782 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 310 | } |
| Patrick Venture | 66235d4 | 2019-10-11 08:31:27 -0700 | [diff] [blame] | 311 | else |
| James Feist | 251c782 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 312 | { |
| Josh Lehan | 883fb3a | 2020-02-27 14:41:39 -0800 | [diff] [blame] | 313 | ++cLoMidstate; |
| James Feist | 251c782 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 314 | } |
| 315 | } |
| Josh Lehan | 883fb3a | 2020-02-27 14:41:39 -0800 | [diff] [blame] | 316 | else |
| 317 | { |
| George Liu | 6198435 | 2025-02-24 14:47:34 +0800 | [diff] [blame] | 318 | lg2::error("Error determining threshold direction"); |
| Josh Lehan | 883fb3a | 2020-02-27 14:41:39 -0800 | [diff] [blame] | 319 | } |
| James Feist | 251c782 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 320 | } |
| Josh Lehan | 883fb3a | 2020-02-27 14:41:39 -0800 | [diff] [blame] | 321 | |
| Ed Tanous | 8a17c30 | 2021-09-02 15:07:11 -0700 | [diff] [blame] | 322 | // Throttle debug output, so that it does not continuously spam |
| 323 | ++cDebugThrottle; |
| 324 | if (cDebugThrottle >= 1000) |
| Josh Lehan | 883fb3a | 2020-02-27 14:41:39 -0800 | [diff] [blame] | 325 | { |
| Ed Tanous | 8a17c30 | 2021-09-02 15:07:11 -0700 | [diff] [blame] | 326 | cDebugThrottle = 0; |
| Alexander Hansen | 89be614 | 2025-09-18 15:36:16 +0200 | [diff] [blame^] | 327 | lg2::debug("checkThresholds: High T= {HIGH_TRUE}, F= {HIGH_FALSE}," |
| 328 | " M= {HIGH_MIDSTATE}, Low T= {LOW_TRUE}, F= {LOW_FALSE}," |
| 329 | " M= {LOW_MIDSTATE}", |
| 330 | "HIGH_TRUE", cHiTrue, "HIGH_FALSE", cHiFalse, |
| 331 | "HIGH_MIDSTATE", cHiMidstate, "LOW_TRUE", cLoTrue, |
| 332 | "LOW_FALSE", cLoFalse, "LOW_MIDSTATE", cLoMidstate); |
| Josh Lehan | 883fb3a | 2020-02-27 14:41:39 -0800 | [diff] [blame] | 333 | } |
| 334 | |
| James Feist | 46342ec | 2019-03-06 14:03:41 -0800 | [diff] [blame] | 335 | return thresholdChanges; |
| 336 | } |
| 337 | |
| Zhikui Ren | 12c2c0e | 2021-04-28 17:21:21 -0700 | [diff] [blame] | 338 | void ThresholdTimer::startTimer(const std::weak_ptr<Sensor>& weakSensor, |
| 339 | const Threshold& threshold, bool assert, |
| Jeff Lin | d9cd704 | 2020-11-20 15:49:28 +0800 | [diff] [blame] | 340 | double assertValue) |
| 341 | { |
| 342 | struct TimerUsed timerUsed = {}; |
| 343 | constexpr const size_t waitTime = 5; |
| 344 | TimerPair* pair = nullptr; |
| 345 | |
| 346 | for (TimerPair& timer : timers) |
| 347 | { |
| 348 | if (!timer.first.used) |
| 349 | { |
| 350 | pair = &timer; |
| 351 | break; |
| 352 | } |
| 353 | } |
| 354 | if (pair == nullptr) |
| 355 | { |
| Ed Tanous | 9b4a20e | 2022-09-06 08:47:11 -0700 | [diff] [blame] | 356 | pair = &timers.emplace_back(timerUsed, boost::asio::steady_timer(io)); |
| Jeff Lin | d9cd704 | 2020-11-20 15:49:28 +0800 | [diff] [blame] | 357 | } |
| 358 | |
| 359 | pair->first.used = true; |
| 360 | pair->first.level = threshold.level; |
| 361 | pair->first.direction = threshold.direction; |
| 362 | pair->first.assert = assert; |
| Ed Tanous | 83db50c | 2023-03-01 10:20:24 -0800 | [diff] [blame] | 363 | pair->second.expires_after(std::chrono::seconds(waitTime)); |
| Zhikui Ren | 12c2c0e | 2021-04-28 17:21:21 -0700 | [diff] [blame] | 364 | pair->second.async_wait([weakSensor, pair, threshold, assert, |
| Jeff Lin | d9cd704 | 2020-11-20 15:49:28 +0800 | [diff] [blame] | 365 | assertValue](boost::system::error_code ec) { |
| Zhikui Ren | 12c2c0e | 2021-04-28 17:21:21 -0700 | [diff] [blame] | 366 | auto sensorPtr = weakSensor.lock(); |
| 367 | if (!sensorPtr) |
| 368 | { |
| 369 | return; // owner sensor has been destructed |
| 370 | } |
| 371 | // pair is valid as long as sensor is valid |
| Jeff Lin | d9cd704 | 2020-11-20 15:49:28 +0800 | [diff] [blame] | 372 | pair->first.used = false; |
| 373 | |
| 374 | if (ec == boost::asio::error::operation_aborted) |
| 375 | { |
| 376 | return; // we're being canceled |
| 377 | } |
| 378 | if (ec) |
| 379 | { |
| George Liu | 6198435 | 2025-02-24 14:47:34 +0800 | [diff] [blame] | 380 | lg2::error("timer error: '{ERROR_MESSAGE}'", "ERROR_MESSAGE", |
| 381 | ec.message()); |
| Jeff Lin | d9cd704 | 2020-11-20 15:49:28 +0800 | [diff] [blame] | 382 | return; |
| 383 | } |
| Zhikui Ren | 12c2c0e | 2021-04-28 17:21:21 -0700 | [diff] [blame] | 384 | if (sensorPtr->readingStateGood()) |
| Jeff Lin | d9cd704 | 2020-11-20 15:49:28 +0800 | [diff] [blame] | 385 | { |
| Zhikui Ren | 12c2c0e | 2021-04-28 17:21:21 -0700 | [diff] [blame] | 386 | assertThresholds(sensorPtr.get(), assertValue, threshold.level, |
| Jeff Lin | d9cd704 | 2020-11-20 15:49:28 +0800 | [diff] [blame] | 387 | threshold.direction, assert); |
| 388 | } |
| 389 | }); |
| 390 | } |
| 391 | |
| James Feist | 46342ec | 2019-03-06 14:03:41 -0800 | [diff] [blame] | 392 | bool checkThresholds(Sensor* sensor) |
| 393 | { |
| James Feist | 7b18b1e | 2019-05-14 13:42:09 -0700 | [diff] [blame] | 394 | bool status = true; |
| Zhikui Ren | 59b8b9e | 2020-06-26 18:34:22 -0700 | [diff] [blame] | 395 | std::vector<ChangeParam> changes = checkThresholds(sensor, sensor->value); |
| 396 | for (const auto& change : changes) |
| James Feist | 46342ec | 2019-03-06 14:03:41 -0800 | [diff] [blame] | 397 | { |
| Zhikui Ren | 59b8b9e | 2020-06-26 18:34:22 -0700 | [diff] [blame] | 398 | assertThresholds(sensor, change.assertValue, change.threshold.level, |
| 399 | change.threshold.direction, change.asserted); |
| 400 | if (change.threshold.level == thresholds::Level::CRITICAL && |
| 401 | change.asserted) |
| James Feist | 46342ec | 2019-03-06 14:03:41 -0800 | [diff] [blame] | 402 | { |
| James Feist | 7b18b1e | 2019-05-14 13:42:09 -0700 | [diff] [blame] | 403 | status = false; |
| James Feist | 46342ec | 2019-03-06 14:03:41 -0800 | [diff] [blame] | 404 | } |
| 405 | } |
| 406 | |
| James Feist | dc6c55f | 2018-10-31 12:53:20 -0700 | [diff] [blame] | 407 | return status; |
| James Feist | 251c782 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 408 | } |
| 409 | |
| Zhikui Ren | 12c2c0e | 2021-04-28 17:21:21 -0700 | [diff] [blame] | 410 | void checkThresholdsPowerDelay(const std::weak_ptr<Sensor>& weakSensor, |
| 411 | ThresholdTimer& thresholdTimer) |
| James Feist | 46342ec | 2019-03-06 14:03:41 -0800 | [diff] [blame] | 412 | { |
| Zhikui Ren | 12c2c0e | 2021-04-28 17:21:21 -0700 | [diff] [blame] | 413 | auto sensorPtr = weakSensor.lock(); |
| 414 | if (!sensorPtr) |
| 415 | { |
| 416 | return; // sensor is destructed, should never be here |
| 417 | } |
| James Feist | 46342ec | 2019-03-06 14:03:41 -0800 | [diff] [blame] | 418 | |
| Zhikui Ren | 12c2c0e | 2021-04-28 17:21:21 -0700 | [diff] [blame] | 419 | Sensor* sensor = sensorPtr.get(); |
| Zhikui Ren | 59b8b9e | 2020-06-26 18:34:22 -0700 | [diff] [blame] | 420 | std::vector<ChangeParam> changes = checkThresholds(sensor, sensor->value); |
| 421 | for (const auto& change : changes) |
| James Feist | 46342ec | 2019-03-06 14:03:41 -0800 | [diff] [blame] | 422 | { |
| Zhikui Ren | bf7cbc8 | 2020-07-02 08:44:00 -0700 | [diff] [blame] | 423 | // When CPU is powered off, some volatges are expected to |
| 424 | // go below low thresholds. Filter these events with thresholdTimer. |
| 425 | // 1. always delay the assertion of low events to see if they are |
| 426 | // caused by power off event. |
| 427 | // 2. conditional delay the de-assertion of low events if there is |
| 428 | // an existing timer for assertion. |
| 429 | // 3. no delays for de-assert of low events if there is an existing |
| 430 | // de-assert for low event. This means 2nd de-assert would happen |
| 431 | // first and when timer expires for the previous one, no additional |
| 432 | // signal will be logged. |
| 433 | // 4. no delays for all high events. |
| 434 | if (change.threshold.direction == thresholds::Direction::LOW) |
| James Feist | 46342ec | 2019-03-06 14:03:41 -0800 | [diff] [blame] | 435 | { |
| Zhikui Ren | bf7cbc8 | 2020-07-02 08:44:00 -0700 | [diff] [blame] | 436 | if (change.asserted || thresholdTimer.hasActiveTimer( |
| 437 | change.threshold, !change.asserted)) |
| 438 | { |
| Zhikui Ren | 12c2c0e | 2021-04-28 17:21:21 -0700 | [diff] [blame] | 439 | thresholdTimer.startTimer(weakSensor, change.threshold, |
| 440 | change.asserted, change.assertValue); |
| Zhikui Ren | bf7cbc8 | 2020-07-02 08:44:00 -0700 | [diff] [blame] | 441 | continue; |
| 442 | } |
| James Feist | 46342ec | 2019-03-06 14:03:41 -0800 | [diff] [blame] | 443 | } |
| Zhikui Ren | bf7cbc8 | 2020-07-02 08:44:00 -0700 | [diff] [blame] | 444 | assertThresholds(sensor, change.assertValue, change.threshold.level, |
| 445 | change.threshold.direction, change.asserted); |
| James Feist | 46342ec | 2019-03-06 14:03:41 -0800 | [diff] [blame] | 446 | } |
| 447 | } |
| 448 | |
| Zhikui Ren | 59b8b9e | 2020-06-26 18:34:22 -0700 | [diff] [blame] | 449 | void assertThresholds(Sensor* sensor, double assertValue, |
| 450 | thresholds::Level level, thresholds::Direction direction, |
| 451 | bool assert) |
| James Feist | 251c782 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 452 | { |
| Jayashree Dhanapal | 5667808 | 2022-01-04 17:27:20 +0530 | [diff] [blame] | 453 | std::shared_ptr<sdbusplus::asio::dbus_interface> interface = |
| 454 | sensor->getThresholdInterface(level); |
| 455 | |
| James Feist | 251c782 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 456 | if (!interface) |
| 457 | { |
| George Liu | 6198435 | 2025-02-24 14:47:34 +0800 | [diff] [blame] | 458 | lg2::info("trying to set uninitialized interface"); |
| James Feist | 251c782 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 459 | return; |
| 460 | } |
| Zhikui Ren | 59b8b9e | 2020-06-26 18:34:22 -0700 | [diff] [blame] | 461 | |
| Ed Tanous | 2049bd2 | 2022-07-09 07:20:26 -0700 | [diff] [blame] | 462 | std::string property = Sensor::propertyAlarm(level, direction); |
| Jayashree Dhanapal | 45f2702 | 2021-12-07 12:56:35 +0530 | [diff] [blame] | 463 | if (property.empty()) |
| 464 | { |
| George Liu | 6198435 | 2025-02-24 14:47:34 +0800 | [diff] [blame] | 465 | lg2::info("Alarm property is empty"); |
| Jayashree Dhanapal | 45f2702 | 2021-12-07 12:56:35 +0530 | [diff] [blame] | 466 | return; |
| 467 | } |
| Zhikui Ren | 59b8b9e | 2020-06-26 18:34:22 -0700 | [diff] [blame] | 468 | if (interface->set_property<bool, true>(property, assert)) |
| 469 | { |
| 470 | try |
| 471 | { |
| 472 | // msg.get_path() is interface->get_object_path() |
| Patrick Williams | 92f8f51 | 2022-07-22 19:26:55 -0500 | [diff] [blame] | 473 | sdbusplus::message_t msg = |
| Zhikui Ren | 59b8b9e | 2020-06-26 18:34:22 -0700 | [diff] [blame] | 474 | interface->new_signal("ThresholdAsserted"); |
| 475 | |
| 476 | msg.append(sensor->name, interface->get_interface_name(), property, |
| 477 | assert, assertValue); |
| 478 | msg.signal_send(); |
| 479 | } |
| Patrick Williams | 92f8f51 | 2022-07-22 19:26:55 -0500 | [diff] [blame] | 480 | catch (const sdbusplus::exception_t& e) |
| Zhikui Ren | 59b8b9e | 2020-06-26 18:34:22 -0700 | [diff] [blame] | 481 | { |
| George Liu | 6198435 | 2025-02-24 14:47:34 +0800 | [diff] [blame] | 482 | lg2::error( |
| 483 | "Failed to send thresholdAsserted signal with assertValue"); |
| Zhikui Ren | 59b8b9e | 2020-06-26 18:34:22 -0700 | [diff] [blame] | 484 | } |
| 485 | } |
| James Feist | 251c782 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 486 | } |
| 487 | |
| Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 488 | bool parseThresholdsFromAttr( |
| James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 489 | std::vector<thresholds::Threshold>& thresholdVector, |
| Vijay Khemka | 86dea2b | 2019-06-06 11:14:37 -0700 | [diff] [blame] | 490 | const std::string& inputPath, const double& scaleFactor, |
| Chris Sides | a327923 | 2023-04-24 16:08:13 -0500 | [diff] [blame] | 491 | const double& offset, const double& hysteresis) |
| James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 492 | { |
| Zbigniew Kurzynski | 0a4c480 | 2020-04-01 11:22:27 +0200 | [diff] [blame] | 493 | const boost::container::flat_map< |
| 494 | std::string, std::vector<std::tuple<const char*, thresholds::Level, |
| 495 | thresholds::Direction, double>>> |
| 496 | map = { |
| 497 | {"average", |
| 498 | { |
| 499 | std::make_tuple("average_min", Level::WARNING, Direction::LOW, |
| 500 | 0.0), |
| 501 | std::make_tuple("average_max", Level::WARNING, Direction::HIGH, |
| 502 | 0.0), |
| 503 | }}, |
| 504 | {"input", |
| 505 | { |
| 506 | std::make_tuple("min", Level::WARNING, Direction::LOW, 0.0), |
| 507 | std::make_tuple("max", Level::WARNING, Direction::HIGH, 0.0), |
| 508 | std::make_tuple("lcrit", Level::CRITICAL, Direction::LOW, 0.0), |
| 509 | std::make_tuple("crit", Level::CRITICAL, Direction::HIGH, |
| 510 | offset), |
| 511 | }}, |
| 512 | }; |
| 513 | |
| 514 | if (auto fileParts = splitFileName(inputPath)) |
| James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 515 | { |
| Zbigniew Kurzynski | dbfd466 | 2020-09-28 18:06:00 +0200 | [diff] [blame] | 516 | auto& [type, nr, item] = *fileParts; |
| Patrick Williams | 80c6805 | 2025-05-13 23:31:49 -0400 | [diff] [blame] | 517 | if (map.contains(item)) |
| Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 518 | { |
| Zbigniew Kurzynski | 0a4c480 | 2020-04-01 11:22:27 +0200 | [diff] [blame] | 519 | for (const auto& t : map.at(item)) |
| 520 | { |
| Ed Tanous | 2049bd2 | 2022-07-09 07:20:26 -0700 | [diff] [blame] | 521 | const auto& [suffix, level, direction, offset] = t; |
| Patrick Williams | 2aaf717 | 2024-08-16 15:20:40 -0400 | [diff] [blame] | 522 | auto attrPath = |
| 523 | boost::replace_all_copy(inputPath, item, suffix); |
| Zbigniew Kurzynski | 0a4c480 | 2020-04-01 11:22:27 +0200 | [diff] [blame] | 524 | if (auto val = readFile(attrPath, scaleFactor)) |
| 525 | { |
| 526 | *val += offset; |
| Alexander Hansen | 89be614 | 2025-09-18 15:36:16 +0200 | [diff] [blame^] | 527 | lg2::debug("Threshold: '{PATH}': '{VALUE}'", "PATH", |
| 528 | attrPath, "VALUE", *val); |
| Chris Sides | a327923 | 2023-04-24 16:08:13 -0500 | [diff] [blame] | 529 | thresholdVector.emplace_back(level, direction, *val, |
| 530 | hysteresis); |
| Zbigniew Kurzynski | 0a4c480 | 2020-04-01 11:22:27 +0200 | [diff] [blame] | 531 | } |
| 532 | } |
| Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 533 | } |
| James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 534 | } |
| James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 535 | return true; |
| 536 | } |
| 537 | |
| Jayashree Dhanapal | 5667808 | 2022-01-04 17:27:20 +0530 | [diff] [blame] | 538 | std::string getInterface(const Level thresholdLevel) |
| James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 539 | { |
| Ed Tanous | c8fed20 | 2022-01-12 14:24:45 -0800 | [diff] [blame] | 540 | for (const ThresholdDefinition& thresh : thresProp) |
| James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 541 | { |
| Ed Tanous | c8fed20 | 2022-01-12 14:24:45 -0800 | [diff] [blame] | 542 | if (thresh.level == thresholdLevel) |
| 543 | { |
| 544 | return std::string("xyz.openbmc_project.Sensor.Threshold.") + |
| 545 | thresh.levelName; |
| 546 | } |
| James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 547 | } |
| Ed Tanous | c8fed20 | 2022-01-12 14:24:45 -0800 | [diff] [blame] | 548 | return ""; |
| James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 549 | } |
| 550 | } // namespace thresholds |