blob: 4377bf948ed35458992c78feab0431d585e2d39d [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, " ", "_"),
James Feistce3fca42018-11-21 12:58:24 -080046 std::move(_thresholds), sensorConfiguration, objectType, maxReading,
James Feist961bf092020-07-01 16:38:12 -070047 minReading, 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 Kurzynski0a4c4802020-04-01 11:22:27 +020059 auto [type, nr, item] = *fileParts;
60 std::string interfacePath;
61 if (type.compare("power") == 0)
62 {
63 interfacePath = "/xyz/openbmc_project/sensors/power/" + name;
64 }
65 else
66 {
67 interfacePath =
68 "/xyz/openbmc_project/sensors/temperature/" + name;
69 }
James Feist078f2322019-03-08 11:09:05 -080070
Zbigniew Kurzynski0a4c4802020-04-01 11:22:27 +020071 sensorInterface = objectServer.add_interface(
72 interfacePath, "xyz.openbmc_project.Sensor.Value");
73 if (thresholds::hasWarningInterface(thresholds))
74 {
75 thresholdInterfaceWarning = objectServer.add_interface(
76 interfacePath,
77 "xyz.openbmc_project.Sensor.Threshold.Warning");
78 }
79 if (thresholds::hasCriticalInterface(thresholds))
80 {
81 thresholdInterfaceCritical = objectServer.add_interface(
82 interfacePath,
83 "xyz.openbmc_project.Sensor.Threshold.Critical");
84 }
85 association = objectServer.add_interface(interfacePath,
86 association::interface);
87
88 setInitialProperties(conn);
89 }
James Feist6714a252018-09-10 15:26:18 -070090 }
James Feist961bf092020-07-01 16:38:12 -070091
92 // call setup always as not all sensors call setInitialProperties
James Feist71d31b22019-01-02 16:57:54 -080093 setupPowerMatch(conn);
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070094 setupRead();
James Feist6714a252018-09-10 15:26:18 -070095}
96
97CPUSensor::~CPUSensor()
98{
99 // close the input dev to cancel async operations
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700100 inputDev.close();
101 waitTimer.cancel();
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800102 if (show)
103 {
104 objServer.remove_interface(thresholdInterfaceWarning);
105 objServer.remove_interface(thresholdInterfaceCritical);
106 objServer.remove_interface(sensorInterface);
James Feist078f2322019-03-08 11:09:05 -0800107 objServer.remove_interface(association);
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800108 }
James Feist6714a252018-09-10 15:26:18 -0700109}
110
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700111void CPUSensor::setupRead(void)
James Feist6714a252018-09-10 15:26:18 -0700112{
James Feistc22b8422020-07-07 14:40:39 -0700113 if (readingStateGood())
114 {
115 inputDev.close();
116 int fd = open(path.c_str(), O_RDONLY);
117 if (fd >= 0)
118 {
119 inputDev.assign(fd);
120
121 boost::asio::async_read_until(
122 inputDev, readBuf, '\n',
123 [&](const boost::system::error_code& ec,
124 std::size_t /*bytes_transfered*/) { handleResponse(ec); });
125 }
126 else
127 {
128 std::cerr << name << " unable to open fd!\n";
129 pollTime = sensorFailedPollTimeMs;
130 }
131 }
132 else
133 {
134 pollTime = sensorFailedPollTimeMs;
135 markAvailable(false);
136 }
137 waitTimer.expires_from_now(boost::posix_time::milliseconds(pollTime));
138 waitTimer.async_wait([&](const boost::system::error_code& ec) {
139 if (ec == boost::asio::error::operation_aborted)
140 {
141 return; // we're being canceled
142 }
143 setupRead();
144 });
James Feist6714a252018-09-10 15:26:18 -0700145}
146
Zbigniew Kurzynski8d8d8d72020-05-29 19:21:24 +0200147void CPUSensor::updateMinMaxValues(void)
148{
149 const boost::container::flat_map<
150 std::string,
151 std::vector<std::tuple<const char*, std::reference_wrapper<double>,
152 const char*>>>
153 map = {
154 {
155 "cap",
156 {
157 std::make_tuple("cap_max", std::ref(maxValue), "MaxValue"),
158 std::make_tuple("cap_min", std::ref(minValue), "MinValue"),
159 },
160 },
161 };
162
Zbigniew Kurzynski63f38662020-06-09 13:02:11 +0200163 if (auto fileParts = splitFileName(path))
Zbigniew Kurzynski8d8d8d72020-05-29 19:21:24 +0200164 {
165 auto [fileType, fileNr, fileItem] = *fileParts;
166 const auto mapIt = map.find(fileItem);
167 if (mapIt != map.cend())
168 {
169 for (const auto& vectorItem : mapIt->second)
170 {
171 auto [suffix, oldValue, dbusName] = vectorItem;
172 auto attrPath = boost::replace_all_copy(path, fileItem, suffix);
Zbigniew Kurzynski63f38662020-06-09 13:02:11 +0200173 if (auto newVal =
174 readFile(attrPath, CPUSensor::sensorScaleFactor))
Zbigniew Kurzynski8d8d8d72020-05-29 19:21:24 +0200175 {
Zbigniew Kurzynski4f45e422020-06-09 12:42:15 +0200176 updateProperty(sensorInterface, oldValue, *newVal,
177 dbusName);
Zbigniew Kurzynski8d8d8d72020-05-29 19:21:24 +0200178 }
179 else
180 {
181 if (isPowerOn())
182 {
Zbigniew Kurzynski4f45e422020-06-09 12:42:15 +0200183 updateProperty(sensorInterface, oldValue, 0, dbusName);
Zbigniew Kurzynski8d8d8d72020-05-29 19:21:24 +0200184 }
185 else
186 {
Zbigniew Kurzynski4f45e422020-06-09 12:42:15 +0200187 updateProperty(sensorInterface, oldValue,
188 std::numeric_limits<double>::quiet_NaN(),
189 dbusName);
Zbigniew Kurzynski8d8d8d72020-05-29 19:21:24 +0200190 }
191 }
192 }
193 }
194 }
195}
196
James Feistd8705872019-02-08 13:26:09 -0800197void CPUSensor::handleResponse(const boost::system::error_code& err)
James Feist6714a252018-09-10 15:26:18 -0700198{
James Feist838529b2020-09-02 16:57:05 -0700199
James Feist6714a252018-09-10 15:26:18 -0700200 if (err == boost::system::errc::bad_file_descriptor)
201 {
202 return; // we're being destroyed
203 }
James Feist838529b2020-09-02 16:57:05 -0700204 else if (err == boost::system::errc::operation_canceled)
205 {
206 if (readingStateGood())
207 {
208 if (!loggedInterfaceDown)
209 {
210 std::cerr << name << " interface down!\n";
211 loggedInterfaceDown = true;
212 }
213 pollTime = 10000 + rand() % 10000;
214 markFunctional(false);
215 }
216 return;
217 }
218 loggedInterfaceDown = false;
James Feistc22b8422020-07-07 14:40:39 -0700219 pollTime = CPUSensor::sensorPollMs;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700220 std::istream responseStream(&readBuf);
James Feist6714a252018-09-10 15:26:18 -0700221 if (!err)
222 {
223 std::string response;
224 try
225 {
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700226 std::getline(responseStream, response);
Zhikui Rend3da1282020-09-11 17:02:01 -0700227 rawValue = std::stod(response);
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700228 responseStream.clear();
Zhikui Rend3da1282020-09-11 17:02:01 -0700229 double nvalue = rawValue / CPUSensor::sensorScaleFactor;
Josh Lehan833661a2020-03-04 17:35:41 -0800230
231 if (show)
James Feist6714a252018-09-10 15:26:18 -0700232 {
Josh Lehan833661a2020-03-04 17:35:41 -0800233 updateValue(nvalue);
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800234 }
Josh Lehan833661a2020-03-04 17:35:41 -0800235 else
236 {
237 value = nvalue;
238 }
Zbigniew Kurzynski98be9842020-09-07 18:49:15 +0200239 if (minMaxReadCounter++ % 8 == 0)
240 {
241 updateMinMaxValues();
242 }
Josh Lehan833661a2020-03-04 17:35:41 -0800243
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800244 double gTcontrol = gCpuSensors[nameTcontrol]
245 ? gCpuSensors[nameTcontrol]->value
246 : std::numeric_limits<double>::quiet_NaN();
247 if (gTcontrol != privTcontrol)
248 {
249 privTcontrol = gTcontrol;
250
251 if (!thresholds.empty())
252 {
253 std::vector<thresholds::Threshold> newThresholds;
254 if (parseThresholdsFromAttr(newThresholds, path,
Vijay Khemka86dea2b2019-06-06 11:14:37 -0700255 CPUSensor::sensorScaleFactor,
256 dtsOffset))
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800257 {
258 if (!std::equal(thresholds.begin(), thresholds.end(),
259 newThresholds.begin(),
260 newThresholds.end()))
261 {
262 thresholds = newThresholds;
263 if (show)
264 {
265 thresholds::updateThresholds(this);
266 }
267 }
268 }
269 else
270 {
271 std::cerr << "Failure to update thresholds for " << name
272 << "\n";
273 }
Jae Hyun Yoo95b8a2d2019-02-25 20:15:09 -0800274 }
275 }
James Feist6714a252018-09-10 15:26:18 -0700276 }
James Feistd8705872019-02-08 13:26:09 -0800277 catch (const std::invalid_argument&)
James Feist6714a252018-09-10 15:26:18 -0700278 {
James Feist961bf092020-07-01 16:38:12 -0700279 incrementError();
James Feist6714a252018-09-10 15:26:18 -0700280 }
281 }
282 else
283 {
James Feist1169eb42018-10-31 10:08:47 -0700284 pollTime = sensorFailedPollTimeMs;
James Feist961bf092020-07-01 16:38:12 -0700285 incrementError();
James Feist6714a252018-09-10 15:26:18 -0700286 }
287
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700288 responseStream.clear();
James Feist6714a252018-09-10 15:26:18 -0700289}
290
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700291void CPUSensor::checkThresholds(void)
James Feist6714a252018-09-10 15:26:18 -0700292{
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800293 if (show)
294 {
295 thresholds::checkThresholds(this);
296 }
James Feist6714a252018-09-10 15:26:18 -0700297}