blob: 2bebcd8f721ab7b701a43fe4fc1215ae38d4b686 [file] [log] [blame]
James Feist6714a252018-09-10 15:26:18 -07001/*
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 Jefferye73bd0a2023-01-25 10:39:57 +103017#include "IntelCPUSensor.hpp"
18
19#include "Utils.hpp"
20
James Feist6714a252018-09-10 15:26:18 -070021#include <unistd.h>
22
James Feist6714a252018-09-10 15:26:18 -070023#include <boost/algorithm/string/replace.hpp>
James Feist8086aba2020-08-25 16:00:59 -070024#include <boost/asio/read_until.hpp>
James Feist38fb5982020-05-28 10:09:54 -070025#include <sdbusplus/asio/connection.hpp>
26#include <sdbusplus/asio/object_server.hpp>
27
Ed Tanous2049bd22022-07-09 07:20:26 -070028#include <cstddef>
James Feist6714a252018-09-10 15:26:18 -070029#include <iostream>
Patrick Venture96e97db2019-10-31 13:44:38 -070030#include <istream>
James Feist6714a252018-09-10 15:26:18 -070031#include <limits>
Patrick Venture96e97db2019-10-31 13:44:38 -070032#include <memory>
Patrick Venture96e97db2019-10-31 13:44:38 -070033#include <stdexcept>
James Feist6714a252018-09-10 15:26:18 -070034#include <string>
Patrick Venture96e97db2019-10-31 13:44:38 -070035#include <vector>
James Feist6714a252018-09-10 15:26:18 -070036
Thu Nguyen255da6b2022-07-29 10:05:52 +070037IntelCPUSensor::IntelCPUSensor(
38 const std::string& path, const std::string& objectType,
39 sdbusplus::asio::object_server& objectServer,
40 std::shared_ptr<sdbusplus::asio::connection>& conn,
Ed Tanous1f978632023-02-28 18:16:39 -080041 boost::asio::io_context& io, const std::string& sensorName,
Thu Nguyen255da6b2022-07-29 10:05:52 +070042 std::vector<thresholds::Threshold>&& thresholdsIn,
43 const std::string& sensorConfiguration, int cpuId, bool show,
44 double dtsOffset) :
Zhikui Renda98f092021-11-01 09:41:08 -070045 Sensor(escapeName(sensorName), std::move(thresholdsIn), sensorConfiguration,
46 objectType, false, false, 0, 0, conn, PowerState::on),
Ed Tanous2049bd22022-07-09 07:20:26 -070047 objServer(objectServer), inputDev(io), waitTimer(io),
Ed Tanousa771f6a2022-01-14 09:36:51 -080048 nameTcontrol("Tcontrol CPU" + std::to_string(cpuId)), path(path),
Brad Bishopfbb44ad2019-11-08 09:42:37 -050049 privTcontrol(std::numeric_limits<double>::quiet_NaN()),
Thu Nguyen255da6b2022-07-29 10:05:52 +070050 dtsOffset(dtsOffset), show(show), pollTime(IntelCPUSensor::sensorPollMs)
Ed Tanousb429f312022-06-27 16:09:53 -070051
James Feist6714a252018-09-10 15:26:18 -070052{
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -080053 if (show)
James Feist6714a252018-09-10 15:26:18 -070054 {
Zbigniew Kurzynski63f38662020-06-09 13:02:11 +020055 if (auto fileParts = splitFileName(path))
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -080056 {
Zbigniew Kurzynskidbfd4662020-09-28 18:06:00 +020057 auto& [type, nr, item] = *fileParts;
Zbigniew Kurzynski0a4c4802020-04-01 11:22:27 +020058 std::string interfacePath;
Ed Tanousa771f6a2022-01-14 09:36:51 -080059 const char* units = nullptr;
Ed Tanous2049bd22022-07-09 07:20:26 -070060 if (type == "power")
Zbigniew Kurzynski0a4c4802020-04-01 11:22:27 +020061 {
62 interfacePath = "/xyz/openbmc_project/sensors/power/" + name;
Zev Weiss6b6891c2021-04-22 02:46:21 -050063 units = sensor_paths::unitWatts;
Andrei Kartashev6736d4b2020-12-18 18:59:21 +030064 minValue = 0;
65 maxValue = 511;
Zbigniew Kurzynski0a4c4802020-04-01 11:22:27 +020066 }
67 else
68 {
Patrick Williams779c96a2023-05-10 07:50:42 -050069 interfacePath = "/xyz/openbmc_project/sensors/temperature/" +
70 name;
Zev Weiss6b6891c2021-04-22 02:46:21 -050071 units = sensor_paths::unitDegreesC;
Andrei Kartashev6736d4b2020-12-18 18:59:21 +030072 minValue = -128;
73 maxValue = 127;
Zbigniew Kurzynski0a4c4802020-04-01 11:22:27 +020074 }
James Feist078f2322019-03-08 11:09:05 -080075
Zbigniew Kurzynski0a4c4802020-04-01 11:22:27 +020076 sensorInterface = objectServer.add_interface(
77 interfacePath, "xyz.openbmc_project.Sensor.Value");
Jayashree Dhanapal56678082022-01-04 17:27:20 +053078 for (const auto& threshold : thresholds)
Zbigniew Kurzynski0a4c4802020-04-01 11:22:27 +020079 {
Jayashree Dhanapal56678082022-01-04 17:27:20 +053080 std::string interface =
81 thresholds::getInterface(threshold.level);
82 thresholdInterfaces[static_cast<size_t>(threshold.level)] =
83 objectServer.add_interface(interfacePath, interface);
Zbigniew Kurzynski0a4c4802020-04-01 11:22:27 +020084 }
85 association = objectServer.add_interface(interfacePath,
86 association::interface);
87
Andrei Kartashev39287412022-02-04 16:04:47 +030088 setInitialProperties(units);
Zbigniew Kurzynski0a4c4802020-04-01 11:22:27 +020089 }
James Feist6714a252018-09-10 15:26:18 -070090 }
James Feist961bf092020-07-01 16:38:12 -070091
92 // call setup always as not all sensors call setInitialProperties
James Feist71d31b22019-01-02 16:57:54 -080093 setupPowerMatch(conn);
James Feist6714a252018-09-10 15:26:18 -070094}
95
Thu Nguyen255da6b2022-07-29 10:05:52 +070096IntelCPUSensor::~IntelCPUSensor()
James Feist6714a252018-09-10 15:26:18 -070097{
98 // close the input dev to cancel async operations
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070099 inputDev.close();
100 waitTimer.cancel();
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800101 if (show)
102 {
Jayashree Dhanapal56678082022-01-04 17:27:20 +0530103 for (const auto& iface : thresholdInterfaces)
104 {
105 objServer.remove_interface(iface);
106 }
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800107 objServer.remove_interface(sensorInterface);
James Feist078f2322019-03-08 11:09:05 -0800108 objServer.remove_interface(association);
Arun P. Mohananb782eec2021-10-18 16:22:09 +0530109 objServer.remove_interface(availableInterface);
110 objServer.remove_interface(operationalInterface);
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800111 }
James Feist6714a252018-09-10 15:26:18 -0700112}
113
Thu Nguyen255da6b2022-07-29 10:05:52 +0700114void IntelCPUSensor::restartRead(void)
James Feist6714a252018-09-10 15:26:18 -0700115{
Thu Nguyen255da6b2022-07-29 10:05:52 +0700116 std::weak_ptr<IntelCPUSensor> weakRef = weak_from_this();
Ed Tanous83db50c2023-03-01 10:20:24 -0800117 waitTimer.expires_after(std::chrono::milliseconds(pollTime));
Arun Lal K M84545712021-12-31 13:29:56 +0000118 waitTimer.async_wait([weakRef](const boost::system::error_code& ec) {
119 if (ec == boost::asio::error::operation_aborted)
120 {
121 std::cerr << "Failed to reschedule\n";
122 return;
123 }
Thu Nguyen255da6b2022-07-29 10:05:52 +0700124 std::shared_ptr<IntelCPUSensor> self = weakRef.lock();
Arun P. Mohanan04d05062021-10-29 20:30:26 +0530125
Arun Lal K M84545712021-12-31 13:29:56 +0000126 if (self)
127 {
128 self->setupRead();
129 }
130 });
131}
132
Thu Nguyen255da6b2022-07-29 10:05:52 +0700133void IntelCPUSensor::setupRead(void)
Arun Lal K M84545712021-12-31 13:29:56 +0000134{
James Feistc22b8422020-07-07 14:40:39 -0700135 if (readingStateGood())
136 {
137 inputDev.close();
Ed Tanous99c44092022-01-14 09:59:09 -0800138
139 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
Arun Lal K M84545712021-12-31 13:29:56 +0000140 fd = open(path.c_str(), O_RDONLY | O_NONBLOCK);
141 if (fd < 0)
James Feistc22b8422020-07-07 14:40:39 -0700142 {
143 std::cerr << name << " unable to open fd!\n";
Arun Lal K M84545712021-12-31 13:29:56 +0000144 return;
James Feistc22b8422020-07-07 14:40:39 -0700145 }
Arun Lal K M84545712021-12-31 13:29:56 +0000146
147 inputDev.assign(fd);
James Feistc22b8422020-07-07 14:40:39 -0700148 }
149 else
150 {
James Feistc22b8422020-07-07 14:40:39 -0700151 markAvailable(false);
Arun Lal K M84545712021-12-31 13:29:56 +0000152 updateValue(std::numeric_limits<double>::quiet_NaN());
153 restartRead();
154 return;
James Feistc22b8422020-07-07 14:40:39 -0700155 }
Arun Lal K M84545712021-12-31 13:29:56 +0000156
Thu Nguyen255da6b2022-07-29 10:05:52 +0700157 std::weak_ptr<IntelCPUSensor> weakRef = weak_from_this();
Arun Lal K M84545712021-12-31 13:29:56 +0000158 inputDev.async_wait(boost::asio::posix::descriptor_base::wait_read,
159 [weakRef](const boost::system::error_code& ec) {
Thu Nguyen255da6b2022-07-29 10:05:52 +0700160 std::shared_ptr<IntelCPUSensor> self = weakRef.lock();
Arun Lal K M84545712021-12-31 13:29:56 +0000161
Ed Tanousbb679322022-05-16 16:10:00 -0700162 if (self)
163 {
164 self->handleResponse(ec);
165 }
166 });
James Feist6714a252018-09-10 15:26:18 -0700167}
168
Thu Nguyen255da6b2022-07-29 10:05:52 +0700169void IntelCPUSensor::updateMinMaxValues(void)
Zbigniew Kurzynski8d8d8d72020-05-29 19:21:24 +0200170{
171 const boost::container::flat_map<
172 std::string,
173 std::vector<std::tuple<const char*, std::reference_wrapper<double>,
174 const char*>>>
175 map = {
176 {
177 "cap",
178 {
179 std::make_tuple("cap_max", std::ref(maxValue), "MaxValue"),
180 std::make_tuple("cap_min", std::ref(minValue), "MinValue"),
181 },
182 },
183 };
184
Zbigniew Kurzynski63f38662020-06-09 13:02:11 +0200185 if (auto fileParts = splitFileName(path))
Zbigniew Kurzynski8d8d8d72020-05-29 19:21:24 +0200186 {
Zbigniew Kurzynskidbfd4662020-09-28 18:06:00 +0200187 auto& [fileType, fileNr, fileItem] = *fileParts;
Zbigniew Kurzynski8d8d8d72020-05-29 19:21:24 +0200188 const auto mapIt = map.find(fileItem);
189 if (mapIt != map.cend())
190 {
191 for (const auto& vectorItem : mapIt->second)
192 {
Ed Tanous2049bd22022-07-09 07:20:26 -0700193 const auto& [suffix, oldValue, dbusName] = vectorItem;
Zbigniew Kurzynski8d8d8d72020-05-29 19:21:24 +0200194 auto attrPath = boost::replace_all_copy(path, fileItem, suffix);
Patrick Williams779c96a2023-05-10 07:50:42 -0500195 if (auto newVal = readFile(attrPath,
196 IntelCPUSensor::sensorScaleFactor))
Zbigniew Kurzynski8d8d8d72020-05-29 19:21:24 +0200197 {
Zbigniew Kurzynski4f45e422020-06-09 12:42:15 +0200198 updateProperty(sensorInterface, oldValue, *newVal,
199 dbusName);
Zbigniew Kurzynski8d8d8d72020-05-29 19:21:24 +0200200 }
201 else
202 {
203 if (isPowerOn())
204 {
Zbigniew Kurzynski4f45e422020-06-09 12:42:15 +0200205 updateProperty(sensorInterface, oldValue, 0, dbusName);
Zbigniew Kurzynski8d8d8d72020-05-29 19:21:24 +0200206 }
207 else
208 {
Zbigniew Kurzynski4f45e422020-06-09 12:42:15 +0200209 updateProperty(sensorInterface, oldValue,
210 std::numeric_limits<double>::quiet_NaN(),
211 dbusName);
Zbigniew Kurzynski8d8d8d72020-05-29 19:21:24 +0200212 }
213 }
214 }
215 }
216 }
217}
218
Thu Nguyen255da6b2022-07-29 10:05:52 +0700219void IntelCPUSensor::handleResponse(const boost::system::error_code& err)
James Feist6714a252018-09-10 15:26:18 -0700220{
Arun Lal K M84545712021-12-31 13:29:56 +0000221 if ((err == boost::system::errc::bad_file_descriptor) ||
222 (err == boost::asio::error::misc_errors::not_found))
James Feist6714a252018-09-10 15:26:18 -0700223 {
224 return; // we're being destroyed
225 }
Ed Tanous8a57ec02020-10-09 12:46:52 -0700226 if (err == boost::system::errc::operation_canceled)
James Feist838529b2020-09-02 16:57:05 -0700227 {
228 if (readingStateGood())
229 {
230 if (!loggedInterfaceDown)
231 {
232 std::cerr << name << " interface down!\n";
233 loggedInterfaceDown = true;
234 }
Thu Nguyen255da6b2022-07-29 10:05:52 +0700235 pollTime = static_cast<size_t>(IntelCPUSensor::sensorPollMs) * 10U;
James Feist838529b2020-09-02 16:57:05 -0700236 markFunctional(false);
237 }
238 return;
239 }
240 loggedInterfaceDown = false;
Arun Lal K M84545712021-12-31 13:29:56 +0000241
242 if (err)
James Feist6714a252018-09-10 15:26:18 -0700243 {
Arun Lal K M84545712021-12-31 13:29:56 +0000244 pollTime = sensorFailedPollTimeMs;
245 incrementError();
246 return;
247 }
248
249 static constexpr uint32_t bufLen = 128;
250 std::string response;
251 response.resize(bufLen);
252 int rdLen = 0;
253
254 if (fd >= 0)
255 {
256 rdLen = pread(fd, response.data(), bufLen, 0);
257 }
258
259 if (rdLen > 0)
260 {
James Feist6714a252018-09-10 15:26:18 -0700261 try
262 {
Zhikui Rend3da1282020-09-11 17:02:01 -0700263 rawValue = std::stod(response);
Thu Nguyen255da6b2022-07-29 10:05:52 +0700264 double nvalue = rawValue / IntelCPUSensor::sensorScaleFactor;
Josh Lehan833661a2020-03-04 17:35:41 -0800265
266 if (show)
James Feist6714a252018-09-10 15:26:18 -0700267 {
Josh Lehan833661a2020-03-04 17:35:41 -0800268 updateValue(nvalue);
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800269 }
Josh Lehan833661a2020-03-04 17:35:41 -0800270 else
271 {
272 value = nvalue;
273 }
Zbigniew Kurzynski98be9842020-09-07 18:49:15 +0200274 if (minMaxReadCounter++ % 8 == 0)
275 {
276 updateMinMaxValues();
277 }
Josh Lehan833661a2020-03-04 17:35:41 -0800278
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800279 double gTcontrol = gCpuSensors[nameTcontrol]
280 ? gCpuSensors[nameTcontrol]->value
281 : std::numeric_limits<double>::quiet_NaN();
282 if (gTcontrol != privTcontrol)
283 {
284 privTcontrol = gTcontrol;
285
286 if (!thresholds.empty())
287 {
288 std::vector<thresholds::Threshold> newThresholds;
Thu Nguyen255da6b2022-07-29 10:05:52 +0700289 if (parseThresholdsFromAttr(
290 newThresholds, path,
291 IntelCPUSensor::sensorScaleFactor, dtsOffset))
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800292 {
293 if (!std::equal(thresholds.begin(), thresholds.end(),
294 newThresholds.begin(),
295 newThresholds.end()))
296 {
297 thresholds = newThresholds;
298 if (show)
299 {
300 thresholds::updateThresholds(this);
301 }
302 }
303 }
304 else
305 {
306 std::cerr << "Failure to update thresholds for " << name
307 << "\n";
308 }
Jae Hyun Yoo95b8a2d2019-02-25 20:15:09 -0800309 }
310 }
James Feist6714a252018-09-10 15:26:18 -0700311 }
James Feistd8705872019-02-08 13:26:09 -0800312 catch (const std::invalid_argument&)
James Feist6714a252018-09-10 15:26:18 -0700313 {
James Feist961bf092020-07-01 16:38:12 -0700314 incrementError();
James Feist6714a252018-09-10 15:26:18 -0700315 }
316 }
317 else
318 {
James Feist1169eb42018-10-31 10:08:47 -0700319 pollTime = sensorFailedPollTimeMs;
James Feist961bf092020-07-01 16:38:12 -0700320 incrementError();
James Feist6714a252018-09-10 15:26:18 -0700321 }
Arun Lal K M84545712021-12-31 13:29:56 +0000322 restartRead();
James Feist6714a252018-09-10 15:26:18 -0700323}
324
Thu Nguyen255da6b2022-07-29 10:05:52 +0700325void IntelCPUSensor::checkThresholds(void)
James Feist6714a252018-09-10 15:26:18 -0700326{
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800327 if (show)
328 {
329 thresholds::checkThresholds(this);
330 }
James Feist6714a252018-09-10 15:26:18 -0700331}