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