James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 1 | /* |
| 2 | // Copyright (c) 2018 Intel Corporation |
| 3 | // |
| 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | // you may not use this file except in compliance with the License. |
| 6 | // You may obtain a copy of the License at |
| 7 | // |
| 8 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 | */ |
| 16 | |
Patrick Venture | ca44b2f | 2019-10-31 11:02:26 -0700 | [diff] [blame] | 17 | #include "CPUSensor.hpp" |
| 18 | |
| 19 | #include "Utils.hpp" |
| 20 | |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 21 | #include <unistd.h> |
| 22 | |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 23 | #include <boost/algorithm/string/predicate.hpp> |
| 24 | #include <boost/algorithm/string/replace.hpp> |
James Feist | 8086aba | 2020-08-25 16:00:59 -0700 | [diff] [blame] | 25 | #include <boost/asio/read_until.hpp> |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 26 | #include <boost/date_time/posix_time/posix_time.hpp> |
James Feist | 38fb598 | 2020-05-28 10:09:54 -0700 | [diff] [blame] | 27 | #include <sdbusplus/asio/connection.hpp> |
| 28 | #include <sdbusplus/asio/object_server.hpp> |
| 29 | |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 30 | #include <iostream> |
Patrick Venture | 96e97db | 2019-10-31 13:44:38 -0700 | [diff] [blame] | 31 | #include <istream> |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 32 | #include <limits> |
Patrick Venture | 96e97db | 2019-10-31 13:44:38 -0700 | [diff] [blame] | 33 | #include <memory> |
Patrick Venture | 96e97db | 2019-10-31 13:44:38 -0700 | [diff] [blame] | 34 | #include <stdexcept> |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 35 | #include <string> |
Patrick Venture | 96e97db | 2019-10-31 13:44:38 -0700 | [diff] [blame] | 36 | #include <vector> |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 37 | |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 38 | CPUSensor::CPUSensor(const std::string& path, const std::string& objectType, |
| 39 | sdbusplus::asio::object_server& objectServer, |
| 40 | std::shared_ptr<sdbusplus::asio::connection>& conn, |
| 41 | boost::asio::io_service& io, const std::string& sensorName, |
| 42 | std::vector<thresholds::Threshold>&& _thresholds, |
Jae Hyun Yoo | 73ca551 | 2019-02-28 21:20:17 -0800 | [diff] [blame] | 43 | const std::string& sensorConfiguration, int cpuId, |
Vijay Khemka | 86dea2b | 2019-06-06 11:14:37 -0700 | [diff] [blame] | 44 | bool show, double dtsOffset) : |
James Feist | 930fcde | 2019-05-28 12:58:43 -0700 | [diff] [blame] | 45 | Sensor(boost::replace_all_copy(sensorName, " ", "_"), |
James Feist | ce3fca4 | 2018-11-21 12:58:24 -0800 | [diff] [blame] | 46 | std::move(_thresholds), sensorConfiguration, objectType, maxReading, |
James Feist | 961bf09 | 2020-07-01 16:38:12 -0700 | [diff] [blame] | 47 | minReading, PowerState::on), |
James Feist | c22b842 | 2020-07-07 14:40:39 -0700 | [diff] [blame] | 48 | objServer(objectServer), inputDev(io), waitTimer(io), path(path), |
Brad Bishop | fbb44ad | 2019-11-08 09:42:37 -0500 | [diff] [blame] | 49 | privTcontrol(std::numeric_limits<double>::quiet_NaN()), |
Zbigniew Kurzynski | 98be984 | 2020-09-07 18:49:15 +0200 | [diff] [blame] | 50 | dtsOffset(dtsOffset), show(show), pollTime(CPUSensor::sensorPollMs), |
| 51 | minMaxReadCounter(0) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 52 | { |
Jae Hyun Yoo | 73ca551 | 2019-02-28 21:20:17 -0800 | [diff] [blame] | 53 | nameTcontrol = labelTcontrol; |
| 54 | nameTcontrol += " CPU" + std::to_string(cpuId); |
Jae Hyun Yoo | 73ca551 | 2019-02-28 21:20:17 -0800 | [diff] [blame] | 55 | if (show) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 56 | { |
Zbigniew Kurzynski | 63f3866 | 2020-06-09 13:02:11 +0200 | [diff] [blame] | 57 | if (auto fileParts = splitFileName(path)) |
Jae Hyun Yoo | 73ca551 | 2019-02-28 21:20:17 -0800 | [diff] [blame] | 58 | { |
Zbigniew Kurzynski | 0a4c480 | 2020-04-01 11:22:27 +0200 | [diff] [blame] | 59 | auto [type, nr, item] = *fileParts; |
| 60 | std::string interfacePath; |
| 61 | if (type.compare("power") == 0) |
| 62 | { |
| 63 | interfacePath = "/xyz/openbmc_project/sensors/power/" + name; |
| 64 | } |
| 65 | else |
| 66 | { |
| 67 | interfacePath = |
| 68 | "/xyz/openbmc_project/sensors/temperature/" + name; |
| 69 | } |
James Feist | 078f232 | 2019-03-08 11:09:05 -0800 | [diff] [blame] | 70 | |
Zbigniew Kurzynski | 0a4c480 | 2020-04-01 11:22:27 +0200 | [diff] [blame] | 71 | sensorInterface = objectServer.add_interface( |
| 72 | interfacePath, "xyz.openbmc_project.Sensor.Value"); |
| 73 | if (thresholds::hasWarningInterface(thresholds)) |
| 74 | { |
| 75 | thresholdInterfaceWarning = objectServer.add_interface( |
| 76 | interfacePath, |
| 77 | "xyz.openbmc_project.Sensor.Threshold.Warning"); |
| 78 | } |
| 79 | if (thresholds::hasCriticalInterface(thresholds)) |
| 80 | { |
| 81 | thresholdInterfaceCritical = objectServer.add_interface( |
| 82 | interfacePath, |
| 83 | "xyz.openbmc_project.Sensor.Threshold.Critical"); |
| 84 | } |
| 85 | association = objectServer.add_interface(interfacePath, |
| 86 | association::interface); |
| 87 | |
| 88 | setInitialProperties(conn); |
| 89 | } |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 90 | } |
James Feist | 961bf09 | 2020-07-01 16:38:12 -0700 | [diff] [blame] | 91 | |
| 92 | // call setup always as not all sensors call setInitialProperties |
James Feist | 71d31b2 | 2019-01-02 16:57:54 -0800 | [diff] [blame] | 93 | setupPowerMatch(conn); |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 94 | setupRead(); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | CPUSensor::~CPUSensor() |
| 98 | { |
| 99 | // close the input dev to cancel async operations |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 100 | inputDev.close(); |
| 101 | waitTimer.cancel(); |
Jae Hyun Yoo | 73ca551 | 2019-02-28 21:20:17 -0800 | [diff] [blame] | 102 | if (show) |
| 103 | { |
| 104 | objServer.remove_interface(thresholdInterfaceWarning); |
| 105 | objServer.remove_interface(thresholdInterfaceCritical); |
| 106 | objServer.remove_interface(sensorInterface); |
James Feist | 078f232 | 2019-03-08 11:09:05 -0800 | [diff] [blame] | 107 | objServer.remove_interface(association); |
Jae Hyun Yoo | 73ca551 | 2019-02-28 21:20:17 -0800 | [diff] [blame] | 108 | } |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 109 | } |
| 110 | |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 111 | void CPUSensor::setupRead(void) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 112 | { |
James Feist | c22b842 | 2020-07-07 14:40:39 -0700 | [diff] [blame] | 113 | if (readingStateGood()) |
| 114 | { |
| 115 | inputDev.close(); |
| 116 | int fd = open(path.c_str(), O_RDONLY); |
| 117 | if (fd >= 0) |
| 118 | { |
| 119 | inputDev.assign(fd); |
| 120 | |
| 121 | boost::asio::async_read_until( |
| 122 | inputDev, readBuf, '\n', |
| 123 | [&](const boost::system::error_code& ec, |
| 124 | std::size_t /*bytes_transfered*/) { handleResponse(ec); }); |
| 125 | } |
| 126 | else |
| 127 | { |
| 128 | std::cerr << name << " unable to open fd!\n"; |
| 129 | pollTime = sensorFailedPollTimeMs; |
| 130 | } |
| 131 | } |
| 132 | else |
| 133 | { |
| 134 | pollTime = sensorFailedPollTimeMs; |
| 135 | markAvailable(false); |
| 136 | } |
| 137 | waitTimer.expires_from_now(boost::posix_time::milliseconds(pollTime)); |
| 138 | waitTimer.async_wait([&](const boost::system::error_code& ec) { |
| 139 | if (ec == boost::asio::error::operation_aborted) |
| 140 | { |
| 141 | return; // we're being canceled |
| 142 | } |
| 143 | setupRead(); |
| 144 | }); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 145 | } |
| 146 | |
Zbigniew Kurzynski | 8d8d8d7 | 2020-05-29 19:21:24 +0200 | [diff] [blame] | 147 | void CPUSensor::updateMinMaxValues(void) |
| 148 | { |
| 149 | const boost::container::flat_map< |
| 150 | std::string, |
| 151 | std::vector<std::tuple<const char*, std::reference_wrapper<double>, |
| 152 | const char*>>> |
| 153 | map = { |
| 154 | { |
| 155 | "cap", |
| 156 | { |
| 157 | std::make_tuple("cap_max", std::ref(maxValue), "MaxValue"), |
| 158 | std::make_tuple("cap_min", std::ref(minValue), "MinValue"), |
| 159 | }, |
| 160 | }, |
| 161 | }; |
| 162 | |
Zbigniew Kurzynski | 63f3866 | 2020-06-09 13:02:11 +0200 | [diff] [blame] | 163 | if (auto fileParts = splitFileName(path)) |
Zbigniew Kurzynski | 8d8d8d7 | 2020-05-29 19:21:24 +0200 | [diff] [blame] | 164 | { |
| 165 | auto [fileType, fileNr, fileItem] = *fileParts; |
| 166 | const auto mapIt = map.find(fileItem); |
| 167 | if (mapIt != map.cend()) |
| 168 | { |
| 169 | for (const auto& vectorItem : mapIt->second) |
| 170 | { |
| 171 | auto [suffix, oldValue, dbusName] = vectorItem; |
| 172 | auto attrPath = boost::replace_all_copy(path, fileItem, suffix); |
Zbigniew Kurzynski | 63f3866 | 2020-06-09 13:02:11 +0200 | [diff] [blame] | 173 | if (auto newVal = |
| 174 | readFile(attrPath, CPUSensor::sensorScaleFactor)) |
Zbigniew Kurzynski | 8d8d8d7 | 2020-05-29 19:21:24 +0200 | [diff] [blame] | 175 | { |
Zbigniew Kurzynski | 4f45e42 | 2020-06-09 12:42:15 +0200 | [diff] [blame] | 176 | updateProperty(sensorInterface, oldValue, *newVal, |
| 177 | dbusName); |
Zbigniew Kurzynski | 8d8d8d7 | 2020-05-29 19:21:24 +0200 | [diff] [blame] | 178 | } |
| 179 | else |
| 180 | { |
| 181 | if (isPowerOn()) |
| 182 | { |
Zbigniew Kurzynski | 4f45e42 | 2020-06-09 12:42:15 +0200 | [diff] [blame] | 183 | updateProperty(sensorInterface, oldValue, 0, dbusName); |
Zbigniew Kurzynski | 8d8d8d7 | 2020-05-29 19:21:24 +0200 | [diff] [blame] | 184 | } |
| 185 | else |
| 186 | { |
Zbigniew Kurzynski | 4f45e42 | 2020-06-09 12:42:15 +0200 | [diff] [blame] | 187 | updateProperty(sensorInterface, oldValue, |
| 188 | std::numeric_limits<double>::quiet_NaN(), |
| 189 | dbusName); |
Zbigniew Kurzynski | 8d8d8d7 | 2020-05-29 19:21:24 +0200 | [diff] [blame] | 190 | } |
| 191 | } |
| 192 | } |
| 193 | } |
| 194 | } |
| 195 | } |
| 196 | |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 197 | void CPUSensor::handleResponse(const boost::system::error_code& err) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 198 | { |
James Feist | 838529b | 2020-09-02 16:57:05 -0700 | [diff] [blame] | 199 | |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 200 | if (err == boost::system::errc::bad_file_descriptor) |
| 201 | { |
| 202 | return; // we're being destroyed |
| 203 | } |
James Feist | 838529b | 2020-09-02 16:57:05 -0700 | [diff] [blame] | 204 | else if (err == boost::system::errc::operation_canceled) |
| 205 | { |
| 206 | if (readingStateGood()) |
| 207 | { |
| 208 | if (!loggedInterfaceDown) |
| 209 | { |
| 210 | std::cerr << name << " interface down!\n"; |
| 211 | loggedInterfaceDown = true; |
| 212 | } |
| 213 | pollTime = 10000 + rand() % 10000; |
| 214 | markFunctional(false); |
| 215 | } |
| 216 | return; |
| 217 | } |
| 218 | loggedInterfaceDown = false; |
James Feist | c22b842 | 2020-07-07 14:40:39 -0700 | [diff] [blame] | 219 | pollTime = CPUSensor::sensorPollMs; |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 220 | std::istream responseStream(&readBuf); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 221 | if (!err) |
| 222 | { |
| 223 | std::string response; |
| 224 | try |
| 225 | { |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 226 | std::getline(responseStream, response); |
Zhikui Ren | d3da128 | 2020-09-11 17:02:01 -0700 | [diff] [blame^] | 227 | rawValue = std::stod(response); |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 228 | responseStream.clear(); |
Zhikui Ren | d3da128 | 2020-09-11 17:02:01 -0700 | [diff] [blame^] | 229 | double nvalue = rawValue / CPUSensor::sensorScaleFactor; |
Josh Lehan | 833661a | 2020-03-04 17:35:41 -0800 | [diff] [blame] | 230 | |
| 231 | if (show) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 232 | { |
Josh Lehan | 833661a | 2020-03-04 17:35:41 -0800 | [diff] [blame] | 233 | updateValue(nvalue); |
Jae Hyun Yoo | 73ca551 | 2019-02-28 21:20:17 -0800 | [diff] [blame] | 234 | } |
Josh Lehan | 833661a | 2020-03-04 17:35:41 -0800 | [diff] [blame] | 235 | else |
| 236 | { |
| 237 | value = nvalue; |
| 238 | } |
Zbigniew Kurzynski | 98be984 | 2020-09-07 18:49:15 +0200 | [diff] [blame] | 239 | if (minMaxReadCounter++ % 8 == 0) |
| 240 | { |
| 241 | updateMinMaxValues(); |
| 242 | } |
Josh Lehan | 833661a | 2020-03-04 17:35:41 -0800 | [diff] [blame] | 243 | |
Jae Hyun Yoo | 73ca551 | 2019-02-28 21:20:17 -0800 | [diff] [blame] | 244 | double gTcontrol = gCpuSensors[nameTcontrol] |
| 245 | ? gCpuSensors[nameTcontrol]->value |
| 246 | : std::numeric_limits<double>::quiet_NaN(); |
| 247 | if (gTcontrol != privTcontrol) |
| 248 | { |
| 249 | privTcontrol = gTcontrol; |
| 250 | |
| 251 | if (!thresholds.empty()) |
| 252 | { |
| 253 | std::vector<thresholds::Threshold> newThresholds; |
| 254 | if (parseThresholdsFromAttr(newThresholds, path, |
Vijay Khemka | 86dea2b | 2019-06-06 11:14:37 -0700 | [diff] [blame] | 255 | CPUSensor::sensorScaleFactor, |
| 256 | dtsOffset)) |
Jae Hyun Yoo | 73ca551 | 2019-02-28 21:20:17 -0800 | [diff] [blame] | 257 | { |
| 258 | if (!std::equal(thresholds.begin(), thresholds.end(), |
| 259 | newThresholds.begin(), |
| 260 | newThresholds.end())) |
| 261 | { |
| 262 | thresholds = newThresholds; |
| 263 | if (show) |
| 264 | { |
| 265 | thresholds::updateThresholds(this); |
| 266 | } |
| 267 | } |
| 268 | } |
| 269 | else |
| 270 | { |
| 271 | std::cerr << "Failure to update thresholds for " << name |
| 272 | << "\n"; |
| 273 | } |
Jae Hyun Yoo | 95b8a2d | 2019-02-25 20:15:09 -0800 | [diff] [blame] | 274 | } |
| 275 | } |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 276 | } |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 277 | catch (const std::invalid_argument&) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 278 | { |
James Feist | 961bf09 | 2020-07-01 16:38:12 -0700 | [diff] [blame] | 279 | incrementError(); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 280 | } |
| 281 | } |
| 282 | else |
| 283 | { |
James Feist | 1169eb4 | 2018-10-31 10:08:47 -0700 | [diff] [blame] | 284 | pollTime = sensorFailedPollTimeMs; |
James Feist | 961bf09 | 2020-07-01 16:38:12 -0700 | [diff] [blame] | 285 | incrementError(); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 286 | } |
| 287 | |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 288 | responseStream.clear(); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 289 | } |
| 290 | |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 291 | void CPUSensor::checkThresholds(void) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 292 | { |
Jae Hyun Yoo | 73ca551 | 2019-02-28 21:20:17 -0800 | [diff] [blame] | 293 | if (show) |
| 294 | { |
| 295 | thresholds::checkThresholds(this); |
| 296 | } |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 297 | } |