blob: 9458896c5e6e02ac9d94586c4b73e2ec56cf2fec [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)),
Vijay Khemka86dea2b2019-06-06 11:14:37 -070047 path(path), waitTimer(io), show(show), dtsOffset(dtsOffset),
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -080048 privTcontrol(std::numeric_limits<double>::quiet_NaN()), errCount(0)
James Feist6714a252018-09-10 15:26:18 -070049{
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -080050 nameTcontrol = labelTcontrol;
51 nameTcontrol += " CPU" + std::to_string(cpuId);
52
53 if (show)
James Feist6714a252018-09-10 15:26:18 -070054 {
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -080055 sensorInterface = objectServer.add_interface(
James Feist6714a252018-09-10 15:26:18 -070056 "/xyz/openbmc_project/sensors/temperature/" + name,
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -080057 "xyz.openbmc_project.Sensor.Value");
58 if (thresholds::hasWarningInterface(thresholds))
59 {
60 thresholdInterfaceWarning = objectServer.add_interface(
61 "/xyz/openbmc_project/sensors/temperature/" + name,
62 "xyz.openbmc_project.Sensor.Threshold.Warning");
63 }
64 if (thresholds::hasCriticalInterface(thresholds))
65 {
66 thresholdInterfaceCritical = objectServer.add_interface(
67 "/xyz/openbmc_project/sensors/temperature/" + name,
68 "xyz.openbmc_project.Sensor.Threshold.Critical");
69 }
James Feist078f2322019-03-08 11:09:05 -080070 association = objectServer.add_interface(
71 "/xyz/openbmc_project/sensors/temperature/" + name,
James Feist2adc95c2019-09-30 14:55:28 -070072 association::interface);
James Feist078f2322019-03-08 11:09:05 -080073
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -080074 setInitialProperties(conn);
James Feist6714a252018-09-10 15:26:18 -070075 }
James Feist71d31b22019-01-02 16:57:54 -080076 setupPowerMatch(conn);
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070077 setupRead();
James Feist6714a252018-09-10 15:26:18 -070078}
79
80CPUSensor::~CPUSensor()
81{
82 // close the input dev to cancel async operations
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070083 inputDev.close();
84 waitTimer.cancel();
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -080085 if (show)
86 {
87 objServer.remove_interface(thresholdInterfaceWarning);
88 objServer.remove_interface(thresholdInterfaceCritical);
89 objServer.remove_interface(sensorInterface);
James Feist078f2322019-03-08 11:09:05 -080090 objServer.remove_interface(association);
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -080091 }
James Feist6714a252018-09-10 15:26:18 -070092}
93
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070094void CPUSensor::setupRead(void)
James Feist6714a252018-09-10 15:26:18 -070095{
96 boost::asio::async_read_until(
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070097 inputDev, readBuf, '\n',
James Feistd8705872019-02-08 13:26:09 -080098 [&](const boost::system::error_code& ec,
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070099 std::size_t /*bytes_transfered*/) { handleResponse(ec); });
James Feist6714a252018-09-10 15:26:18 -0700100}
101
James Feistd8705872019-02-08 13:26:09 -0800102void CPUSensor::handleResponse(const boost::system::error_code& err)
James Feist6714a252018-09-10 15:26:18 -0700103{
104 if (err == boost::system::errc::bad_file_descriptor)
105 {
106 return; // we're being destroyed
107 }
James Feist1169eb42018-10-31 10:08:47 -0700108 size_t pollTime = CPUSensor::sensorPollMs;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700109 std::istream responseStream(&readBuf);
James Feist6714a252018-09-10 15:26:18 -0700110 if (!err)
111 {
112 std::string response;
113 try
114 {
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700115 std::getline(responseStream, response);
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800116 double nvalue = std::stof(response);
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700117 responseStream.clear();
118 nvalue /= CPUSensor::sensorScaleFactor;
James Feist6714a252018-09-10 15:26:18 -0700119 if (nvalue != value)
120 {
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800121 if (show)
Jae Hyun Yoo95b8a2d2019-02-25 20:15:09 -0800122 {
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800123 updateValue(nvalue);
Jae Hyun Yoo95b8a2d2019-02-25 20:15:09 -0800124 }
125 else
126 {
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800127 value = nvalue;
128 }
129 }
130 double gTcontrol = gCpuSensors[nameTcontrol]
131 ? gCpuSensors[nameTcontrol]->value
132 : std::numeric_limits<double>::quiet_NaN();
133 if (gTcontrol != privTcontrol)
134 {
135 privTcontrol = gTcontrol;
136
137 if (!thresholds.empty())
138 {
139 std::vector<thresholds::Threshold> newThresholds;
140 if (parseThresholdsFromAttr(newThresholds, path,
Vijay Khemka86dea2b2019-06-06 11:14:37 -0700141 CPUSensor::sensorScaleFactor,
142 dtsOffset))
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800143 {
144 if (!std::equal(thresholds.begin(), thresholds.end(),
145 newThresholds.begin(),
146 newThresholds.end()))
147 {
148 thresholds = newThresholds;
149 if (show)
150 {
151 thresholds::updateThresholds(this);
152 }
153 }
154 }
155 else
156 {
157 std::cerr << "Failure to update thresholds for " << name
158 << "\n";
159 }
Jae Hyun Yoo95b8a2d2019-02-25 20:15:09 -0800160 }
161 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700162 errCount = 0;
James Feist6714a252018-09-10 15:26:18 -0700163 }
James Feistd8705872019-02-08 13:26:09 -0800164 catch (const std::invalid_argument&)
James Feist6714a252018-09-10 15:26:18 -0700165 {
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700166 errCount++;
James Feist6714a252018-09-10 15:26:18 -0700167 }
168 }
169 else
170 {
James Feist1169eb42018-10-31 10:08:47 -0700171 pollTime = sensorFailedPollTimeMs;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700172 errCount++;
James Feist6714a252018-09-10 15:26:18 -0700173 }
174
James Feistdc6c55f2018-10-31 12:53:20 -0700175 if (errCount >= warnAfterErrorCount)
James Feist6714a252018-09-10 15:26:18 -0700176 {
177 // only an error if power is on
James Feist71d31b22019-01-02 16:57:54 -0800178 if (isPowerOn())
James Feist6714a252018-09-10 15:26:18 -0700179 {
James Feistdc6c55f2018-10-31 12:53:20 -0700180 // only print once
181 if (errCount == warnAfterErrorCount)
182 {
183 std::cerr << "Failure to read sensor " << name << " at " << path
184 << "\n";
185 }
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800186 if (show)
187 {
188 updateValue(0);
189 }
190 else
191 {
192 value = 0;
193 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700194 errCount++;
James Feist6714a252018-09-10 15:26:18 -0700195 }
196 else
197 {
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700198 errCount = 0; // check power again in 10 cycles
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800199 if (show)
200 {
201 updateValue(std::numeric_limits<double>::quiet_NaN());
202 }
203 else
204 {
205 value = std::numeric_limits<double>::quiet_NaN();
206 }
James Feist6714a252018-09-10 15:26:18 -0700207 }
208 }
209
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700210 responseStream.clear();
211 inputDev.close();
James Feist6714a252018-09-10 15:26:18 -0700212 int fd = open(path.c_str(), O_RDONLY);
Jae Hyun Yooef9665a2019-10-10 10:26:39 -0700213 if (fd < 0)
James Feist6714a252018-09-10 15:26:18 -0700214 {
215 return; // we're no longer valid
216 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700217 inputDev.assign(fd);
James Feist1169eb42018-10-31 10:08:47 -0700218 waitTimer.expires_from_now(boost::posix_time::milliseconds(pollTime));
James Feistd8705872019-02-08 13:26:09 -0800219 waitTimer.async_wait([&](const boost::system::error_code& ec) {
James Feist6714a252018-09-10 15:26:18 -0700220 if (ec == boost::asio::error::operation_aborted)
221 {
222 return; // we're being canceled
223 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700224 setupRead();
James Feist6714a252018-09-10 15:26:18 -0700225 });
226}
227
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700228void CPUSensor::checkThresholds(void)
James Feist6714a252018-09-10 15:26:18 -0700229{
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800230 if (show)
231 {
232 thresholds::checkThresholds(this);
233 }
James Feist6714a252018-09-10 15:26:18 -0700234}