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