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