blob: e955fefe1ddc04b3c838cc3aac16adccf7d24ad3 [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),
James Feistc22b8422020-07-07 14:40:39 -070045 objServer(objectServer), inputDev(io), waitTimer(io), path(path),
Brad Bishopfbb44ad2019-11-08 09:42:37 -050046 privTcontrol(std::numeric_limits<double>::quiet_NaN()),
Zbigniew Kurzynski98be9842020-09-07 18:49:15 +020047 dtsOffset(dtsOffset), show(show), pollTime(CPUSensor::sensorPollMs),
48 minMaxReadCounter(0)
James Feist6714a252018-09-10 15:26:18 -070049{
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -080050 nameTcontrol = labelTcontrol;
51 nameTcontrol += " CPU" + std::to_string(cpuId);
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;
Zev Weiss6b6891c2021-04-22 02:46:21 -050058 const char* units;
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");
77 if (thresholds::hasWarningInterface(thresholds))
78 {
79 thresholdInterfaceWarning = objectServer.add_interface(
80 interfacePath,
81 "xyz.openbmc_project.Sensor.Threshold.Warning");
82 }
83 if (thresholds::hasCriticalInterface(thresholds))
84 {
85 thresholdInterfaceCritical = objectServer.add_interface(
86 interfacePath,
87 "xyz.openbmc_project.Sensor.Threshold.Critical");
88 }
89 association = objectServer.add_interface(interfacePath,
90 association::interface);
91
Zev Weiss6b6891c2021-04-22 02:46:21 -050092 setInitialProperties(conn, units);
Zbigniew Kurzynski0a4c4802020-04-01 11:22:27 +020093 }
James Feist6714a252018-09-10 15:26:18 -070094 }
James Feist961bf092020-07-01 16:38:12 -070095
96 // call setup always as not all sensors call setInitialProperties
James Feist71d31b22019-01-02 16:57:54 -080097 setupPowerMatch(conn);
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070098 setupRead();
James Feist6714a252018-09-10 15:26:18 -070099}
100
101CPUSensor::~CPUSensor()
102{
103 // close the input dev to cancel async operations
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700104 inputDev.close();
105 waitTimer.cancel();
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800106 if (show)
107 {
108 objServer.remove_interface(thresholdInterfaceWarning);
109 objServer.remove_interface(thresholdInterfaceCritical);
110 objServer.remove_interface(sensorInterface);
James Feist078f2322019-03-08 11:09:05 -0800111 objServer.remove_interface(association);
Arun P. Mohananb782eec2021-10-18 16:22:09 +0530112 objServer.remove_interface(availableInterface);
113 objServer.remove_interface(operationalInterface);
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800114 }
James Feist6714a252018-09-10 15:26:18 -0700115}
116
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700117void CPUSensor::setupRead(void)
James Feist6714a252018-09-10 15:26:18 -0700118{
James Feistc22b8422020-07-07 14:40:39 -0700119 if (readingStateGood())
120 {
121 inputDev.close();
122 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',
129 [&](const boost::system::error_code& ec,
130 std::size_t /*bytes_transfered*/) { handleResponse(ec); });
131 }
132 else
133 {
134 std::cerr << name << " unable to open fd!\n";
135 pollTime = sensorFailedPollTimeMs;
136 }
137 }
138 else
139 {
140 pollTime = sensorFailedPollTimeMs;
141 markAvailable(false);
142 }
143 waitTimer.expires_from_now(boost::posix_time::milliseconds(pollTime));
144 waitTimer.async_wait([&](const boost::system::error_code& ec) {
145 if (ec == boost::asio::error::operation_aborted)
146 {
147 return; // we're being canceled
148 }
149 setupRead();
150 });
James Feist6714a252018-09-10 15:26:18 -0700151}
152
Zbigniew Kurzynski8d8d8d72020-05-29 19:21:24 +0200153void CPUSensor::updateMinMaxValues(void)
154{
155 const boost::container::flat_map<
156 std::string,
157 std::vector<std::tuple<const char*, std::reference_wrapper<double>,
158 const char*>>>
159 map = {
160 {
161 "cap",
162 {
163 std::make_tuple("cap_max", std::ref(maxValue), "MaxValue"),
164 std::make_tuple("cap_min", std::ref(minValue), "MinValue"),
165 },
166 },
167 };
168
Zbigniew Kurzynski63f38662020-06-09 13:02:11 +0200169 if (auto fileParts = splitFileName(path))
Zbigniew Kurzynski8d8d8d72020-05-29 19:21:24 +0200170 {
Zbigniew Kurzynskidbfd4662020-09-28 18:06:00 +0200171 auto& [fileType, fileNr, fileItem] = *fileParts;
Zbigniew Kurzynski8d8d8d72020-05-29 19:21:24 +0200172 const auto mapIt = map.find(fileItem);
173 if (mapIt != map.cend())
174 {
175 for (const auto& vectorItem : mapIt->second)
176 {
Zbigniew Kurzynskidbfd4662020-09-28 18:06:00 +0200177 auto& [suffix, oldValue, dbusName] = vectorItem;
Zbigniew Kurzynski8d8d8d72020-05-29 19:21:24 +0200178 auto attrPath = boost::replace_all_copy(path, fileItem, suffix);
Zbigniew Kurzynski63f38662020-06-09 13:02:11 +0200179 if (auto newVal =
180 readFile(attrPath, CPUSensor::sensorScaleFactor))
Zbigniew Kurzynski8d8d8d72020-05-29 19:21:24 +0200181 {
Zbigniew Kurzynski4f45e422020-06-09 12:42:15 +0200182 updateProperty(sensorInterface, oldValue, *newVal,
183 dbusName);
Zbigniew Kurzynski8d8d8d72020-05-29 19:21:24 +0200184 }
185 else
186 {
187 if (isPowerOn())
188 {
Zbigniew Kurzynski4f45e422020-06-09 12:42:15 +0200189 updateProperty(sensorInterface, oldValue, 0, dbusName);
Zbigniew Kurzynski8d8d8d72020-05-29 19:21:24 +0200190 }
191 else
192 {
Zbigniew Kurzynski4f45e422020-06-09 12:42:15 +0200193 updateProperty(sensorInterface, oldValue,
194 std::numeric_limits<double>::quiet_NaN(),
195 dbusName);
Zbigniew Kurzynski8d8d8d72020-05-29 19:21:24 +0200196 }
197 }
198 }
199 }
200 }
201}
202
James Feistd8705872019-02-08 13:26:09 -0800203void CPUSensor::handleResponse(const boost::system::error_code& err)
James Feist6714a252018-09-10 15:26:18 -0700204{
James Feist838529b2020-09-02 16:57:05 -0700205
James Feist6714a252018-09-10 15:26:18 -0700206 if (err == boost::system::errc::bad_file_descriptor)
207 {
208 return; // we're being destroyed
209 }
Ed Tanous8a57ec02020-10-09 12:46:52 -0700210 if (err == boost::system::errc::operation_canceled)
James Feist838529b2020-09-02 16:57:05 -0700211 {
212 if (readingStateGood())
213 {
214 if (!loggedInterfaceDown)
215 {
216 std::cerr << name << " interface down!\n";
217 loggedInterfaceDown = true;
218 }
Ed Tanous8a57ec02020-10-09 12:46:52 -0700219 pollTime = CPUSensor::sensorPollMs * 10u;
James Feist838529b2020-09-02 16:57:05 -0700220 markFunctional(false);
221 }
222 return;
223 }
224 loggedInterfaceDown = false;
James Feistc22b8422020-07-07 14:40:39 -0700225 pollTime = CPUSensor::sensorPollMs;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700226 std::istream responseStream(&readBuf);
James Feist6714a252018-09-10 15:26:18 -0700227 if (!err)
228 {
229 std::string response;
230 try
231 {
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700232 std::getline(responseStream, response);
Zhikui Rend3da1282020-09-11 17:02:01 -0700233 rawValue = std::stod(response);
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700234 responseStream.clear();
Zhikui Rend3da1282020-09-11 17:02:01 -0700235 double nvalue = rawValue / CPUSensor::sensorScaleFactor;
Josh Lehan833661a2020-03-04 17:35:41 -0800236
237 if (show)
James Feist6714a252018-09-10 15:26:18 -0700238 {
Josh Lehan833661a2020-03-04 17:35:41 -0800239 updateValue(nvalue);
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800240 }
Josh Lehan833661a2020-03-04 17:35:41 -0800241 else
242 {
243 value = nvalue;
244 }
Zbigniew Kurzynski98be9842020-09-07 18:49:15 +0200245 if (minMaxReadCounter++ % 8 == 0)
246 {
247 updateMinMaxValues();
248 }
Josh Lehan833661a2020-03-04 17:35:41 -0800249
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800250 double gTcontrol = gCpuSensors[nameTcontrol]
251 ? gCpuSensors[nameTcontrol]->value
252 : std::numeric_limits<double>::quiet_NaN();
253 if (gTcontrol != privTcontrol)
254 {
255 privTcontrol = gTcontrol;
256
257 if (!thresholds.empty())
258 {
259 std::vector<thresholds::Threshold> newThresholds;
260 if (parseThresholdsFromAttr(newThresholds, path,
Vijay Khemka86dea2b2019-06-06 11:14:37 -0700261 CPUSensor::sensorScaleFactor,
262 dtsOffset))
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800263 {
264 if (!std::equal(thresholds.begin(), thresholds.end(),
265 newThresholds.begin(),
266 newThresholds.end()))
267 {
268 thresholds = newThresholds;
269 if (show)
270 {
271 thresholds::updateThresholds(this);
272 }
273 }
274 }
275 else
276 {
277 std::cerr << "Failure to update thresholds for " << name
278 << "\n";
279 }
Jae Hyun Yoo95b8a2d2019-02-25 20:15:09 -0800280 }
281 }
James Feist6714a252018-09-10 15:26:18 -0700282 }
James Feistd8705872019-02-08 13:26:09 -0800283 catch (const std::invalid_argument&)
James Feist6714a252018-09-10 15:26:18 -0700284 {
James Feist961bf092020-07-01 16:38:12 -0700285 incrementError();
James Feist6714a252018-09-10 15:26:18 -0700286 }
287 }
288 else
289 {
James Feist1169eb42018-10-31 10:08:47 -0700290 pollTime = sensorFailedPollTimeMs;
James Feist961bf092020-07-01 16:38:12 -0700291 incrementError();
James Feist6714a252018-09-10 15:26:18 -0700292 }
293
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700294 responseStream.clear();
James Feist6714a252018-09-10 15:26:18 -0700295}
296
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700297void CPUSensor::checkThresholds(void)
James Feist6714a252018-09-10 15:26:18 -0700298{
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800299 if (show)
300 {
301 thresholds::checkThresholds(this);
302 }
James Feist6714a252018-09-10 15:26:18 -0700303}