| 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 | |
| Andrew Jeffery | e73bd0a | 2023-01-25 10:39:57 +1030 | [diff] [blame] | 17 | #include "IntelCPUSensor.hpp" |
| 18 | |
| Ed Tanous | eacbfdd | 2024-04-04 12:00:24 -0700 | [diff] [blame] | 19 | #include "SensorPaths.hpp" |
| 20 | #include "Thresholds.hpp" |
| Andrew Jeffery | e73bd0a | 2023-01-25 10:39:57 +1030 | [diff] [blame] | 21 | #include "Utils.hpp" |
| Ed Tanous | eacbfdd | 2024-04-04 12:00:24 -0700 | [diff] [blame] | 22 | #include "sensor.hpp" |
| Andrew Jeffery | e73bd0a | 2023-01-25 10:39:57 +1030 | [diff] [blame] | 23 | |
| Ed Tanous | eacbfdd | 2024-04-04 12:00:24 -0700 | [diff] [blame] | 24 | #include <fcntl.h> |
| James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 25 | #include <unistd.h> |
| 26 | |
| James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 27 | #include <boost/algorithm/string/replace.hpp> |
| Ed Tanous | eacbfdd | 2024-04-04 12:00:24 -0700 | [diff] [blame] | 28 | #include <boost/asio/error.hpp> |
| 29 | #include <boost/asio/io_context.hpp> |
| 30 | #include <boost/asio/posix/descriptor_base.hpp> |
| 31 | #include <boost/container/flat_map.hpp> |
| George Liu | 4ab892a | 2025-02-20 14:35:49 +0800 | [diff] [blame] | 32 | #include <phosphor-logging/lg2.hpp> |
| James Feist | 38fb598 | 2020-05-28 10:09:54 -0700 | [diff] [blame] | 33 | #include <sdbusplus/asio/connection.hpp> |
| 34 | #include <sdbusplus/asio/object_server.hpp> |
| 35 | |
| Ed Tanous | eacbfdd | 2024-04-04 12:00:24 -0700 | [diff] [blame] | 36 | #include <algorithm> |
| 37 | #include <chrono> |
| Ed Tanous | 2049bd2 | 2022-07-09 07:20:26 -0700 | [diff] [blame] | 38 | #include <cstddef> |
| Ed Tanous | eacbfdd | 2024-04-04 12:00:24 -0700 | [diff] [blame] | 39 | #include <cstdint> |
| 40 | #include <functional> |
| James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 41 | #include <limits> |
| Patrick Venture | 96e97db | 2019-10-31 13:44:38 -0700 | [diff] [blame] | 42 | #include <memory> |
| Patrick Venture | 96e97db | 2019-10-31 13:44:38 -0700 | [diff] [blame] | 43 | #include <stdexcept> |
| James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 44 | #include <string> |
| Ed Tanous | eacbfdd | 2024-04-04 12:00:24 -0700 | [diff] [blame] | 45 | #include <tuple> |
| 46 | #include <utility> |
| Patrick Venture | 96e97db | 2019-10-31 13:44:38 -0700 | [diff] [blame] | 47 | #include <vector> |
| James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 48 | |
| Thu Nguyen | 255da6b | 2022-07-29 10:05:52 +0700 | [diff] [blame] | 49 | IntelCPUSensor::IntelCPUSensor( |
| 50 | const std::string& path, const std::string& objectType, |
| 51 | sdbusplus::asio::object_server& objectServer, |
| 52 | std::shared_ptr<sdbusplus::asio::connection>& conn, |
| Ed Tanous | 1f97863 | 2023-02-28 18:16:39 -0800 | [diff] [blame] | 53 | boost::asio::io_context& io, const std::string& sensorName, |
| Thu Nguyen | 255da6b | 2022-07-29 10:05:52 +0700 | [diff] [blame] | 54 | std::vector<thresholds::Threshold>&& thresholdsIn, |
| 55 | const std::string& sensorConfiguration, int cpuId, bool show, |
| 56 | double dtsOffset) : |
| Zhikui Ren | da98f09 | 2021-11-01 09:41:08 -0700 | [diff] [blame] | 57 | Sensor(escapeName(sensorName), std::move(thresholdsIn), sensorConfiguration, |
| 58 | objectType, false, false, 0, 0, conn, PowerState::on), |
| Ed Tanous | 2049bd2 | 2022-07-09 07:20:26 -0700 | [diff] [blame] | 59 | objServer(objectServer), inputDev(io), waitTimer(io), |
| Ed Tanous | a771f6a | 2022-01-14 09:36:51 -0800 | [diff] [blame] | 60 | nameTcontrol("Tcontrol CPU" + std::to_string(cpuId)), path(path), |
| Brad Bishop | fbb44ad | 2019-11-08 09:42:37 -0500 | [diff] [blame] | 61 | privTcontrol(std::numeric_limits<double>::quiet_NaN()), |
| Ed Tanous | aba6fca | 2025-09-29 13:53:20 -0700 | [diff] [blame] | 62 | dtsOffset(dtsOffset), show(show) |
| James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 63 | { |
| Jae Hyun Yoo | 73ca551 | 2019-02-28 21:20:17 -0800 | [diff] [blame] | 64 | if (show) |
| James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 65 | { |
| Zbigniew Kurzynski | 63f3866 | 2020-06-09 13:02:11 +0200 | [diff] [blame] | 66 | if (auto fileParts = splitFileName(path)) |
| Jae Hyun Yoo | 73ca551 | 2019-02-28 21:20:17 -0800 | [diff] [blame] | 67 | { |
| Zbigniew Kurzynski | dbfd466 | 2020-09-28 18:06:00 +0200 | [diff] [blame] | 68 | auto& [type, nr, item] = *fileParts; |
| Zbigniew Kurzynski | 0a4c480 | 2020-04-01 11:22:27 +0200 | [diff] [blame] | 69 | std::string interfacePath; |
| Ed Tanous | a771f6a | 2022-01-14 09:36:51 -0800 | [diff] [blame] | 70 | const char* units = nullptr; |
| Ed Tanous | 2049bd2 | 2022-07-09 07:20:26 -0700 | [diff] [blame] | 71 | if (type == "power") |
| Zbigniew Kurzynski | 0a4c480 | 2020-04-01 11:22:27 +0200 | [diff] [blame] | 72 | { |
| 73 | interfacePath = "/xyz/openbmc_project/sensors/power/" + name; |
| Zev Weiss | 6b6891c | 2021-04-22 02:46:21 -0500 | [diff] [blame] | 74 | units = sensor_paths::unitWatts; |
| Andrei Kartashev | 6736d4b | 2020-12-18 18:59:21 +0300 | [diff] [blame] | 75 | minValue = 0; |
| 76 | maxValue = 511; |
| Zbigniew Kurzynski | 0a4c480 | 2020-04-01 11:22:27 +0200 | [diff] [blame] | 77 | } |
| 78 | else |
| 79 | { |
| Patrick Williams | 779c96a | 2023-05-10 07:50:42 -0500 | [diff] [blame] | 80 | interfacePath = "/xyz/openbmc_project/sensors/temperature/" + |
| 81 | name; |
| Zev Weiss | 6b6891c | 2021-04-22 02:46:21 -0500 | [diff] [blame] | 82 | units = sensor_paths::unitDegreesC; |
| Andrei Kartashev | 6736d4b | 2020-12-18 18:59:21 +0300 | [diff] [blame] | 83 | minValue = -128; |
| 84 | maxValue = 127; |
| Zbigniew Kurzynski | 0a4c480 | 2020-04-01 11:22:27 +0200 | [diff] [blame] | 85 | } |
| James Feist | 078f232 | 2019-03-08 11:09:05 -0800 | [diff] [blame] | 86 | |
| Zbigniew Kurzynski | 0a4c480 | 2020-04-01 11:22:27 +0200 | [diff] [blame] | 87 | sensorInterface = objectServer.add_interface( |
| 88 | interfacePath, "xyz.openbmc_project.Sensor.Value"); |
| Jayashree Dhanapal | 5667808 | 2022-01-04 17:27:20 +0530 | [diff] [blame] | 89 | for (const auto& threshold : thresholds) |
| Zbigniew Kurzynski | 0a4c480 | 2020-04-01 11:22:27 +0200 | [diff] [blame] | 90 | { |
| Jayashree Dhanapal | 5667808 | 2022-01-04 17:27:20 +0530 | [diff] [blame] | 91 | std::string interface = |
| 92 | thresholds::getInterface(threshold.level); |
| 93 | thresholdInterfaces[static_cast<size_t>(threshold.level)] = |
| 94 | objectServer.add_interface(interfacePath, interface); |
| Zbigniew Kurzynski | 0a4c480 | 2020-04-01 11:22:27 +0200 | [diff] [blame] | 95 | } |
| 96 | association = objectServer.add_interface(interfacePath, |
| 97 | association::interface); |
| 98 | |
| Andrei Kartashev | 3928741 | 2022-02-04 16:04:47 +0300 | [diff] [blame] | 99 | setInitialProperties(units); |
| Zbigniew Kurzynski | 0a4c480 | 2020-04-01 11:22:27 +0200 | [diff] [blame] | 100 | } |
| James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 101 | } |
| James Feist | 961bf09 | 2020-07-01 16:38:12 -0700 | [diff] [blame] | 102 | |
| 103 | // call setup always as not all sensors call setInitialProperties |
| James Feist | 71d31b2 | 2019-01-02 16:57:54 -0800 | [diff] [blame] | 104 | setupPowerMatch(conn); |
| James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 105 | } |
| 106 | |
| Thu Nguyen | 255da6b | 2022-07-29 10:05:52 +0700 | [diff] [blame] | 107 | IntelCPUSensor::~IntelCPUSensor() |
| James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 108 | { |
| 109 | // close the input dev to cancel async operations |
| Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 110 | inputDev.close(); |
| 111 | waitTimer.cancel(); |
| Jae Hyun Yoo | 73ca551 | 2019-02-28 21:20:17 -0800 | [diff] [blame] | 112 | if (show) |
| 113 | { |
| Jayashree Dhanapal | 5667808 | 2022-01-04 17:27:20 +0530 | [diff] [blame] | 114 | for (const auto& iface : thresholdInterfaces) |
| 115 | { |
| 116 | objServer.remove_interface(iface); |
| 117 | } |
| Jae Hyun Yoo | 73ca551 | 2019-02-28 21:20:17 -0800 | [diff] [blame] | 118 | objServer.remove_interface(sensorInterface); |
| James Feist | 078f232 | 2019-03-08 11:09:05 -0800 | [diff] [blame] | 119 | objServer.remove_interface(association); |
| Arun P. Mohanan | b782eec | 2021-10-18 16:22:09 +0530 | [diff] [blame] | 120 | objServer.remove_interface(availableInterface); |
| 121 | objServer.remove_interface(operationalInterface); |
| Jae Hyun Yoo | 73ca551 | 2019-02-28 21:20:17 -0800 | [diff] [blame] | 122 | } |
| James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 123 | } |
| 124 | |
| Ed Tanous | 201a101 | 2024-04-03 18:07:28 -0700 | [diff] [blame] | 125 | void IntelCPUSensor::restartRead() |
| James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 126 | { |
| Thu Nguyen | 255da6b | 2022-07-29 10:05:52 +0700 | [diff] [blame] | 127 | std::weak_ptr<IntelCPUSensor> weakRef = weak_from_this(); |
| Ed Tanous | 83db50c | 2023-03-01 10:20:24 -0800 | [diff] [blame] | 128 | waitTimer.expires_after(std::chrono::milliseconds(pollTime)); |
| Arun Lal K M | 8454571 | 2021-12-31 13:29:56 +0000 | [diff] [blame] | 129 | waitTimer.async_wait([weakRef](const boost::system::error_code& ec) { |
| 130 | if (ec == boost::asio::error::operation_aborted) |
| 131 | { |
| George Liu | 4ab892a | 2025-02-20 14:35:49 +0800 | [diff] [blame] | 132 | lg2::error("Failed to reschedule"); |
| Arun Lal K M | 8454571 | 2021-12-31 13:29:56 +0000 | [diff] [blame] | 133 | return; |
| 134 | } |
| Thu Nguyen | 255da6b | 2022-07-29 10:05:52 +0700 | [diff] [blame] | 135 | std::shared_ptr<IntelCPUSensor> self = weakRef.lock(); |
| Arun P. Mohanan | 04d0506 | 2021-10-29 20:30:26 +0530 | [diff] [blame] | 136 | |
| Arun Lal K M | 8454571 | 2021-12-31 13:29:56 +0000 | [diff] [blame] | 137 | if (self) |
| 138 | { |
| 139 | self->setupRead(); |
| 140 | } |
| 141 | }); |
| 142 | } |
| 143 | |
| Ed Tanous | 201a101 | 2024-04-03 18:07:28 -0700 | [diff] [blame] | 144 | void IntelCPUSensor::setupRead() |
| Arun Lal K M | 8454571 | 2021-12-31 13:29:56 +0000 | [diff] [blame] | 145 | { |
| James Feist | c22b842 | 2020-07-07 14:40:39 -0700 | [diff] [blame] | 146 | if (readingStateGood()) |
| 147 | { |
| 148 | inputDev.close(); |
| Ed Tanous | 99c4409 | 2022-01-14 09:59:09 -0800 | [diff] [blame] | 149 | |
| 150 | // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg) |
| Arun Lal K M | 8454571 | 2021-12-31 13:29:56 +0000 | [diff] [blame] | 151 | fd = open(path.c_str(), O_RDONLY | O_NONBLOCK); |
| 152 | if (fd < 0) |
| James Feist | c22b842 | 2020-07-07 14:40:39 -0700 | [diff] [blame] | 153 | { |
| George Liu | 4ab892a | 2025-02-20 14:35:49 +0800 | [diff] [blame] | 154 | lg2::error("'{NAME}' unable to open fd!", "NAME", name); |
| Arun Lal K M | 8454571 | 2021-12-31 13:29:56 +0000 | [diff] [blame] | 155 | return; |
| James Feist | c22b842 | 2020-07-07 14:40:39 -0700 | [diff] [blame] | 156 | } |
| Arun Lal K M | 8454571 | 2021-12-31 13:29:56 +0000 | [diff] [blame] | 157 | |
| 158 | inputDev.assign(fd); |
| James Feist | c22b842 | 2020-07-07 14:40:39 -0700 | [diff] [blame] | 159 | } |
| 160 | else |
| 161 | { |
| James Feist | c22b842 | 2020-07-07 14:40:39 -0700 | [diff] [blame] | 162 | markAvailable(false); |
| Konstantin Aladyshev | 9b99293 | 2025-01-30 13:54:31 +0300 | [diff] [blame^] | 163 | if (show) |
| 164 | { |
| 165 | updateValue(std::numeric_limits<double>::quiet_NaN()); |
| 166 | } |
| 167 | else |
| 168 | { |
| 169 | value = std::numeric_limits<double>::quiet_NaN(); |
| 170 | } |
| Arun Lal K M | 8454571 | 2021-12-31 13:29:56 +0000 | [diff] [blame] | 171 | restartRead(); |
| 172 | return; |
| James Feist | c22b842 | 2020-07-07 14:40:39 -0700 | [diff] [blame] | 173 | } |
| Arun Lal K M | 8454571 | 2021-12-31 13:29:56 +0000 | [diff] [blame] | 174 | |
| Thu Nguyen | 255da6b | 2022-07-29 10:05:52 +0700 | [diff] [blame] | 175 | std::weak_ptr<IntelCPUSensor> weakRef = weak_from_this(); |
| Arun Lal K M | 8454571 | 2021-12-31 13:29:56 +0000 | [diff] [blame] | 176 | inputDev.async_wait(boost::asio::posix::descriptor_base::wait_read, |
| 177 | [weakRef](const boost::system::error_code& ec) { |
| Patrick Williams | 2aaf717 | 2024-08-16 15:20:40 -0400 | [diff] [blame] | 178 | std::shared_ptr<IntelCPUSensor> self = |
| 179 | weakRef.lock(); |
| Arun Lal K M | 8454571 | 2021-12-31 13:29:56 +0000 | [diff] [blame] | 180 | |
| Patrick Williams | 2aaf717 | 2024-08-16 15:20:40 -0400 | [diff] [blame] | 181 | if (self) |
| 182 | { |
| 183 | self->handleResponse(ec); |
| 184 | } |
| 185 | }); |
| James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 186 | } |
| 187 | |
| Ed Tanous | 201a101 | 2024-04-03 18:07:28 -0700 | [diff] [blame] | 188 | void IntelCPUSensor::updateMinMaxValues() |
| Zbigniew Kurzynski | 8d8d8d7 | 2020-05-29 19:21:24 +0200 | [diff] [blame] | 189 | { |
| 190 | const boost::container::flat_map< |
| 191 | std::string, |
| 192 | std::vector<std::tuple<const char*, std::reference_wrapper<double>, |
| 193 | const char*>>> |
| 194 | map = { |
| 195 | { |
| 196 | "cap", |
| 197 | { |
| 198 | std::make_tuple("cap_max", std::ref(maxValue), "MaxValue"), |
| 199 | std::make_tuple("cap_min", std::ref(minValue), "MinValue"), |
| 200 | }, |
| 201 | }, |
| 202 | }; |
| 203 | |
| Zbigniew Kurzynski | 63f3866 | 2020-06-09 13:02:11 +0200 | [diff] [blame] | 204 | if (auto fileParts = splitFileName(path)) |
| Zbigniew Kurzynski | 8d8d8d7 | 2020-05-29 19:21:24 +0200 | [diff] [blame] | 205 | { |
| Zbigniew Kurzynski | dbfd466 | 2020-09-28 18:06:00 +0200 | [diff] [blame] | 206 | auto& [fileType, fileNr, fileItem] = *fileParts; |
| Zbigniew Kurzynski | 8d8d8d7 | 2020-05-29 19:21:24 +0200 | [diff] [blame] | 207 | const auto mapIt = map.find(fileItem); |
| 208 | if (mapIt != map.cend()) |
| 209 | { |
| 210 | for (const auto& vectorItem : mapIt->second) |
| 211 | { |
| Ed Tanous | 2049bd2 | 2022-07-09 07:20:26 -0700 | [diff] [blame] | 212 | const auto& [suffix, oldValue, dbusName] = vectorItem; |
| Zbigniew Kurzynski | 8d8d8d7 | 2020-05-29 19:21:24 +0200 | [diff] [blame] | 213 | auto attrPath = boost::replace_all_copy(path, fileItem, suffix); |
| Patrick Williams | 2aaf717 | 2024-08-16 15:20:40 -0400 | [diff] [blame] | 214 | if (auto newVal = |
| 215 | readFile(attrPath, IntelCPUSensor::sensorScaleFactor)) |
| Zbigniew Kurzynski | 8d8d8d7 | 2020-05-29 19:21:24 +0200 | [diff] [blame] | 216 | { |
| Zbigniew Kurzynski | 4f45e42 | 2020-06-09 12:42:15 +0200 | [diff] [blame] | 217 | updateProperty(sensorInterface, oldValue, *newVal, |
| 218 | dbusName); |
| Zbigniew Kurzynski | 8d8d8d7 | 2020-05-29 19:21:24 +0200 | [diff] [blame] | 219 | } |
| 220 | else |
| 221 | { |
| 222 | if (isPowerOn()) |
| 223 | { |
| Zbigniew Kurzynski | 4f45e42 | 2020-06-09 12:42:15 +0200 | [diff] [blame] | 224 | updateProperty(sensorInterface, oldValue, 0, dbusName); |
| Zbigniew Kurzynski | 8d8d8d7 | 2020-05-29 19:21:24 +0200 | [diff] [blame] | 225 | } |
| 226 | else |
| 227 | { |
| Zbigniew Kurzynski | 4f45e42 | 2020-06-09 12:42:15 +0200 | [diff] [blame] | 228 | updateProperty(sensorInterface, oldValue, |
| 229 | std::numeric_limits<double>::quiet_NaN(), |
| 230 | dbusName); |
| Zbigniew Kurzynski | 8d8d8d7 | 2020-05-29 19:21:24 +0200 | [diff] [blame] | 231 | } |
| 232 | } |
| 233 | } |
| 234 | } |
| 235 | } |
| 236 | } |
| 237 | |
| Thu Nguyen | 255da6b | 2022-07-29 10:05:52 +0700 | [diff] [blame] | 238 | void IntelCPUSensor::handleResponse(const boost::system::error_code& err) |
| James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 239 | { |
| Arun Lal K M | 8454571 | 2021-12-31 13:29:56 +0000 | [diff] [blame] | 240 | if ((err == boost::system::errc::bad_file_descriptor) || |
| 241 | (err == boost::asio::error::misc_errors::not_found)) |
| James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 242 | { |
| 243 | return; // we're being destroyed |
| 244 | } |
| Ed Tanous | 8a57ec0 | 2020-10-09 12:46:52 -0700 | [diff] [blame] | 245 | if (err == boost::system::errc::operation_canceled) |
| James Feist | 838529b | 2020-09-02 16:57:05 -0700 | [diff] [blame] | 246 | { |
| 247 | if (readingStateGood()) |
| 248 | { |
| 249 | if (!loggedInterfaceDown) |
| 250 | { |
| George Liu | 4ab892a | 2025-02-20 14:35:49 +0800 | [diff] [blame] | 251 | lg2::error("'{NAME}' interface down!", "NAME", name); |
| James Feist | 838529b | 2020-09-02 16:57:05 -0700 | [diff] [blame] | 252 | loggedInterfaceDown = true; |
| 253 | } |
| Thu Nguyen | 255da6b | 2022-07-29 10:05:52 +0700 | [diff] [blame] | 254 | pollTime = static_cast<size_t>(IntelCPUSensor::sensorPollMs) * 10U; |
| James Feist | 838529b | 2020-09-02 16:57:05 -0700 | [diff] [blame] | 255 | markFunctional(false); |
| 256 | } |
| 257 | return; |
| 258 | } |
| 259 | loggedInterfaceDown = false; |
| Arun Lal K M | 8454571 | 2021-12-31 13:29:56 +0000 | [diff] [blame] | 260 | |
| 261 | if (err) |
| James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 262 | { |
| Arun Lal K M | 8454571 | 2021-12-31 13:29:56 +0000 | [diff] [blame] | 263 | pollTime = sensorFailedPollTimeMs; |
| 264 | incrementError(); |
| 265 | return; |
| 266 | } |
| 267 | |
| 268 | static constexpr uint32_t bufLen = 128; |
| 269 | std::string response; |
| 270 | response.resize(bufLen); |
| 271 | int rdLen = 0; |
| 272 | |
| 273 | if (fd >= 0) |
| 274 | { |
| 275 | rdLen = pread(fd, response.data(), bufLen, 0); |
| 276 | } |
| 277 | |
| 278 | if (rdLen > 0) |
| 279 | { |
| James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 280 | try |
| 281 | { |
| Zhikui Ren | d3da128 | 2020-09-11 17:02:01 -0700 | [diff] [blame] | 282 | rawValue = std::stod(response); |
| Thu Nguyen | 255da6b | 2022-07-29 10:05:52 +0700 | [diff] [blame] | 283 | double nvalue = rawValue / IntelCPUSensor::sensorScaleFactor; |
| Josh Lehan | 833661a | 2020-03-04 17:35:41 -0800 | [diff] [blame] | 284 | |
| 285 | if (show) |
| James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 286 | { |
| Josh Lehan | 833661a | 2020-03-04 17:35:41 -0800 | [diff] [blame] | 287 | updateValue(nvalue); |
| Jae Hyun Yoo | 73ca551 | 2019-02-28 21:20:17 -0800 | [diff] [blame] | 288 | } |
| Josh Lehan | 833661a | 2020-03-04 17:35:41 -0800 | [diff] [blame] | 289 | else |
| 290 | { |
| 291 | value = nvalue; |
| 292 | } |
| Zbigniew Kurzynski | 98be984 | 2020-09-07 18:49:15 +0200 | [diff] [blame] | 293 | if (minMaxReadCounter++ % 8 == 0) |
| 294 | { |
| 295 | updateMinMaxValues(); |
| 296 | } |
| Josh Lehan | 833661a | 2020-03-04 17:35:41 -0800 | [diff] [blame] | 297 | |
| Jae Hyun Yoo | 73ca551 | 2019-02-28 21:20:17 -0800 | [diff] [blame] | 298 | double gTcontrol = gCpuSensors[nameTcontrol] |
| 299 | ? gCpuSensors[nameTcontrol]->value |
| 300 | : std::numeric_limits<double>::quiet_NaN(); |
| 301 | if (gTcontrol != privTcontrol) |
| 302 | { |
| 303 | privTcontrol = gTcontrol; |
| 304 | |
| 305 | if (!thresholds.empty()) |
| 306 | { |
| 307 | std::vector<thresholds::Threshold> newThresholds; |
| Thu Nguyen | 255da6b | 2022-07-29 10:05:52 +0700 | [diff] [blame] | 308 | if (parseThresholdsFromAttr( |
| 309 | newThresholds, path, |
| Chris Sides | a327923 | 2023-04-24 16:08:13 -0500 | [diff] [blame] | 310 | IntelCPUSensor::sensorScaleFactor, dtsOffset, 0)) |
| Jae Hyun Yoo | 73ca551 | 2019-02-28 21:20:17 -0800 | [diff] [blame] | 311 | { |
| 312 | if (!std::equal(thresholds.begin(), thresholds.end(), |
| 313 | newThresholds.begin(), |
| 314 | newThresholds.end())) |
| 315 | { |
| 316 | thresholds = newThresholds; |
| 317 | if (show) |
| 318 | { |
| 319 | thresholds::updateThresholds(this); |
| 320 | } |
| 321 | } |
| 322 | } |
| 323 | else |
| 324 | { |
| George Liu | 4ab892a | 2025-02-20 14:35:49 +0800 | [diff] [blame] | 325 | lg2::error("Failure to update thresholds for '{NAME}'", |
| 326 | "NAME", name); |
| Jae Hyun Yoo | 73ca551 | 2019-02-28 21:20:17 -0800 | [diff] [blame] | 327 | } |
| Jae Hyun Yoo | 95b8a2d | 2019-02-25 20:15:09 -0800 | [diff] [blame] | 328 | } |
| 329 | } |
| James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 330 | } |
| James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 331 | catch (const std::invalid_argument&) |
| James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 332 | { |
| James Feist | 961bf09 | 2020-07-01 16:38:12 -0700 | [diff] [blame] | 333 | incrementError(); |
| James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 334 | } |
| 335 | } |
| 336 | else |
| 337 | { |
| James Feist | 1169eb4 | 2018-10-31 10:08:47 -0700 | [diff] [blame] | 338 | pollTime = sensorFailedPollTimeMs; |
| James Feist | 961bf09 | 2020-07-01 16:38:12 -0700 | [diff] [blame] | 339 | incrementError(); |
| James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 340 | } |
| Arun Lal K M | 8454571 | 2021-12-31 13:29:56 +0000 | [diff] [blame] | 341 | restartRead(); |
| James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 342 | } |
| 343 | |
| Ed Tanous | 201a101 | 2024-04-03 18:07:28 -0700 | [diff] [blame] | 344 | void IntelCPUSensor::checkThresholds() |
| James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 345 | { |
| Jae Hyun Yoo | 73ca551 | 2019-02-28 21:20:17 -0800 | [diff] [blame] | 346 | if (show) |
| 347 | { |
| 348 | thresholds::checkThresholds(this); |
| 349 | } |
| James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 350 | } |