Jagpal Singh Gill | 23f091e | 2023-12-10 15:23:19 -0800 | [diff] [blame] | 1 | #include "health_metric.hpp" |
| 2 | |
| 3 | #include <phosphor-logging/lg2.hpp> |
| 4 | |
Jagpal Singh Gill | b94b122 | 2024-03-02 17:53:30 -0800 | [diff] [blame] | 5 | #include <cmath> |
Jagpal Singh Gill | 23f091e | 2023-12-10 15:23:19 -0800 | [diff] [blame] | 6 | #include <numeric> |
| 7 | #include <unordered_map> |
| 8 | |
| 9 | PHOSPHOR_LOG2_USING; |
| 10 | |
| 11 | namespace phosphor::health::metric |
| 12 | { |
| 13 | |
| 14 | using association_t = std::tuple<std::string, std::string, std::string>; |
| 15 | |
Patrick Williams | 2d4cbeb | 2024-12-18 11:21:50 -0500 | [diff] [blame^] | 16 | auto HealthMetric::getPath(MType type, std::string name, SubType subType) |
| 17 | -> std::string |
Jagpal Singh Gill | 23f091e | 2023-12-10 15:23:19 -0800 | [diff] [blame] | 18 | { |
| 19 | std::string path; |
| 20 | switch (subType) |
| 21 | { |
| 22 | case SubType::cpuTotal: |
| 23 | { |
| 24 | return std::string(BmcPath) + "/" + PathIntf::total_cpu; |
| 25 | } |
| 26 | case SubType::cpuKernel: |
| 27 | { |
| 28 | return std::string(BmcPath) + "/" + PathIntf::kernel_cpu; |
| 29 | } |
| 30 | case SubType::cpuUser: |
| 31 | { |
| 32 | return std::string(BmcPath) + "/" + PathIntf::user_cpu; |
| 33 | } |
| 34 | case SubType::memoryAvailable: |
| 35 | { |
| 36 | return std::string(BmcPath) + "/" + PathIntf::available_memory; |
| 37 | } |
| 38 | case SubType::memoryBufferedAndCached: |
| 39 | { |
| 40 | return std::string(BmcPath) + "/" + |
| 41 | PathIntf::buffered_and_cached_memory; |
| 42 | } |
| 43 | case SubType::memoryFree: |
| 44 | { |
| 45 | return std::string(BmcPath) + "/" + PathIntf::free_memory; |
| 46 | } |
| 47 | case SubType::memoryShared: |
| 48 | { |
| 49 | return std::string(BmcPath) + "/" + PathIntf::shared_memory; |
| 50 | } |
| 51 | case SubType::memoryTotal: |
| 52 | { |
| 53 | return std::string(BmcPath) + "/" + PathIntf::total_memory; |
| 54 | } |
Jagpal Singh Gill | 9758280 | 2024-02-27 13:59:11 -0800 | [diff] [blame] | 55 | case SubType::NA: |
Jagpal Singh Gill | 23f091e | 2023-12-10 15:23:19 -0800 | [diff] [blame] | 56 | { |
Patrick Williams | 658efd5 | 2024-03-04 12:53:52 -0600 | [diff] [blame] | 57 | if (type == MType::storage) |
Jagpal Singh Gill | 9758280 | 2024-02-27 13:59:11 -0800 | [diff] [blame] | 58 | { |
| 59 | static constexpr auto nameDelimiter = "_"; |
| 60 | auto storageType = name.substr( |
| 61 | name.find_last_of(nameDelimiter) + 1, name.length()); |
Patrick Williams | ce8b5ae | 2024-08-16 15:21:18 -0400 | [diff] [blame] | 62 | std::ranges::for_each(storageType, [](auto& c) { |
| 63 | c = std::tolower(c); |
| 64 | }); |
Jagpal Singh Gill | 9758280 | 2024-02-27 13:59:11 -0800 | [diff] [blame] | 65 | return std::string(BmcPath) + "/" + PathIntf::storage + "/" + |
| 66 | storageType; |
| 67 | } |
| 68 | else |
| 69 | { |
| 70 | error("Invalid metric {SUBTYPE} for metric {TYPE}", "SUBTYPE", |
| 71 | subType, "TYPE", type); |
| 72 | return ""; |
| 73 | } |
Jagpal Singh Gill | dfe839f | 2024-02-16 09:54:02 -0800 | [diff] [blame] | 74 | } |
Jagpal Singh Gill | 23f091e | 2023-12-10 15:23:19 -0800 | [diff] [blame] | 75 | default: |
| 76 | { |
Jagpal Singh Gill | 9758280 | 2024-02-27 13:59:11 -0800 | [diff] [blame] | 77 | error("Invalid metric {SUBTYPE}", "SUBTYPE", subType); |
Jagpal Singh Gill | 23f091e | 2023-12-10 15:23:19 -0800 | [diff] [blame] | 78 | return ""; |
| 79 | } |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | void HealthMetric::initProperties() |
| 84 | { |
Jagpal Singh Gill | 9758280 | 2024-02-27 13:59:11 -0800 | [diff] [blame] | 85 | switch (type) |
Jagpal Singh Gill | 23f091e | 2023-12-10 15:23:19 -0800 | [diff] [blame] | 86 | { |
Jagpal Singh Gill | 9758280 | 2024-02-27 13:59:11 -0800 | [diff] [blame] | 87 | case MType::cpu: |
Jagpal Singh Gill | 23f091e | 2023-12-10 15:23:19 -0800 | [diff] [blame] | 88 | { |
| 89 | ValueIntf::unit(ValueIntf::Unit::Percent, true); |
| 90 | ValueIntf::minValue(0.0, true); |
| 91 | ValueIntf::maxValue(100.0, true); |
| 92 | break; |
| 93 | } |
Jagpal Singh Gill | 9758280 | 2024-02-27 13:59:11 -0800 | [diff] [blame] | 94 | case MType::memory: |
| 95 | case MType::storage: |
Jagpal Singh Gill | 23f091e | 2023-12-10 15:23:19 -0800 | [diff] [blame] | 96 | { |
| 97 | ValueIntf::unit(ValueIntf::Unit::Bytes, true); |
| 98 | ValueIntf::minValue(0.0, true); |
Jagpal Singh Gill | 9758280 | 2024-02-27 13:59:11 -0800 | [diff] [blame] | 99 | break; |
| 100 | } |
| 101 | case MType::inode: |
| 102 | case MType::unknown: |
| 103 | default: |
| 104 | { |
| 105 | throw std::invalid_argument("Invalid metric type"); |
Jagpal Singh Gill | 23f091e | 2023-12-10 15:23:19 -0800 | [diff] [blame] | 106 | } |
| 107 | } |
Jagpal Singh Gill | c5b18bc | 2024-02-09 15:58:12 -0800 | [diff] [blame] | 108 | ValueIntf::value(std::numeric_limits<double>::quiet_NaN(), true); |
Jagpal Singh Gill | 23f091e | 2023-12-10 15:23:19 -0800 | [diff] [blame] | 109 | |
Patrick Williams | 658efd5 | 2024-03-04 12:53:52 -0600 | [diff] [blame] | 110 | using bound_map_t = std::map<Bound, double>; |
| 111 | std::map<Type, bound_map_t> thresholds; |
Jagpal Singh Gill | 23f091e | 2023-12-10 15:23:19 -0800 | [diff] [blame] | 112 | for (const auto& [key, value] : config.thresholds) |
| 113 | { |
Patrick Williams | 658efd5 | 2024-03-04 12:53:52 -0600 | [diff] [blame] | 114 | auto type = std::get<Type>(key); |
| 115 | auto bound = std::get<Bound>(key); |
Jagpal Singh Gill | 23f091e | 2023-12-10 15:23:19 -0800 | [diff] [blame] | 116 | auto threshold = thresholds.find(type); |
| 117 | if (threshold == thresholds.end()) |
| 118 | { |
| 119 | bound_map_t bounds; |
Jagpal Singh Gill | 6a3884a | 2024-02-24 18:08:23 -0800 | [diff] [blame] | 120 | bounds.emplace(bound, std::numeric_limits<double>::quiet_NaN()); |
Jagpal Singh Gill | 23f091e | 2023-12-10 15:23:19 -0800 | [diff] [blame] | 121 | thresholds.emplace(type, bounds); |
| 122 | } |
| 123 | else |
| 124 | { |
| 125 | threshold->second.emplace(bound, value.value); |
| 126 | } |
| 127 | } |
Jagpal Singh Gill | c5b18bc | 2024-02-09 15:58:12 -0800 | [diff] [blame] | 128 | ThresholdIntf::value(thresholds, true); |
Jagpal Singh Gill | 23f091e | 2023-12-10 15:23:19 -0800 | [diff] [blame] | 129 | } |
| 130 | |
Jagpal Singh Gill | 55fb0c9 | 2024-02-22 18:07:13 -0800 | [diff] [blame] | 131 | bool didThresholdViolate(ThresholdIntf::Bound bound, double thresholdValue, |
| 132 | double value) |
| 133 | { |
| 134 | switch (bound) |
| 135 | { |
| 136 | case ThresholdIntf::Bound::Lower: |
| 137 | { |
| 138 | return (value < thresholdValue); |
| 139 | } |
| 140 | case ThresholdIntf::Bound::Upper: |
| 141 | { |
| 142 | return (value > thresholdValue); |
| 143 | } |
| 144 | default: |
| 145 | { |
Patrick Williams | 67b8ebe | 2024-02-23 20:40:52 -0600 | [diff] [blame] | 146 | error("Invalid threshold bound {BOUND}", "BOUND", bound); |
Jagpal Singh Gill | 55fb0c9 | 2024-02-22 18:07:13 -0800 | [diff] [blame] | 147 | return false; |
| 148 | } |
| 149 | } |
| 150 | } |
| 151 | |
Patrick Williams | 658efd5 | 2024-03-04 12:53:52 -0600 | [diff] [blame] | 152 | void HealthMetric::checkThreshold(Type type, Bound bound, MValue value) |
Jagpal Singh Gill | 23f091e | 2023-12-10 15:23:19 -0800 | [diff] [blame] | 153 | { |
| 154 | auto threshold = std::make_tuple(type, bound); |
| 155 | auto thresholds = ThresholdIntf::value(); |
| 156 | |
| 157 | if (thresholds.contains(type) && thresholds[type].contains(bound)) |
| 158 | { |
Jagpal Singh Gill | 6a3884a | 2024-02-24 18:08:23 -0800 | [diff] [blame] | 159 | auto tConfig = config.thresholds.at(threshold); |
| 160 | auto thresholdValue = tConfig.value / 100 * value.total; |
| 161 | thresholds[type][bound] = thresholdValue; |
| 162 | ThresholdIntf::value(thresholds); |
Jagpal Singh Gill | 23f091e | 2023-12-10 15:23:19 -0800 | [diff] [blame] | 163 | auto assertions = ThresholdIntf::asserted(); |
Jagpal Singh Gill | 6a3884a | 2024-02-24 18:08:23 -0800 | [diff] [blame] | 164 | if (didThresholdViolate(bound, thresholdValue, value.current)) |
Jagpal Singh Gill | 23f091e | 2023-12-10 15:23:19 -0800 | [diff] [blame] | 165 | { |
| 166 | if (!assertions.contains(threshold)) |
| 167 | { |
| 168 | assertions.insert(threshold); |
| 169 | ThresholdIntf::asserted(assertions); |
Jagpal Singh Gill | 6a3884a | 2024-02-24 18:08:23 -0800 | [diff] [blame] | 170 | ThresholdIntf::assertionChanged(type, bound, true, |
| 171 | value.current); |
Jagpal Singh Gill | 23f091e | 2023-12-10 15:23:19 -0800 | [diff] [blame] | 172 | if (tConfig.log) |
| 173 | { |
| 174 | error( |
| 175 | "ASSERT: Health Metric {METRIC} crossed {TYPE} upper threshold", |
Patrick Williams | 67b8ebe | 2024-02-23 20:40:52 -0600 | [diff] [blame] | 176 | "METRIC", config.name, "TYPE", type); |
Jagpal Singh Gill | 23f091e | 2023-12-10 15:23:19 -0800 | [diff] [blame] | 177 | startUnit(bus, tConfig.target); |
| 178 | } |
| 179 | } |
| 180 | return; |
| 181 | } |
| 182 | else if (assertions.contains(threshold)) |
| 183 | { |
| 184 | assertions.erase(threshold); |
| 185 | ThresholdIntf::asserted(assertions); |
Jagpal Singh Gill | 6a3884a | 2024-02-24 18:08:23 -0800 | [diff] [blame] | 186 | ThresholdIntf::assertionChanged(type, bound, false, value.current); |
Jagpal Singh Gill | 23f091e | 2023-12-10 15:23:19 -0800 | [diff] [blame] | 187 | if (config.thresholds.find(threshold)->second.log) |
| 188 | { |
| 189 | info( |
| 190 | "DEASSERT: Health Metric {METRIC} is below {TYPE} upper threshold", |
Patrick Williams | 67b8ebe | 2024-02-23 20:40:52 -0600 | [diff] [blame] | 191 | "METRIC", config.name, "TYPE", type); |
Jagpal Singh Gill | 23f091e | 2023-12-10 15:23:19 -0800 | [diff] [blame] | 192 | } |
| 193 | } |
| 194 | } |
| 195 | } |
| 196 | |
Jagpal Singh Gill | 6a3884a | 2024-02-24 18:08:23 -0800 | [diff] [blame] | 197 | void HealthMetric::checkThresholds(MValue value) |
Jagpal Singh Gill | 23f091e | 2023-12-10 15:23:19 -0800 | [diff] [blame] | 198 | { |
| 199 | if (!ThresholdIntf::value().empty()) |
| 200 | { |
Patrick Williams | 658efd5 | 2024-03-04 12:53:52 -0600 | [diff] [blame] | 201 | for (auto type : {Type::HardShutdown, Type::SoftShutdown, |
| 202 | Type::PerformanceLoss, Type::Critical, Type::Warning}) |
Jagpal Singh Gill | 23f091e | 2023-12-10 15:23:19 -0800 | [diff] [blame] | 203 | { |
Patrick Williams | 658efd5 | 2024-03-04 12:53:52 -0600 | [diff] [blame] | 204 | checkThreshold(type, Bound::Lower, value); |
| 205 | checkThreshold(type, Bound::Upper, value); |
Jagpal Singh Gill | 23f091e | 2023-12-10 15:23:19 -0800 | [diff] [blame] | 206 | } |
| 207 | } |
| 208 | } |
| 209 | |
Jagpal Singh Gill | b94b122 | 2024-03-02 17:53:30 -0800 | [diff] [blame] | 210 | auto HealthMetric::shouldNotify(MValue value) -> bool |
| 211 | { |
| 212 | if (std::isnan(value.current)) |
| 213 | { |
| 214 | return true; |
| 215 | } |
Patrick Williams | ce8b5ae | 2024-08-16 15:21:18 -0400 | [diff] [blame] | 216 | auto changed = std::abs( |
| 217 | (value.current - lastNotifiedValue) / lastNotifiedValue * 100.0); |
Jagpal Singh Gill | a102762 | 2024-03-05 17:57:33 -0800 | [diff] [blame] | 218 | if (changed >= config.hysteresis) |
Jagpal Singh Gill | b94b122 | 2024-03-02 17:53:30 -0800 | [diff] [blame] | 219 | { |
| 220 | lastNotifiedValue = value.current; |
| 221 | return true; |
| 222 | } |
| 223 | return false; |
| 224 | } |
| 225 | |
Jagpal Singh Gill | 23f091e | 2023-12-10 15:23:19 -0800 | [diff] [blame] | 226 | void HealthMetric::update(MValue value) |
| 227 | { |
Jagpal Singh Gill | b94b122 | 2024-03-02 17:53:30 -0800 | [diff] [blame] | 228 | ValueIntf::value(value.current, !shouldNotify(value)); |
Jagpal Singh Gill | 8fd4df2 | 2024-03-01 15:40:26 -0800 | [diff] [blame] | 229 | |
| 230 | // Maintain window size for threshold calculation |
Jagpal Singh Gill | 23f091e | 2023-12-10 15:23:19 -0800 | [diff] [blame] | 231 | if (history.size() >= config.windowSize) |
| 232 | { |
| 233 | history.pop_front(); |
| 234 | } |
Jagpal Singh Gill | 6a3884a | 2024-02-24 18:08:23 -0800 | [diff] [blame] | 235 | history.push_back(value.current); |
Jagpal Singh Gill | 23f091e | 2023-12-10 15:23:19 -0800 | [diff] [blame] | 236 | |
| 237 | if (history.size() < config.windowSize) |
| 238 | { |
| 239 | // Wait for the metric to have enough samples to calculate average |
Jagpal Singh Gill | 23f091e | 2023-12-10 15:23:19 -0800 | [diff] [blame] | 240 | return; |
| 241 | } |
| 242 | |
Patrick Williams | ce8b5ae | 2024-08-16 15:21:18 -0400 | [diff] [blame] | 243 | double average = |
| 244 | (std::accumulate(history.begin(), history.end(), 0.0)) / history.size(); |
Jagpal Singh Gill | 8fd4df2 | 2024-03-01 15:40:26 -0800 | [diff] [blame] | 245 | value.current = average; |
Jagpal Singh Gill | 6a3884a | 2024-02-24 18:08:23 -0800 | [diff] [blame] | 246 | checkThresholds(value); |
Jagpal Singh Gill | 23f091e | 2023-12-10 15:23:19 -0800 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | void HealthMetric::create(const paths_t& bmcPaths) |
| 250 | { |
| 251 | info("Create Health Metric: {METRIC}", "METRIC", config.name); |
| 252 | initProperties(); |
| 253 | |
| 254 | std::vector<association_t> associations; |
| 255 | static constexpr auto forwardAssociation = "measuring"; |
| 256 | static constexpr auto reverseAssociation = "measured_by"; |
| 257 | for (const auto& bmcPath : bmcPaths) |
| 258 | { |
| 259 | /* |
| 260 | * This metric is "measuring" the health for the BMC at bmcPath |
| 261 | * The BMC at bmcPath is "measured_by" this metric. |
| 262 | */ |
| 263 | associations.push_back( |
| 264 | {forwardAssociation, reverseAssociation, bmcPath}); |
| 265 | } |
| 266 | AssociationIntf::associations(associations); |
| 267 | } |
| 268 | |
| 269 | } // namespace phosphor::health::metric |