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 | { |
| 198 | if (err == boost::system::errc::bad_file_descriptor) |
| 199 | { |
| 200 | return; // we're being destroyed |
| 201 | } |
James Feist | c22b842 | 2020-07-07 14:40:39 -0700 | [diff] [blame] | 202 | pollTime = CPUSensor::sensorPollMs; |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 203 | std::istream responseStream(&readBuf); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 204 | if (!err) |
| 205 | { |
| 206 | std::string response; |
| 207 | try |
| 208 | { |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 209 | std::getline(responseStream, response); |
Josh Lehan | 833661a | 2020-03-04 17:35:41 -0800 | [diff] [blame] | 210 | double nvalue = std::stod(response); |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 211 | responseStream.clear(); |
| 212 | nvalue /= CPUSensor::sensorScaleFactor; |
Josh Lehan | 833661a | 2020-03-04 17:35:41 -0800 | [diff] [blame] | 213 | |
| 214 | if (show) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 215 | { |
Josh Lehan | 833661a | 2020-03-04 17:35:41 -0800 | [diff] [blame] | 216 | updateValue(nvalue); |
Jae Hyun Yoo | 73ca551 | 2019-02-28 21:20:17 -0800 | [diff] [blame] | 217 | } |
Josh Lehan | 833661a | 2020-03-04 17:35:41 -0800 | [diff] [blame] | 218 | else |
| 219 | { |
| 220 | value = nvalue; |
| 221 | } |
Zbigniew Kurzynski | 8d8d8d7 | 2020-05-29 19:21:24 +0200 | [diff] [blame] | 222 | updateMinMaxValues(); |
Josh Lehan | 833661a | 2020-03-04 17:35:41 -0800 | [diff] [blame] | 223 | |
Jae Hyun Yoo | 73ca551 | 2019-02-28 21:20:17 -0800 | [diff] [blame] | 224 | double gTcontrol = gCpuSensors[nameTcontrol] |
| 225 | ? gCpuSensors[nameTcontrol]->value |
| 226 | : std::numeric_limits<double>::quiet_NaN(); |
| 227 | if (gTcontrol != privTcontrol) |
| 228 | { |
| 229 | privTcontrol = gTcontrol; |
| 230 | |
| 231 | if (!thresholds.empty()) |
| 232 | { |
| 233 | std::vector<thresholds::Threshold> newThresholds; |
| 234 | if (parseThresholdsFromAttr(newThresholds, path, |
Vijay Khemka | 86dea2b | 2019-06-06 11:14:37 -0700 | [diff] [blame] | 235 | CPUSensor::sensorScaleFactor, |
| 236 | dtsOffset)) |
Jae Hyun Yoo | 73ca551 | 2019-02-28 21:20:17 -0800 | [diff] [blame] | 237 | { |
| 238 | if (!std::equal(thresholds.begin(), thresholds.end(), |
| 239 | newThresholds.begin(), |
| 240 | newThresholds.end())) |
| 241 | { |
| 242 | thresholds = newThresholds; |
| 243 | if (show) |
| 244 | { |
| 245 | thresholds::updateThresholds(this); |
| 246 | } |
| 247 | } |
| 248 | } |
| 249 | else |
| 250 | { |
| 251 | std::cerr << "Failure to update thresholds for " << name |
| 252 | << "\n"; |
| 253 | } |
Jae Hyun Yoo | 95b8a2d | 2019-02-25 20:15:09 -0800 | [diff] [blame] | 254 | } |
| 255 | } |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 256 | } |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 257 | catch (const std::invalid_argument&) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 258 | { |
James Feist | 961bf09 | 2020-07-01 16:38:12 -0700 | [diff] [blame] | 259 | incrementError(); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 260 | } |
| 261 | } |
| 262 | else |
| 263 | { |
James Feist | 1169eb4 | 2018-10-31 10:08:47 -0700 | [diff] [blame] | 264 | pollTime = sensorFailedPollTimeMs; |
James Feist | 961bf09 | 2020-07-01 16:38:12 -0700 | [diff] [blame] | 265 | incrementError(); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 266 | } |
| 267 | |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 268 | responseStream.clear(); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 269 | } |
| 270 | |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 271 | void CPUSensor::checkThresholds(void) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 272 | { |
Jae Hyun Yoo | 73ca551 | 2019-02-28 21:20:17 -0800 | [diff] [blame] | 273 | if (show) |
| 274 | { |
| 275 | thresholds::checkThresholds(this); |
| 276 | } |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 277 | } |