blob: afc4706a74f0e6cd1a3e095d1fd9885ec3e69fc8 [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>
25#include <boost/date_time/posix_time/posix_time.hpp>
James Feist38fb5982020-05-28 10:09:54 -070026#include <sdbusplus/asio/connection.hpp>
27#include <sdbusplus/asio/object_server.hpp>
28
James Feist6714a252018-09-10 15:26:18 -070029#include <iostream>
Patrick Venture96e97db2019-10-31 13:44:38 -070030#include <istream>
James Feist6714a252018-09-10 15:26:18 -070031#include <limits>
Patrick Venture96e97db2019-10-31 13:44:38 -070032#include <memory>
Patrick Venture96e97db2019-10-31 13:44:38 -070033#include <stdexcept>
James Feist6714a252018-09-10 15:26:18 -070034#include <string>
Patrick Venture96e97db2019-10-31 13:44:38 -070035#include <vector>
James Feist6714a252018-09-10 15:26:18 -070036
James Feistd8705872019-02-08 13:26:09 -080037CPUSensor::CPUSensor(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>&& _thresholds,
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -080042 const std::string& sensorConfiguration, int cpuId,
Vijay Khemka86dea2b2019-06-06 11:14:37 -070043 bool show, double dtsOffset) :
James Feist930fcde2019-05-28 12:58:43 -070044 Sensor(boost::replace_all_copy(sensorName, " ", "_"),
James Feistce3fca42018-11-21 12:58:24 -080045 std::move(_thresholds), sensorConfiguration, objectType, maxReading,
46 minReading),
James Feist71d31b22019-01-02 16:57:54 -080047 objServer(objectServer), inputDev(io, open(path.c_str(), O_RDONLY)),
Brad Bishopfbb44ad2019-11-08 09:42:37 -050048 waitTimer(io), path(path),
49 privTcontrol(std::numeric_limits<double>::quiet_NaN()),
50 dtsOffset(dtsOffset), show(show), errCount(0)
James Feist6714a252018-09-10 15:26:18 -070051{
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -080052 nameTcontrol = labelTcontrol;
53 nameTcontrol += " CPU" + std::to_string(cpuId);
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -080054 if (show)
James Feist6714a252018-09-10 15:26:18 -070055 {
Zbigniew Kurzynski63f38662020-06-09 13:02:11 +020056 if (auto fileParts = splitFileName(path))
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -080057 {
Zbigniew Kurzynski0a4c4802020-04-01 11:22:27 +020058 auto [type, nr, item] = *fileParts;
59 std::string interfacePath;
60 if (type.compare("power") == 0)
61 {
62 interfacePath = "/xyz/openbmc_project/sensors/power/" + name;
63 }
64 else
65 {
66 interfacePath =
67 "/xyz/openbmc_project/sensors/temperature/" + name;
68 }
James Feist078f2322019-03-08 11:09:05 -080069
Zbigniew Kurzynski0a4c4802020-04-01 11:22:27 +020070 sensorInterface = objectServer.add_interface(
71 interfacePath, "xyz.openbmc_project.Sensor.Value");
72 if (thresholds::hasWarningInterface(thresholds))
73 {
74 thresholdInterfaceWarning = objectServer.add_interface(
75 interfacePath,
76 "xyz.openbmc_project.Sensor.Threshold.Warning");
77 }
78 if (thresholds::hasCriticalInterface(thresholds))
79 {
80 thresholdInterfaceCritical = objectServer.add_interface(
81 interfacePath,
82 "xyz.openbmc_project.Sensor.Threshold.Critical");
83 }
84 association = objectServer.add_interface(interfacePath,
85 association::interface);
86
87 setInitialProperties(conn);
88 }
James Feist6714a252018-09-10 15:26:18 -070089 }
James Feist71d31b22019-01-02 16:57:54 -080090 setupPowerMatch(conn);
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070091 setupRead();
James Feist6714a252018-09-10 15:26:18 -070092}
93
94CPUSensor::~CPUSensor()
95{
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 {
101 objServer.remove_interface(thresholdInterfaceWarning);
102 objServer.remove_interface(thresholdInterfaceCritical);
103 objServer.remove_interface(sensorInterface);
James Feist078f2322019-03-08 11:09:05 -0800104 objServer.remove_interface(association);
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800105 }
James Feist6714a252018-09-10 15:26:18 -0700106}
107
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700108void CPUSensor::setupRead(void)
James Feist6714a252018-09-10 15:26:18 -0700109{
110 boost::asio::async_read_until(
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700111 inputDev, readBuf, '\n',
James Feistd8705872019-02-08 13:26:09 -0800112 [&](const boost::system::error_code& ec,
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700113 std::size_t /*bytes_transfered*/) { handleResponse(ec); });
James Feist6714a252018-09-10 15:26:18 -0700114}
115
Zbigniew Kurzynski8d8d8d72020-05-29 19:21:24 +0200116void CPUSensor::updateMinMaxValues(void)
117{
118 const boost::container::flat_map<
119 std::string,
120 std::vector<std::tuple<const char*, std::reference_wrapper<double>,
121 const char*>>>
122 map = {
123 {
124 "cap",
125 {
126 std::make_tuple("cap_max", std::ref(maxValue), "MaxValue"),
127 std::make_tuple("cap_min", std::ref(minValue), "MinValue"),
128 },
129 },
130 };
131
Zbigniew Kurzynski63f38662020-06-09 13:02:11 +0200132 if (auto fileParts = splitFileName(path))
Zbigniew Kurzynski8d8d8d72020-05-29 19:21:24 +0200133 {
134 auto [fileType, fileNr, fileItem] = *fileParts;
135 const auto mapIt = map.find(fileItem);
136 if (mapIt != map.cend())
137 {
138 for (const auto& vectorItem : mapIt->second)
139 {
140 auto [suffix, oldValue, dbusName] = vectorItem;
141 auto attrPath = boost::replace_all_copy(path, fileItem, suffix);
Zbigniew Kurzynski63f38662020-06-09 13:02:11 +0200142 if (auto newVal =
143 readFile(attrPath, CPUSensor::sensorScaleFactor))
Zbigniew Kurzynski8d8d8d72020-05-29 19:21:24 +0200144 {
Zbigniew Kurzynski4f45e422020-06-09 12:42:15 +0200145 updateProperty(sensorInterface, oldValue, *newVal,
146 dbusName);
Zbigniew Kurzynski8d8d8d72020-05-29 19:21:24 +0200147 }
148 else
149 {
150 if (isPowerOn())
151 {
Zbigniew Kurzynski4f45e422020-06-09 12:42:15 +0200152 updateProperty(sensorInterface, oldValue, 0, dbusName);
Zbigniew Kurzynski8d8d8d72020-05-29 19:21:24 +0200153 }
154 else
155 {
Zbigniew Kurzynski4f45e422020-06-09 12:42:15 +0200156 updateProperty(sensorInterface, oldValue,
157 std::numeric_limits<double>::quiet_NaN(),
158 dbusName);
Zbigniew Kurzynski8d8d8d72020-05-29 19:21:24 +0200159 }
160 }
161 }
162 }
163 }
164}
165
James Feistd8705872019-02-08 13:26:09 -0800166void CPUSensor::handleResponse(const boost::system::error_code& err)
James Feist6714a252018-09-10 15:26:18 -0700167{
168 if (err == boost::system::errc::bad_file_descriptor)
169 {
170 return; // we're being destroyed
171 }
James Feist1169eb42018-10-31 10:08:47 -0700172 size_t pollTime = CPUSensor::sensorPollMs;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700173 std::istream responseStream(&readBuf);
James Feist6714a252018-09-10 15:26:18 -0700174 if (!err)
175 {
176 std::string response;
177 try
178 {
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700179 std::getline(responseStream, response);
Josh Lehan833661a2020-03-04 17:35:41 -0800180 double nvalue = std::stod(response);
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700181 responseStream.clear();
182 nvalue /= CPUSensor::sensorScaleFactor;
Josh Lehan833661a2020-03-04 17:35:41 -0800183
184 if (show)
James Feist6714a252018-09-10 15:26:18 -0700185 {
Josh Lehan833661a2020-03-04 17:35:41 -0800186 updateValue(nvalue);
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800187 }
Josh Lehan833661a2020-03-04 17:35:41 -0800188 else
189 {
190 value = nvalue;
191 }
Zbigniew Kurzynski8d8d8d72020-05-29 19:21:24 +0200192 updateMinMaxValues();
Josh Lehan833661a2020-03-04 17:35:41 -0800193
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800194 double gTcontrol = gCpuSensors[nameTcontrol]
195 ? gCpuSensors[nameTcontrol]->value
196 : std::numeric_limits<double>::quiet_NaN();
197 if (gTcontrol != privTcontrol)
198 {
199 privTcontrol = gTcontrol;
200
201 if (!thresholds.empty())
202 {
203 std::vector<thresholds::Threshold> newThresholds;
204 if (parseThresholdsFromAttr(newThresholds, path,
Vijay Khemka86dea2b2019-06-06 11:14:37 -0700205 CPUSensor::sensorScaleFactor,
206 dtsOffset))
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800207 {
208 if (!std::equal(thresholds.begin(), thresholds.end(),
209 newThresholds.begin(),
210 newThresholds.end()))
211 {
212 thresholds = newThresholds;
213 if (show)
214 {
215 thresholds::updateThresholds(this);
216 }
217 }
218 }
219 else
220 {
221 std::cerr << "Failure to update thresholds for " << name
222 << "\n";
223 }
Jae Hyun Yoo95b8a2d2019-02-25 20:15:09 -0800224 }
225 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700226 errCount = 0;
James Feist6714a252018-09-10 15:26:18 -0700227 }
James Feistd8705872019-02-08 13:26:09 -0800228 catch (const std::invalid_argument&)
James Feist6714a252018-09-10 15:26:18 -0700229 {
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700230 errCount++;
James Feist6714a252018-09-10 15:26:18 -0700231 }
232 }
233 else
234 {
James Feist1169eb42018-10-31 10:08:47 -0700235 pollTime = sensorFailedPollTimeMs;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700236 errCount++;
James Feist6714a252018-09-10 15:26:18 -0700237 }
238
James Feistdc6c55f2018-10-31 12:53:20 -0700239 if (errCount >= warnAfterErrorCount)
James Feist6714a252018-09-10 15:26:18 -0700240 {
241 // only an error if power is on
James Feist71d31b22019-01-02 16:57:54 -0800242 if (isPowerOn())
James Feist6714a252018-09-10 15:26:18 -0700243 {
James Feistdc6c55f2018-10-31 12:53:20 -0700244 // only print once
245 if (errCount == warnAfterErrorCount)
246 {
247 std::cerr << "Failure to read sensor " << name << " at " << path
248 << "\n";
249 }
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800250 if (show)
251 {
252 updateValue(0);
253 }
254 else
255 {
256 value = 0;
257 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700258 errCount++;
James Feist6714a252018-09-10 15:26:18 -0700259 }
260 else
261 {
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700262 errCount = 0; // check power again in 10 cycles
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800263 if (show)
264 {
265 updateValue(std::numeric_limits<double>::quiet_NaN());
266 }
267 else
268 {
269 value = std::numeric_limits<double>::quiet_NaN();
270 }
James Feist6714a252018-09-10 15:26:18 -0700271 }
272 }
273
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700274 responseStream.clear();
275 inputDev.close();
James Feist6714a252018-09-10 15:26:18 -0700276 int fd = open(path.c_str(), O_RDONLY);
Jae Hyun Yooef9665a2019-10-10 10:26:39 -0700277 if (fd < 0)
James Feist6714a252018-09-10 15:26:18 -0700278 {
279 return; // we're no longer valid
280 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700281 inputDev.assign(fd);
James Feist1169eb42018-10-31 10:08:47 -0700282 waitTimer.expires_from_now(boost::posix_time::milliseconds(pollTime));
James Feistd8705872019-02-08 13:26:09 -0800283 waitTimer.async_wait([&](const boost::system::error_code& ec) {
James Feist6714a252018-09-10 15:26:18 -0700284 if (ec == boost::asio::error::operation_aborted)
285 {
286 return; // we're being canceled
287 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700288 setupRead();
James Feist6714a252018-09-10 15:26:18 -0700289 });
290}
291
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700292void CPUSensor::checkThresholds(void)
James Feist6714a252018-09-10 15:26:18 -0700293{
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800294 if (show)
295 {
296 thresholds::checkThresholds(this);
297 }
James Feist6714a252018-09-10 15:26:18 -0700298}