blob: 8f04e99ea08f6a26df98ac60a82887f544daf88f [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 Feist6714a252018-09-10 15:26:18 -070023#include <boost/date_time/posix_time/posix_time.hpp>
James Feist38fb5982020-05-28 10:09:54 -070024#include <sdbusplus/asio/connection.hpp>
25#include <sdbusplus/asio/object_server.hpp>
26
Ed Tanous2049bd22022-07-09 07:20:26 -070027#include <cstddef>
James Feist6714a252018-09-10 15:26:18 -070028#include <iostream>
Patrick Venture96e97db2019-10-31 13:44:38 -070029#include <istream>
James Feist6714a252018-09-10 15:26:18 -070030#include <limits>
Patrick Venture96e97db2019-10-31 13:44:38 -070031#include <memory>
Patrick Venture96e97db2019-10-31 13:44:38 -070032#include <stdexcept>
James Feist6714a252018-09-10 15:26:18 -070033#include <string>
Patrick Venture96e97db2019-10-31 13:44:38 -070034#include <vector>
James Feist6714a252018-09-10 15:26:18 -070035
Thu Nguyen255da6b2022-07-29 10:05:52 +070036IntelCPUSensor::IntelCPUSensor(
37 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>&& thresholdsIn,
42 const std::string& sensorConfiguration, int cpuId, bool show,
43 double dtsOffset) :
Zhikui Renda98f092021-11-01 09:41:08 -070044 Sensor(escapeName(sensorName), std::move(thresholdsIn), sensorConfiguration,
45 objectType, false, false, 0, 0, conn, PowerState::on),
Ed Tanous2049bd22022-07-09 07:20:26 -070046 objServer(objectServer), inputDev(io), waitTimer(io),
Ed Tanousa771f6a2022-01-14 09:36:51 -080047 nameTcontrol("Tcontrol CPU" + std::to_string(cpuId)), path(path),
Brad Bishopfbb44ad2019-11-08 09:42:37 -050048 privTcontrol(std::numeric_limits<double>::quiet_NaN()),
Thu Nguyen255da6b2022-07-29 10:05:52 +070049 dtsOffset(dtsOffset), show(show), pollTime(IntelCPUSensor::sensorPollMs)
Ed Tanousb429f312022-06-27 16:09:53 -070050
James Feist6714a252018-09-10 15:26:18 -070051{
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -080052 if (show)
James Feist6714a252018-09-10 15:26:18 -070053 {
Zbigniew Kurzynski63f38662020-06-09 13:02:11 +020054 if (auto fileParts = splitFileName(path))
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -080055 {
Zbigniew Kurzynskidbfd4662020-09-28 18:06:00 +020056 auto& [type, nr, item] = *fileParts;
Zbigniew Kurzynski0a4c4802020-04-01 11:22:27 +020057 std::string interfacePath;
Ed Tanousa771f6a2022-01-14 09:36:51 -080058 const char* units = nullptr;
Ed Tanous2049bd22022-07-09 07:20:26 -070059 if (type == "power")
Zbigniew Kurzynski0a4c4802020-04-01 11:22:27 +020060 {
61 interfacePath = "/xyz/openbmc_project/sensors/power/" + name;
Zev Weiss6b6891c2021-04-22 02:46:21 -050062 units = sensor_paths::unitWatts;
Andrei Kartashev6736d4b2020-12-18 18:59:21 +030063 minValue = 0;
64 maxValue = 511;
Zbigniew Kurzynski0a4c4802020-04-01 11:22:27 +020065 }
66 else
67 {
68 interfacePath =
69 "/xyz/openbmc_project/sensors/temperature/" + name;
Zev Weiss6b6891c2021-04-22 02:46:21 -050070 units = sensor_paths::unitDegreesC;
Andrei Kartashev6736d4b2020-12-18 18:59:21 +030071 minValue = -128;
72 maxValue = 127;
Zbigniew Kurzynski0a4c4802020-04-01 11:22:27 +020073 }
James Feist078f2322019-03-08 11:09:05 -080074
Zbigniew Kurzynski0a4c4802020-04-01 11:22:27 +020075 sensorInterface = objectServer.add_interface(
76 interfacePath, "xyz.openbmc_project.Sensor.Value");
Jayashree Dhanapal56678082022-01-04 17:27:20 +053077 for (const auto& threshold : thresholds)
Zbigniew Kurzynski0a4c4802020-04-01 11:22:27 +020078 {
Jayashree Dhanapal56678082022-01-04 17:27:20 +053079 std::string interface =
80 thresholds::getInterface(threshold.level);
81 thresholdInterfaces[static_cast<size_t>(threshold.level)] =
82 objectServer.add_interface(interfacePath, interface);
Zbigniew Kurzynski0a4c4802020-04-01 11:22:27 +020083 }
84 association = objectServer.add_interface(interfacePath,
85 association::interface);
86
Andrei Kartashev39287412022-02-04 16:04:47 +030087 setInitialProperties(units);
Zbigniew Kurzynski0a4c4802020-04-01 11:22:27 +020088 }
James Feist6714a252018-09-10 15:26:18 -070089 }
James Feist961bf092020-07-01 16:38:12 -070090
91 // call setup always as not all sensors call setInitialProperties
James Feist71d31b22019-01-02 16:57:54 -080092 setupPowerMatch(conn);
James Feist6714a252018-09-10 15:26:18 -070093}
94
Thu Nguyen255da6b2022-07-29 10:05:52 +070095IntelCPUSensor::~IntelCPUSensor()
James Feist6714a252018-09-10 15:26:18 -070096{
97 // close the input dev to cancel async operations
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070098 inputDev.close();
99 waitTimer.cancel();
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800100 if (show)
101 {
Jayashree Dhanapal56678082022-01-04 17:27:20 +0530102 for (const auto& iface : thresholdInterfaces)
103 {
104 objServer.remove_interface(iface);
105 }
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800106 objServer.remove_interface(sensorInterface);
James Feist078f2322019-03-08 11:09:05 -0800107 objServer.remove_interface(association);
Arun P. Mohananb782eec2021-10-18 16:22:09 +0530108 objServer.remove_interface(availableInterface);
109 objServer.remove_interface(operationalInterface);
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800110 }
James Feist6714a252018-09-10 15:26:18 -0700111}
112
Thu Nguyen255da6b2022-07-29 10:05:52 +0700113void IntelCPUSensor::restartRead(void)
James Feist6714a252018-09-10 15:26:18 -0700114{
Thu Nguyen255da6b2022-07-29 10:05:52 +0700115 std::weak_ptr<IntelCPUSensor> weakRef = weak_from_this();
Arun Lal K M84545712021-12-31 13:29:56 +0000116 waitTimer.expires_from_now(boost::posix_time::milliseconds(pollTime));
117 waitTimer.async_wait([weakRef](const boost::system::error_code& ec) {
118 if (ec == boost::asio::error::operation_aborted)
119 {
120 std::cerr << "Failed to reschedule\n";
121 return;
122 }
Thu Nguyen255da6b2022-07-29 10:05:52 +0700123 std::shared_ptr<IntelCPUSensor> self = weakRef.lock();
Arun P. Mohanan04d05062021-10-29 20:30:26 +0530124
Arun Lal K M84545712021-12-31 13:29:56 +0000125 if (self)
126 {
127 self->setupRead();
128 }
129 });
130}
131
Thu Nguyen255da6b2022-07-29 10:05:52 +0700132void IntelCPUSensor::setupRead(void)
Arun Lal K M84545712021-12-31 13:29:56 +0000133{
James Feistc22b8422020-07-07 14:40:39 -0700134 if (readingStateGood())
135 {
136 inputDev.close();
Ed Tanous99c44092022-01-14 09:59:09 -0800137
138 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
Arun Lal K M84545712021-12-31 13:29:56 +0000139 fd = open(path.c_str(), O_RDONLY | O_NONBLOCK);
140 if (fd < 0)
James Feistc22b8422020-07-07 14:40:39 -0700141 {
142 std::cerr << name << " unable to open fd!\n";
Arun Lal K M84545712021-12-31 13:29:56 +0000143 return;
James Feistc22b8422020-07-07 14:40:39 -0700144 }
Arun Lal K M84545712021-12-31 13:29:56 +0000145
146 inputDev.assign(fd);
James Feistc22b8422020-07-07 14:40:39 -0700147 }
148 else
149 {
James Feistc22b8422020-07-07 14:40:39 -0700150 markAvailable(false);
Arun Lal K M84545712021-12-31 13:29:56 +0000151 updateValue(std::numeric_limits<double>::quiet_NaN());
152 restartRead();
153 return;
James Feistc22b8422020-07-07 14:40:39 -0700154 }
Arun Lal K M84545712021-12-31 13:29:56 +0000155
Thu Nguyen255da6b2022-07-29 10:05:52 +0700156 std::weak_ptr<IntelCPUSensor> weakRef = weak_from_this();
Arun Lal K M84545712021-12-31 13:29:56 +0000157 inputDev.async_wait(boost::asio::posix::descriptor_base::wait_read,
158 [weakRef](const boost::system::error_code& ec) {
Thu Nguyen255da6b2022-07-29 10:05:52 +0700159 std::shared_ptr<IntelCPUSensor> self = weakRef.lock();
Arun Lal K M84545712021-12-31 13:29:56 +0000160
Ed Tanousbb679322022-05-16 16:10:00 -0700161 if (self)
162 {
163 self->handleResponse(ec);
164 }
165 });
James Feist6714a252018-09-10 15:26:18 -0700166}
167
Thu Nguyen255da6b2022-07-29 10:05:52 +0700168void IntelCPUSensor::updateMinMaxValues(void)
Zbigniew Kurzynski8d8d8d72020-05-29 19:21:24 +0200169{
170 const boost::container::flat_map<
171 std::string,
172 std::vector<std::tuple<const char*, std::reference_wrapper<double>,
173 const char*>>>
174 map = {
175 {
176 "cap",
177 {
178 std::make_tuple("cap_max", std::ref(maxValue), "MaxValue"),
179 std::make_tuple("cap_min", std::ref(minValue), "MinValue"),
180 },
181 },
182 };
183
Zbigniew Kurzynski63f38662020-06-09 13:02:11 +0200184 if (auto fileParts = splitFileName(path))
Zbigniew Kurzynski8d8d8d72020-05-29 19:21:24 +0200185 {
Zbigniew Kurzynskidbfd4662020-09-28 18:06:00 +0200186 auto& [fileType, fileNr, fileItem] = *fileParts;
Zbigniew Kurzynski8d8d8d72020-05-29 19:21:24 +0200187 const auto mapIt = map.find(fileItem);
188 if (mapIt != map.cend())
189 {
190 for (const auto& vectorItem : mapIt->second)
191 {
Ed Tanous2049bd22022-07-09 07:20:26 -0700192 const auto& [suffix, oldValue, dbusName] = vectorItem;
Zbigniew Kurzynski8d8d8d72020-05-29 19:21:24 +0200193 auto attrPath = boost::replace_all_copy(path, fileItem, suffix);
Zbigniew Kurzynski63f38662020-06-09 13:02:11 +0200194 if (auto newVal =
Thu Nguyen255da6b2022-07-29 10:05:52 +0700195 readFile(attrPath, IntelCPUSensor::sensorScaleFactor))
Zbigniew Kurzynski8d8d8d72020-05-29 19:21:24 +0200196 {
Zbigniew Kurzynski4f45e422020-06-09 12:42:15 +0200197 updateProperty(sensorInterface, oldValue, *newVal,
198 dbusName);
Zbigniew Kurzynski8d8d8d72020-05-29 19:21:24 +0200199 }
200 else
201 {
202 if (isPowerOn())
203 {
Zbigniew Kurzynski4f45e422020-06-09 12:42:15 +0200204 updateProperty(sensorInterface, oldValue, 0, dbusName);
Zbigniew Kurzynski8d8d8d72020-05-29 19:21:24 +0200205 }
206 else
207 {
Zbigniew Kurzynski4f45e422020-06-09 12:42:15 +0200208 updateProperty(sensorInterface, oldValue,
209 std::numeric_limits<double>::quiet_NaN(),
210 dbusName);
Zbigniew Kurzynski8d8d8d72020-05-29 19:21:24 +0200211 }
212 }
213 }
214 }
215 }
216}
217
Thu Nguyen255da6b2022-07-29 10:05:52 +0700218void IntelCPUSensor::handleResponse(const boost::system::error_code& err)
James Feist6714a252018-09-10 15:26:18 -0700219{
Arun Lal K M84545712021-12-31 13:29:56 +0000220 if ((err == boost::system::errc::bad_file_descriptor) ||
221 (err == boost::asio::error::misc_errors::not_found))
James Feist6714a252018-09-10 15:26:18 -0700222 {
223 return; // we're being destroyed
224 }
Ed Tanous8a57ec02020-10-09 12:46:52 -0700225 if (err == boost::system::errc::operation_canceled)
James Feist838529b2020-09-02 16:57:05 -0700226 {
227 if (readingStateGood())
228 {
229 if (!loggedInterfaceDown)
230 {
231 std::cerr << name << " interface down!\n";
232 loggedInterfaceDown = true;
233 }
Thu Nguyen255da6b2022-07-29 10:05:52 +0700234 pollTime = static_cast<size_t>(IntelCPUSensor::sensorPollMs) * 10U;
James Feist838529b2020-09-02 16:57:05 -0700235 markFunctional(false);
236 }
237 return;
238 }
239 loggedInterfaceDown = false;
Arun Lal K M84545712021-12-31 13:29:56 +0000240
241 if (err)
James Feist6714a252018-09-10 15:26:18 -0700242 {
Arun Lal K M84545712021-12-31 13:29:56 +0000243 pollTime = sensorFailedPollTimeMs;
244 incrementError();
245 return;
246 }
247
248 static constexpr uint32_t bufLen = 128;
249 std::string response;
250 response.resize(bufLen);
251 int rdLen = 0;
252
253 if (fd >= 0)
254 {
255 rdLen = pread(fd, response.data(), bufLen, 0);
256 }
257
258 if (rdLen > 0)
259 {
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}