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