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()), |
James Feist | c22b842 | 2020-07-07 14:40:39 -0700 | [diff] [blame] | 50 | dtsOffset(dtsOffset), show(show), pollTime(CPUSensor::sensorPollMs) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 51 | { |
Jae Hyun Yoo | 73ca551 | 2019-02-28 21:20:17 -0800 | [diff] [blame] | 52 | nameTcontrol = labelTcontrol; |
| 53 | nameTcontrol += " CPU" + std::to_string(cpuId); |
Jae Hyun Yoo | 73ca551 | 2019-02-28 21:20:17 -0800 | [diff] [blame] | 54 | if (show) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 55 | { |
Zbigniew Kurzynski | 63f3866 | 2020-06-09 13:02:11 +0200 | [diff] [blame] | 56 | if (auto fileParts = splitFileName(path)) |
Jae Hyun Yoo | 73ca551 | 2019-02-28 21:20:17 -0800 | [diff] [blame] | 57 | { |
Zbigniew Kurzynski | 0a4c480 | 2020-04-01 11:22:27 +0200 | [diff] [blame] | 58 | auto [type, nr, item] = *fileParts; |
| 59 | std::string interfacePath; |
| 60 | if (type.compare("power") == 0) |
| 61 | { |
| 62 | interfacePath = "/xyz/openbmc_project/sensors/power/" + name; |
| 63 | } |
| 64 | else |
| 65 | { |
| 66 | interfacePath = |
| 67 | "/xyz/openbmc_project/sensors/temperature/" + name; |
| 68 | } |
James Feist | 078f232 | 2019-03-08 11:09:05 -0800 | [diff] [blame] | 69 | |
Zbigniew Kurzynski | 0a4c480 | 2020-04-01 11:22:27 +0200 | [diff] [blame] | 70 | sensorInterface = objectServer.add_interface( |
| 71 | interfacePath, "xyz.openbmc_project.Sensor.Value"); |
| 72 | if (thresholds::hasWarningInterface(thresholds)) |
| 73 | { |
| 74 | thresholdInterfaceWarning = objectServer.add_interface( |
| 75 | interfacePath, |
| 76 | "xyz.openbmc_project.Sensor.Threshold.Warning"); |
| 77 | } |
| 78 | if (thresholds::hasCriticalInterface(thresholds)) |
| 79 | { |
| 80 | thresholdInterfaceCritical = objectServer.add_interface( |
| 81 | interfacePath, |
| 82 | "xyz.openbmc_project.Sensor.Threshold.Critical"); |
| 83 | } |
| 84 | association = objectServer.add_interface(interfacePath, |
| 85 | association::interface); |
| 86 | |
| 87 | setInitialProperties(conn); |
| 88 | } |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 89 | } |
James Feist | 961bf09 | 2020-07-01 16:38:12 -0700 | [diff] [blame] | 90 | |
| 91 | // call setup always as not all sensors call setInitialProperties |
James Feist | 71d31b2 | 2019-01-02 16:57:54 -0800 | [diff] [blame] | 92 | setupPowerMatch(conn); |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 93 | setupRead(); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | CPUSensor::~CPUSensor() |
| 97 | { |
| 98 | // close the input dev to cancel async operations |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 99 | inputDev.close(); |
| 100 | waitTimer.cancel(); |
Jae Hyun Yoo | 73ca551 | 2019-02-28 21:20:17 -0800 | [diff] [blame] | 101 | if (show) |
| 102 | { |
| 103 | objServer.remove_interface(thresholdInterfaceWarning); |
| 104 | objServer.remove_interface(thresholdInterfaceCritical); |
| 105 | objServer.remove_interface(sensorInterface); |
James Feist | 078f232 | 2019-03-08 11:09:05 -0800 | [diff] [blame] | 106 | objServer.remove_interface(association); |
Jae Hyun Yoo | 73ca551 | 2019-02-28 21:20:17 -0800 | [diff] [blame] | 107 | } |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 108 | } |
| 109 | |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 110 | void CPUSensor::setupRead(void) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 111 | { |
James Feist | c22b842 | 2020-07-07 14:40:39 -0700 | [diff] [blame] | 112 | if (readingStateGood()) |
| 113 | { |
| 114 | inputDev.close(); |
| 115 | int fd = open(path.c_str(), O_RDONLY); |
| 116 | if (fd >= 0) |
| 117 | { |
| 118 | inputDev.assign(fd); |
| 119 | |
| 120 | boost::asio::async_read_until( |
| 121 | inputDev, readBuf, '\n', |
| 122 | [&](const boost::system::error_code& ec, |
| 123 | std::size_t /*bytes_transfered*/) { handleResponse(ec); }); |
| 124 | } |
| 125 | else |
| 126 | { |
| 127 | std::cerr << name << " unable to open fd!\n"; |
| 128 | pollTime = sensorFailedPollTimeMs; |
| 129 | } |
| 130 | } |
| 131 | else |
| 132 | { |
| 133 | pollTime = sensorFailedPollTimeMs; |
| 134 | markAvailable(false); |
| 135 | } |
| 136 | waitTimer.expires_from_now(boost::posix_time::milliseconds(pollTime)); |
| 137 | waitTimer.async_wait([&](const boost::system::error_code& ec) { |
| 138 | if (ec == boost::asio::error::operation_aborted) |
| 139 | { |
| 140 | return; // we're being canceled |
| 141 | } |
| 142 | setupRead(); |
| 143 | }); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 144 | } |
| 145 | |
Zbigniew Kurzynski | 8d8d8d7 | 2020-05-29 19:21:24 +0200 | [diff] [blame] | 146 | void CPUSensor::updateMinMaxValues(void) |
| 147 | { |
| 148 | const boost::container::flat_map< |
| 149 | std::string, |
| 150 | std::vector<std::tuple<const char*, std::reference_wrapper<double>, |
| 151 | const char*>>> |
| 152 | map = { |
| 153 | { |
| 154 | "cap", |
| 155 | { |
| 156 | std::make_tuple("cap_max", std::ref(maxValue), "MaxValue"), |
| 157 | std::make_tuple("cap_min", std::ref(minValue), "MinValue"), |
| 158 | }, |
| 159 | }, |
| 160 | }; |
| 161 | |
Zbigniew Kurzynski | 63f3866 | 2020-06-09 13:02:11 +0200 | [diff] [blame] | 162 | if (auto fileParts = splitFileName(path)) |
Zbigniew Kurzynski | 8d8d8d7 | 2020-05-29 19:21:24 +0200 | [diff] [blame] | 163 | { |
| 164 | auto [fileType, fileNr, fileItem] = *fileParts; |
| 165 | const auto mapIt = map.find(fileItem); |
| 166 | if (mapIt != map.cend()) |
| 167 | { |
| 168 | for (const auto& vectorItem : mapIt->second) |
| 169 | { |
| 170 | auto [suffix, oldValue, dbusName] = vectorItem; |
| 171 | auto attrPath = boost::replace_all_copy(path, fileItem, suffix); |
Zbigniew Kurzynski | 63f3866 | 2020-06-09 13:02:11 +0200 | [diff] [blame] | 172 | if (auto newVal = |
| 173 | readFile(attrPath, CPUSensor::sensorScaleFactor)) |
Zbigniew Kurzynski | 8d8d8d7 | 2020-05-29 19:21:24 +0200 | [diff] [blame] | 174 | { |
Zbigniew Kurzynski | 4f45e42 | 2020-06-09 12:42:15 +0200 | [diff] [blame] | 175 | updateProperty(sensorInterface, oldValue, *newVal, |
| 176 | dbusName); |
Zbigniew Kurzynski | 8d8d8d7 | 2020-05-29 19:21:24 +0200 | [diff] [blame] | 177 | } |
| 178 | else |
| 179 | { |
| 180 | if (isPowerOn()) |
| 181 | { |
Zbigniew Kurzynski | 4f45e42 | 2020-06-09 12:42:15 +0200 | [diff] [blame] | 182 | updateProperty(sensorInterface, oldValue, 0, dbusName); |
Zbigniew Kurzynski | 8d8d8d7 | 2020-05-29 19:21:24 +0200 | [diff] [blame] | 183 | } |
| 184 | else |
| 185 | { |
Zbigniew Kurzynski | 4f45e42 | 2020-06-09 12:42:15 +0200 | [diff] [blame] | 186 | updateProperty(sensorInterface, oldValue, |
| 187 | std::numeric_limits<double>::quiet_NaN(), |
| 188 | dbusName); |
Zbigniew Kurzynski | 8d8d8d7 | 2020-05-29 19:21:24 +0200 | [diff] [blame] | 189 | } |
| 190 | } |
| 191 | } |
| 192 | } |
| 193 | } |
| 194 | } |
| 195 | |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 196 | void CPUSensor::handleResponse(const boost::system::error_code& err) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 197 | { |
James Feist | 838529b | 2020-09-02 16:57:05 -0700 | [diff] [blame] | 198 | |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 199 | if (err == boost::system::errc::bad_file_descriptor) |
| 200 | { |
| 201 | return; // we're being destroyed |
| 202 | } |
James Feist | 838529b | 2020-09-02 16:57:05 -0700 | [diff] [blame] | 203 | else if (err == boost::system::errc::operation_canceled) |
| 204 | { |
| 205 | if (readingStateGood()) |
| 206 | { |
| 207 | if (!loggedInterfaceDown) |
| 208 | { |
| 209 | std::cerr << name << " interface down!\n"; |
| 210 | loggedInterfaceDown = true; |
| 211 | } |
| 212 | pollTime = 10000 + rand() % 10000; |
| 213 | markFunctional(false); |
| 214 | } |
| 215 | return; |
| 216 | } |
| 217 | loggedInterfaceDown = false; |
James Feist | c22b842 | 2020-07-07 14:40:39 -0700 | [diff] [blame] | 218 | pollTime = CPUSensor::sensorPollMs; |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 219 | std::istream responseStream(&readBuf); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 220 | if (!err) |
| 221 | { |
| 222 | std::string response; |
| 223 | try |
| 224 | { |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 225 | std::getline(responseStream, response); |
Zhikui Ren | 9bf6804 | 2020-08-13 15:31:26 -0700 | [diff] [blame] | 226 | rawValue = std::stod(response); |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 227 | responseStream.clear(); |
Zhikui Ren | 9bf6804 | 2020-08-13 15:31:26 -0700 | [diff] [blame] | 228 | double nvalue = rawValue / CPUSensor::sensorScaleFactor; |
Josh Lehan | 833661a | 2020-03-04 17:35:41 -0800 | [diff] [blame] | 229 | |
| 230 | if (show) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 231 | { |
Josh Lehan | 833661a | 2020-03-04 17:35:41 -0800 | [diff] [blame] | 232 | updateValue(nvalue); |
Jae Hyun Yoo | 73ca551 | 2019-02-28 21:20:17 -0800 | [diff] [blame] | 233 | } |
Josh Lehan | 833661a | 2020-03-04 17:35:41 -0800 | [diff] [blame] | 234 | else |
| 235 | { |
| 236 | value = nvalue; |
| 237 | } |
Zbigniew Kurzynski | 8d8d8d7 | 2020-05-29 19:21:24 +0200 | [diff] [blame] | 238 | updateMinMaxValues(); |
Josh Lehan | 833661a | 2020-03-04 17:35:41 -0800 | [diff] [blame] | 239 | |
Jae Hyun Yoo | 73ca551 | 2019-02-28 21:20:17 -0800 | [diff] [blame] | 240 | double gTcontrol = gCpuSensors[nameTcontrol] |
| 241 | ? gCpuSensors[nameTcontrol]->value |
| 242 | : std::numeric_limits<double>::quiet_NaN(); |
| 243 | if (gTcontrol != privTcontrol) |
| 244 | { |
| 245 | privTcontrol = gTcontrol; |
| 246 | |
| 247 | if (!thresholds.empty()) |
| 248 | { |
| 249 | std::vector<thresholds::Threshold> newThresholds; |
| 250 | if (parseThresholdsFromAttr(newThresholds, path, |
Vijay Khemka | 86dea2b | 2019-06-06 11:14:37 -0700 | [diff] [blame] | 251 | CPUSensor::sensorScaleFactor, |
| 252 | dtsOffset)) |
Jae Hyun Yoo | 73ca551 | 2019-02-28 21:20:17 -0800 | [diff] [blame] | 253 | { |
| 254 | if (!std::equal(thresholds.begin(), thresholds.end(), |
| 255 | newThresholds.begin(), |
| 256 | newThresholds.end())) |
| 257 | { |
| 258 | thresholds = newThresholds; |
| 259 | if (show) |
| 260 | { |
| 261 | thresholds::updateThresholds(this); |
| 262 | } |
| 263 | } |
| 264 | } |
| 265 | else |
| 266 | { |
| 267 | std::cerr << "Failure to update thresholds for " << name |
| 268 | << "\n"; |
| 269 | } |
Jae Hyun Yoo | 95b8a2d | 2019-02-25 20:15:09 -0800 | [diff] [blame] | 270 | } |
| 271 | } |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 272 | } |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 273 | catch (const std::invalid_argument&) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 274 | { |
James Feist | 961bf09 | 2020-07-01 16:38:12 -0700 | [diff] [blame] | 275 | incrementError(); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 276 | } |
| 277 | } |
| 278 | else |
| 279 | { |
James Feist | 1169eb4 | 2018-10-31 10:08:47 -0700 | [diff] [blame] | 280 | pollTime = sensorFailedPollTimeMs; |
James Feist | 961bf09 | 2020-07-01 16:38:12 -0700 | [diff] [blame] | 281 | incrementError(); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 282 | } |
| 283 | |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 284 | responseStream.clear(); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 285 | } |
| 286 | |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 287 | void CPUSensor::checkThresholds(void) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 288 | { |
Jae Hyun Yoo | 73ca551 | 2019-02-28 21:20:17 -0800 | [diff] [blame] | 289 | if (show) |
| 290 | { |
| 291 | thresholds::checkThresholds(this); |
| 292 | } |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 293 | } |