blob: 241b14f7d734fc9626dace8f433500d7c52fcdfe [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>
26#include <iostream>
Patrick Venture96e97db2019-10-31 13:44:38 -070027#include <istream>
James Feist6714a252018-09-10 15:26:18 -070028#include <limits>
Patrick Venture96e97db2019-10-31 13:44:38 -070029#include <memory>
James Feist6714a252018-09-10 15:26:18 -070030#include <sdbusplus/asio/connection.hpp>
31#include <sdbusplus/asio/object_server.hpp>
Patrick Venture96e97db2019-10-31 13:44:38 -070032#include <stdexcept>
James Feist6714a252018-09-10 15:26:18 -070033#include <string>
Patrick Venture96e97db2019-10-31 13:44:38 -070034#include <vector>
James Feist6714a252018-09-10 15:26:18 -070035
James Feistd8705872019-02-08 13:26:09 -080036CPUSensor::CPUSensor(const std::string& path, const std::string& objectType,
37 sdbusplus::asio::object_server& objectServer,
38 std::shared_ptr<sdbusplus::asio::connection>& conn,
39 boost::asio::io_service& io, const std::string& sensorName,
40 std::vector<thresholds::Threshold>&& _thresholds,
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -080041 const std::string& sensorConfiguration, int cpuId,
Vijay Khemka86dea2b2019-06-06 11:14:37 -070042 bool show, double dtsOffset) :
James Feist930fcde2019-05-28 12:58:43 -070043 Sensor(boost::replace_all_copy(sensorName, " ", "_"),
James Feistce3fca42018-11-21 12:58:24 -080044 std::move(_thresholds), sensorConfiguration, objectType, maxReading,
45 minReading),
James Feist71d31b22019-01-02 16:57:54 -080046 objServer(objectServer), inputDev(io, open(path.c_str(), O_RDONLY)),
Brad Bishopfbb44ad2019-11-08 09:42:37 -050047 waitTimer(io), path(path),
48 privTcontrol(std::numeric_limits<double>::quiet_NaN()),
49 dtsOffset(dtsOffset), show(show), errCount(0)
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);
53
54 if (show)
James Feist6714a252018-09-10 15:26:18 -070055 {
Zbigniew Kurzynski0a4c4802020-04-01 11:22:27 +020056 if (auto fileParts = thresholds::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
James Feistd8705872019-02-08 13:26:09 -0800116void CPUSensor::handleResponse(const boost::system::error_code& err)
James Feist6714a252018-09-10 15:26:18 -0700117{
118 if (err == boost::system::errc::bad_file_descriptor)
119 {
120 return; // we're being destroyed
121 }
James Feist1169eb42018-10-31 10:08:47 -0700122 size_t pollTime = CPUSensor::sensorPollMs;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700123 std::istream responseStream(&readBuf);
James Feist6714a252018-09-10 15:26:18 -0700124 if (!err)
125 {
126 std::string response;
127 try
128 {
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700129 std::getline(responseStream, response);
Josh Lehan833661a2020-03-04 17:35:41 -0800130 double nvalue = std::stod(response);
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700131 responseStream.clear();
132 nvalue /= CPUSensor::sensorScaleFactor;
Josh Lehan833661a2020-03-04 17:35:41 -0800133
134 if (show)
James Feist6714a252018-09-10 15:26:18 -0700135 {
Josh Lehan833661a2020-03-04 17:35:41 -0800136 updateValue(nvalue);
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800137 }
Josh Lehan833661a2020-03-04 17:35:41 -0800138 else
139 {
140 value = nvalue;
141 }
142
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800143 double gTcontrol = gCpuSensors[nameTcontrol]
144 ? gCpuSensors[nameTcontrol]->value
145 : std::numeric_limits<double>::quiet_NaN();
146 if (gTcontrol != privTcontrol)
147 {
148 privTcontrol = gTcontrol;
149
150 if (!thresholds.empty())
151 {
152 std::vector<thresholds::Threshold> newThresholds;
153 if (parseThresholdsFromAttr(newThresholds, path,
Vijay Khemka86dea2b2019-06-06 11:14:37 -0700154 CPUSensor::sensorScaleFactor,
155 dtsOffset))
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800156 {
157 if (!std::equal(thresholds.begin(), thresholds.end(),
158 newThresholds.begin(),
159 newThresholds.end()))
160 {
161 thresholds = newThresholds;
162 if (show)
163 {
164 thresholds::updateThresholds(this);
165 }
166 }
167 }
168 else
169 {
170 std::cerr << "Failure to update thresholds for " << name
171 << "\n";
172 }
Jae Hyun Yoo95b8a2d2019-02-25 20:15:09 -0800173 }
174 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700175 errCount = 0;
James Feist6714a252018-09-10 15:26:18 -0700176 }
James Feistd8705872019-02-08 13:26:09 -0800177 catch (const std::invalid_argument&)
James Feist6714a252018-09-10 15:26:18 -0700178 {
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700179 errCount++;
James Feist6714a252018-09-10 15:26:18 -0700180 }
181 }
182 else
183 {
James Feist1169eb42018-10-31 10:08:47 -0700184 pollTime = sensorFailedPollTimeMs;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700185 errCount++;
James Feist6714a252018-09-10 15:26:18 -0700186 }
187
James Feistdc6c55f2018-10-31 12:53:20 -0700188 if (errCount >= warnAfterErrorCount)
James Feist6714a252018-09-10 15:26:18 -0700189 {
190 // only an error if power is on
James Feist71d31b22019-01-02 16:57:54 -0800191 if (isPowerOn())
James Feist6714a252018-09-10 15:26:18 -0700192 {
James Feistdc6c55f2018-10-31 12:53:20 -0700193 // only print once
194 if (errCount == warnAfterErrorCount)
195 {
196 std::cerr << "Failure to read sensor " << name << " at " << path
197 << "\n";
198 }
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800199 if (show)
200 {
201 updateValue(0);
202 }
203 else
204 {
205 value = 0;
206 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700207 errCount++;
James Feist6714a252018-09-10 15:26:18 -0700208 }
209 else
210 {
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700211 errCount = 0; // check power again in 10 cycles
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800212 if (show)
213 {
214 updateValue(std::numeric_limits<double>::quiet_NaN());
215 }
216 else
217 {
218 value = std::numeric_limits<double>::quiet_NaN();
219 }
James Feist6714a252018-09-10 15:26:18 -0700220 }
221 }
222
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700223 responseStream.clear();
224 inputDev.close();
James Feist6714a252018-09-10 15:26:18 -0700225 int fd = open(path.c_str(), O_RDONLY);
Jae Hyun Yooef9665a2019-10-10 10:26:39 -0700226 if (fd < 0)
James Feist6714a252018-09-10 15:26:18 -0700227 {
228 return; // we're no longer valid
229 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700230 inputDev.assign(fd);
James Feist1169eb42018-10-31 10:08:47 -0700231 waitTimer.expires_from_now(boost::posix_time::milliseconds(pollTime));
James Feistd8705872019-02-08 13:26:09 -0800232 waitTimer.async_wait([&](const boost::system::error_code& ec) {
James Feist6714a252018-09-10 15:26:18 -0700233 if (ec == boost::asio::error::operation_aborted)
234 {
235 return; // we're being canceled
236 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700237 setupRead();
James Feist6714a252018-09-10 15:26:18 -0700238 });
239}
240
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700241void CPUSensor::checkThresholds(void)
James Feist6714a252018-09-10 15:26:18 -0700242{
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800243 if (show)
244 {
245 thresholds::checkThresholds(this);
246 }
James Feist6714a252018-09-10 15:26:18 -0700247}