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