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