blob: ea56d03904c203e5d1d7f7d05cddc0c6ca9bd6d4 [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
Patrick Ventureca44b2f2019-10-31 11:02:26 -070017#include "CPUSensor.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/predicate.hpp>
24#include <boost/algorithm/string/replace.hpp>
James Feist8086aba2020-08-25 16:00:59 -070025#include <boost/asio/read_until.hpp>
James Feist6714a252018-09-10 15:26:18 -070026#include <boost/date_time/posix_time/posix_time.hpp>
James Feist38fb5982020-05-28 10:09:54 -070027#include <sdbusplus/asio/connection.hpp>
28#include <sdbusplus/asio/object_server.hpp>
29
James Feist6714a252018-09-10 15:26:18 -070030#include <iostream>
Patrick Venture96e97db2019-10-31 13:44:38 -070031#include <istream>
James Feist6714a252018-09-10 15:26:18 -070032#include <limits>
Patrick Venture96e97db2019-10-31 13:44:38 -070033#include <memory>
Patrick Venture96e97db2019-10-31 13:44:38 -070034#include <stdexcept>
James Feist6714a252018-09-10 15:26:18 -070035#include <string>
Patrick Venture96e97db2019-10-31 13:44:38 -070036#include <vector>
James Feist6714a252018-09-10 15:26:18 -070037
James Feistd8705872019-02-08 13:26:09 -080038CPUSensor::CPUSensor(const std::string& path, const std::string& objectType,
39 sdbusplus::asio::object_server& objectServer,
40 std::shared_ptr<sdbusplus::asio::connection>& conn,
41 boost::asio::io_service& io, const std::string& sensorName,
42 std::vector<thresholds::Threshold>&& _thresholds,
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -080043 const std::string& sensorConfiguration, int cpuId,
Vijay Khemka86dea2b2019-06-06 11:14:37 -070044 bool show, double dtsOffset) :
James Feist930fcde2019-05-28 12:58:43 -070045 Sensor(boost::replace_all_copy(sensorName, " ", "_"),
Andrei Kartashev6736d4b2020-12-18 18:59:21 +030046 std::move(_thresholds), sensorConfiguration, objectType, 0, 0, conn,
47 PowerState::on),
James Feistc22b8422020-07-07 14:40:39 -070048 objServer(objectServer), inputDev(io), waitTimer(io), path(path),
Brad Bishopfbb44ad2019-11-08 09:42:37 -050049 privTcontrol(std::numeric_limits<double>::quiet_NaN()),
Zbigniew Kurzynski98be9842020-09-07 18:49:15 +020050 dtsOffset(dtsOffset), show(show), pollTime(CPUSensor::sensorPollMs),
51 minMaxReadCounter(0)
James Feist6714a252018-09-10 15:26:18 -070052{
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -080053 nameTcontrol = labelTcontrol;
54 nameTcontrol += " CPU" + std::to_string(cpuId);
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -080055 if (show)
James Feist6714a252018-09-10 15:26:18 -070056 {
Zbigniew Kurzynski63f38662020-06-09 13:02:11 +020057 if (auto fileParts = splitFileName(path))
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -080058 {
Zbigniew Kurzynskidbfd4662020-09-28 18:06:00 +020059 auto& [type, nr, item] = *fileParts;
Zbigniew Kurzynski0a4c4802020-04-01 11:22:27 +020060 std::string interfacePath;
61 if (type.compare("power") == 0)
62 {
63 interfacePath = "/xyz/openbmc_project/sensors/power/" + name;
Andrei Kartashev6736d4b2020-12-18 18:59:21 +030064 minValue = 0;
65 maxValue = 511;
Zbigniew Kurzynski0a4c4802020-04-01 11:22:27 +020066 }
67 else
68 {
69 interfacePath =
70 "/xyz/openbmc_project/sensors/temperature/" + name;
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
92 setInitialProperties(conn);
93 }
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);
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800112 }
James Feist6714a252018-09-10 15:26:18 -0700113}
114
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700115void CPUSensor::setupRead(void)
James Feist6714a252018-09-10 15:26:18 -0700116{
James Feistc22b8422020-07-07 14:40:39 -0700117 if (readingStateGood())
118 {
119 inputDev.close();
120 int fd = open(path.c_str(), O_RDONLY);
121 if (fd >= 0)
122 {
123 inputDev.assign(fd);
124
125 boost::asio::async_read_until(
126 inputDev, readBuf, '\n',
127 [&](const boost::system::error_code& ec,
128 std::size_t /*bytes_transfered*/) { handleResponse(ec); });
129 }
130 else
131 {
132 std::cerr << name << " unable to open fd!\n";
133 pollTime = sensorFailedPollTimeMs;
134 }
135 }
136 else
137 {
138 pollTime = sensorFailedPollTimeMs;
139 markAvailable(false);
140 }
141 waitTimer.expires_from_now(boost::posix_time::milliseconds(pollTime));
142 waitTimer.async_wait([&](const boost::system::error_code& ec) {
143 if (ec == boost::asio::error::operation_aborted)
144 {
145 return; // we're being canceled
146 }
147 setupRead();
148 });
James Feist6714a252018-09-10 15:26:18 -0700149}
150
Zbigniew Kurzynski8d8d8d72020-05-29 19:21:24 +0200151void CPUSensor::updateMinMaxValues(void)
152{
153 const boost::container::flat_map<
154 std::string,
155 std::vector<std::tuple<const char*, std::reference_wrapper<double>,
156 const char*>>>
157 map = {
158 {
159 "cap",
160 {
161 std::make_tuple("cap_max", std::ref(maxValue), "MaxValue"),
162 std::make_tuple("cap_min", std::ref(minValue), "MinValue"),
163 },
164 },
165 };
166
Zbigniew Kurzynski63f38662020-06-09 13:02:11 +0200167 if (auto fileParts = splitFileName(path))
Zbigniew Kurzynski8d8d8d72020-05-29 19:21:24 +0200168 {
Zbigniew Kurzynskidbfd4662020-09-28 18:06:00 +0200169 auto& [fileType, fileNr, fileItem] = *fileParts;
Zbigniew Kurzynski8d8d8d72020-05-29 19:21:24 +0200170 const auto mapIt = map.find(fileItem);
171 if (mapIt != map.cend())
172 {
173 for (const auto& vectorItem : mapIt->second)
174 {
Zbigniew Kurzynskidbfd4662020-09-28 18:06:00 +0200175 auto& [suffix, oldValue, dbusName] = vectorItem;
Zbigniew Kurzynski8d8d8d72020-05-29 19:21:24 +0200176 auto attrPath = boost::replace_all_copy(path, fileItem, suffix);
Zbigniew Kurzynski63f38662020-06-09 13:02:11 +0200177 if (auto newVal =
178 readFile(attrPath, CPUSensor::sensorScaleFactor))
Zbigniew Kurzynski8d8d8d72020-05-29 19:21:24 +0200179 {
Zbigniew Kurzynski4f45e422020-06-09 12:42:15 +0200180 updateProperty(sensorInterface, oldValue, *newVal,
181 dbusName);
Zbigniew Kurzynski8d8d8d72020-05-29 19:21:24 +0200182 }
183 else
184 {
185 if (isPowerOn())
186 {
Zbigniew Kurzynski4f45e422020-06-09 12:42:15 +0200187 updateProperty(sensorInterface, oldValue, 0, dbusName);
Zbigniew Kurzynski8d8d8d72020-05-29 19:21:24 +0200188 }
189 else
190 {
Zbigniew Kurzynski4f45e422020-06-09 12:42:15 +0200191 updateProperty(sensorInterface, oldValue,
192 std::numeric_limits<double>::quiet_NaN(),
193 dbusName);
Zbigniew Kurzynski8d8d8d72020-05-29 19:21:24 +0200194 }
195 }
196 }
197 }
198 }
199}
200
James Feistd8705872019-02-08 13:26:09 -0800201void CPUSensor::handleResponse(const boost::system::error_code& err)
James Feist6714a252018-09-10 15:26:18 -0700202{
James Feist838529b2020-09-02 16:57:05 -0700203
James Feist6714a252018-09-10 15:26:18 -0700204 if (err == boost::system::errc::bad_file_descriptor)
205 {
206 return; // we're being destroyed
207 }
James Feist838529b2020-09-02 16:57:05 -0700208 else if (err == boost::system::errc::operation_canceled)
209 {
210 if (readingStateGood())
211 {
212 if (!loggedInterfaceDown)
213 {
214 std::cerr << name << " interface down!\n";
215 loggedInterfaceDown = true;
216 }
217 pollTime = 10000 + rand() % 10000;
218 markFunctional(false);
219 }
220 return;
221 }
222 loggedInterfaceDown = false;
James Feistc22b8422020-07-07 14:40:39 -0700223 pollTime = CPUSensor::sensorPollMs;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700224 std::istream responseStream(&readBuf);
James Feist6714a252018-09-10 15:26:18 -0700225 if (!err)
226 {
227 std::string response;
228 try
229 {
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700230 std::getline(responseStream, response);
Zhikui Rend3da1282020-09-11 17:02:01 -0700231 rawValue = std::stod(response);
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700232 responseStream.clear();
Zhikui Rend3da1282020-09-11 17:02:01 -0700233 double nvalue = rawValue / CPUSensor::sensorScaleFactor;
Josh Lehan833661a2020-03-04 17:35:41 -0800234
235 if (show)
James Feist6714a252018-09-10 15:26:18 -0700236 {
Josh Lehan833661a2020-03-04 17:35:41 -0800237 updateValue(nvalue);
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800238 }
Josh Lehan833661a2020-03-04 17:35:41 -0800239 else
240 {
241 value = nvalue;
242 }
Zbigniew Kurzynski98be9842020-09-07 18:49:15 +0200243 if (minMaxReadCounter++ % 8 == 0)
244 {
245 updateMinMaxValues();
246 }
Josh Lehan833661a2020-03-04 17:35:41 -0800247
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800248 double gTcontrol = gCpuSensors[nameTcontrol]
249 ? gCpuSensors[nameTcontrol]->value
250 : std::numeric_limits<double>::quiet_NaN();
251 if (gTcontrol != privTcontrol)
252 {
253 privTcontrol = gTcontrol;
254
255 if (!thresholds.empty())
256 {
257 std::vector<thresholds::Threshold> newThresholds;
258 if (parseThresholdsFromAttr(newThresholds, path,
Vijay Khemka86dea2b2019-06-06 11:14:37 -0700259 CPUSensor::sensorScaleFactor,
260 dtsOffset))
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800261 {
262 if (!std::equal(thresholds.begin(), thresholds.end(),
263 newThresholds.begin(),
264 newThresholds.end()))
265 {
266 thresholds = newThresholds;
267 if (show)
268 {
269 thresholds::updateThresholds(this);
270 }
271 }
272 }
273 else
274 {
275 std::cerr << "Failure to update thresholds for " << name
276 << "\n";
277 }
Jae Hyun Yoo95b8a2d2019-02-25 20:15:09 -0800278 }
279 }
James Feist6714a252018-09-10 15:26:18 -0700280 }
James Feistd8705872019-02-08 13:26:09 -0800281 catch (const std::invalid_argument&)
James Feist6714a252018-09-10 15:26:18 -0700282 {
James Feist961bf092020-07-01 16:38:12 -0700283 incrementError();
James Feist6714a252018-09-10 15:26:18 -0700284 }
285 }
286 else
287 {
James Feist1169eb42018-10-31 10:08:47 -0700288 pollTime = sensorFailedPollTimeMs;
James Feist961bf092020-07-01 16:38:12 -0700289 incrementError();
James Feist6714a252018-09-10 15:26:18 -0700290 }
291
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700292 responseStream.clear();
James Feist6714a252018-09-10 15:26:18 -0700293}
294
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700295void CPUSensor::checkThresholds(void)
James Feist6714a252018-09-10 15:26:18 -0700296{
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800297 if (show)
298 {
299 thresholds::checkThresholds(this);
300 }
James Feist6714a252018-09-10 15:26:18 -0700301}