blob: 291d72f458f2c48f6ac02f8f418284333fcbdc1d [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()),
James Feistc22b8422020-07-07 14:40:39 -070050 dtsOffset(dtsOffset), show(show), pollTime(CPUSensor::sensorPollMs)
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 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);
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070093 setupRead();
James Feist6714a252018-09-10 15:26:18 -070094}
95
96CPUSensor::~CPUSensor()
97{
98 // close the input dev to cancel async operations
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070099 inputDev.close();
100 waitTimer.cancel();
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800101 if (show)
102 {
103 objServer.remove_interface(thresholdInterfaceWarning);
104 objServer.remove_interface(thresholdInterfaceCritical);
105 objServer.remove_interface(sensorInterface);
James Feist078f2322019-03-08 11:09:05 -0800106 objServer.remove_interface(association);
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800107 }
James Feist6714a252018-09-10 15:26:18 -0700108}
109
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700110void CPUSensor::setupRead(void)
James Feist6714a252018-09-10 15:26:18 -0700111{
James Feistc22b8422020-07-07 14:40:39 -0700112 if (readingStateGood())
113 {
114 inputDev.close();
115 int fd = open(path.c_str(), O_RDONLY);
116 if (fd >= 0)
117 {
118 inputDev.assign(fd);
119
120 boost::asio::async_read_until(
121 inputDev, readBuf, '\n',
122 [&](const boost::system::error_code& ec,
123 std::size_t /*bytes_transfered*/) { handleResponse(ec); });
124 }
125 else
126 {
127 std::cerr << name << " unable to open fd!\n";
128 pollTime = sensorFailedPollTimeMs;
129 }
130 }
131 else
132 {
133 pollTime = sensorFailedPollTimeMs;
134 markAvailable(false);
135 }
136 waitTimer.expires_from_now(boost::posix_time::milliseconds(pollTime));
137 waitTimer.async_wait([&](const boost::system::error_code& ec) {
138 if (ec == boost::asio::error::operation_aborted)
139 {
140 return; // we're being canceled
141 }
142 setupRead();
143 });
James Feist6714a252018-09-10 15:26:18 -0700144}
145
Zbigniew Kurzynski8d8d8d72020-05-29 19:21:24 +0200146void CPUSensor::updateMinMaxValues(void)
147{
148 const boost::container::flat_map<
149 std::string,
150 std::vector<std::tuple<const char*, std::reference_wrapper<double>,
151 const char*>>>
152 map = {
153 {
154 "cap",
155 {
156 std::make_tuple("cap_max", std::ref(maxValue), "MaxValue"),
157 std::make_tuple("cap_min", std::ref(minValue), "MinValue"),
158 },
159 },
160 };
161
Zbigniew Kurzynski63f38662020-06-09 13:02:11 +0200162 if (auto fileParts = splitFileName(path))
Zbigniew Kurzynski8d8d8d72020-05-29 19:21:24 +0200163 {
164 auto [fileType, fileNr, fileItem] = *fileParts;
165 const auto mapIt = map.find(fileItem);
166 if (mapIt != map.cend())
167 {
168 for (const auto& vectorItem : mapIt->second)
169 {
170 auto [suffix, oldValue, dbusName] = vectorItem;
171 auto attrPath = boost::replace_all_copy(path, fileItem, suffix);
Zbigniew Kurzynski63f38662020-06-09 13:02:11 +0200172 if (auto newVal =
173 readFile(attrPath, CPUSensor::sensorScaleFactor))
Zbigniew Kurzynski8d8d8d72020-05-29 19:21:24 +0200174 {
Zbigniew Kurzynski4f45e422020-06-09 12:42:15 +0200175 updateProperty(sensorInterface, oldValue, *newVal,
176 dbusName);
Zbigniew Kurzynski8d8d8d72020-05-29 19:21:24 +0200177 }
178 else
179 {
180 if (isPowerOn())
181 {
Zbigniew Kurzynski4f45e422020-06-09 12:42:15 +0200182 updateProperty(sensorInterface, oldValue, 0, dbusName);
Zbigniew Kurzynski8d8d8d72020-05-29 19:21:24 +0200183 }
184 else
185 {
Zbigniew Kurzynski4f45e422020-06-09 12:42:15 +0200186 updateProperty(sensorInterface, oldValue,
187 std::numeric_limits<double>::quiet_NaN(),
188 dbusName);
Zbigniew Kurzynski8d8d8d72020-05-29 19:21:24 +0200189 }
190 }
191 }
192 }
193 }
194}
195
James Feistd8705872019-02-08 13:26:09 -0800196void CPUSensor::handleResponse(const boost::system::error_code& err)
James Feist6714a252018-09-10 15:26:18 -0700197{
198 if (err == boost::system::errc::bad_file_descriptor)
199 {
200 return; // we're being destroyed
201 }
James Feistc22b8422020-07-07 14:40:39 -0700202 pollTime = CPUSensor::sensorPollMs;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700203 std::istream responseStream(&readBuf);
James Feist6714a252018-09-10 15:26:18 -0700204 if (!err)
205 {
206 std::string response;
207 try
208 {
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700209 std::getline(responseStream, response);
Josh Lehan833661a2020-03-04 17:35:41 -0800210 double nvalue = std::stod(response);
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700211 responseStream.clear();
212 nvalue /= CPUSensor::sensorScaleFactor;
Josh Lehan833661a2020-03-04 17:35:41 -0800213
214 if (show)
James Feist6714a252018-09-10 15:26:18 -0700215 {
Josh Lehan833661a2020-03-04 17:35:41 -0800216 updateValue(nvalue);
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800217 }
Josh Lehan833661a2020-03-04 17:35:41 -0800218 else
219 {
220 value = nvalue;
221 }
Zbigniew Kurzynski8d8d8d72020-05-29 19:21:24 +0200222 updateMinMaxValues();
Josh Lehan833661a2020-03-04 17:35:41 -0800223
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800224 double gTcontrol = gCpuSensors[nameTcontrol]
225 ? gCpuSensors[nameTcontrol]->value
226 : std::numeric_limits<double>::quiet_NaN();
227 if (gTcontrol != privTcontrol)
228 {
229 privTcontrol = gTcontrol;
230
231 if (!thresholds.empty())
232 {
233 std::vector<thresholds::Threshold> newThresholds;
234 if (parseThresholdsFromAttr(newThresholds, path,
Vijay Khemka86dea2b2019-06-06 11:14:37 -0700235 CPUSensor::sensorScaleFactor,
236 dtsOffset))
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800237 {
238 if (!std::equal(thresholds.begin(), thresholds.end(),
239 newThresholds.begin(),
240 newThresholds.end()))
241 {
242 thresholds = newThresholds;
243 if (show)
244 {
245 thresholds::updateThresholds(this);
246 }
247 }
248 }
249 else
250 {
251 std::cerr << "Failure to update thresholds for " << name
252 << "\n";
253 }
Jae Hyun Yoo95b8a2d2019-02-25 20:15:09 -0800254 }
255 }
James Feist6714a252018-09-10 15:26:18 -0700256 }
James Feistd8705872019-02-08 13:26:09 -0800257 catch (const std::invalid_argument&)
James Feist6714a252018-09-10 15:26:18 -0700258 {
James Feist961bf092020-07-01 16:38:12 -0700259 incrementError();
James Feist6714a252018-09-10 15:26:18 -0700260 }
261 }
262 else
263 {
James Feist1169eb42018-10-31 10:08:47 -0700264 pollTime = sensorFailedPollTimeMs;
James Feist961bf092020-07-01 16:38:12 -0700265 incrementError();
James Feist6714a252018-09-10 15:26:18 -0700266 }
267
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700268 responseStream.clear();
James Feist6714a252018-09-10 15:26:18 -0700269}
270
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700271void CPUSensor::checkThresholds(void)
James Feist6714a252018-09-10 15:26:18 -0700272{
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800273 if (show)
274 {
275 thresholds::checkThresholds(this);
276 }
James Feist6714a252018-09-10 15:26:18 -0700277}