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