blob: 34105749168a16c18d93e37dba7bea661833dc25 [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
17#include <unistd.h>
18
Thu Nguyen255da6b2022-07-29 10:05:52 +070019#include <IntelCPUSensor.hpp>
Ed Tanous8a57ec02020-10-09 12:46:52 -070020#include <Utils.hpp>
James Feist6714a252018-09-10 15:26:18 -070021#include <boost/algorithm/string/replace.hpp>
James Feist8086aba2020-08-25 16:00:59 -070022#include <boost/asio/read_until.hpp>
James Feist38fb5982020-05-28 10:09:54 -070023#include <sdbusplus/asio/connection.hpp>
24#include <sdbusplus/asio/object_server.hpp>
25
Ed Tanous2049bd22022-07-09 07:20:26 -070026#include <cstddef>
James Feist6714a252018-09-10 15:26:18 -070027#include <iostream>
Patrick Venture96e97db2019-10-31 13:44:38 -070028#include <istream>
James Feist6714a252018-09-10 15:26:18 -070029#include <limits>
Patrick Venture96e97db2019-10-31 13:44:38 -070030#include <memory>
Patrick Venture96e97db2019-10-31 13:44:38 -070031#include <stdexcept>
James Feist6714a252018-09-10 15:26:18 -070032#include <string>
Patrick Venture96e97db2019-10-31 13:44:38 -070033#include <vector>
James Feist6714a252018-09-10 15:26:18 -070034
Thu Nguyen255da6b2022-07-29 10:05:52 +070035IntelCPUSensor::IntelCPUSensor(
36 const std::string& path, const std::string& objectType,
37 sdbusplus::asio::object_server& objectServer,
38 std::shared_ptr<sdbusplus::asio::connection>& conn,
39 boost::asio::io_service& io, const std::string& sensorName,
40 std::vector<thresholds::Threshold>&& thresholdsIn,
41 const std::string& sensorConfiguration, int cpuId, bool show,
42 double dtsOffset) :
Zhikui Renda98f092021-11-01 09:41:08 -070043 Sensor(escapeName(sensorName), std::move(thresholdsIn), sensorConfiguration,
44 objectType, false, false, 0, 0, conn, PowerState::on),
Ed Tanous2049bd22022-07-09 07:20:26 -070045 objServer(objectServer), inputDev(io), waitTimer(io),
Ed Tanousa771f6a2022-01-14 09:36:51 -080046 nameTcontrol("Tcontrol CPU" + std::to_string(cpuId)), path(path),
Brad Bishopfbb44ad2019-11-08 09:42:37 -050047 privTcontrol(std::numeric_limits<double>::quiet_NaN()),
Thu Nguyen255da6b2022-07-29 10:05:52 +070048 dtsOffset(dtsOffset), show(show), pollTime(IntelCPUSensor::sensorPollMs)
Ed Tanousb429f312022-06-27 16:09:53 -070049
James Feist6714a252018-09-10 15:26:18 -070050{
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -080051 if (show)
James Feist6714a252018-09-10 15:26:18 -070052 {
Zbigniew Kurzynski63f38662020-06-09 13:02:11 +020053 if (auto fileParts = splitFileName(path))
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -080054 {
Zbigniew Kurzynskidbfd4662020-09-28 18:06:00 +020055 auto& [type, nr, item] = *fileParts;
Zbigniew Kurzynski0a4c4802020-04-01 11:22:27 +020056 std::string interfacePath;
Ed Tanousa771f6a2022-01-14 09:36:51 -080057 const char* units = nullptr;
Ed Tanous2049bd22022-07-09 07:20:26 -070058 if (type == "power")
Zbigniew Kurzynski0a4c4802020-04-01 11:22:27 +020059 {
60 interfacePath = "/xyz/openbmc_project/sensors/power/" + name;
Zev Weiss6b6891c2021-04-22 02:46:21 -050061 units = sensor_paths::unitWatts;
Andrei Kartashev6736d4b2020-12-18 18:59:21 +030062 minValue = 0;
63 maxValue = 511;
Zbigniew Kurzynski0a4c4802020-04-01 11:22:27 +020064 }
65 else
66 {
67 interfacePath =
68 "/xyz/openbmc_project/sensors/temperature/" + name;
Zev Weiss6b6891c2021-04-22 02:46:21 -050069 units = sensor_paths::unitDegreesC;
Andrei Kartashev6736d4b2020-12-18 18:59:21 +030070 minValue = -128;
71 maxValue = 127;
Zbigniew Kurzynski0a4c4802020-04-01 11:22:27 +020072 }
James Feist078f2322019-03-08 11:09:05 -080073
Zbigniew Kurzynski0a4c4802020-04-01 11:22:27 +020074 sensorInterface = objectServer.add_interface(
75 interfacePath, "xyz.openbmc_project.Sensor.Value");
Jayashree Dhanapal56678082022-01-04 17:27:20 +053076 for (const auto& threshold : thresholds)
Zbigniew Kurzynski0a4c4802020-04-01 11:22:27 +020077 {
Jayashree Dhanapal56678082022-01-04 17:27:20 +053078 std::string interface =
79 thresholds::getInterface(threshold.level);
80 thresholdInterfaces[static_cast<size_t>(threshold.level)] =
81 objectServer.add_interface(interfacePath, interface);
Zbigniew Kurzynski0a4c4802020-04-01 11:22:27 +020082 }
83 association = objectServer.add_interface(interfacePath,
84 association::interface);
85
Andrei Kartashev39287412022-02-04 16:04:47 +030086 setInitialProperties(units);
Zbigniew Kurzynski0a4c4802020-04-01 11:22:27 +020087 }
James Feist6714a252018-09-10 15:26:18 -070088 }
James Feist961bf092020-07-01 16:38:12 -070089
90 // call setup always as not all sensors call setInitialProperties
James Feist71d31b22019-01-02 16:57:54 -080091 setupPowerMatch(conn);
James Feist6714a252018-09-10 15:26:18 -070092}
93
Thu Nguyen255da6b2022-07-29 10:05:52 +070094IntelCPUSensor::~IntelCPUSensor()
James Feist6714a252018-09-10 15:26:18 -070095{
96 // close the input dev to cancel async operations
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070097 inputDev.close();
98 waitTimer.cancel();
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -080099 if (show)
100 {
Jayashree Dhanapal56678082022-01-04 17:27:20 +0530101 for (const auto& iface : thresholdInterfaces)
102 {
103 objServer.remove_interface(iface);
104 }
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800105 objServer.remove_interface(sensorInterface);
James Feist078f2322019-03-08 11:09:05 -0800106 objServer.remove_interface(association);
Arun P. Mohananb782eec2021-10-18 16:22:09 +0530107 objServer.remove_interface(availableInterface);
108 objServer.remove_interface(operationalInterface);
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800109 }
James Feist6714a252018-09-10 15:26:18 -0700110}
111
Thu Nguyen255da6b2022-07-29 10:05:52 +0700112void IntelCPUSensor::restartRead(void)
James Feist6714a252018-09-10 15:26:18 -0700113{
Thu Nguyen255da6b2022-07-29 10:05:52 +0700114 std::weak_ptr<IntelCPUSensor> weakRef = weak_from_this();
Ed Tanous9b4a20e2022-09-06 08:47:11 -0700115 waitTimer.expires_from_now(std::chrono::milliseconds(pollTime));
Arun Lal K M84545712021-12-31 13:29:56 +0000116 waitTimer.async_wait([weakRef](const boost::system::error_code& ec) {
117 if (ec == boost::asio::error::operation_aborted)
118 {
119 std::cerr << "Failed to reschedule\n";
120 return;
121 }
Thu Nguyen255da6b2022-07-29 10:05:52 +0700122 std::shared_ptr<IntelCPUSensor> self = weakRef.lock();
Arun P. Mohanan04d05062021-10-29 20:30:26 +0530123
Arun Lal K M84545712021-12-31 13:29:56 +0000124 if (self)
125 {
126 self->setupRead();
127 }
128 });
129}
130
Thu Nguyen255da6b2022-07-29 10:05:52 +0700131void IntelCPUSensor::setupRead(void)
Arun Lal K M84545712021-12-31 13:29:56 +0000132{
James Feistc22b8422020-07-07 14:40:39 -0700133 if (readingStateGood())
134 {
135 inputDev.close();
Ed Tanous99c44092022-01-14 09:59:09 -0800136
137 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
Arun Lal K M84545712021-12-31 13:29:56 +0000138 fd = open(path.c_str(), O_RDONLY | O_NONBLOCK);
139 if (fd < 0)
James Feistc22b8422020-07-07 14:40:39 -0700140 {
141 std::cerr << name << " unable to open fd!\n";
Arun Lal K M84545712021-12-31 13:29:56 +0000142 return;
James Feistc22b8422020-07-07 14:40:39 -0700143 }
Arun Lal K M84545712021-12-31 13:29:56 +0000144
145 inputDev.assign(fd);
James Feistc22b8422020-07-07 14:40:39 -0700146 }
147 else
148 {
James Feistc22b8422020-07-07 14:40:39 -0700149 markAvailable(false);
Arun Lal K M84545712021-12-31 13:29:56 +0000150 updateValue(std::numeric_limits<double>::quiet_NaN());
151 restartRead();
152 return;
James Feistc22b8422020-07-07 14:40:39 -0700153 }
Arun Lal K M84545712021-12-31 13:29:56 +0000154
Thu Nguyen255da6b2022-07-29 10:05:52 +0700155 std::weak_ptr<IntelCPUSensor> weakRef = weak_from_this();
Arun Lal K M84545712021-12-31 13:29:56 +0000156 inputDev.async_wait(boost::asio::posix::descriptor_base::wait_read,
157 [weakRef](const boost::system::error_code& ec) {
Thu Nguyen255da6b2022-07-29 10:05:52 +0700158 std::shared_ptr<IntelCPUSensor> self = weakRef.lock();
Arun Lal K M84545712021-12-31 13:29:56 +0000159
Ed Tanousbb679322022-05-16 16:10:00 -0700160 if (self)
161 {
162 self->handleResponse(ec);
163 }
164 });
James Feist6714a252018-09-10 15:26:18 -0700165}
166
Thu Nguyen255da6b2022-07-29 10:05:52 +0700167void IntelCPUSensor::updateMinMaxValues(void)
Zbigniew Kurzynski8d8d8d72020-05-29 19:21:24 +0200168{
169 const boost::container::flat_map<
170 std::string,
171 std::vector<std::tuple<const char*, std::reference_wrapper<double>,
172 const char*>>>
173 map = {
174 {
175 "cap",
176 {
177 std::make_tuple("cap_max", std::ref(maxValue), "MaxValue"),
178 std::make_tuple("cap_min", std::ref(minValue), "MinValue"),
179 },
180 },
181 };
182
Zbigniew Kurzynski63f38662020-06-09 13:02:11 +0200183 if (auto fileParts = splitFileName(path))
Zbigniew Kurzynski8d8d8d72020-05-29 19:21:24 +0200184 {
Zbigniew Kurzynskidbfd4662020-09-28 18:06:00 +0200185 auto& [fileType, fileNr, fileItem] = *fileParts;
Zbigniew Kurzynski8d8d8d72020-05-29 19:21:24 +0200186 const auto mapIt = map.find(fileItem);
187 if (mapIt != map.cend())
188 {
189 for (const auto& vectorItem : mapIt->second)
190 {
Ed Tanous2049bd22022-07-09 07:20:26 -0700191 const auto& [suffix, oldValue, dbusName] = vectorItem;
Zbigniew Kurzynski8d8d8d72020-05-29 19:21:24 +0200192 auto attrPath = boost::replace_all_copy(path, fileItem, suffix);
Zbigniew Kurzynski63f38662020-06-09 13:02:11 +0200193 if (auto newVal =
Thu Nguyen255da6b2022-07-29 10:05:52 +0700194 readFile(attrPath, IntelCPUSensor::sensorScaleFactor))
Zbigniew Kurzynski8d8d8d72020-05-29 19:21:24 +0200195 {
Zbigniew Kurzynski4f45e422020-06-09 12:42:15 +0200196 updateProperty(sensorInterface, oldValue, *newVal,
197 dbusName);
Zbigniew Kurzynski8d8d8d72020-05-29 19:21:24 +0200198 }
199 else
200 {
201 if (isPowerOn())
202 {
Zbigniew Kurzynski4f45e422020-06-09 12:42:15 +0200203 updateProperty(sensorInterface, oldValue, 0, dbusName);
Zbigniew Kurzynski8d8d8d72020-05-29 19:21:24 +0200204 }
205 else
206 {
Zbigniew Kurzynski4f45e422020-06-09 12:42:15 +0200207 updateProperty(sensorInterface, oldValue,
208 std::numeric_limits<double>::quiet_NaN(),
209 dbusName);
Zbigniew Kurzynski8d8d8d72020-05-29 19:21:24 +0200210 }
211 }
212 }
213 }
214 }
215}
216
Thu Nguyen255da6b2022-07-29 10:05:52 +0700217void IntelCPUSensor::handleResponse(const boost::system::error_code& err)
James Feist6714a252018-09-10 15:26:18 -0700218{
Arun Lal K M84545712021-12-31 13:29:56 +0000219 if ((err == boost::system::errc::bad_file_descriptor) ||
220 (err == boost::asio::error::misc_errors::not_found))
James Feist6714a252018-09-10 15:26:18 -0700221 {
222 return; // we're being destroyed
223 }
Ed Tanous8a57ec02020-10-09 12:46:52 -0700224 if (err == boost::system::errc::operation_canceled)
James Feist838529b2020-09-02 16:57:05 -0700225 {
226 if (readingStateGood())
227 {
228 if (!loggedInterfaceDown)
229 {
230 std::cerr << name << " interface down!\n";
231 loggedInterfaceDown = true;
232 }
Thu Nguyen255da6b2022-07-29 10:05:52 +0700233 pollTime = static_cast<size_t>(IntelCPUSensor::sensorPollMs) * 10U;
James Feist838529b2020-09-02 16:57:05 -0700234 markFunctional(false);
235 }
236 return;
237 }
238 loggedInterfaceDown = false;
Arun Lal K M84545712021-12-31 13:29:56 +0000239
240 if (err)
James Feist6714a252018-09-10 15:26:18 -0700241 {
Arun Lal K M84545712021-12-31 13:29:56 +0000242 pollTime = sensorFailedPollTimeMs;
243 incrementError();
244 return;
245 }
246
247 static constexpr uint32_t bufLen = 128;
248 std::string response;
249 response.resize(bufLen);
250 int rdLen = 0;
251
252 if (fd >= 0)
253 {
254 rdLen = pread(fd, response.data(), bufLen, 0);
255 }
256
257 if (rdLen > 0)
258 {
259
James Feist6714a252018-09-10 15:26:18 -0700260 try
261 {
Zhikui Rend3da1282020-09-11 17:02:01 -0700262 rawValue = std::stod(response);
Thu Nguyen255da6b2022-07-29 10:05:52 +0700263 double nvalue = rawValue / IntelCPUSensor::sensorScaleFactor;
Josh Lehan833661a2020-03-04 17:35:41 -0800264
265 if (show)
James Feist6714a252018-09-10 15:26:18 -0700266 {
Josh Lehan833661a2020-03-04 17:35:41 -0800267 updateValue(nvalue);
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800268 }
Josh Lehan833661a2020-03-04 17:35:41 -0800269 else
270 {
271 value = nvalue;
272 }
Zbigniew Kurzynski98be9842020-09-07 18:49:15 +0200273 if (minMaxReadCounter++ % 8 == 0)
274 {
275 updateMinMaxValues();
276 }
Josh Lehan833661a2020-03-04 17:35:41 -0800277
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800278 double gTcontrol = gCpuSensors[nameTcontrol]
279 ? gCpuSensors[nameTcontrol]->value
280 : std::numeric_limits<double>::quiet_NaN();
281 if (gTcontrol != privTcontrol)
282 {
283 privTcontrol = gTcontrol;
284
285 if (!thresholds.empty())
286 {
287 std::vector<thresholds::Threshold> newThresholds;
Thu Nguyen255da6b2022-07-29 10:05:52 +0700288 if (parseThresholdsFromAttr(
289 newThresholds, path,
290 IntelCPUSensor::sensorScaleFactor, dtsOffset))
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800291 {
292 if (!std::equal(thresholds.begin(), thresholds.end(),
293 newThresholds.begin(),
294 newThresholds.end()))
295 {
296 thresholds = newThresholds;
297 if (show)
298 {
299 thresholds::updateThresholds(this);
300 }
301 }
302 }
303 else
304 {
305 std::cerr << "Failure to update thresholds for " << name
306 << "\n";
307 }
Jae Hyun Yoo95b8a2d2019-02-25 20:15:09 -0800308 }
309 }
James Feist6714a252018-09-10 15:26:18 -0700310 }
James Feistd8705872019-02-08 13:26:09 -0800311 catch (const std::invalid_argument&)
James Feist6714a252018-09-10 15:26:18 -0700312 {
James Feist961bf092020-07-01 16:38:12 -0700313 incrementError();
James Feist6714a252018-09-10 15:26:18 -0700314 }
315 }
316 else
317 {
James Feist1169eb42018-10-31 10:08:47 -0700318 pollTime = sensorFailedPollTimeMs;
James Feist961bf092020-07-01 16:38:12 -0700319 incrementError();
James Feist6714a252018-09-10 15:26:18 -0700320 }
Arun Lal K M84545712021-12-31 13:29:56 +0000321 restartRead();
James Feist6714a252018-09-10 15:26:18 -0700322}
323
Thu Nguyen255da6b2022-07-29 10:05:52 +0700324void IntelCPUSensor::checkThresholds(void)
James Feist6714a252018-09-10 15:26:18 -0700325{
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800326 if (show)
327 {
328 thresholds::checkThresholds(this);
329 }
James Feist6714a252018-09-10 15:26:18 -0700330}