blob: 0fdb2f49445eab1b71e01c09c683c35e0a0f565d [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
Ed Tanous8a57ec02020-10-09 12:46:52 -070019#include <CPUSensor.hpp>
20#include <Utils.hpp>
James Feist6714a252018-09-10 15:26:18 -070021#include <boost/algorithm/string/predicate.hpp>
22#include <boost/algorithm/string/replace.hpp>
James Feist8086aba2020-08-25 16:00:59 -070023#include <boost/asio/read_until.hpp>
James Feist6714a252018-09-10 15:26:18 -070024#include <boost/date_time/posix_time/posix_time.hpp>
James Feist38fb5982020-05-28 10:09:54 -070025#include <sdbusplus/asio/connection.hpp>
26#include <sdbusplus/asio/object_server.hpp>
27
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
James Feistd8705872019-02-08 13:26:09 -080036CPUSensor::CPUSensor(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,
Jeff Lin7b7a9de2021-02-22 11:16:27 +080040 std::vector<thresholds::Threshold>&& thresholdsIn,
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -080041 const std::string& sensorConfiguration, int cpuId,
Vijay Khemka86dea2b2019-06-06 11:14:37 -070042 bool show, 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),
Arun P. Mohanan04d05062021-10-29 20:30:26 +053045 std::enable_shared_from_this<CPUSensor>(), objServer(objectServer),
Ed Tanousa771f6a2022-01-14 09:36:51 -080046 inputDev(io), waitTimer(io),
47 nameTcontrol("Tcontrol CPU" + std::to_string(cpuId)), path(path),
Brad Bishopfbb44ad2019-11-08 09:42:37 -050048 privTcontrol(std::numeric_limits<double>::quiet_NaN()),
Zbigniew Kurzynski98be9842020-09-07 18:49:15 +020049 dtsOffset(dtsOffset), show(show), pollTime(CPUSensor::sensorPollMs),
50 minMaxReadCounter(0)
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;
Zbigniew Kurzynski0a4c4802020-04-01 11:22:27 +020059 if (type.compare("power") == 0)
60 {
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
95CPUSensor::~CPUSensor()
96{
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
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700113void CPUSensor::setupRead(void)
James Feist6714a252018-09-10 15:26:18 -0700114{
Arun P. Mohanan04d05062021-10-29 20:30:26 +0530115 std::weak_ptr<CPUSensor> weakRef = weak_from_this();
116
James Feistc22b8422020-07-07 14:40:39 -0700117 if (readingStateGood())
118 {
119 inputDev.close();
Ed Tanous99c44092022-01-14 09:59:09 -0800120
121 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
James Feistc22b8422020-07-07 14:40:39 -0700122 int fd = open(path.c_str(), O_RDONLY);
123 if (fd >= 0)
124 {
125 inputDev.assign(fd);
126
127 boost::asio::async_read_until(
128 inputDev, readBuf, '\n',
Arun P. Mohanan04d05062021-10-29 20:30:26 +0530129 [weakRef](const boost::system::error_code& ec,
130 std::size_t /*bytes_transfered*/) {
131 std::shared_ptr<CPUSensor> self = weakRef.lock();
132 if (!self)
133 {
134 return;
135 }
136 self->handleResponse(ec);
137 });
James Feistc22b8422020-07-07 14:40:39 -0700138 }
139 else
140 {
141 std::cerr << name << " unable to open fd!\n";
142 pollTime = sensorFailedPollTimeMs;
143 }
144 }
145 else
146 {
147 pollTime = sensorFailedPollTimeMs;
148 markAvailable(false);
149 }
150 waitTimer.expires_from_now(boost::posix_time::milliseconds(pollTime));
Arun P. Mohanan04d05062021-10-29 20:30:26 +0530151 waitTimer.async_wait([weakRef](const boost::system::error_code& ec) {
James Feistc22b8422020-07-07 14:40:39 -0700152 if (ec == boost::asio::error::operation_aborted)
153 {
154 return; // we're being canceled
155 }
Arun P. Mohanan04d05062021-10-29 20:30:26 +0530156 std::shared_ptr<CPUSensor> self = weakRef.lock();
157 if (!self)
158 {
159 return;
160 }
161 self->setupRead();
James Feistc22b8422020-07-07 14:40:39 -0700162 });
James Feist6714a252018-09-10 15:26:18 -0700163}
164
Zbigniew Kurzynski8d8d8d72020-05-29 19:21:24 +0200165void CPUSensor::updateMinMaxValues(void)
166{
167 const boost::container::flat_map<
168 std::string,
169 std::vector<std::tuple<const char*, std::reference_wrapper<double>,
170 const char*>>>
171 map = {
172 {
173 "cap",
174 {
175 std::make_tuple("cap_max", std::ref(maxValue), "MaxValue"),
176 std::make_tuple("cap_min", std::ref(minValue), "MinValue"),
177 },
178 },
179 };
180
Zbigniew Kurzynski63f38662020-06-09 13:02:11 +0200181 if (auto fileParts = splitFileName(path))
Zbigniew Kurzynski8d8d8d72020-05-29 19:21:24 +0200182 {
Zbigniew Kurzynskidbfd4662020-09-28 18:06:00 +0200183 auto& [fileType, fileNr, fileItem] = *fileParts;
Zbigniew Kurzynski8d8d8d72020-05-29 19:21:24 +0200184 const auto mapIt = map.find(fileItem);
185 if (mapIt != map.cend())
186 {
187 for (const auto& vectorItem : mapIt->second)
188 {
Zbigniew Kurzynskidbfd4662020-09-28 18:06:00 +0200189 auto& [suffix, oldValue, dbusName] = vectorItem;
Zbigniew Kurzynski8d8d8d72020-05-29 19:21:24 +0200190 auto attrPath = boost::replace_all_copy(path, fileItem, suffix);
Zbigniew Kurzynski63f38662020-06-09 13:02:11 +0200191 if (auto newVal =
192 readFile(attrPath, CPUSensor::sensorScaleFactor))
Zbigniew Kurzynski8d8d8d72020-05-29 19:21:24 +0200193 {
Zbigniew Kurzynski4f45e422020-06-09 12:42:15 +0200194 updateProperty(sensorInterface, oldValue, *newVal,
195 dbusName);
Zbigniew Kurzynski8d8d8d72020-05-29 19:21:24 +0200196 }
197 else
198 {
199 if (isPowerOn())
200 {
Zbigniew Kurzynski4f45e422020-06-09 12:42:15 +0200201 updateProperty(sensorInterface, oldValue, 0, dbusName);
Zbigniew Kurzynski8d8d8d72020-05-29 19:21:24 +0200202 }
203 else
204 {
Zbigniew Kurzynski4f45e422020-06-09 12:42:15 +0200205 updateProperty(sensorInterface, oldValue,
206 std::numeric_limits<double>::quiet_NaN(),
207 dbusName);
Zbigniew Kurzynski8d8d8d72020-05-29 19:21:24 +0200208 }
209 }
210 }
211 }
212 }
213}
214
James Feistd8705872019-02-08 13:26:09 -0800215void CPUSensor::handleResponse(const boost::system::error_code& err)
James Feist6714a252018-09-10 15:26:18 -0700216{
James Feist838529b2020-09-02 16:57:05 -0700217
James Feist6714a252018-09-10 15:26:18 -0700218 if (err == boost::system::errc::bad_file_descriptor)
219 {
220 return; // we're being destroyed
221 }
Ed Tanous8a57ec02020-10-09 12:46:52 -0700222 if (err == boost::system::errc::operation_canceled)
James Feist838529b2020-09-02 16:57:05 -0700223 {
224 if (readingStateGood())
225 {
226 if (!loggedInterfaceDown)
227 {
228 std::cerr << name << " interface down!\n";
229 loggedInterfaceDown = true;
230 }
Ed Tanous8a57ec02020-10-09 12:46:52 -0700231 pollTime = CPUSensor::sensorPollMs * 10u;
James Feist838529b2020-09-02 16:57:05 -0700232 markFunctional(false);
233 }
234 return;
235 }
236 loggedInterfaceDown = false;
James Feistc22b8422020-07-07 14:40:39 -0700237 pollTime = CPUSensor::sensorPollMs;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700238 std::istream responseStream(&readBuf);
James Feist6714a252018-09-10 15:26:18 -0700239 if (!err)
240 {
241 std::string response;
242 try
243 {
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700244 std::getline(responseStream, response);
Zhikui Rend3da1282020-09-11 17:02:01 -0700245 rawValue = std::stod(response);
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700246 responseStream.clear();
Zhikui Rend3da1282020-09-11 17:02:01 -0700247 double nvalue = rawValue / CPUSensor::sensorScaleFactor;
Josh Lehan833661a2020-03-04 17:35:41 -0800248
249 if (show)
James Feist6714a252018-09-10 15:26:18 -0700250 {
Josh Lehan833661a2020-03-04 17:35:41 -0800251 updateValue(nvalue);
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800252 }
Josh Lehan833661a2020-03-04 17:35:41 -0800253 else
254 {
255 value = nvalue;
256 }
Zbigniew Kurzynski98be9842020-09-07 18:49:15 +0200257 if (minMaxReadCounter++ % 8 == 0)
258 {
259 updateMinMaxValues();
260 }
Josh Lehan833661a2020-03-04 17:35:41 -0800261
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800262 double gTcontrol = gCpuSensors[nameTcontrol]
263 ? gCpuSensors[nameTcontrol]->value
264 : std::numeric_limits<double>::quiet_NaN();
265 if (gTcontrol != privTcontrol)
266 {
267 privTcontrol = gTcontrol;
268
269 if (!thresholds.empty())
270 {
271 std::vector<thresholds::Threshold> newThresholds;
272 if (parseThresholdsFromAttr(newThresholds, path,
Vijay Khemka86dea2b2019-06-06 11:14:37 -0700273 CPUSensor::sensorScaleFactor,
274 dtsOffset))
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800275 {
276 if (!std::equal(thresholds.begin(), thresholds.end(),
277 newThresholds.begin(),
278 newThresholds.end()))
279 {
280 thresholds = newThresholds;
281 if (show)
282 {
283 thresholds::updateThresholds(this);
284 }
285 }
286 }
287 else
288 {
289 std::cerr << "Failure to update thresholds for " << name
290 << "\n";
291 }
Jae Hyun Yoo95b8a2d2019-02-25 20:15:09 -0800292 }
293 }
James Feist6714a252018-09-10 15:26:18 -0700294 }
James Feistd8705872019-02-08 13:26:09 -0800295 catch (const std::invalid_argument&)
James Feist6714a252018-09-10 15:26:18 -0700296 {
James Feist961bf092020-07-01 16:38:12 -0700297 incrementError();
James Feist6714a252018-09-10 15:26:18 -0700298 }
299 }
300 else
301 {
James Feist1169eb42018-10-31 10:08:47 -0700302 pollTime = sensorFailedPollTimeMs;
James Feist961bf092020-07-01 16:38:12 -0700303 incrementError();
James Feist6714a252018-09-10 15:26:18 -0700304 }
305
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700306 responseStream.clear();
James Feist6714a252018-09-10 15:26:18 -0700307}
308
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700309void CPUSensor::checkThresholds(void)
James Feist6714a252018-09-10 15:26:18 -0700310{
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800311 if (show)
312 {
313 thresholds::checkThresholds(this);
314 }
James Feist6714a252018-09-10 15:26:18 -0700315}