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