blob: 49e65ac79cd9efa4d00de9961e2f8c69f0b99954 [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,
James Feist961bf092020-07-01 16:38:12 -070046 minReading, PowerState::on),
James Feistc22b8422020-07-07 14:40:39 -070047 objServer(objectServer), inputDev(io), waitTimer(io), path(path),
Brad Bishopfbb44ad2019-11-08 09:42:37 -050048 privTcontrol(std::numeric_limits<double>::quiet_NaN()),
James Feistc22b8422020-07-07 14:40:39 -070049 dtsOffset(dtsOffset), show(show), pollTime(CPUSensor::sensorPollMs)
James Feist6714a252018-09-10 15:26:18 -070050{
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -080051 nameTcontrol = labelTcontrol;
52 nameTcontrol += " CPU" + std::to_string(cpuId);
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -080053 if (show)
James Feist6714a252018-09-10 15:26:18 -070054 {
Zbigniew Kurzynski63f38662020-06-09 13:02:11 +020055 if (auto fileParts = splitFileName(path))
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -080056 {
Zbigniew Kurzynski0a4c4802020-04-01 11:22:27 +020057 auto [type, nr, item] = *fileParts;
58 std::string interfacePath;
59 if (type.compare("power") == 0)
60 {
61 interfacePath = "/xyz/openbmc_project/sensors/power/" + name;
62 }
63 else
64 {
65 interfacePath =
66 "/xyz/openbmc_project/sensors/temperature/" + name;
67 }
James Feist078f2322019-03-08 11:09:05 -080068
Zbigniew Kurzynski0a4c4802020-04-01 11:22:27 +020069 sensorInterface = objectServer.add_interface(
70 interfacePath, "xyz.openbmc_project.Sensor.Value");
71 if (thresholds::hasWarningInterface(thresholds))
72 {
73 thresholdInterfaceWarning = objectServer.add_interface(
74 interfacePath,
75 "xyz.openbmc_project.Sensor.Threshold.Warning");
76 }
77 if (thresholds::hasCriticalInterface(thresholds))
78 {
79 thresholdInterfaceCritical = objectServer.add_interface(
80 interfacePath,
81 "xyz.openbmc_project.Sensor.Threshold.Critical");
82 }
83 association = objectServer.add_interface(interfacePath,
84 association::interface);
85
86 setInitialProperties(conn);
87 }
James Feist6714a252018-09-10 15:26:18 -070088 }
James Feist961bf092020-07-01 16:38:12 -070089
90 // call setup always as not all sensors call setInitialProperties
James Feist71d31b22019-01-02 16:57:54 -080091 setupPowerMatch(conn);
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070092 setupRead();
James Feist6714a252018-09-10 15:26:18 -070093}
94
95CPUSensor::~CPUSensor()
96{
97 // close the input dev to cancel async operations
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070098 inputDev.close();
99 waitTimer.cancel();
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800100 if (show)
101 {
102 objServer.remove_interface(thresholdInterfaceWarning);
103 objServer.remove_interface(thresholdInterfaceCritical);
104 objServer.remove_interface(sensorInterface);
James Feist078f2322019-03-08 11:09:05 -0800105 objServer.remove_interface(association);
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800106 }
James Feist6714a252018-09-10 15:26:18 -0700107}
108
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700109void CPUSensor::setupRead(void)
James Feist6714a252018-09-10 15:26:18 -0700110{
James Feistc22b8422020-07-07 14:40:39 -0700111 if (readingStateGood())
112 {
113 inputDev.close();
114 int fd = open(path.c_str(), O_RDONLY);
115 if (fd >= 0)
116 {
117 inputDev.assign(fd);
118
119 boost::asio::async_read_until(
120 inputDev, readBuf, '\n',
121 [&](const boost::system::error_code& ec,
122 std::size_t /*bytes_transfered*/) { handleResponse(ec); });
123 }
124 else
125 {
126 std::cerr << name << " unable to open fd!\n";
127 pollTime = sensorFailedPollTimeMs;
128 }
129 }
130 else
131 {
132 pollTime = sensorFailedPollTimeMs;
133 markAvailable(false);
134 }
135 waitTimer.expires_from_now(boost::posix_time::milliseconds(pollTime));
136 waitTimer.async_wait([&](const boost::system::error_code& ec) {
137 if (ec == boost::asio::error::operation_aborted)
138 {
139 return; // we're being canceled
140 }
141 setupRead();
142 });
James Feist6714a252018-09-10 15:26:18 -0700143}
144
Zbigniew Kurzynski8d8d8d72020-05-29 19:21:24 +0200145void CPUSensor::updateMinMaxValues(void)
146{
147 const boost::container::flat_map<
148 std::string,
149 std::vector<std::tuple<const char*, std::reference_wrapper<double>,
150 const char*>>>
151 map = {
152 {
153 "cap",
154 {
155 std::make_tuple("cap_max", std::ref(maxValue), "MaxValue"),
156 std::make_tuple("cap_min", std::ref(minValue), "MinValue"),
157 },
158 },
159 };
160
Zbigniew Kurzynski63f38662020-06-09 13:02:11 +0200161 if (auto fileParts = splitFileName(path))
Zbigniew Kurzynski8d8d8d72020-05-29 19:21:24 +0200162 {
163 auto [fileType, fileNr, fileItem] = *fileParts;
164 const auto mapIt = map.find(fileItem);
165 if (mapIt != map.cend())
166 {
167 for (const auto& vectorItem : mapIt->second)
168 {
169 auto [suffix, oldValue, dbusName] = vectorItem;
170 auto attrPath = boost::replace_all_copy(path, fileItem, suffix);
Zbigniew Kurzynski63f38662020-06-09 13:02:11 +0200171 if (auto newVal =
172 readFile(attrPath, CPUSensor::sensorScaleFactor))
Zbigniew Kurzynski8d8d8d72020-05-29 19:21:24 +0200173 {
Zbigniew Kurzynski4f45e422020-06-09 12:42:15 +0200174 updateProperty(sensorInterface, oldValue, *newVal,
175 dbusName);
Zbigniew Kurzynski8d8d8d72020-05-29 19:21:24 +0200176 }
177 else
178 {
179 if (isPowerOn())
180 {
Zbigniew Kurzynski4f45e422020-06-09 12:42:15 +0200181 updateProperty(sensorInterface, oldValue, 0, dbusName);
Zbigniew Kurzynski8d8d8d72020-05-29 19:21:24 +0200182 }
183 else
184 {
Zbigniew Kurzynski4f45e422020-06-09 12:42:15 +0200185 updateProperty(sensorInterface, oldValue,
186 std::numeric_limits<double>::quiet_NaN(),
187 dbusName);
Zbigniew Kurzynski8d8d8d72020-05-29 19:21:24 +0200188 }
189 }
190 }
191 }
192 }
193}
194
James Feistd8705872019-02-08 13:26:09 -0800195void CPUSensor::handleResponse(const boost::system::error_code& err)
James Feist6714a252018-09-10 15:26:18 -0700196{
197 if (err == boost::system::errc::bad_file_descriptor)
198 {
199 return; // we're being destroyed
200 }
James Feistc22b8422020-07-07 14:40:39 -0700201 pollTime = CPUSensor::sensorPollMs;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700202 std::istream responseStream(&readBuf);
James Feist6714a252018-09-10 15:26:18 -0700203 if (!err)
204 {
205 std::string response;
206 try
207 {
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700208 std::getline(responseStream, response);
Josh Lehan833661a2020-03-04 17:35:41 -0800209 double nvalue = std::stod(response);
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700210 responseStream.clear();
211 nvalue /= CPUSensor::sensorScaleFactor;
Josh Lehan833661a2020-03-04 17:35:41 -0800212
213 if (show)
James Feist6714a252018-09-10 15:26:18 -0700214 {
Josh Lehan833661a2020-03-04 17:35:41 -0800215 updateValue(nvalue);
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800216 }
Josh Lehan833661a2020-03-04 17:35:41 -0800217 else
218 {
219 value = nvalue;
220 }
Zbigniew Kurzynski8d8d8d72020-05-29 19:21:24 +0200221 updateMinMaxValues();
Josh Lehan833661a2020-03-04 17:35:41 -0800222
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800223 double gTcontrol = gCpuSensors[nameTcontrol]
224 ? gCpuSensors[nameTcontrol]->value
225 : std::numeric_limits<double>::quiet_NaN();
226 if (gTcontrol != privTcontrol)
227 {
228 privTcontrol = gTcontrol;
229
230 if (!thresholds.empty())
231 {
232 std::vector<thresholds::Threshold> newThresholds;
233 if (parseThresholdsFromAttr(newThresholds, path,
Vijay Khemka86dea2b2019-06-06 11:14:37 -0700234 CPUSensor::sensorScaleFactor,
235 dtsOffset))
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800236 {
237 if (!std::equal(thresholds.begin(), thresholds.end(),
238 newThresholds.begin(),
239 newThresholds.end()))
240 {
241 thresholds = newThresholds;
242 if (show)
243 {
244 thresholds::updateThresholds(this);
245 }
246 }
247 }
248 else
249 {
250 std::cerr << "Failure to update thresholds for " << name
251 << "\n";
252 }
Jae Hyun Yoo95b8a2d2019-02-25 20:15:09 -0800253 }
254 }
James Feist6714a252018-09-10 15:26:18 -0700255 }
James Feistd8705872019-02-08 13:26:09 -0800256 catch (const std::invalid_argument&)
James Feist6714a252018-09-10 15:26:18 -0700257 {
James Feist961bf092020-07-01 16:38:12 -0700258 incrementError();
James Feist6714a252018-09-10 15:26:18 -0700259 }
260 }
261 else
262 {
James Feist1169eb42018-10-31 10:08:47 -0700263 pollTime = sensorFailedPollTimeMs;
James Feist961bf092020-07-01 16:38:12 -0700264 incrementError();
James Feist6714a252018-09-10 15:26:18 -0700265 }
266
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700267 responseStream.clear();
James Feist6714a252018-09-10 15:26:18 -0700268}
269
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700270void CPUSensor::checkThresholds(void)
James Feist6714a252018-09-10 15:26:18 -0700271{
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800272 if (show)
273 {
274 thresholds::checkThresholds(this);
275 }
James Feist6714a252018-09-10 15:26:18 -0700276}