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 | { |
Ed Tanous | c8fed20 | 2022-01-12 14:24:45 -0800 | [diff] [blame] | 22 | Level findThresholdLevel(uint8_t sev) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 23 | { |
Ed Tanous | c8fed20 | 2022-01-12 14:24:45 -0800 | [diff] [blame] | 24 | for (const ThresholdDefinition& prop : thresProp) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 25 | { |
Ed Tanous | c8fed20 | 2022-01-12 14:24:45 -0800 | [diff] [blame] | 26 | if (prop.sevOrder == sev) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 27 | { |
Jayashree Dhanapal | 45f2702 | 2021-12-07 12:56:35 +0530 | [diff] [blame] | 28 | return prop.level; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 29 | } |
| 30 | } |
Jayashree Dhanapal | 45f2702 | 2021-12-07 12:56:35 +0530 | [diff] [blame] | 31 | return Level::ERROR; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 32 | } |
| 33 | |
Ed Tanous | c8fed20 | 2022-01-12 14:24:45 -0800 | [diff] [blame] | 34 | Direction findThresholdDirection(const std::string& direct) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 35 | { |
Ed Tanous | c8fed20 | 2022-01-12 14:24:45 -0800 | [diff] [blame] | 36 | if (direct == "greater than") |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 37 | { |
Ed Tanous | c8fed20 | 2022-01-12 14:24:45 -0800 | [diff] [blame] | 38 | return Direction::HIGH; |
| 39 | } |
| 40 | if (direct == "less than") |
| 41 | { |
| 42 | return Direction::LOW; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 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 | |
Ed Tanous | c8fed20 | 2022-01-12 14:24:45 -0800 | [diff] [blame] | 47 | bool isValidLevel(Level lev) |
Jayashree Dhanapal | 5667808 | 2022-01-04 17:27:20 +0530 | [diff] [blame] | 48 | { |
Ed Tanous | c8fed20 | 2022-01-12 14:24:45 -0800 | [diff] [blame] | 49 | for (const ThresholdDefinition& prop : thresProp) |
Jayashree Dhanapal | 5667808 | 2022-01-04 17:27:20 +0530 | [diff] [blame] | 50 | { |
Ed Tanous | c8fed20 | 2022-01-12 14:24:45 -0800 | [diff] [blame] | 51 | if (prop.level == lev) |
Jayashree Dhanapal | 5667808 | 2022-01-04 17:27:20 +0530 | [diff] [blame] | 52 | { |
| 53 | return true; |
| 54 | } |
| 55 | } |
| 56 | return false; |
| 57 | } |
| 58 | |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 59 | bool parseThresholdsFromConfig( |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 60 | const SensorData& sensorData, |
| 61 | std::vector<thresholds::Threshold>& thresholdVector, |
Matt Spinler | 5636d52 | 2021-03-17 14:52:18 -0500 | [diff] [blame] | 62 | const std::string* matchLabel, const int* sensorIndex) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 63 | { |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 64 | for (const auto& item : sensorData) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 65 | { |
| 66 | if (item.first.find("Thresholds") == std::string::npos) |
| 67 | { |
| 68 | continue; |
| 69 | } |
| 70 | if (matchLabel != nullptr) |
| 71 | { |
| 72 | auto labelFind = item.second.find("Label"); |
| 73 | if (labelFind == item.second.end()) |
Ed Tanous | 8a57ec0 | 2020-10-09 12:46:52 -0700 | [diff] [blame] | 74 | { |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 75 | continue; |
Ed Tanous | 8a57ec0 | 2020-10-09 12:46:52 -0700 | [diff] [blame] | 76 | } |
James Feist | 3eb8262 | 2019-02-08 13:10:22 -0800 | [diff] [blame] | 77 | if (std::visit(VariantToStringVisitor(), labelFind->second) != |
| 78 | *matchLabel) |
Ed Tanous | 8a57ec0 | 2020-10-09 12:46:52 -0700 | [diff] [blame] | 79 | { |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 80 | continue; |
Ed Tanous | 8a57ec0 | 2020-10-09 12:46:52 -0700 | [diff] [blame] | 81 | } |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 82 | } |
Matt Spinler | 5636d52 | 2021-03-17 14:52:18 -0500 | [diff] [blame] | 83 | |
| 84 | if (sensorIndex != nullptr) |
| 85 | { |
| 86 | auto indexFind = item.second.find("Index"); |
| 87 | |
| 88 | // If we're checking for index 1, a missing Index is OK. |
| 89 | if ((indexFind == item.second.end()) && (*sensorIndex != 1)) |
| 90 | { |
| 91 | continue; |
| 92 | } |
| 93 | |
| 94 | if ((indexFind != item.second.end()) && |
| 95 | (std::visit(VariantToIntVisitor(), indexFind->second) != |
| 96 | *sensorIndex)) |
| 97 | { |
| 98 | continue; |
| 99 | } |
| 100 | } |
| 101 | |
Rashmica Gupta | 1e34cec | 2021-08-31 16:47:39 +1000 | [diff] [blame] | 102 | double hysteresis = std::numeric_limits<double>::quiet_NaN(); |
| 103 | auto hysteresisFind = item.second.find("Hysteresis"); |
| 104 | if (hysteresisFind != item.second.end()) |
| 105 | { |
| 106 | hysteresis = |
| 107 | std::visit(VariantToDoubleVisitor(), hysteresisFind->second); |
| 108 | } |
| 109 | |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 110 | auto directionFind = item.second.find("Direction"); |
| 111 | auto severityFind = item.second.find("Severity"); |
| 112 | auto valueFind = item.second.find("Value"); |
| 113 | if (valueFind == item.second.end() || |
| 114 | severityFind == item.second.end() || |
| 115 | directionFind == item.second.end()) |
| 116 | { |
Matt Spinler | 5636d52 | 2021-03-17 14:52:18 -0500 | [diff] [blame] | 117 | std::cerr << "Malformed threshold on configuration interface " |
| 118 | << item.first << "\n"; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 119 | return false; |
| 120 | } |
Rashmica Gupta | d2208a9 | 2022-01-15 13:04:22 +1100 | [diff] [blame] | 121 | unsigned int severity = |
Jayashree Dhanapal | 45f2702 | 2021-12-07 12:56:35 +0530 | [diff] [blame] | 122 | std::visit(VariantToUnsignedIntVisitor(), severityFind->second); |
| 123 | |
| 124 | std::string directions = |
| 125 | std::visit(VariantToStringVisitor(), directionFind->second); |
| 126 | |
Ed Tanous | c8fed20 | 2022-01-12 14:24:45 -0800 | [diff] [blame] | 127 | Level level = findThresholdLevel(severity); |
| 128 | Direction direction = findThresholdDirection(directions); |
Jayashree Dhanapal | 45f2702 | 2021-12-07 12:56:35 +0530 | [diff] [blame] | 129 | |
Jayashree Dhanapal | 091c92c | 2022-01-11 14:55:20 +0530 | [diff] [blame] | 130 | if ((level == Level::ERROR) || (direction == Direction::ERROR)) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 131 | { |
Jayashree Dhanapal | 45f2702 | 2021-12-07 12:56:35 +0530 | [diff] [blame] | 132 | continue; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 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 | |
Rashmica Gupta | 1e34cec | 2021-08-31 16:47:39 +1000 | [diff] [blame] | 136 | thresholdVector.emplace_back(level, direction, val, hysteresis); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 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 | } |
Rashmica Gupta | d2208a9 | 2022-01-15 13:04:22 +1100 | [diff] [blame] | 185 | unsigned int severity = std::visit( |
| 186 | VariantToUnsignedIntVisitor(), 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); |
Ed Tanous | c8fed20 | 2022-01-12 14:24:45 -0800 | [diff] [blame] | 190 | if ((findThresholdLevel(severity) != threshold.level) || |
| 191 | (findThresholdDirection(dir) != threshold.direction)) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 192 | { |
| 193 | return; // not the droid we're looking for |
| 194 | } |
| 195 | |
James Feist | 3eb8262 | 2019-02-08 13:10:22 -0800 | [diff] [blame] | 196 | std::variant<double> value(threshold.value); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 197 | conn->async_method_call( |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 198 | [](const boost::system::error_code& ec) { |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 199 | if (ec) |
| 200 | { |
| 201 | std::cerr << "Error setting threshold " << ec |
| 202 | << "\n"; |
| 203 | } |
| 204 | }, |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 205 | entityManagerName, path, "org.freedesktop.DBus.Properties", |
| 206 | "Set", thresholdInterface, "Value", value); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 207 | }, |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 208 | entityManagerName, path, "org.freedesktop.DBus.Properties", |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 209 | "GetAll", thresholdInterface); |
| 210 | } |
| 211 | } |
| 212 | |
Jae Hyun Yoo | 95b8a2d | 2019-02-25 20:15:09 -0800 | [diff] [blame] | 213 | void updateThresholds(Sensor* sensor) |
| 214 | { |
Jae Hyun Yoo | 95b8a2d | 2019-02-25 20:15:09 -0800 | [diff] [blame] | 215 | for (const auto& threshold : sensor->thresholds) |
| 216 | { |
Ed Tanous | c8fed20 | 2022-01-12 14:24:45 -0800 | [diff] [blame] | 217 | if (!isValidLevel(threshold.level)) |
Jae Hyun Yoo | 95b8a2d | 2019-02-25 20:15:09 -0800 | [diff] [blame] | 218 | { |
| 219 | continue; |
| 220 | } |
Jayashree Dhanapal | 5667808 | 2022-01-04 17:27:20 +0530 | [diff] [blame] | 221 | std::shared_ptr<sdbusplus::asio::dbus_interface> interface = |
| 222 | sensor->getThresholdInterface(threshold.level); |
| 223 | |
Jae Hyun Yoo | 95b8a2d | 2019-02-25 20:15:09 -0800 | [diff] [blame] | 224 | if (!interface) |
| 225 | { |
| 226 | continue; |
| 227 | } |
Jayashree Dhanapal | 45f2702 | 2021-12-07 12:56:35 +0530 | [diff] [blame] | 228 | |
| 229 | std::string property = |
| 230 | sensor->propertyLevel(threshold.level, threshold.direction); |
| 231 | if (property.empty()) |
| 232 | { |
| 233 | continue; |
| 234 | } |
Jae Hyun Yoo | 95b8a2d | 2019-02-25 20:15:09 -0800 | [diff] [blame] | 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 | } |
Rashmica Gupta | 1e34cec | 2021-08-31 16:47:39 +1000 | [diff] [blame] | 287 | else if (value < (threshold.value - threshold.hysteresis)) |
Josh Lehan | 883fb3a | 2020-02-27 14:41:39 -0800 | [diff] [blame] | 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 | } |
Rashmica Gupta | 1e34cec | 2021-08-31 16:47:39 +1000 | [diff] [blame] | 310 | else if (value > (threshold.value + threshold.hysteresis)) |
Josh Lehan | 883fb3a | 2020-02-27 14:41:39 -0800 | [diff] [blame] | 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 | |
Ed Tanous | 8a17c30 | 2021-09-02 15:07:11 -0700 | [diff] [blame] | 326 | // Throttle debug output, so that it does not continuously spam |
| 327 | ++cDebugThrottle; |
| 328 | if (cDebugThrottle >= 1000) |
Josh Lehan | 883fb3a | 2020-02-27 14:41:39 -0800 | [diff] [blame] | 329 | { |
Ed Tanous | 8a17c30 | 2021-09-02 15:07:11 -0700 | [diff] [blame] | 330 | cDebugThrottle = 0; |
| 331 | if constexpr (debug) |
Josh Lehan | 883fb3a | 2020-02-27 14:41:39 -0800 | [diff] [blame] | 332 | { |
Josh Lehan | 883fb3a | 2020-02-27 14:41:39 -0800 | [diff] [blame] | 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 | |
Zhikui Ren | 12c2c0e | 2021-04-28 17:21:21 -0700 | [diff] [blame] | 343 | void ThresholdTimer::startTimer(const std::weak_ptr<Sensor>& weakSensor, |
| 344 | const Threshold& threshold, bool assert, |
Jeff Lin | d9cd704 | 2020-11-20 15:49:28 +0800 | [diff] [blame] | 345 | double assertValue) |
| 346 | { |
| 347 | struct TimerUsed timerUsed = {}; |
| 348 | constexpr const size_t waitTime = 5; |
| 349 | TimerPair* pair = nullptr; |
| 350 | |
| 351 | for (TimerPair& timer : timers) |
| 352 | { |
| 353 | if (!timer.first.used) |
| 354 | { |
| 355 | pair = &timer; |
| 356 | break; |
| 357 | } |
| 358 | } |
| 359 | if (pair == nullptr) |
| 360 | { |
| 361 | pair = &timers.emplace_back(timerUsed, boost::asio::deadline_timer(io)); |
| 362 | } |
| 363 | |
| 364 | pair->first.used = true; |
| 365 | pair->first.level = threshold.level; |
| 366 | pair->first.direction = threshold.direction; |
| 367 | pair->first.assert = assert; |
| 368 | pair->second.expires_from_now(boost::posix_time::seconds(waitTime)); |
Zhikui Ren | 12c2c0e | 2021-04-28 17:21:21 -0700 | [diff] [blame] | 369 | pair->second.async_wait([weakSensor, pair, threshold, assert, |
Jeff Lin | d9cd704 | 2020-11-20 15:49:28 +0800 | [diff] [blame] | 370 | assertValue](boost::system::error_code ec) { |
Zhikui Ren | 12c2c0e | 2021-04-28 17:21:21 -0700 | [diff] [blame] | 371 | auto sensorPtr = weakSensor.lock(); |
| 372 | if (!sensorPtr) |
| 373 | { |
| 374 | return; // owner sensor has been destructed |
| 375 | } |
| 376 | // pair is valid as long as sensor is valid |
Jeff Lin | d9cd704 | 2020-11-20 15:49:28 +0800 | [diff] [blame] | 377 | pair->first.used = false; |
| 378 | |
| 379 | if (ec == boost::asio::error::operation_aborted) |
| 380 | { |
| 381 | return; // we're being canceled |
| 382 | } |
| 383 | if (ec) |
| 384 | { |
| 385 | std::cerr << "timer error: " << ec.message() << "\n"; |
| 386 | return; |
| 387 | } |
Zhikui Ren | 12c2c0e | 2021-04-28 17:21:21 -0700 | [diff] [blame] | 388 | if (sensorPtr->readingStateGood()) |
Jeff Lin | d9cd704 | 2020-11-20 15:49:28 +0800 | [diff] [blame] | 389 | { |
Zhikui Ren | 12c2c0e | 2021-04-28 17:21:21 -0700 | [diff] [blame] | 390 | assertThresholds(sensorPtr.get(), assertValue, threshold.level, |
Jeff Lin | d9cd704 | 2020-11-20 15:49:28 +0800 | [diff] [blame] | 391 | threshold.direction, assert); |
| 392 | } |
| 393 | }); |
| 394 | } |
| 395 | |
James Feist | 46342ec | 2019-03-06 14:03:41 -0800 | [diff] [blame] | 396 | bool checkThresholds(Sensor* sensor) |
| 397 | { |
James Feist | 7b18b1e | 2019-05-14 13:42:09 -0700 | [diff] [blame] | 398 | bool status = true; |
Zhikui Ren | 59b8b9e | 2020-06-26 18:34:22 -0700 | [diff] [blame] | 399 | std::vector<ChangeParam> changes = checkThresholds(sensor, sensor->value); |
| 400 | for (const auto& change : changes) |
James Feist | 46342ec | 2019-03-06 14:03:41 -0800 | [diff] [blame] | 401 | { |
Zhikui Ren | 59b8b9e | 2020-06-26 18:34:22 -0700 | [diff] [blame] | 402 | assertThresholds(sensor, change.assertValue, change.threshold.level, |
| 403 | change.threshold.direction, change.asserted); |
| 404 | if (change.threshold.level == thresholds::Level::CRITICAL && |
| 405 | change.asserted) |
James Feist | 46342ec | 2019-03-06 14:03:41 -0800 | [diff] [blame] | 406 | { |
James Feist | 7b18b1e | 2019-05-14 13:42:09 -0700 | [diff] [blame] | 407 | status = false; |
James Feist | 46342ec | 2019-03-06 14:03:41 -0800 | [diff] [blame] | 408 | } |
| 409 | } |
| 410 | |
James Feist | dc6c55f | 2018-10-31 12:53:20 -0700 | [diff] [blame] | 411 | return status; |
James Feist | 251c782 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 412 | } |
| 413 | |
Zhikui Ren | 12c2c0e | 2021-04-28 17:21:21 -0700 | [diff] [blame] | 414 | void checkThresholdsPowerDelay(const std::weak_ptr<Sensor>& weakSensor, |
| 415 | ThresholdTimer& thresholdTimer) |
James Feist | 46342ec | 2019-03-06 14:03:41 -0800 | [diff] [blame] | 416 | { |
Zhikui Ren | 12c2c0e | 2021-04-28 17:21:21 -0700 | [diff] [blame] | 417 | auto sensorPtr = weakSensor.lock(); |
| 418 | if (!sensorPtr) |
| 419 | { |
| 420 | return; // sensor is destructed, should never be here |
| 421 | } |
James Feist | 46342ec | 2019-03-06 14:03:41 -0800 | [diff] [blame] | 422 | |
Zhikui Ren | 12c2c0e | 2021-04-28 17:21:21 -0700 | [diff] [blame] | 423 | Sensor* sensor = sensorPtr.get(); |
Zhikui Ren | 59b8b9e | 2020-06-26 18:34:22 -0700 | [diff] [blame] | 424 | std::vector<ChangeParam> changes = checkThresholds(sensor, sensor->value); |
| 425 | for (const auto& change : changes) |
James Feist | 46342ec | 2019-03-06 14:03:41 -0800 | [diff] [blame] | 426 | { |
Zhikui Ren | bf7cbc8 | 2020-07-02 08:44:00 -0700 | [diff] [blame] | 427 | // When CPU is powered off, some volatges are expected to |
| 428 | // go below low thresholds. Filter these events with thresholdTimer. |
| 429 | // 1. always delay the assertion of low events to see if they are |
| 430 | // caused by power off event. |
| 431 | // 2. conditional delay the de-assertion of low events if there is |
| 432 | // an existing timer for assertion. |
| 433 | // 3. no delays for de-assert of low events if there is an existing |
| 434 | // de-assert for low event. This means 2nd de-assert would happen |
| 435 | // first and when timer expires for the previous one, no additional |
| 436 | // signal will be logged. |
| 437 | // 4. no delays for all high events. |
| 438 | if (change.threshold.direction == thresholds::Direction::LOW) |
James Feist | 46342ec | 2019-03-06 14:03:41 -0800 | [diff] [blame] | 439 | { |
Zhikui Ren | bf7cbc8 | 2020-07-02 08:44:00 -0700 | [diff] [blame] | 440 | if (change.asserted || thresholdTimer.hasActiveTimer( |
| 441 | change.threshold, !change.asserted)) |
| 442 | { |
Zhikui Ren | 12c2c0e | 2021-04-28 17:21:21 -0700 | [diff] [blame] | 443 | thresholdTimer.startTimer(weakSensor, change.threshold, |
| 444 | change.asserted, change.assertValue); |
Zhikui Ren | bf7cbc8 | 2020-07-02 08:44:00 -0700 | [diff] [blame] | 445 | continue; |
| 446 | } |
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 | assertThresholds(sensor, change.assertValue, change.threshold.level, |
| 449 | change.threshold.direction, change.asserted); |
James Feist | 46342ec | 2019-03-06 14:03:41 -0800 | [diff] [blame] | 450 | } |
| 451 | } |
| 452 | |
Zhikui Ren | 59b8b9e | 2020-06-26 18:34:22 -0700 | [diff] [blame] | 453 | void assertThresholds(Sensor* sensor, double assertValue, |
| 454 | thresholds::Level level, thresholds::Direction direction, |
| 455 | bool assert) |
James Feist | 251c782 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 456 | { |
Ed Tanous | c8fed20 | 2022-01-12 14:24:45 -0800 | [diff] [blame] | 457 | if (!isValidLevel(level)) |
James Feist | 251c782 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 458 | { |
James Feist | 251c782 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 459 | return; |
| 460 | } |
Jayashree Dhanapal | 5667808 | 2022-01-04 17:27:20 +0530 | [diff] [blame] | 461 | |
| 462 | std::shared_ptr<sdbusplus::asio::dbus_interface> interface = |
| 463 | sensor->getThresholdInterface(level); |
| 464 | |
James Feist | 251c782 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 465 | if (!interface) |
| 466 | { |
| 467 | std::cout << "trying to set uninitialized interface\n"; |
| 468 | return; |
| 469 | } |
Zhikui Ren | 59b8b9e | 2020-06-26 18:34:22 -0700 | [diff] [blame] | 470 | |
Jayashree Dhanapal | 45f2702 | 2021-12-07 12:56:35 +0530 | [diff] [blame] | 471 | std::string property = sensor->propertyAlarm(level, direction); |
| 472 | if (property.empty()) |
| 473 | { |
| 474 | std::cout << "Alarm property is empty \n"; |
| 475 | return; |
| 476 | } |
Zhikui Ren | 59b8b9e | 2020-06-26 18:34:22 -0700 | [diff] [blame] | 477 | if (interface->set_property<bool, true>(property, assert)) |
| 478 | { |
| 479 | try |
| 480 | { |
| 481 | // msg.get_path() is interface->get_object_path() |
| 482 | sdbusplus::message::message msg = |
| 483 | interface->new_signal("ThresholdAsserted"); |
| 484 | |
| 485 | msg.append(sensor->name, interface->get_interface_name(), property, |
| 486 | assert, assertValue); |
| 487 | msg.signal_send(); |
| 488 | } |
| 489 | catch (const sdbusplus::exception::exception& e) |
| 490 | { |
| 491 | std::cerr |
| 492 | << "Failed to send thresholdAsserted signal with assertValue\n"; |
| 493 | } |
| 494 | } |
James Feist | 251c782 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 495 | } |
| 496 | |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 497 | bool parseThresholdsFromAttr( |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 498 | std::vector<thresholds::Threshold>& thresholdVector, |
Vijay Khemka | 86dea2b | 2019-06-06 11:14:37 -0700 | [diff] [blame] | 499 | const std::string& inputPath, const double& scaleFactor, |
| 500 | const double& offset) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 501 | { |
Zbigniew Kurzynski | 0a4c480 | 2020-04-01 11:22:27 +0200 | [diff] [blame] | 502 | const boost::container::flat_map< |
| 503 | std::string, std::vector<std::tuple<const char*, thresholds::Level, |
| 504 | thresholds::Direction, double>>> |
| 505 | map = { |
| 506 | {"average", |
| 507 | { |
| 508 | std::make_tuple("average_min", Level::WARNING, Direction::LOW, |
| 509 | 0.0), |
| 510 | std::make_tuple("average_max", Level::WARNING, Direction::HIGH, |
| 511 | 0.0), |
| 512 | }}, |
| 513 | {"input", |
| 514 | { |
| 515 | std::make_tuple("min", Level::WARNING, Direction::LOW, 0.0), |
| 516 | std::make_tuple("max", Level::WARNING, Direction::HIGH, 0.0), |
| 517 | std::make_tuple("lcrit", Level::CRITICAL, Direction::LOW, 0.0), |
| 518 | std::make_tuple("crit", Level::CRITICAL, Direction::HIGH, |
| 519 | offset), |
| 520 | }}, |
| 521 | }; |
| 522 | |
| 523 | if (auto fileParts = splitFileName(inputPath)) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 524 | { |
Zbigniew Kurzynski | dbfd466 | 2020-09-28 18:06:00 +0200 | [diff] [blame] | 525 | auto& [type, nr, item] = *fileParts; |
Zbigniew Kurzynski | 0a4c480 | 2020-04-01 11:22:27 +0200 | [diff] [blame] | 526 | if (map.count(item) != 0) |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 527 | { |
Zbigniew Kurzynski | 0a4c480 | 2020-04-01 11:22:27 +0200 | [diff] [blame] | 528 | for (const auto& t : map.at(item)) |
| 529 | { |
Zbigniew Kurzynski | dbfd466 | 2020-09-28 18:06:00 +0200 | [diff] [blame] | 530 | auto& [suffix, level, direction, offset] = t; |
Zbigniew Kurzynski | 0a4c480 | 2020-04-01 11:22:27 +0200 | [diff] [blame] | 531 | auto attrPath = |
| 532 | boost::replace_all_copy(inputPath, item, suffix); |
| 533 | if (auto val = readFile(attrPath, scaleFactor)) |
| 534 | { |
| 535 | *val += offset; |
Ed Tanous | 8a57ec0 | 2020-10-09 12:46:52 -0700 | [diff] [blame] | 536 | if (debug) |
Zbigniew Kurzynski | 0a4c480 | 2020-04-01 11:22:27 +0200 | [diff] [blame] | 537 | { |
| 538 | std::cout << "Threshold: " << attrPath << ": " << *val |
| 539 | << "\n"; |
| 540 | } |
| 541 | thresholdVector.emplace_back(level, direction, *val); |
| 542 | } |
| 543 | } |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 544 | } |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 545 | } |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 546 | return true; |
| 547 | } |
| 548 | |
Jayashree Dhanapal | 5667808 | 2022-01-04 17:27:20 +0530 | [diff] [blame] | 549 | std::string getInterface(const Level thresholdLevel) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 550 | { |
Ed Tanous | c8fed20 | 2022-01-12 14:24:45 -0800 | [diff] [blame] | 551 | for (const ThresholdDefinition& thresh : thresProp) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 552 | { |
Ed Tanous | c8fed20 | 2022-01-12 14:24:45 -0800 | [diff] [blame] | 553 | if (thresh.level == thresholdLevel) |
| 554 | { |
| 555 | return std::string("xyz.openbmc_project.Sensor.Threshold.") + |
| 556 | thresh.levelName; |
| 557 | } |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 558 | } |
Ed Tanous | c8fed20 | 2022-01-12 14:24:45 -0800 | [diff] [blame] | 559 | return ""; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 560 | } |
| 561 | } // namespace thresholds |